Saturday, August 23, 2014

[Level 1] git ignore

If you want git not to commit some temporary files or log files...etc., you could create .gitignore file to ignore them. If you don't have any idea about which file should be ignore, you could also download ignore templates that prepared by github.
mkdir ~/git-home
cd ~/git-home
git init
git clone https://github.com/github/gitignore
cd ~/my_py_project
cp ~/git-home/gitignore/Python.gitignore ./.gitignore

Wish this helps.

regards,
Stanley Huang

Tuesday, August 19, 2014

[Level 1] HOWTO Installing Packages from Yaourt AUR in Archlinux.

If you want to install packages from Yaourt(Yet AnOther User Repository Tool ) AUR(Archlinux User Repository), you could follow the following steps. a. Install yaourt by downloading source code.
1. Install development environment.
$ sudo pacman -S base-devel
$ sudo pacman -S yajl

2. Download, build and install package-query
$ curl -O https://aur.archlinux.org/packages/pa/package-query/package-query.tar.gz
$ tar zxvf ./package-query.tar.gz
$ cd ./package-query
$ makepkg -si

3. Download, build and install Yaourt
$ wget https://aur.archlinux.org/packages/ya/yaourt/yaourt.tar.gz
$ tar zxvf ./yaourt.tar.gz
$ cd ./yaourt
$ makepkg -si
b. Install yaourt from repository.
1. setup repository
$ sudo su
# cat >> /etc/pacman.conf <<EOF
[archlinuxfr]
SigLevel = Never
Server = http://repo.archlinux.fr/$arch
EOF
2. update repository and install yaourt
# pacman -Syu
# pacman -Ss yaourt

Wish this helps.

regards,
Stanley Huang

Friday, August 15, 2014

[Level 1] Create git repository by command.

A script for creating git repository quickly. http://viget.com/extend/create-a-github-repo-from-the-command-line
Wish this helps.

regards,
Stanley Huang

Thursday, August 14, 2014

[ Level 1 ] Go installation and "Hello World" sample.

A friend of mine told me, if you could choose the programming language, choose the fashion one. I tried to use Go becuase it's "Go fashion"(Chinglish, means, fashion enough). The following script is the way to install Go in CentOS, you could refer to it if you also used CentOS.
#!/bin/bash 
# install Go, gcc for Go
rpm -q golang >/dev/null 2>&1 || sudo yum -y install golang libgo gcc-go

# Hello World
[ -e ./HelloWorld.go ] || cat > ./HelloWorld.go <<EOF
package main
import "fmt"
func main() {
  fmt.Println("Hellow World!")
}
EOF

# run as interpreter script
go run ./HelloWorld.go

# compile script and run it.
gccgo -g -o ./HelloWorld.out ./HelloWorld.go && ./HelloWorld.out


Wish this helps.

regards,
Stanley Huang

Tuesday, August 12, 2014

[ Level 1 ] Tips for sublime text.

Good tips for sublime.
http://nipunbatra.wordpress.com/2014/01/30/plugins-for-python-development-in-sublime-text/

Wish this helps.

regards,
Stanley Huang

Wednesday, July 23, 2014

[Level 1] Install skype in CentOS 7.

If you want to install skype in CentOS 7. You need to download 32 bit library. Or you could lookup nux-desktop and it already has made for 32 bit libraries that required by skype.
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm;
sudo yum localinstall http://download.skype.com/linux/skype-4.3.0.37-fedora.i586.rpm;

Wish this helps.

regards,
Stanley Huang

Friday, July 18, 2014

[Level 1] How to install 32 bit java for Juniper VPN in CentOS.

##
## Precondition: you have to download 32 bit openjdk first!
##

compat_32="compat-libstdc++-296"
libs_for_vpn="xterm ld-linux.so.2 libstdc++.so.6 libz.so.1 libXext.so.6 libXrender.so.1"
java_64_rpms="java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.x86_64 icedtea-web-1.4.1-0.el6.x86_64"
libs_for_java_32="giflib.i686 nss.i686 libpng12.so.0 libpulse.so.0 rhino"

java_32_rpm_url="ftp://ftp.muug.mb.ca/mirror/centos/6.5/os/i386/Packages/java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.i686.rpm" 

java_32_rpm="`basename $java_32_rpm_url`"

download_dir="/tmp"

 

## download 32bit java

cd $download_dir; wget $java_32_rpm_url 

 

## remove native openjdk
sudo yum -y remove java-1.7.0-openjdk

## install 32 compatible libraries
for app in $compat_32
do
  sudo yum -y install $app
done

