Skip to content

Instantly share code, notes, and snippets.

@edc
Last active December 16, 2015 10:49
Show Gist options
  • Save edc/5422837 to your computer and use it in GitHub Desktop.
Save edc/5422837 to your computer and use it in GitHub Desktop.
A simple bootstrap generator using virtualenv. It packs a standalone script (`src.py`), a run script (`run.sh`) into the generated bootstrap script. The bootstrap script will also install dependency based on `requirements.txt`, and run the pip command in a controlled shell environment declared in `bootstrap.env`. The controlled environment allow…
import sys
import os
import json
import virtualenv
SOURCE = 'src.py'
RUNSH = 'run.sh'
REQUIREMENTS = 'requirements.txt'
BOOTSTRAPENV = 'bootstrap.env'
#Example bootstrap.env:
#
# # environment variables used in bootstrap pip call
# # add !reset to start from an empty environment
# !reset
# LD_LIBRARY_PATH=/opt/R/2.10.1-gcc/lib64/R/lib
# PATH=/usr/bin:/bin:/opt/R/2.10.1-gcc/bin
if len(sys.argv) > 1:
dir = sys.argv[1]
else:
dir = 'dist'
if os.path.exists(REQUIREMENTS):
requires = [i.strip() for i in open(REQUIREMENTS).readlines()]
else:
requires = []
env = []
if os.path.exists(BOOTSTRAPENV):
lines = [i.split('#')[0].strip() for i in open(BOOTSTRAPENV).readlines()]
for line in lines:
if '!reset' == line:
env = ['env', '-i'] + env
elif line:
env.append(line)
src_py = open(SOURCE).read()
run_sh = open(RUNSH).read()
output = virtualenv.create_bootstrap_script('''
import subprocess, os
def adjust_options(options, args):
if len(args) == 0:
args.append('.venv')
options.python = 'python2.7'
def after_install(options, home_dir):
home_dir = os.path.abspath(home_dir)
requires = {requires}
env = {env}
run_sh = {run_sh}
for r in requires:
subprocess.call(env +
[os.path.join(home_dir, 'bin', 'pip'), 'install', r])
# rewrite a new main.py with hashbang
py = os.path.join(home_dir, 'bin', 'python2.7')
main_py = '#!' + py + '\\n' + {src_py}
open('main.py', 'w').write(main_py)
os.chmod('main.py', 0755)
# unpack run.sh
open('run.sh', 'w').write(run_sh)
os.chmod('run.sh', 0755)
print "Boostrap is successful. Now you can run './run.sh'"
'''.format(env=json.dumps(env), run_sh=json.dumps(run_sh),
requires=json.dumps(requires), src_py=json.dumps(src_py)))
if not os.path.exists(dir):
os.mkdir(dir)
open('%s/bootstrap.py' % dir, 'w').write(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment