Mercurial > libervia-backend
annotate distribute_setup.py @ 595:1f160467f5de
Fix pep8 support in src/bridge.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 18 Jan 2013 17:55:35 +0100 |
parents | 6a718ede8be1 |
children | efd92a645220 |
rev | line source |
---|---|
231 | 1 #!python |
2 """Bootstrap distribute installation | |
3 | |
4 If you want to use setuptools in your package's setup.py, just include this | |
5 file in the same directory with it, and add this to the top of your setup.py:: | |
6 | |
7 from distribute_setup import use_setuptools | |
8 use_setuptools() | |
9 | |
10 If you want to require a specific version of setuptools, set a download | |
11 mirror, or use an alternate download directory, you can do so by supplying | |
12 the appropriate options to ``use_setuptools()``. | |
13 | |
14 This file can also be run as a script to install or upgrade setuptools. | |
15 """ | |
16 import os | |
17 import sys | |
18 import time | |
19 import fnmatch | |
20 import tempfile | |
21 import tarfile | |
22 from distutils import log | |
23 | |
24 try: | |
25 from site import USER_SITE | |
26 except ImportError: | |
27 USER_SITE = None | |
28 | |
29 try: | |
30 import subprocess | |
31 | |
32 def _python_cmd(*args): | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
33 args = (sys.executable, ) + args |
231 | 34 return subprocess.call(args) == 0 |
35 | |
36 except ImportError: | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
37 |
231 | 38 # will be used for python 2.3 |
39 def _python_cmd(*args): | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
40 args = (sys.executable, ) + args |
231 | 41 # quoting arguments if windows |
42 if sys.platform == 'win32': | |
43 def quote(arg): | |
44 if ' ' in arg: | |
45 return '"%s"' % arg | |
46 return arg | |
47 args = [quote(arg) for arg in args] | |
48 return os.spawnl(os.P_WAIT, sys.executable, *args) == 0 | |
49 | |
50 DEFAULT_VERSION = "0.6.14" | |
51 DEFAULT_URL = "http://pypi.python.org/packages/source/d/distribute/" | |
52 SETUPTOOLS_FAKED_VERSION = "0.6c11" | |
53 | |
54 SETUPTOOLS_PKG_INFO = """\ | |
55 Metadata-Version: 1.0 | |
56 Name: setuptools | |
57 Version: %s | |
58 Summary: xxxx | |
59 Home-page: xxx | |
60 Author: xxx | |
61 Author-email: xxx | |
62 License: xxx | |
63 Description: xxx | |
64 """ % SETUPTOOLS_FAKED_VERSION | |
65 | |
66 | |
67 def _install(tarball): | |
68 # extracting the tarball | |
69 tmpdir = tempfile.mkdtemp() | |
70 log.warn('Extracting in %s', tmpdir) | |
71 old_wd = os.getcwd() | |
72 try: | |
73 os.chdir(tmpdir) | |
74 tar = tarfile.open(tarball) | |
75 _extractall(tar) | |
76 tar.close() | |
77 | |
78 # going in the directory | |
79 subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0]) | |
80 os.chdir(subdir) | |
81 log.warn('Now working in %s', subdir) | |
82 | |
83 # installing | |
84 log.warn('Installing Distribute') | |
85 if not _python_cmd('setup.py', 'install'): | |
86 log.warn('Something went wrong during the installation.') | |
87 log.warn('See the error message above.') | |
88 finally: | |
89 os.chdir(old_wd) | |
90 | |
91 | |
92 def _build_egg(egg, tarball, to_dir): | |
93 # extracting the tarball | |
94 tmpdir = tempfile.mkdtemp() | |
95 log.warn('Extracting in %s', tmpdir) | |
96 old_wd = os.getcwd() | |
97 try: | |
98 os.chdir(tmpdir) | |
99 tar = tarfile.open(tarball) | |
100 _extractall(tar) | |
101 tar.close() | |
102 | |
103 # going in the directory | |
104 subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0]) | |
105 os.chdir(subdir) | |
106 log.warn('Now working in %s', subdir) | |
107 | |
108 # building an egg | |
109 log.warn('Building a Distribute egg in %s', to_dir) | |
110 _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir) | |
111 | |
112 finally: | |
113 os.chdir(old_wd) | |
114 # returning the result | |
115 log.warn(egg) | |
116 if not os.path.exists(egg): | |
117 raise IOError('Could not build the egg.') | |
118 | |
119 | |
120 def _do_download(version, download_base, to_dir, download_delay): | |
121 egg = os.path.join(to_dir, 'distribute-%s-py%d.%d.egg' | |
122 % (version, sys.version_info[0], sys.version_info[1])) | |
123 if not os.path.exists(egg): | |
124 tarball = download_setuptools(version, download_base, | |
125 to_dir, download_delay) | |
126 _build_egg(egg, tarball, to_dir) | |
127 sys.path.insert(0, egg) | |
128 import setuptools | |
129 setuptools.bootstrap_install_from = egg | |
130 | |
131 | |
132 def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, | |
133 to_dir=os.curdir, download_delay=15, no_fake=True): | |
134 # making sure we use the absolute path | |
135 to_dir = os.path.abspath(to_dir) | |
136 was_imported = 'pkg_resources' in sys.modules or \ | |
137 'setuptools' in sys.modules | |
138 try: | |
139 try: | |
140 import pkg_resources | |
141 if not hasattr(pkg_resources, '_distribute'): | |
142 if not no_fake: | |
143 _fake_setuptools() | |
144 raise ImportError | |
145 except ImportError: | |
146 return _do_download(version, download_base, to_dir, download_delay) | |
147 try: | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
148 pkg_resources.require("distribute>=" + version) |
231 | 149 return |
150 except pkg_resources.VersionConflict: | |
151 e = sys.exc_info()[1] | |
152 if was_imported: | |
153 sys.stderr.write( | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
154 "The required version of distribute (>=%s) is not available,\n" |
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
155 "and can't be installed while this script is running. Please\n" |
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
156 "install a more recent version first, using\n" |
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
157 "'easy_install -U distribute'." |
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
158 "\n\n(Currently using %r)\n" % (version, e.args[0])) |
231 | 159 sys.exit(2) |
160 else: | |
161 del pkg_resources, sys.modules['pkg_resources'] # reload ok | |
162 return _do_download(version, download_base, to_dir, | |
163 download_delay) | |
164 except pkg_resources.DistributionNotFound: | |
165 return _do_download(version, download_base, to_dir, | |
166 download_delay) | |
167 finally: | |
168 if not no_fake: | |
169 _create_fake_setuptools_pkg_info(to_dir) | |
170 | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
171 |
231 | 172 def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, |
173 to_dir=os.curdir, delay=15): | |
174 """Download distribute from a specified location and return its filename | |
175 | |
176 `version` should be a valid distribute version number that is available | |
177 as an egg for download under the `download_base` URL (which should end | |
178 with a '/'). `to_dir` is the directory where the egg will be downloaded. | |
179 `delay` is the number of seconds to pause before an actual download | |
180 attempt. | |
181 """ | |
182 # making sure we use the absolute path | |
183 to_dir = os.path.abspath(to_dir) | |
184 try: | |
185 from urllib.request import urlopen | |
186 except ImportError: | |
187 from urllib2 import urlopen | |
188 tgz_name = "distribute-%s.tar.gz" % version | |
189 url = download_base + tgz_name | |
190 saveto = os.path.join(to_dir, tgz_name) | |
191 src = dst = None | |
192 if not os.path.exists(saveto): # Avoid repeated downloads | |
193 try: | |
194 log.warn("Downloading %s", url) | |
195 src = urlopen(url) | |
196 # Read/write all in one block, so we don't create a corrupt file | |
197 # if the download is interrupted. | |
198 data = src.read() | |
199 dst = open(saveto, "wb") | |
200 dst.write(data) | |
201 finally: | |
202 if src: | |
203 src.close() | |
204 if dst: | |
205 dst.close() | |
206 return os.path.realpath(saveto) | |
207 | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
208 |
231 | 209 def _no_sandbox(function): |
210 def __no_sandbox(*args, **kw): | |
211 try: | |
212 from setuptools.sandbox import DirectorySandbox | |
213 if not hasattr(DirectorySandbox, '_old'): | |
214 def violation(*args): | |
215 pass | |
216 DirectorySandbox._old = DirectorySandbox._violation | |
217 DirectorySandbox._violation = violation | |
218 patched = True | |
219 else: | |
220 patched = False | |
221 except ImportError: | |
222 patched = False | |
223 | |
224 try: | |
225 return function(*args, **kw) | |
226 finally: | |
227 if patched: | |
228 DirectorySandbox._violation = DirectorySandbox._old | |
229 del DirectorySandbox._old | |
230 | |
231 return __no_sandbox | |
232 | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
233 |
231 | 234 def _patch_file(path, content): |
235 """Will backup the file then patch it""" | |
236 existing_content = open(path).read() | |
237 if existing_content == content: | |
238 # already patched | |
239 log.warn('Already patched.') | |
240 return False | |
241 log.warn('Patching...') | |
242 _rename_path(path) | |
243 f = open(path, 'w') | |
244 try: | |
245 f.write(content) | |
246 finally: | |
247 f.close() | |
248 return True | |
249 | |
250 _patch_file = _no_sandbox(_patch_file) | |
251 | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
252 |
231 | 253 def _same_content(path, content): |
254 return open(path).read() == content | |
255 | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
256 |
231 | 257 def _rename_path(path): |
258 new_name = path + '.OLD.%s' % time.time() | |
259 log.warn('Renaming %s into %s', path, new_name) | |
260 os.rename(path, new_name) | |
261 return new_name | |
262 | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
263 |
231 | 264 def _remove_flat_installation(placeholder): |
265 if not os.path.isdir(placeholder): | |
266 log.warn('Unkown installation at %s', placeholder) | |
267 return False | |
268 found = False | |
269 for file in os.listdir(placeholder): | |
270 if fnmatch.fnmatch(file, 'setuptools*.egg-info'): | |
271 found = True | |
272 break | |
273 if not found: | |
274 log.warn('Could not locate setuptools*.egg-info') | |
275 return | |
276 | |
277 log.warn('Removing elements out of the way...') | |
278 pkg_info = os.path.join(placeholder, file) | |
279 if os.path.isdir(pkg_info): | |
280 patched = _patch_egg_dir(pkg_info) | |
281 else: | |
282 patched = _patch_file(pkg_info, SETUPTOOLS_PKG_INFO) | |
283 | |
284 if not patched: | |
285 log.warn('%s already patched.', pkg_info) | |
286 return False | |
287 # now let's move the files out of the way | |
288 for element in ('setuptools', 'pkg_resources.py', 'site.py'): | |
289 element = os.path.join(placeholder, element) | |
290 if os.path.exists(element): | |
291 _rename_path(element) | |
292 else: | |
293 log.warn('Could not find the %s element of the ' | |
294 'Setuptools distribution', element) | |
295 return True | |
296 | |
297 _remove_flat_installation = _no_sandbox(_remove_flat_installation) | |
298 | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
299 |
231 | 300 def _after_install(dist): |
301 log.warn('After install bootstrap.') | |
302 placeholder = dist.get_command_obj('install').install_purelib | |
303 _create_fake_setuptools_pkg_info(placeholder) | |
304 | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
305 |
231 | 306 def _create_fake_setuptools_pkg_info(placeholder): |
307 if not placeholder or not os.path.exists(placeholder): | |
308 log.warn('Could not find the install location') | |
309 return | |
310 pyver = '%s.%s' % (sys.version_info[0], sys.version_info[1]) | |
311 setuptools_file = 'setuptools-%s-py%s.egg-info' % \ | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
312 (SETUPTOOLS_FAKED_VERSION, pyver) |
231 | 313 pkg_info = os.path.join(placeholder, setuptools_file) |
314 if os.path.exists(pkg_info): | |
315 log.warn('%s already exists', pkg_info) | |
316 return | |
317 | |
318 log.warn('Creating %s', pkg_info) | |
319 f = open(pkg_info, 'w') | |
320 try: | |
321 f.write(SETUPTOOLS_PKG_INFO) | |
322 finally: | |
323 f.close() | |
324 | |
325 pth_file = os.path.join(placeholder, 'setuptools.pth') | |
326 log.warn('Creating %s', pth_file) | |
327 f = open(pth_file, 'w') | |
328 try: | |
329 f.write(os.path.join(os.curdir, setuptools_file)) | |
330 finally: | |
331 f.close() | |
332 | |
333 _create_fake_setuptools_pkg_info = _no_sandbox(_create_fake_setuptools_pkg_info) | |
334 | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
335 |
231 | 336 def _patch_egg_dir(path): |
337 # let's check if it's already patched | |
338 pkg_info = os.path.join(path, 'EGG-INFO', 'PKG-INFO') | |
339 if os.path.exists(pkg_info): | |
340 if _same_content(pkg_info, SETUPTOOLS_PKG_INFO): | |
341 log.warn('%s already patched.', pkg_info) | |
342 return False | |
343 _rename_path(path) | |
344 os.mkdir(path) | |
345 os.mkdir(os.path.join(path, 'EGG-INFO')) | |
346 pkg_info = os.path.join(path, 'EGG-INFO', 'PKG-INFO') | |
347 f = open(pkg_info, 'w') | |
348 try: | |
349 f.write(SETUPTOOLS_PKG_INFO) | |
350 finally: | |
351 f.close() | |
352 return True | |
353 | |
354 _patch_egg_dir = _no_sandbox(_patch_egg_dir) | |
355 | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
356 |
231 | 357 def _before_install(): |
358 log.warn('Before install bootstrap.') | |
359 _fake_setuptools() | |
360 | |
361 | |
362 def _under_prefix(location): | |
363 if 'install' not in sys.argv: | |
364 return True | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
365 args = sys.argv[sys.argv.index('install') + 1:] |
231 | 366 for index, arg in enumerate(args): |
367 for option in ('--root', '--prefix'): | |
368 if arg.startswith('%s=' % option): | |
369 top_dir = arg.split('root=')[-1] | |
370 return location.startswith(top_dir) | |
371 elif arg == option: | |
372 if len(args) > index: | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
373 top_dir = args[index + 1] |
231 | 374 return location.startswith(top_dir) |
375 if arg == '--user' and USER_SITE is not None: | |
376 return location.startswith(USER_SITE) | |
377 return True | |
378 | |
379 | |
380 def _fake_setuptools(): | |
381 log.warn('Scanning installed packages') | |
382 try: | |
383 import pkg_resources | |
384 except ImportError: | |
385 # we're cool | |
386 log.warn('Setuptools or Distribute does not seem to be installed.') | |
387 return | |
388 ws = pkg_resources.working_set | |
389 try: | |
390 setuptools_dist = ws.find(pkg_resources.Requirement.parse('setuptools', | |
391 replacement=False)) | |
392 except TypeError: | |
393 # old distribute API | |
394 setuptools_dist = ws.find(pkg_resources.Requirement.parse('setuptools')) | |
395 | |
396 if setuptools_dist is None: | |
397 log.warn('No setuptools distribution found') | |
398 return | |
399 # detecting if it was already faked | |
400 setuptools_location = setuptools_dist.location | |
401 log.warn('Setuptools installation detected at %s', setuptools_location) | |
402 | |
403 # if --root or --preix was provided, and if | |
404 # setuptools is not located in them, we don't patch it | |
405 if not _under_prefix(setuptools_location): | |
406 log.warn('Not patching, --root or --prefix is installing Distribute' | |
407 ' in another location') | |
408 return | |
409 | |
410 # let's see if its an egg | |
411 if not setuptools_location.endswith('.egg'): | |
412 log.warn('Non-egg installation') | |
413 res = _remove_flat_installation(setuptools_location) | |
414 if not res: | |
415 return | |
416 else: | |
417 log.warn('Egg installation') | |
418 pkg_info = os.path.join(setuptools_location, 'EGG-INFO', 'PKG-INFO') | |
419 if (os.path.exists(pkg_info) and | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
420 _same_content(pkg_info, SETUPTOOLS_PKG_INFO)): |
231 | 421 log.warn('Already patched.') |
422 return | |
423 log.warn('Patching...') | |
424 # let's create a fake egg replacing setuptools one | |
425 res = _patch_egg_dir(setuptools_location) | |
426 if not res: | |
427 return | |
428 log.warn('Patched done.') | |
429 _relaunch() | |
430 | |
431 | |
432 def _relaunch(): | |
433 log.warn('Relaunching...') | |
434 # we have to relaunch the process | |
435 # pip marker to avoid a relaunch bug | |
436 if sys.argv[:3] == ['-c', 'install', '--single-version-externally-managed']: | |
437 sys.argv[0] = 'setup.py' | |
438 args = [sys.executable] + sys.argv | |
439 sys.exit(subprocess.call(args)) | |
440 | |
441 | |
442 def _extractall(self, path=".", members=None): | |
443 """Extract all members from the archive to the current working | |
444 directory and set owner, modification time and permissions on | |
445 directories afterwards. `path' specifies a different directory | |
446 to extract to. `members' is optional and must be a subset of the | |
447 list returned by getmembers(). | |
448 """ | |
449 import copy | |
450 import operator | |
451 from tarfile import ExtractError | |
452 directories = [] | |
453 | |
454 if members is None: | |
455 members = self | |
456 | |
457 for tarinfo in members: | |
458 if tarinfo.isdir(): | |
459 # Extract directories with a safe mode. | |
460 directories.append(tarinfo) | |
461 tarinfo = copy.copy(tarinfo) | |
586
6a718ede8be1
Fix coding style in setup.py.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
231
diff
changeset
|
462 tarinfo.mode = 448 # decimal for oct 0700 |
231 | 463 self.extract(tarinfo, path) |
464 | |
465 # Reverse sort directories. | |
466 if sys.version_info < (2, 4): | |
467 def sorter(dir1, dir2): | |
468 return cmp(dir1.name, dir2.name) | |
469 directories.sort(sorter) | |
470 directories.reverse() | |
471 else: | |
472 directories.sort(key=operator.attrgetter('name'), reverse=True) | |
473 | |
474 # Set correct owner, mtime and filemode on directories. | |
475 for tarinfo in directories: | |
476 dirpath = os.path.join(path, tarinfo.name) | |
477 try: | |
478 self.chown(tarinfo, dirpath) | |
479 self.utime(tarinfo, dirpath) | |
480 self.chmod(tarinfo, dirpath) | |
481 except ExtractError: | |
482 e = sys.exc_info()[1] | |
483 if self.errorlevel > 1: | |
484 raise | |
485 else: | |
486 self._dbg(1, "tarfile: %s" % e) | |
487 | |
488 | |
489 def main(argv, version=DEFAULT_VERSION): | |
490 """Install or upgrade setuptools and EasyInstall""" | |
491 tarball = download_setuptools() | |
492 _install(tarball) | |
493 | |
494 | |
495 if __name__ == '__main__': | |
496 main(sys.argv[1:]) |