|
1 """ |
|
2 Call this like ``python create_python_env.py``; it will |
|
3 refresh the project-boot.py script |
|
4 |
|
5 -prerequisite: |
|
6 |
|
7 - virtualenv |
|
8 |
|
9 - python project-boot.py --unzip-setuptools --no-site-packages --clear --type-install=local <path_to_venv> |
|
10 |
|
11 """ |
|
12 |
|
13 import os |
|
14 import subprocess |
|
15 import re |
|
16 import sys |
|
17 |
|
18 |
|
19 here = os.path.dirname(os.path.abspath(__file__)) |
|
20 base_dir = here |
|
21 script_name = os.path.join(base_dir, 'project-boot.py') |
|
22 |
|
23 import virtualenv |
|
24 |
|
25 # things to install |
|
26 # - psycopg2 -> pip |
|
27 # - PIL -> pip |
|
28 # - pyxml -> pip |
|
29 # - 4Suite-xml - easy_install ftp://ftp.4suite.org/pub/4Suite/4Suite-XML-1.0.2.tar.bz2 |
|
30 # - pylucene - script |
|
31 |
|
32 src_base = os.path.join(here,"res","src").replace("\\","/") |
|
33 lib_path = os.path.abspath(os.path.join(here,"res","lib")).replace("\\","/") |
|
34 |
|
35 EXTRA_TEXT = "URLS = { \n" |
|
36 |
|
37 EXTRA_TEXT += " 'DISTRIBUTE' : { 'setup': 'distribute', 'url': 'http://pypi.python.org/packages/source/d/distribute/distribute-0.6.14.tar.gz', 'local': '"+ os.path.abspath(os.path.join(src_base,"distribute-0.6.14.tar.gz")).replace("\\","/")+"'},\n" |
|
38 EXTRA_TEXT += " 'MERCURIAL' : { 'setup': 'distribute', 'url': 'http://pypi.python.org/packages/source/d/mercurial/mercurial-1.7.5.tar.gz', 'local': '"+ os.path.abspath(os.path.join(src_base,"mercurial-1.7.5.tar.gz")).replace("\\","/")+"'},\n" |
|
39 EXTRA_TEXT += "}\n" |
|
40 |
|
41 EXTRA_TEXT += "import sys\n" |
|
42 EXTRA_TEXT += "sys.path.append('"+lib_path+"')\n" |
|
43 |
|
44 EXTRA_TEXT += """ |
|
45 |
|
46 import shutil |
|
47 import tarfile |
|
48 import zipfile |
|
49 import urllib |
|
50 import platform |
|
51 |
|
52 |
|
53 INDEX_URL = 'http://pypi.python.org/simple/' |
|
54 |
|
55 |
|
56 def extend_parser(parser): |
|
57 parser.add_option( |
|
58 '--index-url', |
|
59 metavar='INDEX_URL', |
|
60 dest='index_url', |
|
61 default='', |
|
62 help='base URL of Python Package Index') |
|
63 parser.add_option( |
|
64 '--type-install', |
|
65 metavar='type_install', |
|
66 dest='type_install', |
|
67 default='local', |
|
68 help='type install : local, url, setup') |
|
69 parser.add_option( |
|
70 '--ignore-packages', |
|
71 metavar='ignore_packages', |
|
72 dest='ignore_packages', |
|
73 default=None, |
|
74 help='list of comma separated keys for package to ignore') |
|
75 |
|
76 |
|
77 |
|
78 def adjust_options(options, args): |
|
79 pass |
|
80 |
|
81 |
|
82 def after_install(options, home_dir): |
|
83 home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir) |
|
84 base_dir = os.path.dirname(home_dir) |
|
85 src_dir = join(home_dir, 'src') |
|
86 tmp_dir = join(home_dir, 'tmp') |
|
87 ensure_dir(src_dir) |
|
88 ensure_dir(tmp_dir) |
|
89 system_str = platform.system() |
|
90 |
|
91 res_source_key = options.type_install |
|
92 |
|
93 ignore_packages = [] |
|
94 |
|
95 if options.ignore_packages : |
|
96 ignore_packages = options.ignore_packages.split(",") |
|
97 |
|
98 logger.indent += 2 |
|
99 try: |
|
100 NORMAL_INSTALL = [ #(key,method, option_str, extra_env) |
|
101 ('DISTRIBUTE', 'pip', None, None), |
|
102 ('MERCURIAL', 'pip', None, None), |
|
103 ] |
|
104 |
|
105 |
|
106 for key, method, option_str, extra_env in NORMAL_INSTALL: |
|
107 if key not in ignore_packages: |
|
108 normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir) |
|
109 |
|
110 logger.notify("Clear source dir") |
|
111 shutil.rmtree(src_dir) |
|
112 |
|
113 finally: |
|
114 logger.indent -= 2 |
|
115 script_dir = join(base_dir, bin_dir) |
|
116 logger.notify('Run "%s Package" to install new packages that provide builds' |
|
117 % join(script_dir, 'easy_install')) |
|
118 |
|
119 |
|
120 def normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir): |
|
121 logger.notify("Install %s from %s with %s" % (key,URLS[key][res_source_key],method)) |
|
122 if method == 'pip': |
|
123 if sys.platform == 'win32': |
|
124 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'pip')), 'install', '-E', os.path.abspath(home_dir), URLS[key][res_source_key]] |
|
125 else: |
|
126 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', '-E', os.path.abspath(home_dir), URLS[key][res_source_key]] |
|
127 if option_str : |
|
128 args.insert(4,option_str) |
|
129 call_subprocess(args, |
|
130 cwd=os.path.abspath(tmp_dir), |
|
131 filter_stdout=filter_python_develop, |
|
132 show_stdout=True, |
|
133 extra_env=extra_env) |
|
134 else: |
|
135 if sys.platform == 'win32': |
|
136 args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'easy_install')), URLS[key][res_source_key]] |
|
137 else: |
|
138 args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), URLS[key][res_source_key]] |
|
139 if option_str : |
|
140 args.insert(1,option_str) |
|
141 call_subprocess(args, |
|
142 cwd=os.path.abspath(tmp_dir), |
|
143 filter_stdout=filter_python_develop, |
|
144 show_stdout=True, |
|
145 extra_env=extra_env) |
|
146 |
|
147 |
|
148 def ensure_dir(dir): |
|
149 if not os.path.exists(dir): |
|
150 logger.notify('Creating directory %s' % dir) |
|
151 os.makedirs(dir) |
|
152 |
|
153 def filter_python_develop(line): |
|
154 if not line.strip(): |
|
155 return Logger.DEBUG |
|
156 for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ', |
|
157 'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ', |
|
158 'creating ', 'Copying ']: |
|
159 if line.startswith(prefix): |
|
160 return Logger.DEBUG |
|
161 return Logger.NOTIFY |
|
162 """ |
|
163 |
|
164 def main(): |
|
165 python_version = ".".join(map(str,sys.version_info[0:2])) |
|
166 text = virtualenv.create_bootstrap_script(EXTRA_TEXT, python_version=python_version) |
|
167 if os.path.exists(script_name): |
|
168 f = open(script_name) |
|
169 cur_text = f.read() |
|
170 f.close() |
|
171 else: |
|
172 cur_text = '' |
|
173 print 'Updating %s' % script_name |
|
174 if cur_text == 'text': |
|
175 print 'No update' |
|
176 else: |
|
177 print 'Script changed; updating...' |
|
178 f = open(script_name, 'w') |
|
179 f.write(text) |
|
180 f.close() |
|
181 |
|
182 if __name__ == '__main__': |
|
183 main() |
|
184 |