Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7724a6c

Browse files
committed
Packaging cleanup: remove conditionals for < 2.6 support.
PEP 370 features and sys.dont_write_bytecode are always available in 3.3; the distutils2 backport still has the conditionals. I also renamed an internal misnamed method and fixed a few things (“packaging2” name, stray print, unused import, fd leak).
1 parent 37ccd6f commit 7724a6c

16 files changed

Lines changed: 46 additions & 95 deletions

Lib/packaging/command/build_ext.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55
import sys
6+
import site
67
import logging
78
import sysconfig
89

@@ -15,9 +16,6 @@
1516
from packaging.compiler.extension import Extension
1617
from packaging import logger
1718

18-
import site
19-
HAS_USER_SITE = True
20-
2119
if os.name == 'nt':
2220
from packaging.compiler.msvccompiler import get_build_version
2321
MSVC_VERSION = int(get_build_version())
@@ -62,6 +60,8 @@ class build_ext(Command):
6260
('inplace', 'i',
6361
"ignore build-lib and put compiled extensions into the source " +
6462
"directory alongside your pure Python modules"),
63+
('user', None,
64+
"add user include, library and rpath"),
6565
('include-dirs=', 'I',
6666
"list of directories to search for header files" + sep_by),
6767
('define=', 'D',
@@ -88,12 +88,8 @@ class build_ext(Command):
8888
"path to the SWIG executable"),
8989
]
9090

91-
boolean_options = ['inplace', 'debug', 'force']
91+
boolean_options = ['inplace', 'debug', 'force', 'user']
9292

93-
if HAS_USER_SITE:
94-
user_options.append(('user', None,
95-
"add user include, library and rpath"))
96-
boolean_options.append('user')
9793

9894
help_options = [
9995
('help-compiler', None,
@@ -120,8 +116,7 @@ def initialize_options(self):
120116
self.compiler = None
121117
self.swig = None
122118
self.swig_opts = None
123-
if HAS_USER_SITE:
124-
self.user = None
119+
self.user = None
125120

126121
def finalize_options(self):
127122
self.set_undefined_options('build',
@@ -270,7 +265,7 @@ def finalize_options(self):
270265
self.swig_opts = self.swig_opts.split(' ')
271266

272267
# Finally add the user include and library directories if requested
273-
if HAS_USER_SITE and self.user:
268+
if self.user:
274269
user_include = os.path.join(site.USER_BASE, "include")
275270
user_lib = os.path.join(site.USER_BASE, "lib")
276271
if os.path.isdir(user_include):

Lib/packaging/command/build_py.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def build_packages(self):
388388
self.build_module(module, module_file, package)
389389

390390
def byte_compile(self, files):
391-
if hasattr(sys, 'dont_write_bytecode') and sys.dont_write_bytecode:
391+
if sys.dont_write_bytecode:
392392
logger.warning('%s: byte-compiling is disabled, skipping.',
393393
self.get_command_name())
394394
return

Lib/packaging/command/install_dist.py

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
from packaging.errors import PackagingOptionError
1515

1616

17-
HAS_USER_SITE = True
18-
19-
2017
class install_dist(Command):
2118

2219
description = "install everything from build directory"
@@ -27,6 +24,9 @@ class install_dist(Command):
2724
"installation prefix"),
2825
('exec-prefix=', None,
2926
"(Unix only) prefix for platform-specific files"),
27+
('user', None,
28+
"install in user site-packages directory [%s]" %
29+
get_path('purelib', '%s_user' % os.name)),
3030
('home=', None,
3131
"(Unix only) home directory to install under"),
3232

@@ -97,15 +97,7 @@ class install_dist(Command):
9797
]
9898

9999
boolean_options = ['compile', 'force', 'skip-build', 'no-distinfo',
100-
'requested', 'no-record']
101-
102-
if HAS_USER_SITE:
103-
user_options.append(
104-
('user', None,
105-
"install in user site-packages directory [%s]" %
106-
get_path('purelib', '%s_user' % os.name)))
107-
108-
boolean_options.append('user')
100+
'requested', 'no-record', 'user']
109101

110102
negative_opt = {'no-compile': 'compile', 'no-requested': 'requested'}
111103

