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 | |
| 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 |