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

Skip to content

Commit 1231a4e

Browse files
committed
initial import of the packaging package in the standard library
1 parent 566f8a6 commit 1231a4e

193 files changed

Lines changed: 30378 additions & 151 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Lib/packaging/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Support for packaging, distribution and installation of Python projects.
2+
3+
Third-party tools can use parts of packaging as building blocks
4+
without causing the other modules to be imported:
5+
6+
import packaging.version
7+
import packaging.metadata
8+
import packaging.pypi.simple
9+
import packaging.tests.pypi_server
10+
"""
11+
12+
from logging import getLogger
13+
14+
__all__ = ['__version__', 'logger']
15+
16+
__version__ = "1.0a3"
17+
logger = getLogger('packaging')

Lib/packaging/_trove.py

Lines changed: 552 additions & 0 deletions
Large diffs are not rendered by default.

Lib/packaging/command/__init__.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Subpackage containing all standard commands."""
2+
3+
from packaging.errors import PackagingModuleError
4+
from packaging.util import resolve_name
5+
6+
__all__ = ['get_command_names', 'set_command', 'get_command_class',
7+
'STANDARD_COMMANDS']
8+
9+
_COMMANDS = {
10+
'check': 'packaging.command.check.check',
11+
'test': 'packaging.command.test.test',
12+
'build': 'packaging.command.build.build',
13+
'build_py': 'packaging.command.build_py.build_py',
14+
'build_ext': 'packaging.command.build_ext.build_ext',
15+
'build_clib': 'packaging.command.build_clib.build_clib',
16+
'build_scripts': 'packaging.command.build_scripts.build_scripts',
17+
'clean': 'packaging.command.clean.clean',
18+
'install_dist': 'packaging.command.install_dist.install_dist',
19+
'install_lib': 'packaging.command.install_lib.install_lib',
20+
'install_headers': 'packaging.command.install_headers.install_headers',
21+
'install_scripts': 'packaging.command.install_scripts.install_scripts',
22+
'install_data': 'packaging.command.install_data.install_data',
23+
'install_distinfo':
24+
'packaging.command.install_distinfo.install_distinfo',
25+
'sdist': 'packaging.command.sdist.sdist',
26+
'bdist': 'packaging.command.bdist.bdist',
27+
'bdist_dumb': 'packaging.command.bdist_dumb.bdist_dumb',
28+
'bdist_wininst': 'packaging.command.bdist_wininst.bdist_wininst',
29+
'register': 'packaging.command.register.register',
30+
'upload': 'packaging.command.upload.upload',
31+
'upload_docs': 'packaging.command.upload_docs.upload_docs'}
32+
33+
STANDARD_COMMANDS = set(_COMMANDS)
34+
35+
36+
def get_command_names():
37+
"""Return registered commands"""
38+
return sorted(_COMMANDS)
39+
40+
41+
def set_command(location):
42+
cls = resolve_name(location)
43+
# XXX we want to do the duck-type checking here
44+
_COMMANDS[cls.get_command_name()] = cls
45+
46+
47+
def get_command_class(name):
48+
"""Return the registered command"""
49+
try:
50+
cls = _COMMANDS[name]
51+
if isinstance(cls, str):
52+
cls = resolve_name(cls)
53+
_COMMANDS[name] = cls
54+
return cls
55+
except KeyError:
56+
raise PackagingModuleError("Invalid command %s" % name)

