Wednesday, May 29, 2013

[ Level 1 ] create cpio file with find.

Cpio is a good tool to pack files in one image, and it's fast.
You could use find command to find files that you want and also use gzip to compress it.
ex.
$ find ./ -print | cpio -o -Hnewc | gzip > /tmp/my.cpio.gz

Wish this helps.

regards,
Stanley Huang

Tuesday, May 28, 2013

[ Level 2 ] Create an egg for Python in Ubuntu.

How to create an egg file for Python.
First of all, you must have setuptools module.
$ sudo apt-get -y install python-setuptools

Now, you could try to create an empty egg now.
$ mkdir /tmp/demo
$ cd /tmp/demo
$ cat &ht; ./setup.py <<EOF
#!/bin/env python
#-*- coding:utf-8 -*-
from setuptools import setup
setup()
EOF
$ python setup.py bdist_egg ## bdist_egg is the option for creating egg.
$ ls -ALb
build  dist  setup.py  UNKNOWN.egg-info
You could find, we have three more directories after you execute setup.py
build ->
dist -> final egg file
UNKNOW.egg-info -> egg info

Now, we could give setuptools more information about egg.
cat > ./setup.py <<EOF
#!/bin/env python
#-*- coding:utf-8 -*-
from setuptools import setup, find_packages
setup(
        name = "my_first_egg",
        version="0.0.1",
        packages = find_packages(),
        zip_safe = False,

        description = "my first egg.",
        long_description = "long description of my first egg.",
        author = "Stanley Huang",
        author_email = "wenlien1001 at gmail.com",

        license = "GPLv2",
        keywords = ("demo", "python", "egg"),
        platforms = "Independant",
        url = "http://stanley-huang.blogspot.tw/",
        )
EOF
And you could create a sub folder named as "name" attribute (my_first_egg) and put your source code into ti.
$ mkdir ./my_first_app
$ touch ./my_first_app/__init__.py
$ cat > ./my_first_app/helloworld.py <<EOF
#!/bin/env python
#-*- coding:utf-8 -*-
print 'Hello World!'
EOF
$ chomd a+x ./my_first_app/helloworld.py
$ python setup.py bdist_egg
$ file ./dist/my_first_egg-0.0.1-py2.7.egg 
./dist/my_first_egg-0.0.1-py2.7.egg: Zip archive data, at least v2.0 to extract
$ unzip -l ./dist/my_first_egg-0.0.1-py2.7.egg
Archive:  ./dist/my_first_egg-0.0.1-py2.7.egg
  Length      Date    Time    Name
---------  ---------- -----   ----
        1  2013-05-28 08:40   EGG-INFO/dependency_links.txt
      239  2013-05-28 08:40   EGG-INFO/SOURCES.txt
        1  2013-05-28 08:40   EGG-INFO/not-zip-safe
       13  2013-05-28 08:40   EGG-INFO/top_level.txt
      295  2013-05-28 08:40   EGG-INFO/PKG-INFO
       38  2013-05-28 08:40   my_first_egg/helloworld.py
        0  2013-05-28 08:39   my_first_egg/__init__.py
      164  2013-05-28 08:40   my_first_egg/helloworld.pyc
      140  2013-05-28 08:40   my_first_egg/__init__.pyc
---------                     -------
      891                     9 files

Usually we put source file in src folder, therefore, you may want to change finding direcotory.
You could change the setting in setup.py
packages=find_packages('src'),
package_dir = {'':'src'}
You could try it!
Wish this helps.

regards,
Stanley Huang

Wednesday, May 22, 2013

[ Level 2 ] Fix duplicate vg/lv name.

1. scan vgs:
$ vgscan
2. get vg information:
$ vgdisplay
  --- Volume group ---
  VG Name               vg00
...
  VG UUID               FUoK9F-9mgs-jO4M-vub1-Y7zs-6wpQ-uM2S11
  --- Volume group ---
  VG Name               vg00
...
  VG UUID               FUoK9F-9mgs-jO4M-vub1-Y7zs-6wpQ-uM2S12
3. export vg
$ vgexport
4. rename vg name
$ vgrename FUoK9F-9mgs-jO4M-vub1-Y7zs-6wpQ-uM2S12 vg01
5. import vg
$ vgimport vg01
6. get vg information:
$ vgdisplay
  --- Volume group ---
  VG Name               vg00
...
  VG UUID               FUoK9F-9mgs-jO4M-vub1-Y7zs-6wpQ-uM2S11
  --- Volume group ---
  VG Name               vg01
...
  VG UUID               FUoK9F-9mgs-jO4M-vub1-Y7zs-6wpQ-uM2S12
7. get lv information
$ lvscan
  ACTIVE            '/dev/vg00/lv00' [13.99 GiB] inherit
  inactive            '/dev/vg01/lv00' [13.99 GiB] inherit
8. active vg01
$ vgchange --ignorelockingfailure --noudevsync --sysinit -ay vg01 >/dev/null 2>&1 &&
9. rename lv name(optional) 
$ lvrename vg01 lv00 lv01
10. get lv information
$ lvscan
  ACTIVE            '/dev/vg00/lv00' [13.99 GiB] inherit
  ACTIVE            '/dev/vg01/lv01' [13.99 GiB] inherit
