Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 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 | |
Éric Araujo | 83ab3f3 | 2011-08-30 01:19:02 +0200 | [diff] [blame] | 16 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 17 | class 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 Araujo | 83ab3f3 | 2011-08-30 01:19:02 +0200 | [diff] [blame] | 27 | "archive format to create (tar, gztar, zip)"), |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 28 | ('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 Araujo | 83ab3f3 | 2011-08-30 01:19:02 +0200 | [diff] [blame] | 48 | default_format = {'posix': 'gztar', |
| 49 | 'nt': 'zip', |
| 50 | 'os2': 'zip'} |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 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 |
Éric Araujo | b9fe54c | 2011-08-30 01:42:50 +0200 | [diff] [blame^] | 58 | self.skip_build = None |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 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: |
Éric Araujo | 83ab3f3 | 2011-08-30 01:19:02 +0200 | [diff] [blame] | 72 | raise PackagingPlatformError( |
| 73 | "don't know how to create dumb built distributions " |
| 74 | "on platform %s" % os.name) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 75 | |
Éric Araujo | b9fe54c | 2011-08-30 01:42:50 +0200 | [diff] [blame^] | 76 | self.set_undefined_options('bdist', |
| 77 | 'dist_dir', 'plat_name', 'skip_build') |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 78 | |
| 79 | def run(self): |
| 80 | if not self.skip_build: |
| 81 | self.run_command('build') |
| 82 | |
| 83 | install = self.get_reinitialized_command('install_dist', |
| 84 | reinit_subcommands=True) |
| 85 | 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 |