Lib/packaging/command/bdist.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
"""Create a built (binary) distribution.
2+
3+
If a --formats option was given on the command line, this command will
4+
call the corresponding bdist_* commands; if the option was absent, a
5+
bdist_* command depending on the current platform will be called.
6+
"""
7+
8+
import os
9+
10+
from packaging import util
11+
from packaging.command.cmd import Command
12+
from packaging.errors import PackagingPlatformError, PackagingOptionError
13+
14+
15+
def show_formats():
16+
"""Print list of available formats (arguments to "--format" option).
17+
"""
18+
from packaging.fancy_getopt import FancyGetopt
19+
formats = []
20+
for format in bdist.format_commands:
21+
formats.append(("formats=" + format, None,
22+
bdist.format_command[format][1]))
23+
pretty_printer = FancyGetopt(formats)
24+
pretty_printer.print_help("List of available distribution formats:")
25+
26+
27+
class bdist(Command):
28+
29+
description = "create a built (binary) distribution"
30+
31+
user_options = [('bdist-base=', 'b',
32+
"temporary directory for creating built distributions"),
33+
('plat-name=', 'p',
34+
"platform name to embed in generated filenames "
35+
"(default: %s)" % util.get_platform()),
36+
('formats=', None,
37+
"formats for distribution (comma-separated list)"),
38+
('dist-dir=', 'd',
39+
"directory to put final built distributions in "
40+
"[default: dist]"),
41+
('skip-build', None,
42+
"skip rebuilding everything (for testing/debugging)"),
43+
('owner=', 'u',
44+
"Owner name used when creating a tar file"
45+
" [default: current user]"),
46+
('group=', 'g',
47+
"Group name used when creating a tar file"
48+
" [default: current group]"),
49+
]
50+
51+
boolean_options = ['skip-build']
52+
53+
help_options = [
54+
('help-formats', None,
55+
"lists available distribution formats", show_formats),
56+
]
57+
58+
# This is of course very simplistic. The various UNIX family operating
59+
# systems have their specific formats, but they are out of scope for us;
60+
# bdist_dumb is, well, dumb; it's more a building block for other
61+
# packaging tools than a real end-user binary format.
62+
default_format = {'posix': 'gztar',
63+
'nt': 'zip',
64+
'os2': 'zip'}
65+
66+
# Establish the preferred order (for the --help-formats option).
67+
format_commands = ['gztar', 'bztar', 'ztar', 'tar',
68+
'wininst', 'zip', 'msi']
69+
70+
# And the real information.
71+
format_command = {'gztar': ('bdist_dumb', "gzip'ed tar file"),
72+
'bztar': ('bdist_dumb', "bzip2'ed tar file"),
73+
'ztar': ('bdist_dumb', "compressed tar file"),
74+
'tar': ('bdist_dumb', "tar file"),
75+
'wininst': ('bdist_wininst',
76+
"Windows executable installer"),
77+
'zip': ('bdist_dumb', "ZIP file"),
78+
'msi': ('bdist_msi', "Microsoft Installer")
79+
}
80+
81+
82+
def initialize_options(self):
83+
self.bdist_base = None
84+
self.plat_name = None
85+
self.formats = None
86+
self.dist_dir = None
87+
self.skip_build = False
88+
self.group = None
89+
self.owner = None
90+
91+
def finalize_options(self):
92+
# have to finalize 'plat_name' before 'bdist_base'
93+
if self.plat_name is None:
94+
if self.skip_build:
95+
self.plat_name = util.get_platform()
96+
else:
97+
self.plat_name = self.get_finalized_command('build').plat_name
98+
99+
# 'bdist_base' -- parent of per-built-distribution-format
100+
# temporary directories (eg. we'll probably have
101+
# "build/bdist.<plat>/dumb", etc.)
102+
if self.bdist_base is None:
103+
build_base = self.get_finalized_command('build').build_base
104+
self.bdist_base = os.path.join(build_base,
105+
'bdist.' + self.plat_name)
106+
107+
self.ensure_string_list('formats')
108+
if self.formats is None:
109+
try:
110+
self.formats = [self.default_format[os.name]]
111+
except KeyError:
112+
raise PackagingPlatformError("don't know how to create built distributions " + \
113+
"on platform %s" % os.name)
114+
115+
if self.dist_dir is None:
116+
self.dist_dir = "dist"
117+
118+
def run(self):
119+
# Figure out which sub-commands we need to run.
120+
commands = []
121+
for format in self.formats:
122+
try:
123+
commands.append(self.format_command[format][0])
124+
except KeyError:
125+
raise PackagingOptionError("invalid format '%s'" % format)
126+
127+
# Reinitialize and run each command.
128+
for i in range(len(self.formats)):
129+
cmd_name = commands[i]
130+
sub_cmd = self.get_reinitialized_command(cmd_name)
131+
132+
# passing the owner and group names for tar archiving
133+
if cmd_name == 'bdist_dumb':
134+
sub_cmd.owner = self.owner
135+
sub_cmd.group = self.group
136+
137+
# If we're going to need to run this command again, tell it to
138+
# keep its temporary files around so subsequent runs go faster.
139+
if cmd_name in commands[i+1:]:
140+
sub_cmd.keep_temp = True
141+
self.run_command(cmd_name)
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
"""Create a "dumb" built distribution.
2+
3+
A dumb distribution is just an archive meant to be unpacked under
4+
sys.prefix or sys.exec_prefix.
5+
"""
6+
7+
import os
8+
9+
from shutil import rmtree
10+
from sysconfig import get_python_version
11+
from packaging.util import get_platform
12+
from packaging.command.cmd import Command
13+
from packaging.errors import PackagingPlatformError
14+
from packaging import logger
15+
16+
class bdist_dumb(Command):
17+
18+
description = 'create a "dumb" built distribution'
19+
20+
user_options = [('bdist-dir=', 'd',
21+
"temporary directory for creating the distribution"),
22+
('plat-name=', 'p',
23+
"platform name to embed in generated filenames "
24+
"(default: %s)" % get_platform()),
25+
('format=', 'f',
26+
"archive format to create (tar, ztar, gztar, zip)"),
27+
('keep-temp', 'k',
28+
"keep the pseudo-installation tree around after " +
29+
"creating the distribution archive"),
30+
('dist-dir=', 'd',
31+
"directory to put final built distributions in"),
32+
('skip-build', None,
33+
"skip rebuilding everything (for testing/debugging)"),
34+
('relative', None,
35+
"build the archive using relative paths"
36+
"(default: false)"),
37+
('owner=', 'u',
38+
"Owner name used when creating a tar file"
39+
" [default: current user]"),
40+
('group=', 'g',
41+
"Group name used when creating a tar file"
42+
" [default: current group]"),
43+
]
44+
45+
boolean_options = ['keep-temp', 'skip-build', 'relative']
46+
47+
default_format = { 'posix': 'gztar',
48+
'nt': 'zip',
49+
'os2': 'zip' }
50+
51+
52+
def initialize_options(self):
53+
self.bdist_dir = None
54+
self.plat_name = None
55+
self.format = None
56+
self.keep_temp = False
57+
self.dist_dir = None
58+
self.skip_build = False
59+
self.relative = False
60+
self.owner = None
61+
self.group = None
62+
63+
def finalize_options(self):
64+
if self.bdist_dir is None:
65+
bdist_base = self.get_finalized_command('bdist').bdist_base
66+
self.bdist_dir = os.path.join(bdist_base, 'dumb')
67+
68+
if self.format is None:
69+
try:
70+
self.format = self.default_format[os.name]
71+
except KeyError:
72+
raise PackagingPlatformError(("don't know how to create dumb built distributions " +
73+
"on platform %s") % os.name)
74+
75+
self.set_undefined_options('bdist', 'dist_dir', 'plat_name')
76+
77+
def run(self):
78+
if not self.skip_build:
79+
self.run_command('build')
80+
81+
install = self.get_reinitialized_command('install_dist',
82+
reinit_subcommands=True)
83+
install.root = self.bdist_dir
84+
install.skip_build = self.skip_build
85+
install.warn_dir = False
86+
87+
logger.info("installing to %s", self.bdist_dir)
88+
self.run_command('install_dist')
89+
90+
# And make an archive relative to the root of the
91+
# pseudo-installation tree.
92+
archive_basename = "%s.%s" % (self.distribution.get_fullname(),
93+
self.plat_name)
94+
95+
# OS/2 objects to any ":" characters in a filename (such as when
96+
# a timestamp is used in a version) so change them to hyphens.
97+
if os.name == "os2":
98+
archive_basename = archive_basename.replace(":", "-")
99+
100+
pseudoinstall_root = os.path.join(self.dist_dir, archive_basename)
101+
if not self.relative:
102+
archive_root = self.bdist_dir
103+
else:
104+
if (self.distribution.has_ext_modules() and
105+
(install.install_base != install.install_platbase)):
106+
raise PackagingPlatformError(
107+
"can't make a dumb built distribution where base and "
108+
"platbase are different (%r, %r)" %
109+
(install.install_base, install.install_platbase))
110+
else:
111+
archive_root = os.path.join(
112+
self.bdist_dir,
113+
self._ensure_relative(install.install_base))
114+
115+
# Make the archive
116+
filename = self.make_archive(pseudoinstall_root,
117+
self.format, root_dir=archive_root,
118+
owner=self.owner, group=self.group)
119+
if self.distribution.has_ext_modules():
120+
pyversion = get_python_version()
121+
else:
122+
pyversion = 'any'
123+
self.distribution.dist_files.append(('bdist_dumb', pyversion,
124+
filename))
125+
126+
if not self.keep_temp:
127+
if self.dry_run:
128+
logger.info('removing %s', self.bdist_dir)
129+
else:
130+
rmtree(self.bdist_dir)
131+
132+
def _ensure_relative(self, path):
133+
# copied from dir_util, deleted
134+
drive, path = os.path.splitdrive(path)
135+
if path[0:1] == os.sep:
136+
path = drive + path[1:]
137+
return path

0 commit comments

Comments
 (0)