how to use command line tool in python @osdc.tw 2011

37
本簡報圖檔下載於網際網路者,特別聲明為自由軟體推廣演講中進行「合理使用」,請讀者不要任意移置他用。 本簡報採用 創用 CC 「姓名標示 - 非商業性」 3.0 台灣條款 Use command line tool in Python 陳信屹 , Hsin-Yi Chen (hychen), <[email protected]> 2011/03/26, OSDC.tw 2011, 中央研究院人文科學大樓

Upload: hsinyi-chen

Post on 14-May-2015

2.590 views

Category:

Technology


8 download

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

Page 1: How to use command line tool in Python @OSDC.tw 2011

本簡報圖檔下載於網際網路者,特別聲明為自由軟體推廣演講中進行「合理使用」,請讀者不要任意移置他用。

本簡報採用 創用 CC「姓名標示 -非商業性」 3.0 台灣條款

Use command line tool in Python

陳信屹 , Hsin-Yi Chen (hychen), <[email protected]>

2011/03/26, OSDC.tw 2011, 中央研究院人文科學大樓

Page 2: How to use command line tool in Python @OSDC.tw 2011

開始前就先進廣告 !

■ Python Conference Taiwan 2011!

■ 歡迎投稿 , Call for paper!

■ 今年 5/28 ( 六 )

■ http://wiki.python.org.tw/PycTW2011

Page 3: How to use command line tool in Python @OSDC.tw 2011

Who am I?

■ ID: hychen ( 阿宅都靠 ID 認親的 )

■ AboutMe: http://about.me/hychen

■ HackingThursday Meeting 成員

http://hackingthursday.wikidot.com/

Page 4: How to use command line tool in Python @OSDC.tw 2011

再來一個廣告 !

■ Hacking Thursday 是由幾位居住於台北地區的自由軟體 /開放原碼開發者所發起的聚會。

■ 八卦閒聊 / 技術交流 / 點子發想 / 找人一起做專案

■ 每週四晚上於特定咖啡店 , 通常十幾人

■ http://www.hackingthursday.org/

H4 Meeting – hacking, health, happy, heyheyhey

Page 5: How to use command line tool in Python @OSDC.tw 2011

http://timc.idv.tw/wordcloud/zh/#feed:http://hychen.wuweig.org/?feed=atom

Page 6: How to use command line tool in Python @OSDC.tw 2011

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

Page 7: How to use command line tool in Python @OSDC.tw 2011

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

Page 8: How to use command line tool in Python @OSDC.tw 2011

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

系統

整合

Page 9: How to use command line tool in Python @OSDC.tw 2011

Agenda

■ Execute a command and get result

■ Command Args and Options ↔ Python function args / kwargs

■ Making command line tool Python binding faster - UCLTIP

Page 10: How to use command line tool in Python @OSDC.tw 2011

Execute a command and get result

四十歲之後,草木竹石皆可為劍。 ~ 笑傲江湖 , 金庸

Page 11: How to use command line tool in Python @OSDC.tw 2011

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

Page 12: How to use command line tool in Python @OSDC.tw 2011

commands.getoutput

■ Execute shell commands via os.popen() and return status, output.

■ Can use shell variable, ex $HOME

Page 13: How to use command line tool in Python @OSDC.tw 2011

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

Page 14: How to use command line tool in Python @OSDC.tw 2011

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

Page 15: How to use command line tool in Python @OSDC.tw 2011

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}']

Page 16: How to use command line tool in Python @OSDC.tw 2011

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

Page 17: How to use command line tool in Python @OSDC.tw 2011

subprocess - Replacing shell pipe line

■ subprocess.Popen : control process

■ subprocess.PIPE

Page 18: How to use command line tool in Python @OSDC.tw 2011

●Command Args and Options ↔ Python function args / kwargs

#/usr/bin/env pythonfrom GitPython Import this_idea

# ls --quoting-style=cls(quoting-style='c')

Page 19: How to use command line tool in Python @OSDC.tw 2011

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)

Page 20: How to use command line tool in Python @OSDC.tw 2011

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

Page 21: How to use command line tool in Python @OSDC.tw 2011

Introduce UCLTIP

Use Command Line Tool In Python

Page 22: How to use command line tool in Python @OSDC.tw 2011

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)

Page 23: How to use command line tool in Python @OSDC.tw 2011

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

Page 24: How to use command line tool in Python @OSDC.tw 2011

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)

Page 25: How to use command line tool in Python @OSDC.tw 2011

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')

Page 26: How to use command line tool in Python @OSDC.tw 2011

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')

Page 27: How to use command line tool in Python @OSDC.tw 2011

command with sub command

■ Without prefix

■ git

■ pbuilder

■ apt-get

■ ...

■ With prefix

■ Zenity

■ ...

apt-get install vim zenity --info=text

Page 28: How to use command line tool in Python @OSDC.tw 2011

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')

Page 29: How to use command line tool in Python @OSDC.tw 2011

Use UCLTIP to create a CLI tool Python Binding

「這世界滿是假象, 我行的也是邪道。 」

~ 道士下山 , 徐皓峰

Page 30: How to use command line tool in Python @OSDC.tw 2011

Python Binding with Native Code (Cython or Ctype)

Page 31: How to use command line tool in Python @OSDC.tw 2011

Python Binding (UCLTIP))

Page 32: How to use command line tool in Python @OSDC.tw 2011

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

Page 33: How to use command line tool in Python @OSDC.tw 2011

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

Page 34: How to use command line tool in Python @OSDC.tw 2011

UCLTIP Installation

■ user@host# add-apt-repository ppa:ossug-hychen/python-ucltip

■ user@host# apt-get install python-ucltip

■ 要嘗鮮請用 github 上的版本 github.com/hychen/ucltip

Page 35: How to use command line tool in Python @OSDC.tw 2011

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/

Page 36: How to use command line tool in Python @OSDC.tw 2011

Reference 2

■ VSGUI – Implemeted Zenity binding by UCLTIP 0.1 (may not work now) https://github.com/hychen/vsgui

Page 37: How to use command line tool in Python @OSDC.tw 2011

本簡報授權聲明

■ 此簡報內容採用 Creative Commons 「姓名標示 – 非商業性 台灣 3.0 版」授權條款

■ 陳信屹 , Hsin-Yi Chen (hychen)

■ Email: [email protected]

■ Http://about.me/hychen