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

Download How to use command line tool in Python @OSDC.tw 2011

Post on 14-May-2015

2.572 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

  • 1.Use command line tool in Python , Hsin-Yi Chen (hychen), 2011/03/26, OSDC.tw 2011, CC - 3.0

2. ! Python Conference Taiwan 2011! , Call for paper! 5/28 ( ) http://wiki.python.org.tw/PycTW2011 3. Who am I? ID: hychen ( ID ) AboutMe: http://about.me/hychen HackingThursday Meeting http://hackingthursday.wikidot.com/ 4. ! Hacking Thursday / / / / , http://www.hackingthursday.org/H4 Meeting hacking, health, happy, heyheyhey 5. http://timc.idv.tw/wordcloud/zh/#feed:http://hychen.wuweig.org/?feed=atom 6. 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 7. The evolution of a Folk Programmer, Me ~2006 Linux Server / Dorm Network Management Play many linux distro / Shell Script, Perl, php 2007 play online 2008 Web 2.0 Development PHP / Javascript / Python / 2009 Embedded Linux Wireless Driver Integration C / Python / Shell Script 2010~ Ubuntu Customization/Integration Python / Shell Script 8. The evolution of a Folk Programmer, Me ~2006 Linux Server / Dorm Network Management Play many linux distro / Shell Script, Perl, php 2007 play online 2008 Web 2.0 Development PHP / Javascript / Python / 2009 Embedded Linux Wireless Driver Integration C / Python / Shell Script 2010~ Ubuntu Customization/Integration Python / Shell Script 9. Agenda Execute a command and get result Command Args and Options Python function args /kwargs Making command line tool Python binding faster - UCLTIP 10. Execute a command and get result ~ , 11. 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 12. commands.getoutput Execute shell commands via os.popen() andreturn status, output. Can use shell variable, ex $HOME 13. Moresubprocess.* security To spawn processes, connect to their input/output/errorpipes, 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.* 14. 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 15. If you want run subprocess.call with string arg... Shlex a lexical analyzer class for simple shell-likesyntaxes. awk {print 1} [awk, {print 1}] 16. 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 17. subprocess - Replacing shell pipe line subprocess.Popen : control process subprocess.PIPE 18. Command Args and Options Python function args / kwargs#/usr/bin/env pythonfrom GitPython Import this_idea# ls --quoting-style=cls(quoting-style=c) 19. 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) 20. Python function kwargs Function keyarguments is a dict (key-value pair list) like hash in Perl 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 21. Introduce UCLTIP UseCommandLineToolInPython 22. Transform CLI Tool arguments The command string combine the option string if the valueis True(boolean) Example user@host: expr 1 + 3 expr(1, +, 3) 23. 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 24. Transform CLI Tool Boolean option style The command string combine the option string if the valueis True(boolean) Example -d func(d=True) --dry-run func(dry_run=True) 25. Transform CLI Tool Key-Value option style The command string will execute combines If the optionvalue is string or number Example:-t maverick func(t=maverick)--text hello func(text=hello) 26. Transform CLI Tool Key-Value option style II The command string will execute combines If the optionvalue is string or number Example:-t= maverick func(t=maverick)--text= hello func(text=hello) 27. command with sub command Without prefix With prefix git Zenity pbuilder ... apt-get ...apt-get install vimzenity --info=text 28. 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) 29. Use UCLTIP to create a CLI tool Python Binding ~ , 30. Python Binding with Native Code(Cython or Ctype) 31. Python Binding (UCLTIP)) 32. UCLTIP helper ucltip.use_helper Shourcut _c ucltip.SingeCmd _d ucltip.CmdDispatcher , cquery branch cQuery execute commands like jQuery (Is it useful????) _q(echo "a b c d e").c(awk "{print $3}").read() , master branch register_singlecmds create multiple SingleCmd one time 33. 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 34. UCLTIP Installation user@host# add-apt-repository ppa:ossug-hychen/python-ucltip user@host# apt-get install python-ucltip github github.com/hychen/ucltip 35. 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/ 36. Reference 2 VSGUI Implemeted Zenity binding by UCLTIP 0.1 (may notwork now) https://github.com/hychen/vsgui 37. Creative Commons 3.0 , Hsin-Yi Chen (hychen) Email: ossug.hychen@gmail.com Http://about.me/hychen

Recommended

View more >