壳牌自动化工具,如类固醇制造。
shell-utils的Python项目详细描述
说明
当需要使用shell命令自动执行某些进程时,shell_utils
库提供了一些方便的实用程序。
在这里,您可以编写bash脚本,也可以使用python脚本中的subprocess
、os
和sys
模块编写bash脚本。
自动化脚本的一些模式和快捷方式。
假设我们有一个新项目需要自动化一些构建过程。我们可能会尝试编写makefile或bash 帮助完成该任务的脚本。如果这对你有用的话,太好了。不过,如果你和我一样,你更喜欢用python处理所有事情。
我们可以使用shell实用程序创建一个自动化脚本,该脚本的行为与makefile非常相似,但是 我们想要Python。
对click
库的一些熟悉将有帮助。
pip3 install shell_utils shell_utils generate_runner
这将生成一个可执行的python脚本,其中包含以下代码
#!/usr/bin/env python3importosfrompathlibimportPathfromshell_utilsimportshell,cd,env,path,quietimportclick@click.group()defmain():""" Development tasks; programmatically generated """# ensure we're running commands from project rootroot=Path(__file__).parent.absolute()cwd=Path().absolute()ifroot!=cwd:click.secho(f'Navigating from {cwd} to {root}',fg='yellow')os.chdir(root)if__name__=='__main__':main()
现在假设我们使用sphinx生成项目目录docs
中的文档。
如果我们想创建一个命令来重新生成文档,并在完成后打开一个浏览器窗口,
我们可以在生成的run.py
脚本中添加以下代码
@main.command()@click.option('--no-browser',is_flag=True,help="Don't open browser after building docs.")defdocs(no_browser):""" Generate Sphinx HTML documentation, including API docs. """shell(""" rm -f docs/shell_utils.rst rm -f docs/modules.rst rm -rf docs/shell_utils* sphinx-apidoc -o docs/ shell_utils """)withcd('docs'):shell('make clean')shell('make html')shell('cp -rf docs/_build/html/ public/')ifno_browser:returnshell('open public/index.html')
然后,我们可以执行以下命令来执行我们想要的操作:
./run.py docs
发送到shell
函数的字符串将在bash
子进程shell中执行。在他们被处决之前,
shell
函数将命令打印到stdout
,类似于Makefile
。
另外,请注意,我们使用上下文管理器将目录更改为docs
,这样就可以将命令传递给shell
函数
将在该目录中执行。一旦超出上下文管理器的作用域,进一步的shell
函数命令将再次运行
从项目根目录。
函数和上下文管理器
外壳
在bash shell中执行给定的命令。它只是subprocess.run
周围的一个薄包装,添加了一些方便的功能,
例如在执行之前打印它将要运行的命令。
fromshell_utilsimportshellp1=shell('echo hello, world')print(p1)p2=shell('echo goodbye, cruel world',capture=True)print('captured the string:',p2.stdout)
输出
user@hostname executing...
echo goodbye, cruel world
captured the string: goodbye, cruel world
cd
在上下文范围内临时更改当前工作目录。
在python外壳中…
fromshell_utilsimportshell,cdwithcd('~'):shell('echo $PWD')shell('mkdir -p foo')withcd('foo'):shell('echo $PWD')shell('echo $PWD')
输出
user@hostname executing... echo$PWD /Users/user user@hostname executing... mkdir -p foo user@hostname executing... echo$PWD /Users/user/foo user@hostname executing... echo$PWD /Users/user
环境
临时更改环境变量
fromshell_utilsimportenvimportosprint(os.getenv('foo','nothing'))withenv(foo='bar'):print(os.getenv('foo'))print(os.getenv('foo','nothing again'))
输出
nothing bar nothing again
路径
改变$path的env
上下文管理器的特例。它将~
展开到主目录并返回
$path变量的元素作为列表。
fromshell_utilsimportpathimportosdefprint_path():print('$PATH ==',os.getenv('PATH'))print_path()withpath('~',prepend=True)asplist:print_path()print(plist)
输出
$PATH== /Users/user/.venvs/shell-utils-py3.7/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin $PATH== /Users/user:/Users/user/.venvs/shell-utils-py3.7/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin ['/Users/user', '/Users/user/.venvs/shell-utils-py3.7/bin', '/usr/local/sbin', '/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin', '/Library/TeX/texbin']