how to use command line tool in python @osdc.tw 2011
DESCRIPTION
introduce UCLTIP in OSDC.tw 2011 UCLTIP is a library makes you use command line tool in Python more easier. The original idea and most of basic codes are from GitPython project http://pypi.python.org/pypi/GitPython/TRANSCRIPT
本簡報圖檔下載於網際網路者,特別聲明為自由軟體推廣演講中進行「合理使用」,請讀者不要任意移置他用。
本簡報採用 創用 CC「姓名標示 -非商業性」 3.0 台灣條款
Use command line tool in Python
陳信屹 , Hsin-Yi Chen (hychen), <[email protected]>
2011/03/26, OSDC.tw 2011, 中央研究院人文科學大樓
開始前就先進廣告 !
■ Python Conference Taiwan 2011!
■ 歡迎投稿 , Call for paper!
■ 今年 5/28 ( 六 )
■ http://wiki.python.org.tw/PycTW2011
Who am I?
■ ID: hychen ( 阿宅都靠 ID 認親的 )
■ AboutMe: http://about.me/hychen
■ HackingThursday Meeting 成員
http://hackingthursday.wikidot.com/
再來一個廣告 !
■ Hacking Thursday 是由幾位居住於台北地區的自由軟體 /開放原碼開發者所發起的聚會。
■ 八卦閒聊 / 技術交流 / 點子發想 / 找人一起做專案
■ 每週四晚上於特定咖啡店 , 通常十幾人
■ http://www.hackingthursday.org/
H4 Meeting – hacking, health, happy, heyheyhey
http://timc.idv.tw/wordcloud/zh/#feed:http://hychen.wuweig.org/?feed=atom
The evolution of a Folk Programmer, Me
■ ~2006 Linux Server / Dorm Network Management
■ 2007 play 國軍 online
■ 2008 Web 2.0 Development
■ 2009 Embedded Linux Wireless Driver Integration
■ 2010~ Ubuntu Customization/Integration
The evolution of a Folk Programmer, Me
■ ~2006 Linux Server / Dorm Network Management
■ 2007 play 國軍 online
■ 2008 Web 2.0 Development
■ 2009 Embedded Linux Wireless Driver Integration
■ 2010~ Ubuntu Customization/Integration
PHP / Javascript / Python /
Play many linux distro / Shell Script, Perl, php
C / Python / Shell Script
Python / Shell Script
The evolution of a Folk Programmer, Me
■ ~2006 Linux Server / Dorm Network Management
■ 2007 play 國軍 online
■ 2008 Web 2.0 Development
■ 2009 Embedded Linux Wireless Driver Integration
■ 2010~ Ubuntu Customization/Integration
PHP / Javascript / Python /
Play many linux distro / Shell Script, Perl, php
C / Python / Shell Script
Python / Shell Script
系統
整合
Agenda
■ Execute a command and get result
■ Command Args and Options ↔ Python function args / kwargs
■ Making command line tool Python binding faster - UCLTIP
Execute a command and get result
四十歲之後,草木竹石皆可為劍。 ~ 笑傲江湖 , 金庸
os.system
■ Executing the command (a string) in a subshell.
■ Can use shell variable, ex $HOME
■ Can only get return code
■ Success → return 0
■ Failed → return any integer number != 0
commands.getoutput
■ Execute shell commands via os.popen() and return status, output.
■ Can use shell variable, ex $HOME
■
subprocess.*
■ To spawn processes, connect to their input/output/error pipes, and obtain their return codes.
■ Never call /bin/sh implicitly by default
■ Can not use shell variabl, ex $HOME
■ Replacement of
■ os.system, os.spawn*, os.popen*, popen2*, commands.*
Moresecurity
subprocess – Replacing os.system
■ subprocess.call
■ The arguments are list
■ Can only get return code
■ Success → return 0
■ Failed → return integer number != 0
■ subprocess.check_call : raise CalledProcessError if Failed
If you want run subprocess.call with string arg...
■ Shlex – a lexical analyzer class for simple shell-like syntaxes.
■ 'awk “{print 1}”' → ['awk', '{print 1}']
■
subprocess.Popen
■ spawn processes, connect to their input/output/error pipes
■ more control of process
■ Popen.communicate – get result
■ Popen.wait – wait command executed complete
■ Popen.sdtin ← None or subprocess.PIPE or File
■ Popen.stdout ← None or subprocess.PIPE or File
subprocess - Replacing shell pipe line
■ subprocess.Popen : control process
■ subprocess.PIPE
■
●Command Args and Options ↔ Python function args / kwargs
#/usr/bin/env pythonfrom GitPython Import this_idea
# ls --quoting-style=cls(quoting-style='c')
Python – function args
■ Function is a object too, every thing is object in Python
■ Function arguments is a tuple (a immutable list)
■ decouple the arguments tuple
■ func(args[0], args[1], args[2]...)↔func(*args)
Python – function kwargs
■ Function keyarguments is a dict (key-value pair list)
■ decouple the keyarguments
■ func(key1=value1, key2=value2,....) ↔ func(**kwargs)
■ func(args[0], key1=value1,...) ↔ func(*args, **kwargs)
■ non-keyword arg must before keyword arg
like hash in Perl
Introduce UCLTIP
Use Command Line Tool In Python
Transform CLI Tool arguments
■ The command string combine the option string if the value is True(boolean)
■ Example
■ user@host: expr 1 + 3 → expr(1, '+', 3)
UCLTIP - SingleCmd
■ For command without sub command
■ Create a callable instance by implemented __call__
■ Raises ComandNotFound if command not exists
■ Return
■ Success → return result string
■ Failed → raises CommandExecutedFalur
Transform CLI Tool Boolean option style
■ The command string combine the option string if the value is True(boolean)
■ Example
■ -d → func(d=True)
■ --dry-run → func(dry_run=True)
Transform CLI Tool Key-Value option style
■ The command string will execute combines If the option value is string or number
■ Example:
■ -t maverick → func(t='maverick')
■ --text hello → func(text='hello')
Transform CLI Tool Key-Value option style II
■ The command string will execute combines If the option value is string or number
■ Example:
■ -t=maverick → func(t='maverick')
■ --text=hello → func(text='hello')
command with sub command
■ Without prefix
■ git
■ pbuilder
■ apt-get
■ ...
■ With prefix
■ Zenity
■ ...
apt-get install vim zenity --info=text
UCLTIP - CmdDispatcher
■ For CLI tool has sub command
■ Command name → instance
■ Subcommand name → method name
■ args, options → method args, kwargs
■ Example:
■ apt-get install vim git -t maverick
■ apt-get.install('vim', 'git', t='maverick')
Use UCLTIP to create a CLI tool Python Binding
「這世界滿是假象, 我行的也是邪道。 」
~ 道士下山 , 徐皓峰
Python Binding with Native Code (Cython or Ctype)
Python Binding (UCLTIP))
UCLTIP helper
■ ucltip.use_helper
■ Shourcut
■ _c ↔ ucltip.SingeCmd
■ _d ↔ ucltip.CmdDispatcher
■ cQuery – execute commands like jQuery (Is it useful????)
■ _q('echo "a b c d e"').c('awk "{print $3}"').read()
■ register_singlecmds – create multiple SingleCmd one time
開發版本才有 , 在 cquery branch
開發版本才有 , 在 master branch
Conclusion of UCLTIP
■ Try to find a convenient way to use CLI tool in Python
■ Can use CLI tool in Python by OO way
■ Can create a work CLI tool Python binding soon
■ Performance may be not good
■ Need to modify codes if CLI tool input/output is changed
UCLTIP Installation
■ user@host# add-apt-repository ppa:ossug-hychen/python-ucltip
■ user@host# apt-get install python-ucltip
■ 要嘗鮮請用 github 上的版本 github.com/hychen/ucltip
Reference 1
■ UCLTIP Homepage :http://pypi.python.org/pypi/ucltip/
■ UCLTIP Source Code: http://github.com/hychen/ucltip
■ UCLTIP 0.1 Intro: http://hychen.wuweig.org/?p=748
■ Video of Create CLI Python binding faster by UCLTIP - http://tinyurl.com/4u4owp5
■ GitPython: http://gitorious.org/projects/git-python/
Reference 2
■ VSGUI – Implemeted Zenity binding by UCLTIP 0.1 (may not work now) https://github.com/hychen/vsgui
本簡報授權聲明
■ 此簡報內容採用 Creative Commons 「姓名標示 – 非商業性 台灣 3.0 版」授權條款
■ 陳信屹 , Hsin-Yi Chen (hychen)
■ Email: [email protected]
■ Http://about.me/hychen