LoginSignup
18
18

More than 5 years have passed since last update.

fabricのお役立ちコンテキストマネージャ

Posted at

fabric.api.path: $PATHを操作できる

fabric.api.path を使うとPATHをいじれる。

例として、Pythonをソースからビルドし、/usr/local/bin 配下に配置し、ここのpythonを実行する。

fabpath.py
import fabric.api as api

with api.path("/usr/local/bin", behavior="prepend"):
    api.run("python -V")

behavior="prepend" を指定することによって

PATH=/usr/local/bin:$PATH

このように展開される。

fabric.api.prefix: 事前処理を実行させる

fabric.api.prefix を使うとwithブロックの中のすべてのrun, sudoの命令の前に指定した処理を挿入できる。

例えばvirtualenvを有効にする場合に使う。

enablevenv.py
import fabric.api as api

with api.prefix(". /home/www/bin/activate"):
    fabric.api.run("./manage.py syncdb")

fabric.api.shell_env: シェルの環境変数を追加する

例として、pythonコマンドでDjangoのスクリプトを実行する。

djscript.py
import fabric.api as api

with api.env_shell("PYTHONPATH=. DJANGO_SETTINGS_MODULE=settings.imagawa"):
    api.run("python dosomething.py")
18
18
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18