blob: 548e3c4930c8f2d5cd061a0957a55d3f3bc8dc66 [file] [log] [blame]
Tarek Ziade1231a4e2011-05-19 13:07:25 +02001"""Create a "dumb" built distribution.
2
3A dumb distribution is just an archive meant to be unpacked under
4sys.prefix or sys.exec_prefix.
5"""
6
7import os
Tarek Ziade1231a4e2011-05-19 13:07:25 +02008from shutil import rmtree
9from sysconfig import get_python_version
Éric Araujo505f0eb2011-09-19 15:12:23 +020010
Tarek Ziade1231a4e2011-05-19 13:07:25 +020011from packaging.util import get_platform
12from packaging.command.cmd import Command
13from packaging.errors import PackagingPlatformError
14from packaging import logger
15
Éric Araujo83ab3f32011-08-30 01:19:02 +020016
Tarek Ziade1231a4e2011-05-19 13:07:25 +020017class bdist_dumb(Command):
18
19 description = 'create a "dumb" built distribution'
20
21 user_options = [('bdist-dir=', 'd',
22 "temporary directory for creating the distribution"),
23 ('plat-name=', 'p',
24 "platform name to embed in generated filenames "
25 "(default: %s)" % get_platform()),
26 ('format=', 'f',
Éric Araujo505f0eb2011-09-19 15:12:23 +020027 "archive format to create (tar, gztar, bztar, zip)"),
Tarek Ziade1231a4e2011-05-19 13:07:25 +020028 ('keep-temp', 'k',
29 "keep the pseudo-installation tree around after " +
30 "creating the distribution archive"),
31 ('dist-dir=', 'd',
32 "directory to put final built distributions in"),
33 ('skip-build', None,
34 "skip rebuilding everything (for testing/debugging)"),
35 ('relative', None,
36 "build the archive using relative paths"
37 "(default: false)"),
38 ('owner=', 'u',
39 "Owner name used when creating a tar file"
40 " [default: current user]"),
41 ('group=', 'g',
42 "Group name used when creating a tar file"
43 " [default: current group]"),
44 ]
45
46 boolean_options = ['keep-temp', 'skip-build', 'relative']
47
Éric Araujo83ab3f32011-08-30 01:19:02 +020048 default_format = {'posix': 'gztar',
49 'nt': 'zip',
50 'os2': 'zip'}
Tarek Ziade1231a4e2011-05-19 13:07:25 +020051
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
Éric Araujob9fe54c2011-08-30 01:42:50 +020058 self.skip_build = None
Tarek Ziade1231a4e2011-05-19 13:07:25 +020059 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:
Éric Araujo83ab3f32011-08-30 01:19:02 +020072 raise PackagingPlatformError(
73 "don't know how to create dumb built distributions "
74 "on platform %s" % os.name)
Tarek Ziade1231a4e2011-05-19 13:07:25 +020075
Éric Araujob9fe54c2011-08-30 01:42:50 +020076 self.set_undefined_options('bdist',
77 'dist_dir', 'plat_name', 'skip_build')
Tarek Ziade1231a4e2011-05-19 13:07:25 +020078
79 def run(self):
80 if not self.skip_build:
81 self.run_command('build')
82
Éric Araujoa963e0d2011-11-06 06:54:05 +010083 install = self.reinitialize_command('install_dist',
84 reinit_subcommands=True)
Tarek Ziade1231a4e2011-05-19 13:07:25 +020085 install.root = self.bdist_dir
86 install.skip_build = self.skip_build
87 install.warn_dir = False
88
89 logger.info("installing to %s", self.bdist_dir)
90 self.run_command('install_dist')
91
92 # And make an archive relative to the root of the
93 # pseudo-installation tree.
94 archive_basename = "%s.%s" % (self.distribution.get_fullname(),
95 self.plat_name)
96
97 # OS/2 objects to any ":" characters in a filename (such as when
98 # a timestamp is used in a version) so change them to hyphens.
99 if os.name == "os2":
100 archive_basename = archive_basename.replace(":", "-")
101
102 pseudoinstall_root = os.path.join(self.dist_dir, archive_basename)
103 if not self.relative:
104 archive_root = self.bdist_dir
105 else:
106 if (self.distribution.has_ext_modules() and
107 (install.install_base != install.install_platbase)):
108 raise PackagingPlatformError(
109 "can't make a dumb built distribution where base and "
110 "platbase are different (%r, %r)" %
111 (install.install_base, install.install_platbase))
112 else:
113 archive_root = os.path.join(
114 self.bdist_dir,
115 self._ensure_relative(install.install_base))
116
117 # Make the archive
118 filename = self.make_archive(pseudoinstall_root,
119 self.format, root_dir=archive_root,
120 owner=self.owner, group=self.group)
121 if self.distribution.has_ext_modules():
122 pyversion = get_python_version()
123 else:
124 pyversion = 'any'
125 self.distribution.dist_files.append(('bdist_dumb', pyversion,
126 filename))
127
128 if not self.keep_temp:
129 if self.dry_run:
130 logger.info('removing %s', self.bdist_dir)
131 else:
132 rmtree(self.bdist_dir)
133
134 def _ensure_relative(self, path):
135 # copied from dir_util, deleted
136 drive, path = os.path.splitdrive(path)
137 if path[0:1] == os.sep:
138 path = drive + path[1:]
139 return path