Wish this helps.

regards,
Stanley Huang

Monday, May 6, 2013

[ Level 2 ] Get last argument in bash.

You could use the following methods to get the last argument that you pass to script.
#!/bin/bash
getLastArg1() {
  for last
  do
    true
  done
  echo $last
}

getLastArg2() {
  echo $@ | awk '{print($NF)}'
}

getLastArg3() {
  eval echo `echo \\$${#@}`
}

getLastArg1 $@
getLastArg2 $@
getLastArg3 $@
$ ./test.sh a b c
c
c
c
$

Wish this helps.

regards,
Stanley Huang

Sunday, May 5, 2013

[ Level 3 ] Test your Python code.

Ref:
https://python-guide.readthedocs.org/en/latest/writing/tests.html

Mock:
http://www.voidspace.org.uk/python/mock/getting-started.html#

Wish this helps.

regards,
Stanley Huang

[ Level 2 ] Tips of unittest for Python.

There are tips for Python:
1. Use mock for replace method.
ex.
myMock=mox.Mox()
myMock.StubOutWithMock(myModule, 'replaceMethod')
myModule.replaceMethod(input).AndReturn('Hello World!')
myMock.ReplayAll()
expected = 'Hello World!'
self.assertEqual(module.replaceMethod(input), expected)
myMock.VerifyAll()  #verify if all mocks be executed.
myMock.UnsetStubs() #release all mocks

2. Use -m to test one method only.
ex.
$ python -m unittest myApp.TestClass.testMethod

Wish this helps.

regards,
Stanley Huang

[ Level 2 ] Coverage in Python.

$ sudo pip install coverage
$ coverage run myApp.py arg1 arg2...
$ coverage report -m
$ coverage html
$ coverage help (run) ## or coverage run --help

Commands of coverage:
    run – Run a Python program and collect execution data.
    report – Report coverage results.
    html – Produce annotated HTML listings with coverage results.
    xml – Produce an XML report with coverage results.
    annotate – Annotate source files with coverage results.
    erase – Erase previously collected coverage data.
    combine – Combine together a number of data files.
    debug – Get diagnostic information.

Ref:
http://nedbatchelder.com/code/coverage/
http://nedbatchelder.com/code/coverage/cmd.html#cmd

Wish this helps.

regards,
Stanley Huang

Saturday, May 4, 2013

[ Level 1 ] The popular MySQL GUI client tools

There are the popular MySQL GUI client tools:
. Aqua Data Studio. http://www.aquafold.com/aquadatastudio_downloads.html
. CLIENT FOR MYSQL BY ENGINSITE. http://www.enginsite.com/Download.htm
. dbForge Studio. http://www.devart.com/dbforge/mysql/studio/ 
. DBTools Manager. http://www.dbtools.com.br/EN/dbmanagerpro/ 
. DbVisualizer. http://www.dbvis.com/
. Dreamcoder for MySQL. http://www.sqldeveloper.net/database-tools/mysql/overview.html
. HeidiSQL. http://www.heidisql.com/ 
. Navicat. http://www.navicat.com/en/products/navicat_mysql/mysql_overview.html
. MyDB Studio. http://www.mydb-studio.com 
. phpMyAdmin. http://www.phpmyadmin.net/home_page/
. Sequel Pro. http://www.sequelpro.com/
. SQL Examiner Suite. http://www.sqlaccessories.com/SQL_Examiner_Suite/
. SQL Maestro MySQL Tools Family. http://www.sqlmaestro.com/products/mysql/ 
. SQLWave. http://www.nerocode.com/ 
. SQLyog. https://www.webyog.com/
. SQuirreL. http://squirrel-sql.sourceforge.net/
. Toad for MySQL. http://www.quest.com/toad-for-mysql/
. Workbench. http://dev.mysql.com/downloads/workbench/
. xBaseView. http://www.xbaseview.com/download.htm
Ref:
http://www.databasejournal.com/features/mysql/article.php/3880961/Top-10-MySQL-GUI-Tools.htm
http://stackoverflow.com/questions/9185/what-is-the-best-mysql-client-application-for-windows
http://www.sqldeveloper.net/database-tools/mysql/overview.html
http://www.freshdesignweb.com/10top-mysql-client-interface-gui.html

Wish this helps.

regards,
Stanley Huang

[ Level 3 ] How to build MySQL client tool - mysql

First, you have to download MySQL source and use the following command to build it.
$ ./configure --without-server --enable-thread-safe-client 
  --with-client-ldflags=-all-static --prefix=/usr/local/mysql 
   --with-machine-type=powerpc --with-zlib-dir=/usr/local/zlib 
    --without-debug  --without-docs --with-big-tables 
If you don't want to build it, there is a way to get it. (The sample is for Ubuntu only.)
$ apt-get install libmysqlclient15-dev

Wish this helps.

regards,
Stanley Huang