@@ -115,8 +107,7 @@ def initialize_options(self):
115107
self.prefix = None
116108
self.exec_prefix = None
117109
self.home = None
118-
if HAS_USER_SITE:
119-
self.user = False
110+
self.user = False
120111

121112
# These select only the installation base; it's up to the user to
122113
# specify the installation scheme (currently, that means supplying
@@ -135,9 +126,8 @@ def initialize_options(self):
135126
self.install_lib = None # set to either purelib or platlib
136127
self.install_scripts = None
137128
self.install_data = None
138-
if HAS_USER_SITE:
139-
self.install_userbase = get_config_var('userbase')
140-
self.install_usersite = get_path('purelib', '%s_user' % os.name)
129+
self.install_userbase = get_config_var('userbase')
130+
self.install_usersite = get_path('purelib', '%s_user' % os.name)
141131

142132
self.compile = None
143133
self.optimize = None
@@ -219,9 +209,8 @@ def finalize_options(self):
219209
raise PackagingOptionError(
220210
"must supply either home or prefix/exec-prefix -- not both")
221211

222-
if HAS_USER_SITE and self.user and (
223-
self.prefix or self.exec_prefix or self.home or
224-
self.install_base or self.install_platbase):
212+
if self.user and (self.prefix or self.exec_prefix or self.home or
213+
self.install_base or self.install_platbase):
225214
raise PackagingOptionError(
226215
"can't combine user with prefix/exec_prefix/home or "
227216
"install_base/install_platbase")
@@ -274,11 +263,9 @@ def finalize_options(self):
274263
'exec_prefix': exec_prefix,
275264
'srcdir': srcdir,
276265
'projectbase': projectbase,
277-
}
278-
279-
if HAS_USER_SITE:
280-
self.config_vars['userbase'] = self.install_userbase
281-
self.config_vars['usersite'] = self.install_usersite
266+
'userbase': self.install_userbase,
267+
'usersite': self.install_usersite,
268+
}
282269

283270
self.expand_basedirs()
284271

@@ -295,9 +282,9 @@ def finalize_options(self):
295282

296283
self.dump_dirs("post-expand_dirs()")
297284

298-
# Create directories in the home dir:
299-
if HAS_USER_SITE and self.user:
300-
self.create_home_path()
285+
# Create directories under USERBASE
286+
if self.user:
287+
self.create_user_dirs()
301288

302289
# Pick the actual directory to install all modules to: either
303290
# install_purelib or install_platlib, depending on whether this
@@ -311,10 +298,8 @@ def finalize_options(self):
311298

312299
# Convert directories from Unix /-separated syntax to the local
313300
# convention.
314-
self.convert_paths('lib', 'purelib', 'platlib',
315-
'scripts', 'data', 'headers')
316-
if HAS_USER_SITE:
317-
self.convert_paths('userbase', 'usersite')
301+
self.convert_paths('lib', 'purelib', 'platlib', 'scripts',
302+
'data', 'headers', 'userbase', 'usersite')
318303

319304
# Well, we're not actually fully completely finalized yet: we still
320305
# have to deal with 'extra_path', which is the hack for allowing
@@ -355,7 +340,7 @@ def finalize_unix(self):
355340
"installation scheme is incomplete")
356341
return
357342

358-
if HAS_USER_SITE and self.user:
343+
if self.user:
359344
if self.install_userbase is None:
360345
raise PackagingPlatformError(
361346
"user base directory is not specified")
@@ -383,7 +368,7 @@ def finalize_unix(self):
383368

384369
def finalize_other(self):
385370
"""Finalize options for non-posix platforms"""
386-
if HAS_USER_SITE and self.user:
371+
if self.user:
387372
if self.install_userbase is None:
388373
raise PackagingPlatformError(
389374
"user base directory is not specified")
@@ -494,10 +479,8 @@ def change_roots(self, *names):
494479
attr = "install_" + name
495480
setattr(self, attr, change_root(self.root, getattr(self, attr)))
496481

497-
def create_home_path(self):
498-
"""Create directories under ~."""
499-
if HAS_USER_SITE and not self.user:
500-
return
482+
def create_user_dirs(self):
483+
"""Create directories under USERBASE as needed."""
501484
home = convert_path(os.path.expanduser("~"))
502485
for name, path in self.config_vars.items():
503486
if path.startswith(home) and not os.path.isdir(path):