## install libraries for vpn
for app in $libs_for_vpn
do
  sudo yum -y install $app
done

## install 64 bit java
for app in $java_64_rpms
do
  sudo yum -y install $app
done

## setup alternative java
sudo update-alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java

## install libraries for 32 bit java
for app in $libs_for_java_32
do
  sudo yum -y install $app
done

## install 32 bit java
sudo rpm -ihv $java_32_rpm
Wish this helps.

regards,
Stanley Huang

Thursday, July 17, 2014

[Level 1] How to yum to install specific version in CentOS.

If you want to use yum to install specific version in CentOS. First, you have to use option "--showduplicates" to get duplicate versions
# yum --showduplicates list java-1.7.0-openjdk
Loaded plugins: fastestmirror, refresh-packagekit, security, versionlock
Loading mirror speeds from cached hostfile
 * base: mirror01.idc.hinet.net
 * elrepo: ftp.ne.jp
 * epel: ftp.cuhk.edu.hk
 * extras: mirror01.idc.hinet.net
 * remi: remi.kazukioishi.net
 * rpmforge: ftp.riken.jp
 * updates: mirror01.idc.hinet.net
Available Packages
java-1.7.0-openjdk.x86_64           1:1.7.0.45-2.4.3.3.el6              base   
java-1.7.0-openjdk.x86_64           1:1.7.0.45-2.4.3.4.el6_5            updates
java-1.7.0-openjdk.x86_64           1:1.7.0.51-2.4.4.1.el6_5            updates
java-1.7.0-openjdk.x86_64           1:1.7.0.55-2.4.7.1.el6_5            updates
java-1.7.0-openjdk.x86_64           1:1.7.0.65-2.5.1.2.el6_5            updates
After you got the information of versions, then you could install option of yum to install specific version.
# yum install java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.x86_64
Loaded plugins: fastestmirror, refresh-packagekit, security, versionlock
Loading mirror speeds from cached hostfile
 * base: mirror01.idc.hinet.net
 * elrepo: ftp.ne.jp
 * epel: ftp.cuhk.edu.hk
 * extras: mirror01.idc.hinet.net
 * remi: remi.kazukioishi.net
 * rpmforge: ftp.riken.jp
 * updates: mirror01.idc.hinet.net
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package java-1.7.0-openjdk.x86_64 1:1.7.0.45-2.4.3.3.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================
 Package                              Arch                     Version                                   Repository              Size
======================================================================================================================================
Installing:
 java-1.7.0-openjdk                   x86_64                   1:1.7.0.45-2.4.3.3.el6                    base                    26 M

Transaction Summary
======================================================================================================================================
Install       1 Package(s)

Total download size: 26 M
Installed size: 90 M
Is this ok [y/N]: y
...

Wish this helps.

regards,
Stanley Huang

Wednesday, June 18, 2014

[Level 2] Python reverse engineering.

If you want to reverse engineering of python source code, you could use pylint to get class diagram. ref: http://manpages.ubuntu.com/manpages/saucy/man1/pyreverse.1.html http://www.logilab.org/6883 http://planet.logilab.fr/index.php?post_id=74
$ apt-cache search pyreverse
pylint - python code static checker and UML diagram generator
...
$ sudo apt-get -y install pylint

