comparison distribute_setup.py @ 586:6a718ede8be1

Fix coding style in setup.py.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:27 +0100
parents f68a9a429c88
children efd92a645220
comparison
equal deleted inserted replaced
585:9902ec2d8d9b 586:6a718ede8be1
28 28
29 try: 29 try:
30 import subprocess 30 import subprocess
31 31
32 def _python_cmd(*args): 32 def _python_cmd(*args):
33 args = (sys.executable,) + args 33 args = (sys.executable, ) + args
34 return subprocess.call(args) == 0 34 return subprocess.call(args) == 0
35 35
36 except ImportError: 36 except ImportError:
37
37 # will be used for python 2.3 38 # will be used for python 2.3
38 def _python_cmd(*args): 39 def _python_cmd(*args):
39 args = (sys.executable,) + args 40 args = (sys.executable, ) + args
40 # quoting arguments if windows 41 # quoting arguments if windows
41 if sys.platform == 'win32': 42 if sys.platform == 'win32':
42 def quote(arg): 43 def quote(arg):
43 if ' ' in arg: 44 if ' ' in arg:
44 return '"%s"' % arg 45 return '"%s"' % arg
142 _fake_setuptools() 143 _fake_setuptools()
143 raise ImportError 144 raise ImportError
144 except ImportError: 145 except ImportError:
145 return _do_download(version, download_base, to_dir, download_delay) 146 return _do_download(version, download_base, to_dir, download_delay)
146 try: 147 try:
147 pkg_resources.require("distribute>="+version) 148 pkg_resources.require("distribute>=" + version)
148 return 149 return
149 except pkg_resources.VersionConflict: 150 except pkg_resources.VersionConflict:
150 e = sys.exc_info()[1] 151 e = sys.exc_info()[1]
151 if was_imported: 152 if was_imported:
152 sys.stderr.write( 153 sys.stderr.write(
153 "The required version of distribute (>=%s) is not available,\n" 154 "The required version of distribute (>=%s) is not available,\n"
154 "and can't be installed while this script is running. Please\n" 155 "and can't be installed while this script is running. Please\n"
155 "install a more recent version first, using\n" 156 "install a more recent version first, using\n"
156 "'easy_install -U distribute'." 157 "'easy_install -U distribute'."
157 "\n\n(Currently using %r)\n" % (version, e.args[0])) 158 "\n\n(Currently using %r)\n" % (version, e.args[0]))
158 sys.exit(2) 159 sys.exit(2)
159 else: 160 else:
160 del pkg_resources, sys.modules['pkg_resources'] # reload ok 161 del pkg_resources, sys.modules['pkg_resources'] # reload ok
161 return _do_download(version, download_base, to_dir, 162 return _do_download(version, download_base, to_dir,
162 download_delay) 163 download_delay)
164 return _do_download(version, download_base, to_dir, 165 return _do_download(version, download_base, to_dir,
165 download_delay) 166 download_delay)
166 finally: 167 finally:
167 if not no_fake: 168 if not no_fake:
168 _create_fake_setuptools_pkg_info(to_dir) 169 _create_fake_setuptools_pkg_info(to_dir)
170
169 171
170 def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, 172 def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
171 to_dir=os.curdir, delay=15): 173 to_dir=os.curdir, delay=15):
172 """Download distribute from a specified location and return its filename 174 """Download distribute from a specified location and return its filename
173 175
201 src.close() 203 src.close()
202 if dst: 204 if dst:
203 dst.close() 205 dst.close()
204 return os.path.realpath(saveto) 206 return os.path.realpath(saveto)
205 207
208
206 def _no_sandbox(function): 209 def _no_sandbox(function):
207 def __no_sandbox(*args, **kw): 210 def __no_sandbox(*args, **kw):
208 try: 211 try:
209 from setuptools.sandbox import DirectorySandbox 212 from setuptools.sandbox import DirectorySandbox
210 if not hasattr(DirectorySandbox, '_old'): 213 if not hasattr(DirectorySandbox, '_old'):
225 DirectorySandbox._violation = DirectorySandbox._old 228 DirectorySandbox._violation = DirectorySandbox._old
226 del DirectorySandbox._old 229 del DirectorySandbox._old
227 230
228 return __no_sandbox 231 return __no_sandbox
229 232
233
230 def _patch_file(path, content): 234 def _patch_file(path, content):
231 """Will backup the file then patch it""" 235 """Will backup the file then patch it"""
232 existing_content = open(path).read() 236 existing_content = open(path).read()
233 if existing_content == content: 237 if existing_content == content:
234 # already patched 238 # already patched
243 f.close() 247 f.close()
244 return True 248 return True
245 249
246 _patch_file = _no_sandbox(_patch_file) 250 _patch_file = _no_sandbox(_patch_file)
247 251
252
248 def _same_content(path, content): 253 def _same_content(path, content):
249 return open(path).read() == content 254 return open(path).read() == content
255
250 256
251 def _rename_path(path): 257 def _rename_path(path):
252 new_name = path + '.OLD.%s' % time.time() 258 new_name = path + '.OLD.%s' % time.time()
253 log.warn('Renaming %s into %s', path, new_name) 259 log.warn('Renaming %s into %s', path, new_name)
254 os.rename(path, new_name) 260 os.rename(path, new_name)
255 return new_name 261 return new_name
262
256 263
257 def _remove_flat_installation(placeholder): 264 def _remove_flat_installation(placeholder):
258 if not os.path.isdir(placeholder): 265 if not os.path.isdir(placeholder):
259 log.warn('Unkown installation at %s', placeholder) 266 log.warn('Unkown installation at %s', placeholder)
260 return False 267 return False
287 'Setuptools distribution', element) 294 'Setuptools distribution', element)
288 return True 295 return True
289 296
290 _remove_flat_installation = _no_sandbox(_remove_flat_installation) 297 _remove_flat_installation = _no_sandbox(_remove_flat_installation)
291 298
299
292 def _after_install(dist): 300 def _after_install(dist):
293 log.warn('After install bootstrap.') 301 log.warn('After install bootstrap.')
294 placeholder = dist.get_command_obj('install').install_purelib 302 placeholder = dist.get_command_obj('install').install_purelib
295 _create_fake_setuptools_pkg_info(placeholder) 303 _create_fake_setuptools_pkg_info(placeholder)
296 304
305
297 def _create_fake_setuptools_pkg_info(placeholder): 306 def _create_fake_setuptools_pkg_info(placeholder):
298 if not placeholder or not os.path.exists(placeholder): 307 if not placeholder or not os.path.exists(placeholder):
299 log.warn('Could not find the install location') 308 log.warn('Could not find the install location')
300 return 309 return
301 pyver = '%s.%s' % (sys.version_info[0], sys.version_info[1]) 310 pyver = '%s.%s' % (sys.version_info[0], sys.version_info[1])
302 setuptools_file = 'setuptools-%s-py%s.egg-info' % \ 311 setuptools_file = 'setuptools-%s-py%s.egg-info' % \
303 (SETUPTOOLS_FAKED_VERSION, pyver) 312 (SETUPTOOLS_FAKED_VERSION, pyver)
304 pkg_info = os.path.join(placeholder, setuptools_file) 313 pkg_info = os.path.join(placeholder, setuptools_file)
305 if os.path.exists(pkg_info): 314 if os.path.exists(pkg_info):
306 log.warn('%s already exists', pkg_info) 315 log.warn('%s already exists', pkg_info)
307 return 316 return
308 317
320 f.write(os.path.join(os.curdir, setuptools_file)) 329 f.write(os.path.join(os.curdir, setuptools_file))
321 finally: 330 finally:
322 f.close() 331 f.close()
323 332
324 _create_fake_setuptools_pkg_info = _no_sandbox(_create_fake_setuptools_pkg_info) 333 _create_fake_setuptools_pkg_info = _no_sandbox(_create_fake_setuptools_pkg_info)
334
325 335
326 def _patch_egg_dir(path): 336 def _patch_egg_dir(path):
327 # let's check if it's already patched 337 # let's check if it's already patched
328 pkg_info = os.path.join(path, 'EGG-INFO', 'PKG-INFO') 338 pkg_info = os.path.join(path, 'EGG-INFO', 'PKG-INFO')
329 if os.path.exists(pkg_info): 339 if os.path.exists(pkg_info):
341 f.close() 351 f.close()
342 return True 352 return True
343 353
344 _patch_egg_dir = _no_sandbox(_patch_egg_dir) 354 _patch_egg_dir = _no_sandbox(_patch_egg_dir)
345 355
356
346 def _before_install(): 357 def _before_install():
347 log.warn('Before install bootstrap.') 358 log.warn('Before install bootstrap.')
348 _fake_setuptools() 359 _fake_setuptools()
349 360
350 361
351 def _under_prefix(location): 362 def _under_prefix(location):
352 if 'install' not in sys.argv: 363 if 'install' not in sys.argv:
353 return True 364 return True
354 args = sys.argv[sys.argv.index('install')+1:] 365 args = sys.argv[sys.argv.index('install') + 1:]
355 for index, arg in enumerate(args): 366 for index, arg in enumerate(args):
356 for option in ('--root', '--prefix'): 367 for option in ('--root', '--prefix'):
357 if arg.startswith('%s=' % option): 368 if arg.startswith('%s=' % option):
358 top_dir = arg.split('root=')[-1] 369 top_dir = arg.split('root=')[-1]
359 return location.startswith(top_dir) 370 return location.startswith(top_dir)
360 elif arg == option: 371 elif arg == option:
361 if len(args) > index: 372 if len(args) > index:
362 top_dir = args[index+1] 373 top_dir = args[index + 1]
363 return location.startswith(top_dir) 374 return location.startswith(top_dir)
364 if arg == '--user' and USER_SITE is not None: 375 if arg == '--user' and USER_SITE is not None:
365 return location.startswith(USER_SITE) 376 return location.startswith(USER_SITE)
366 return True 377 return True
367 378
404 return 415 return
405 else: 416 else:
406 log.warn('Egg installation') 417 log.warn('Egg installation')
407 pkg_info = os.path.join(setuptools_location, 'EGG-INFO', 'PKG-INFO') 418 pkg_info = os.path.join(setuptools_location, 'EGG-INFO', 'PKG-INFO')
408 if (os.path.exists(pkg_info) and 419 if (os.path.exists(pkg_info) and
409 _same_content(pkg_info, SETUPTOOLS_PKG_INFO)): 420 _same_content(pkg_info, SETUPTOOLS_PKG_INFO)):
410 log.warn('Already patched.') 421 log.warn('Already patched.')
411 return 422 return
412 log.warn('Patching...') 423 log.warn('Patching...')
413 # let's create a fake egg replacing setuptools one 424 # let's create a fake egg replacing setuptools one
414 res = _patch_egg_dir(setuptools_location) 425 res = _patch_egg_dir(setuptools_location)
446 for tarinfo in members: 457 for tarinfo in members:
447 if tarinfo.isdir(): 458 if tarinfo.isdir():
448 # Extract directories with a safe mode. 459 # Extract directories with a safe mode.
449 directories.append(tarinfo) 460 directories.append(tarinfo)
450 tarinfo = copy.copy(tarinfo) 461 tarinfo = copy.copy(tarinfo)
451 tarinfo.mode = 448 # decimal for oct 0700 462 tarinfo.mode = 448 # decimal for oct 0700
452 self.extract(tarinfo, path) 463 self.extract(tarinfo, path)
453 464
454 # Reverse sort directories. 465 # Reverse sort directories.
455 if sys.version_info < (2, 4): 466 if sys.version_info < (2, 4):
456 def sorter(dir1, dir2): 467 def sorter(dir1, dir2):