Lib/packaging/command/install_distinfo.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
# Forked from the former install_egg_info command by Josip Djolonga
44

5-
import csv
65
import os
7-
import re
6+
import csv
87
import hashlib
8+
from shutil import rmtree
99

10-
from packaging.command.cmd import Command
1110
from packaging import logger
12-
from shutil import rmtree
11+
from packaging.command.cmd import Command
1312

1413

1514
class install_distinfo(Command):

Lib/packaging/command/install_lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def install(self):
114114
return outfiles
115115

116116
def byte_compile(self, files):
117-
if getattr(sys, 'dont_write_bytecode'):
117+
if sys.dont_write_bytecode:
118118
# XXX do we want this? because a Python runs without bytecode
119119
# doesn't mean that the *dists should not contain bytecode
120120
#--or does it?

Lib/packaging/compiler/bcppcompiler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,5 +352,4 @@ def preprocess(self, source, output_file=None, macros=None,
352352
try:
353353
self.spawn(pp_args)
354354
except PackagingExecError as msg:
355-
print(msg)
356355
raise CompileError(msg)

Lib/packaging/compiler/ccompiler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"""
66

77
import os
8-
import sys
98
from shutil import move
109
from packaging import logger
1110
from packaging.util import split_quoted, execute, newer_group, spawn

Lib/packaging/compiler/unixccompiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class UnixCCompiler(CCompiler):
127127
executables['ranlib'] = ["ranlib"]
128128

129129
# Needed for the filename generation methods provided by the base
130-
# class, CCompiler. NB. whoever instantiates/uses a particular
130+
# class, CCompiler. XXX whoever instantiates/uses a particular
131131
# UnixCCompiler instance should set 'shared_lib_ext' -- we set a
132132
# reasonable common default here, but it's not necessarily used on all
133133
# Unices!

Lib/packaging/dist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ def print_command_list(self, commands, header, max_length):
537537
def _get_command_groups(self):
538538
"""Helper function to retrieve all the command class names divided
539539
into standard commands (listed in
540-
packaging2.command.STANDARD_COMMANDS) and extra commands (given in
540+
packaging.command.STANDARD_COMMANDS) and extra commands (given in
541541
self.cmdclass and not standard commands).
542542
"""
543543
extra_commands = [cmd for cmd in self.cmdclass
@@ -547,7 +547,7 @@ def _get_command_groups(self):
547547
def print_commands(self):
548548
"""Print out a help message listing all available commands with a
549549
description of each. The list is divided into standard commands
550-
(listed in packaging2.command.STANDARD_COMMANDS) and extra commands
550+
(listed in packaging.command.STANDARD_COMMANDS) and extra commands
551551
(given in self.cmdclass and not standard commands). The
552552
descriptions come from the command class attribute
553553
'description'.

Lib/packaging/tests/test_command_build_ext.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@ class BuildExtTestCase(support.TempdirManager,
1818
support.LoggingCatcher,
1919
unittest.TestCase):
2020
def setUp(self):
21-
# Create a simple test environment
22-
# Note that we're making changes to sys.path
2321
super(BuildExtTestCase, self).setUp()
2422
self.tmp_dir = self.mkdtemp()
2523
self.old_user_base = site.USER_BASE
2624
site.USER_BASE = self.mkdtemp()
2725

2826
def tearDown(self):
29-
# Get everything back to normal
30-
if sys.version > "2.6":
31-
site.USER_BASE = self.old_user_base
32-
27+
site.USER_BASE = self.old_user_base
3328
super(BuildExtTestCase, self).tearDown()
3429

3530
def test_build_ext(self):
@@ -94,7 +89,6 @@ def test_solaris_enable_shared(self):
9489
# make sure we get some library dirs under solaris
9590
self.assertGreater(len(cmd.library_dirs), 0)
9691

97-
@unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher')
9892
def test_user_site(self):
9993
dist = Distribution({'name': 'xx'})
10094
cmd = build_ext(dist)

0 commit comments

Comments
 (0)