-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_bootstrap.py
More file actions
130 lines (111 loc) · 4.62 KB
/
auto_bootstrap.py
File metadata and controls
130 lines (111 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os, sys, subprocess, textwrap, glob
ROOT = os.path.dirname(os.path.abspath(__file__))
VENV_URL = "http://github.com/downloads/zyelabs/simple-bootstrap/virtualenv.py"
def setup(project = "TESTING"):
'''
Automatically generates a bootstrap script, and then runs it.
Usage:
python auto_bootstrap.py <virtualenvironment_name>
The auto generated bootstrap scriptuses pip to install all requirements found in requirements/*.txt
Example:
requirements/local_apps.txt
requirements/remote_required.txt
requirements/optional_libs.txt
Requirements files are processed in the following order:
1. any .txt file with "local" in the name
2. any .txt file with "required" in the name
3. any other .txt files
4. any .txt file with "optional" in the name
'''
try:
import virtualenv
except:
print 'Downloading VirtualEnv Package'.ljust(50,'.'),
loc_venv = os.path.join(ROOT, 'virtualenv.py')
download(VENV_URL,loc_venv)
print 'Done\n'
import virtualenv
print 'Creating Bootstrap Script'.ljust(50,'.'),
extra_text = textwrap.dedent('''
"""
Bootstrap include script created with simple-bootstrap & virtualenv
- This scripts sets up a virtualenv
- Install packages using pip, checks "requirements/" dir for .txt files and installs all packages listed in the .txt
- http://github.com/zyelabs/simple-bootstrap
"""
import glob
def get_ordered_files(path):
f = []
for ifile in glob.glob( os.path.join(path,"requirements", '*.txt') ):
if ifile.find("local")>-1:
f.append((0,ifile))
elif ifile.find("required")>-1:
f.append((1,ifile))
elif ifile.find("optional")>-1:
f.append((3,ifile))
else:
f.append((2,ifile))
f.sort()
return f
def after_install(options, home_dir):
pwd = os.path.dirname(os.path.abspath(__file__))
if sys.platform == 'win32':
bin = "Scripts"
cmd_list = [os.path.join(home_dir,bin,"pip"), "install",
"-E",os.path.join(pwd, home_dir),
"--enable-site-packages",
"--requirement"]
else:
bin = "bin"
cmd_list = ["python",os.path.join(pwd,"pip.py"), "install",
"-E",os.path.join(pwd, home_dir),
"--enable-site-packages",
"--requirement"]
try:
import pip
try:
print "Found pip, moving along".ljust(50,'.')
f = open('pip.py', 'r')
except:
print os.path.join(pwd, home_dir)
print "Found pip, moving along".ljust(50,'.')
cmd_list = ["pip", "install",
"-E",os.path.join(pwd, home_dir),
"--enable-site-packages",
"--requirement"]
except:
print "Downloading pip".ljust(50,'.')
import urllib2
fileurl = "http://github.com/downloads/zyelabs/simple-bootstrap/pip.py"
tofile = os.path.join(pwd,"pip.py")
u = urllib2.urlopen(fileurl)
localFile = open(tofile, 'w')
localFile.write(u.read())
localFile.close()
print "Installing Requirements".ljust(50,'.')
files = get_ordered_files(pwd)
subprocess.call(["python", os.path.join(home_dir,bin,"activate_this.py")])
for f in files:
print "Requirements file ", f[1]
print ''.ljust(50,'.')
file_cmd = cmd_list + [f[1]]
subprocess.call(file_cmd)
print "Done" ''')
bootstrap_text = virtualenv.create_bootstrap_script(extra_text)
f = open('bootstrap.py', 'w').write(bootstrap_text)
print 'Done\n'
print 'Executing Bootstrap Script'.ljust(50,'.')
subprocess.call(["python", "bootstrap.py",project ])
print '\n'
print 'Environment Has been setup'.ljust(50,".")
def download(fileurl, tofile):
import urllib2
u = urllib2.urlopen(fileurl)
localFile = open(tofile, 'w')
localFile.write(u.read())
localFile.close()
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'Please specify destination folder name as first argument'
else:
setup(str(sys.argv[1]))