|
64
|
1 |
import sys |
|
|
2 |
import os |
|
|
3 |
import os.path |
|
|
4 |
import shutil |
|
|
5 |
import tarfile |
|
|
6 |
import zipfile |
|
|
7 |
import urllib |
|
|
8 |
import platform |
|
|
9 |
import patch |
|
|
10 |
|
|
|
11 |
join = os.path.join |
|
|
12 |
system_str = platform.system() |
|
|
13 |
|
|
|
14 |
URLS = { |
|
|
15 |
#'': {'setup': '', 'url':'', 'local':''}, |
|
|
16 |
'DISTRIBUTE': {'setup': 'distribute', 'url':'http://pypi.python.org/packages/source/d/distribute/distribute-0.6.24.tar.gz', 'local':"distribute-0.6.24.tar.gz", 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
|
17 |
'PYCRYPTO': {'setup': 'pycrypto', 'url':'https://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz', 'local':'pycrypto-2.6.tar.gz', 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
|
18 |
'SSH': {'setup': 'ssh', 'url':'http://pypi.python.org/packages/source/s/ssh/ssh-1.7.14.tar.gz#md5=4cdd0549ef4699bd67b96264d3b21427', 'local':'ssh-1.7.14.tar.gz', 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
|
19 |
'FABRIC': {'setup': 'fabric', 'url':'https://github.com/fabric/fabric/tarball/1.4.2', 'local':'fabric-1.4.2.tar.gz', 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
|
20 |
'MERCURIAL': {'setup': 'mercurial', 'url':'http://mercurial.selenic.com/release/mercurial-2.2.3.tar.gz', 'local':'mercurial-2.2.3.tar.gz', 'install': {'method': 'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
|
21 |
'REQUEST': {'setup': 'requests', 'url':'https://github.com/kennethreitz/requests/tarball/v0.13.3', 'local':'requests-v0.13.3.tar.gz', 'install' : {'method':'pip', 'option_str': None, 'dict_extra_env': None}}, |
|
|
22 |
} |
|
|
23 |
|
|
|
24 |
|
|
|
25 |
class ResourcesEnv(object): |
|
|
26 |
|
|
|
27 |
def __init__(self, src_base, urls, normal_installs): |
|
|
28 |
self.src_base = src_base |
|
|
29 |
self.URLS = {} |
|
|
30 |
self.__init_url(urls) |
|
|
31 |
self.NORMAL_INSTALL = normal_installs |
|
|
32 |
|
|
|
33 |
def get_src_base_path(self, fpath): |
|
|
34 |
return os.path.abspath(os.path.join(self.src_base, fpath)).replace("\\","/") |
|
|
35 |
|
|
|
36 |
def __add_package_def(self, key, dict): |
|
|
37 |
self.URLS[key] = dict |
|
|
38 |
|
|
|
39 |
def __init_url(self, urls): |
|
|
40 |
for key, url_dict in urls.items(): |
|
|
41 |
url_dict_copy = url_dict.copy() |
|
|
42 |
if not url_dict['url'].startswith("http://"): |
|
|
43 |
url_dict_copy['url'] = self.get_src_base_path(url_dict['url']) |
|
|
44 |
url_dict_copy['local'] = self.get_src_base_path(url_dict['local']) |
|
|
45 |
|
|
|
46 |
self.__add_package_def(key, url_dict_copy ) |
|
|
47 |
|
|
|
48 |
def ensure_dir(dir, logger): |
|
|
49 |
if not os.path.exists(dir): |
|
|
50 |
logger.notify('Creating directory %s' % dir) |
|
|
51 |
os.makedirs(dir) |
|
|
52 |
|
|
|
53 |
def extend_parser(parser): |
|
|
54 |
parser.add_option( |
|
|
55 |
'--index-url', |
|
|
56 |
metavar='INDEX_URL', |
|
|
57 |
dest='index_url', |
|
|
58 |
default='http://pypi.python.org/simple/', |
|
|
59 |
help='base URL of Python Package Index') |
|
|
60 |
parser.add_option( |
|
|
61 |
'--type-install', |
|
|
62 |
metavar='type_install', |
|
|
63 |
dest='type_install', |
|
|
64 |
help='type install : local, url, setup - default : local') |
|
|
65 |
parser.add_option( |
|
|
66 |
'--ignore-packages', |
|
|
67 |
metavar='ignore_packages', |
|
|
68 |
dest='ignore_packages', |
|
|
69 |
default=None, |
|
|
70 |
help='list of comma separated keys for package to ignore') |
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
def lib_generate_install_methods(path_locations, src_base, Logger, call_subprocess, normal_installs, options_to_add=None, urls= None): |
|
|
75 |
|
|
|
76 |
all_urls = URLS.copy() |
|
|
77 |
if urls is not None: |
|
|
78 |
all_urls.update(urls) |
|
|
79 |
|
|
|
80 |
res_env = ResourcesEnv(src_base, all_urls, normal_installs) |
|
|
81 |
|
|
|
82 |
def filter_python_develop(line): |
|
|
83 |
if not line.strip(): |
|
|
84 |
return Logger.DEBUG |
|
|
85 |
for prefix in ['Searching for', 'Reading ', 'Best match: ', 'Processing ', |
|
|
86 |
'Moving ', 'Adding ', 'running ', 'writing ', 'Creating ', |
|
|
87 |
'creating ', 'Copying ']: |
|
|
88 |
if line.startswith(prefix): |
|
|
89 |
return Logger.DEBUG |
|
|
90 |
return Logger.NOTIFY |
|
|
91 |
|
|
|
92 |
|
|
|
93 |
def normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess): |
|
|
94 |
logger.notify("Install %s from %s with %s" % (key,res_env.URLS[key][res_source_key],method)) |
|
|
95 |
if method == 'pip': |
|
|
96 |
if sys.platform == 'win32': |
|
|
97 |
args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'pip')), 'install', res_env.URLS[key][res_source_key]] |
|
|
98 |
else: |
|
|
99 |
args = [os.path.abspath(os.path.join(home_dir, 'bin', 'pip')), 'install', res_env.URLS[key][res_source_key]] |
|
|
100 |
if option_str : |
|
|
101 |
args.insert(4,option_str) |
|
|
102 |
call_subprocess(args, |
|
|
103 |
cwd=os.path.abspath(tmp_dir), |
|
|
104 |
filter_stdout=filter_python_develop, |
|
|
105 |
show_stdout=True, |
|
|
106 |
extra_env=extra_env) |
|
|
107 |
else: |
|
|
108 |
if sys.platform == 'win32': |
|
|
109 |
args = [os.path.abspath(os.path.join(home_dir, 'Scripts', 'easy_install')), res_env.URLS[key][res_source_key]] |
|
|
110 |
else: |
|
|
111 |
args = [os.path.abspath(os.path.join(home_dir, 'bin', 'easy_install')), res_env.URLS[key][res_source_key]] |
|
|
112 |
if option_str : |
|
|
113 |
args.insert(1,option_str) |
|
|
114 |
call_subprocess(args, |
|
|
115 |
cwd=os.path.abspath(tmp_dir), |
|
|
116 |
filter_stdout=filter_python_develop, |
|
|
117 |
show_stdout=True, |
|
|
118 |
extra_env=extra_env) |
|
|
119 |
|
|
|
120 |
|
|
|
121 |
def after_install(options, home_dir): |
|
|
122 |
|
|
|
123 |
global logger |
|
|
124 |
|
|
|
125 |
verbosity = options.verbose - options.quiet |
|
|
126 |
logger = Logger([(Logger.level_for_integer(2-verbosity), sys.stdout)]) |
|
|
127 |
|
|
|
128 |
|
|
|
129 |
home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir) |
|
|
130 |
base_dir = os.path.dirname(home_dir) |
|
|
131 |
src_dir = os.path.join(home_dir, 'src') |
|
|
132 |
tmp_dir = os.path.join(home_dir, 'tmp') |
|
|
133 |
ensure_dir(src_dir, logger) |
|
|
134 |
ensure_dir(tmp_dir, logger) |
|
|
135 |
system_str = platform.system() |
|
|
136 |
|
|
|
137 |
res_source_key = getattr(options, 'type_install') if hasattr(options, 'type_install') else 'local' #.get('type_install', 'local') |
|
|
138 |
if res_source_key is None: |
|
|
139 |
res_source_key = local |
|
|
140 |
|
|
|
141 |
ignore_packages = [] |
|
|
142 |
|
|
|
143 |
if system_str == 'Windows': |
|
|
144 |
default_install_options = {'method': 'easy_install', 'option_str': None, 'dict_extra_env': None} |
|
|
145 |
else: |
|
|
146 |
default_install_options = {'method': 'pip', 'option_str': None, 'dict_extra_env': None} |
|
|
147 |
|
|
|
148 |
if options.ignore_packages : |
|
|
149 |
ignore_packages = options.ignore_packages.split(",") |
|
|
150 |
|
|
|
151 |
logger.indent += 2 |
|
|
152 |
try: |
|
|
153 |
for key in res_env.NORMAL_INSTALL: |
|
|
154 |
if key not in res_env.URLS: |
|
|
155 |
logger.notify("%s not found in def : passing" % (key,)) |
|
|
156 |
install_options = res_env.URLS[key].get('install', None) |
|
|
157 |
if install_options is None: |
|
|
158 |
install_options = default_install_options |
|
|
159 |
method = install_options.get('method', default_install_options['method']) |
|
|
160 |
option_str = install_options.get('option_str', default_install_options['option_str']) |
|
|
161 |
extra_env = install_options.get('dict_extra_env', default_install_options['dict_extra_env']) |
|
|
162 |
#isinstance(lst, (list, tuple)) |
|
|
163 |
if key not in ignore_packages: |
|
|
164 |
if callable(method): |
|
|
165 |
method(option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop) |
|
|
166 |
elif method in globals() and callable(globals()[method]) and method not in ['pip', 'easy_install']: |
|
|
167 |
globals()[method](option_str, extra_env, res_source_key, home_dir, lib_dir, tmp_dir, src_dir, res_env, logger, call_subprocess, filter_python_develop) |
|
|
168 |
else: |
|
|
169 |
normal_install(key, method, option_str, extra_env, res_source_key, home_dir, tmp_dir, res_env, logger, call_subprocess) |
|
|
170 |
|
|
|
171 |
logger.notify("Clear source dir") |
|
|
172 |
shutil.rmtree(src_dir) |
|
|
173 |
|
|
|
174 |
finally: |
|
|
175 |
logger.indent -= 2 |
|
|
176 |
script_dir = join(base_dir, bin_dir) |
|
|
177 |
logger.notify('Run "%s Package" to install new packages that provide builds' |
|
|
178 |
% join(script_dir, 'easy_install')) |
|
|
179 |
|
|
|
180 |
def adjust_options(options, args): |
|
|
181 |
if not options_to_add: |
|
|
182 |
pass |
|
|
183 |
for opt in options_to_add: |
|
|
184 |
test_opt = opt.split('=',1)[0] |
|
|
185 |
if not hasattr(options,test_opt) or getattr(options, test_opt) is None: |
|
|
186 |
setattr(options, test_opt,opt.split('=',1)[1] if "=" in opt else True) |
|
|
187 |
|
|
|
188 |
return adjust_options, extend_parser, after_install |