$ pyreverse ./*.py
$ dotty ./classes_No_Name.dot
$ doggy ./packages_No_Name.dot
or
$ pyreverse -o png ./*.py
$ eog ./classes_No_Name.png
$ eog ./packages_No_Name.png

related projects: http://floss.zoomquiet.io/data/20041231094746/index.html
Wish this helps.

regards,
Stanley Huang

[Level 1] How to trace the source code more efficient.

If you want to trace python call stack, you could get call sequence by using call_seq module. You could download call_seq from pypi website. https://pypi.python.org/pypi/call_seq/0.0.1 After you download it, you could prepare the python script like following.
python from call_seq import CallSeq
trail = CallSeq()
trail.set_trace()
# the code you want to trace.
trail.unset_trace()
trail.dump_to_file('output.json')
After you create the output json file, then you could use browser module of call_seq to get visualization of call sequence.
$ python -m call_seq.browser ./output.json

Wish this helps.

regards,
Stanley Huang

Tuesday, April 8, 2014

[ Level 2 ] Test Singleton Implementation in Python.

Days ago, just search how to use implement "Singleton" design pattern. (http://stackoverflow.com/questions/6760685/crea(ing-a-singleton-in-python) And today, I just need to implement for it. In my case, I need to create mutliple loggers and I also want to use "Singleton" to reduce system resource usage. Therefore, I create a Singleton meta class and also use a parameter called "singleton_id" to define the instance category. Source Code:
#!/bin/env python
class Singleton(object):
    _singleton_key = 'singleton_id'
    _instance = {}
    def __new__(class_, *args, **kwargs):
        if class_._singleton_key not in kwargs.keys():
            kwargs[class_._singleton_key] = ''
        if class_._singleton_key in kwargs and kwargs[class_._singleton_key] not in class_._instance.keys():
            class_._instance[kwargs[class_._singleton_key]] = object.__new__(class_)
        return class_._instance[kwargs[class_._singleton_key]]

class mySingleton(Singleton):
    def __init__(self, name, singleton_id=None):
        self.name = name
        pass

a0 = mySingleton('a')
print a0
print a0.name

a1 = mySingleton('a', singleton_id='a')
print a1
print a1.name

a2 = mySingleton('a', singleton_id='a')
print a2
print a2.name

a3 = mySingleton('b', singleton_id='a')
print a3
print a3.name

a4 = mySingleton('b', singleton_id='a')
print a4
print a4.name

print '----'
print a0
print a0.name
print a1
print a1.name
print a2
print a2.name
Output:
<__main__.mySingleton object at 0x7f2248065f10>
a
<__main__.mySingleton object at 0x7f2248065f50>
a
<__main__.mySingleton object at 0x7f2248065f50>
a
<__main__.mySingleton object at 0x7f2248065f50>
b
<__main__.mySingleton object at 0x7f2248065f50>
b
----
<__main__.mySingleton object at 0x7f2248065f10>
a
<__main__.mySingleton object at 0x7f2248065f50>
b
<__main__.mySingleton object at 0x7f2248065f50>
b

Wish this helps.

regards,
Stanley Huang

Monday, April 7, 2014

[Level 2] pstree implementation with Python.

Because I couldn't find pstree command in our company's product. Therefore, I just search from internet and found someone implement pstree layout with Python. (http://stackoverflow.com/questions/16395207/create-a-process-tree-like-pstree-command-with-python-in-linux) And I just modify it and create a pstree.py system utility.
#!/bin/env python
'''
## Implement pstree in Python

## data structure of tree, cmd_list
tree = {
  0: [1],
  1: [2, 3],
  2: [5, 6, 7, 8],
  ...
}

cmd_list = {
  0: '/sbin/init',
  1: '[kthreadd]',
  ...
}
'''

import os
import sys
import re
tree = {}
cmd_list = {}
def printTree(parent, tree, cmd_list, indent=''):
  print '%s:%s' % (parent, cmd_list[parent])
  if parent not in tree:
    return
  for child in tree[parent][:-1]:
    sys.stdout.write(indent + '|-')
    printTree(child, tree, cmd_list, indent + '| ')
  child = tree[parent][-1]
  sys.stdout.write(indent + '`-')
  printTree(child, tree, cmd_list, indent + '  ')

## __main__
for l in [ re.sub('[ ]+', ' ', l.lstrip().rstrip()) for l in os.popen('ps -e -o pid,ppid,command --no-heading').readlines() ]:
    pid = int(l.split(' ')[0])
    ppid = int(l.split(' ')[1])
    cmd = ' '.join(l.split(' ')[2:])
    if ppid not in tree.keys():
        tree[ppid] = []
    tree[ppid].append(pid)
    cmd_list[pid] = cmd

# print tree
# print cmd_list
pid = 1
if len(sys.argv) > 1:
  pid = int(sys.argv[1])
printTree(pid, tree, cmd_list)

Wish this helps.

regards,
Stanley Huang

Wednesday, April 2, 2014

[Level 1] Create secure web for iPython notebook.

The default protocol for iPython notebook is http and you didn't passphrase to enter notebook. If you want your notebook be secure, you could follow the steps to enable SSL and passphrase for it. 1. create profile:
In [1]: ## create profile for secure web
        !ipython profile create secureweb
2. create passphrase:
In [2]: ## create passphrase
        from IPython.lib import passwd
        passwd(passphrase='passphrase')
Out[2]: 'sha1:24be7c5ab59a:b8b7d3c691b2db67a5ef855b625cb560e125e5e1' 3. Create SSL certificate
$ cd /home/stanley/iPython_notebook/certs
$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
Generating a 1024 bit RSA private key
..............++++++
..........++++++
writing new private key to 'mycert.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:TW
State or Province Name (full name) [Some-State]:Taiwan
Locality Name (eg, city) []:Taipei
Organization Name (eg, company) [Internet Widgits Pty Ltd]:StanleyHuang
Organizational Unit Name (eg, section) []:Stanley
Common Name (e.g. server FQDN or YOUR name) []:Stanley
Email Address []:wenlien1001@gmail.com
$
4. Modify iPython notebook config.
$ cat ~/.config/ipython/profile_secureweb/ipython_notebook_config.py
c = get_config()
c.IPKernelApp.pylab = 'inline'
c.NotebookApp.certfile = u'/home/stanley/iPython_notebook/certs/mycert.pem'
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.password = u'sha1:24be7c5ab59a:b8b7d3c691b2db67a5ef855b625cb560e125e5e1'
c.NotebookApp.port = 9999
...
$
5. Restart iPython notebook, login with https protocol and enter the passphrase.
$ ipython notebook --profile=secureweb

Wish this helps.

regards,
Stanley Huang

[Level 1] Install slideshow support in iPython notebook.

Just found an iPython notebook extension support slideshow and you could install by the following steps. Precondition: Because this introduction would try to clone a github project, you have install git utility first.
$ sudo apt-get -y install git
1. Use the following commands to install slideshow support.
## get porfile directory
profile_dir = get_ipython().profile_dir.location

## clone extension from github
import os
tgt = os.path.join( profile_dir, 'static', 'custom')
!git clone https://github.com/ipython-contrib/IPython-notebook-extensions.git $tgt
%cd $tgt

## create a javascript for supporting slideshow
%%writefile custom.js
// we want strict javascript that fails
// on ambiguous syntax
"using strict";

// do not use notebook loaded  event as it is re-triggerd on
// revert to checkpoint but this allow extesnsion to be loaded
// late enough to work.
//

$([IPython.events]).on('app_initialized.NotebookApp', function(){


    /**  Use path to js file relative to /static/ dir without leading slash, or
     *  js extension.
     *  Link directly to file is js extension.
     *
     *  first argument of require is a **list** that can contains several modules if needed.
     **/

    // require(['custom/noscroll']);
    // require(['custom/clean_start'])
    // require(['custom/toggle_all_line_number'])
    // require(['custom/gist_it']);

    /**
     *  Link to entrypoint if extesnsion is a folder.
     *  to be consistent with commonjs module, the entrypoint is main.js
     *  here youcan also trigger a custom function on load that will do extra
     *  action with the module if needed
     **/
     require(['custom/slidemode/main'],function(slidemode){
    //     // do stuff
     })

});
2. restart iPython Notebook and reload page to get slideshow button. (Just on the right of cell type)
Wish this helps.

regards,
Stanley Huang

Tuesday, April 1, 2014

[Level 1] How to auto restore when start iPython

iPython have a magic command call "alias". This command just command "alias" in unix shell could help you to create alias command in iPython environment. How could we save the alias that we created before, you could use "store" magic command. The alias would be saved in iPython internal db. ex.
In [1]: %alias ipython_alias echo 'hello world'
In [2]: %store ipython_alias
After you exit iPython and restart it again, you need to restore the aliases from internal db.
In [1]: %store -r
And how could we make iPython auto-restore when we launch it? You could modify ipython_config.py in profile. ex. If you use default profile
$> cat /home/stanley/.config/ipython/profile_default/ipython_config.py 
...
# StoreMagics configuration
c.StoreMagics.autorestore = True # uncomment this line and assign autorestore as 'True'
...
You could also create a script to add alias automatically.
## Add often use commands.
security_commands = 'chmod chown scp ssh sudo'
network_commands = 'ifcofig netstat ping route'
system_commands = 'iostat ps top vmstat'
common_commands = 'cat grep head nohup tail'
editor_commands = 'meld soffice vi'
sourcecode_commands = 'apt-cache apt-get easy_install git p4 pip'
## prepare command list
command_list = ' '.join([security_commands, network_commands, system_commands, common_commands, editor_commands, sourcecode_commands])
for cmd in command_list.split(' '):
    %alias $cmd $cmd
    %store $cmd

Wish this helps.

regards,
Stanley Huang

Thursday, February 6, 2014

[ Level 1 ] Install libraries for juniper vpn in 13.10

$ sudo apt-get remove  sudo apt-get remove openjdk-6-jdk icedtea-6-plugin 
$ sudo apt-get install openjdk-7-jdk icedtea-7-plugin
$ sudo apt-get install openjdk-7-jre:i386
$ sudo apt-get install libstdc++6:i386 lib32z1 lib32ncurses5 lib32bz2-1.0 libxext6:i386 libxrender1:i386 libxtst6:i386 libxi6:i386

Wish this helps.

regards,
Stanley Huang