+++ /dev/null
-"""distutils.command.build_py\r
-\r
-Implements the Distutils 'build_py' command."""\r
-\r
-__revision__ = "$Id$"\r
-\r
-import os\r
-import sys\r
-from glob import glob\r
-\r
-from distutils.core import Command\r
-from distutils.errors import DistutilsOptionError, DistutilsFileError\r
-from distutils.util import convert_path\r
-from distutils import log\r
-\r
-class build_py(Command):\r
-\r
- description = "\"build\" pure Python modules (copy to build directory)"\r
-\r
- user_options = [\r
- ('build-lib=', 'd', "directory to \"build\" (copy) to"),\r
- ('compile', 'c', "compile .py to .pyc"),\r
- ('no-compile', None, "don't compile .py files [default]"),\r
- ('optimize=', 'O',\r
- "also compile with optimization: -O1 for \"python -O\", "\r
- "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),\r
- ('force', 'f', "forcibly build everything (ignore file timestamps)"),\r
- ]\r
-\r
- boolean_options = ['compile', 'force']\r
- negative_opt = {'no-compile' : 'compile'}\r
-\r
- def initialize_options(self):\r
- self.build_lib = None\r
- self.py_modules = None\r
- self.package = None\r
- self.package_data = None\r
- self.package_dir = None\r
- self.compile = 0\r
- self.optimize = 0\r
- self.force = None\r
-\r
- def finalize_options(self):\r
- self.set_undefined_options('build',\r
- ('build_lib', 'build_lib'),\r
- ('force', 'force'))\r
-\r
- # Get the distribution options that are aliases for build_py\r
- # options -- list of packages and list of modules.\r
- self.packages = self.distribution.packages\r
- self.py_modules = self.distribution.py_modules\r
- self.package_data = self.distribution.package_data\r
- self.package_dir = {}\r
- if self.distribution.package_dir:\r
- for name, path in self.distribution.package_dir.items():\r
- self.package_dir[name] = convert_path(path)\r
- self.data_files = self.get_data_files()\r
-\r
- # Ick, copied straight from install_lib.py (fancy_getopt needs a\r
- # type system! Hell, *everything* needs a type system!!!)\r
- if not isinstance(self.optimize, int):\r
- try:\r
- self.optimize = int(self.optimize)\r
- assert 0 <= self.optimize <= 2\r
- except (ValueError, AssertionError):\r
- raise DistutilsOptionError("optimize must be 0, 1, or 2")\r
-\r
- def run(self):\r
- # XXX copy_file by default preserves atime and mtime. IMHO this is\r
- # the right thing to do, but perhaps it should be an option -- in\r
- # particular, a site administrator might want installed files to\r
- # reflect the time of installation rather than the last\r
- # modification time before the installed release.\r
-\r
- # XXX copy_file by default preserves mode, which appears to be the\r
- # wrong thing to do: if a file is read-only in the working\r
- # directory, we want it to be installed read/write so that the next\r
- # installation of the same module distribution can overwrite it\r
- # without problems. (This might be a Unix-specific issue.) Thus\r
- # we turn off 'preserve_mode' when copying to the build directory,\r
- # since the build directory is supposed to be exactly what the\r
- # installation will look like (ie. we preserve mode when\r
- # installing).\r
-\r
- # Two options control which modules will be installed: 'packages'\r
- # and 'py_modules'. The former lets us work with whole packages, not\r
- # specifying individual modules at all; the latter is for\r
- # specifying modules one-at-a-time.\r
-\r
- if self.py_modules:\r
- self.build_modules()\r
- if self.packages:\r
- self.build_packages()\r
- self.build_package_data()\r
-\r
- self.byte_compile(self.get_outputs(include_bytecode=0))\r
-\r
- def get_data_files(self):\r
- """Generate list of '(package,src_dir,build_dir,filenames)' tuples"""\r
- data = []\r
- if not self.packages:\r
- return data\r
- for package in self.packages:\r
- # Locate package source directory\r
- src_dir = self.get_package_dir(package)\r
-\r
- # Compute package build directory\r
- build_dir = os.path.join(*([self.build_lib] + package.split('.')))\r
-\r
- # Length of path to strip from found files\r
- plen = 0\r
- if src_dir:\r
- plen = len(src_dir)+1\r
-\r
- # Strip directory from globbed filenames\r
- filenames = [\r
- file[plen:] for file in self.find_data_files(package, src_dir)\r
- ]\r
- data.append((package, src_dir, build_dir, filenames))\r
- return data\r
-\r
- def find_data_files(self, package, src_dir):\r
- """Return filenames for package's data files in 'src_dir'"""\r
- globs = (self.package_data.get('', [])\r
- + self.package_data.get(package, []))\r
- files = []\r
- for pattern in globs:\r
- # Each pattern has to be converted to a platform-specific path\r
- filelist = glob(os.path.join(src_dir, convert_path(pattern)))\r
- # Files that match more than one pattern are only added once\r
- files.extend([fn for fn in filelist if fn not in files])\r
- return files\r
-\r
- def build_package_data(self):\r
- """Copy data files into build directory"""\r
- for package, src_dir, build_dir, filenames in self.data_files:\r
- for filename in filenames:\r
- target = os.path.join(build_dir, filename)\r
- self.mkpath(os.path.dirname(target))\r
- self.copy_file(os.path.join(src_dir, filename), target,\r
- preserve_mode=False)\r
-\r
- def get_package_dir(self, package):\r
- """Return the directory, relative to the top of the source\r
- distribution, where package 'package' should be found\r
- (at least according to the 'package_dir' option, if any)."""\r
-\r
- path = package.split('.')\r
-\r
- if not self.package_dir:\r
- if path:\r
- return os.path.join(*path)\r
- else:\r
- return ''\r
- else:\r
- tail = []\r
- while path:\r
- try:\r
- pdir = self.package_dir['.'.join(path)]\r
- except KeyError:\r
- tail.insert(0, path[-1])\r
- del path[-1]\r
- else:\r
- tail.insert(0, pdir)\r
- return os.path.join(*tail)\r
- else:\r
- # Oops, got all the way through 'path' without finding a\r
- # match in package_dir. If package_dir defines a directory\r
- # for the root (nameless) package, then fallback on it;\r
- # otherwise, we might as well have not consulted\r
- # package_dir at all, as we just use the directory implied\r
- # by 'tail' (which should be the same as the original value\r
- # of 'path' at this point).\r
- pdir = self.package_dir.get('')\r
- if pdir is not None:\r
- tail.insert(0, pdir)\r
-\r
- if tail:\r
- return os.path.join(*tail)\r
- else:\r
- return ''\r
-\r
- def check_package(self, package, package_dir):\r
- # Empty dir name means current directory, which we can probably\r
- # assume exists. Also, os.path.exists and isdir don't know about\r
- # my "empty string means current dir" convention, so we have to\r
- # circumvent them.\r
- if package_dir != "":\r
- if not os.path.exists(package_dir):\r
- raise DistutilsFileError(\r
- "package directory '%s' does not exist" % package_dir)\r
- if not os.path.isdir(package_dir):\r
- raise DistutilsFileError(\r
- "supposed package directory '%s' exists, "\r
- "but is not a directory" % package_dir)\r
-\r
- # Require __init__.py for all but the "root package"\r
- if package:\r
- init_py = os.path.join(package_dir, "__init__.py")\r
- if os.path.isfile(init_py):\r
- return init_py\r
- else:\r
- log.warn(("package init file '%s' not found " +\r
- "(or not a regular file)"), init_py)\r
-\r
- # Either not in a package at all (__init__.py not expected), or\r
- # __init__.py doesn't exist -- so don't return the filename.\r
- return None\r
-\r
- def check_module(self, module, module_file):\r
- if not os.path.isfile(module_file):\r
- log.warn("file %s (for module %s) not found", module_file, module)\r
- return False\r
- else:\r
- return True\r
-\r
- def find_package_modules(self, package, package_dir):\r
- self.check_package(package, package_dir)\r
- module_files = glob(os.path.join(package_dir, "*.py"))\r
- modules = []\r
- setup_script = os.path.abspath(self.distribution.script_name)\r
-\r
- for f in module_files:\r
- abs_f = os.path.abspath(f)\r
- if abs_f != setup_script:\r
- module = os.path.splitext(os.path.basename(f))[0]\r
- modules.append((package, module, f))\r
- else:\r
- self.debug_print("excluding %s" % setup_script)\r
- return modules\r
-\r
- def find_modules(self):\r
- """Finds individually-specified Python modules, ie. those listed by\r
- module name in 'self.py_modules'. Returns a list of tuples (package,\r
- module_base, filename): 'package' is a tuple of the path through\r
- package-space to the module; 'module_base' is the bare (no\r
- packages, no dots) module name, and 'filename' is the path to the\r
- ".py" file (relative to the distribution root) that implements the\r
- module.\r
- """\r
- # Map package names to tuples of useful info about the package:\r
- # (package_dir, checked)\r
- # package_dir - the directory where we'll find source files for\r
- # this package\r
- # checked - true if we have checked that the package directory\r
- # is valid (exists, contains __init__.py, ... ?)\r
- packages = {}\r
-\r
- # List of (package, module, filename) tuples to return\r
- modules = []\r
-\r
- # We treat modules-in-packages almost the same as toplevel modules,\r
- # just the "package" for a toplevel is empty (either an empty\r
- # string or empty list, depending on context). Differences:\r
- # - don't check for __init__.py in directory for empty package\r
- for module in self.py_modules:\r
- path = module.split('.')\r
- package = '.'.join(path[0:-1])\r
- module_base = path[-1]\r
-\r
- try:\r
- (package_dir, checked) = packages[package]\r
- except KeyError:\r
- package_dir = self.get_package_dir(package)\r
- checked = 0\r
-\r
- if not checked:\r
- init_py = self.check_package(package, package_dir)\r
- packages[package] = (package_dir, 1)\r
- if init_py:\r
- modules.append((package, "__init__", init_py))\r
-\r
- # XXX perhaps we should also check for just .pyc files\r
- # (so greedy closed-source bastards can distribute Python\r
- # modules too)\r
- module_file = os.path.join(package_dir, module_base + ".py")\r
- if not self.check_module(module, module_file):\r
- continue\r
-\r
- modules.append((package, module_base, module_file))\r
-\r
- return modules\r
-\r
- def find_all_modules(self):\r
- """Compute the list of all modules that will be built, whether\r
- they are specified one-module-at-a-time ('self.py_modules') or\r
- by whole packages ('self.packages'). Return a list of tuples\r
- (package, module, module_file), just like 'find_modules()' and\r
- 'find_package_modules()' do."""\r
- modules = []\r
- if self.py_modules:\r
- modules.extend(self.find_modules())\r
- if self.packages:\r
- for package in self.packages:\r
- package_dir = self.get_package_dir(package)\r
- m = self.find_package_modules(package, package_dir)\r
- modules.extend(m)\r
- return modules\r
-\r
- def get_source_files(self):\r
- return [module[-1] for module in self.find_all_modules()]\r
-\r
- def get_module_outfile(self, build_dir, package, module):\r
- outfile_path = [build_dir] + list(package) + [module + ".py"]\r
- return os.path.join(*outfile_path)\r
-\r
- def get_outputs(self, include_bytecode=1):\r
- modules = self.find_all_modules()\r
- outputs = []\r
- for (package, module, module_file) in modules:\r
- package = package.split('.')\r
- filename = self.get_module_outfile(self.build_lib, package, module)\r
- outputs.append(filename)\r
- if include_bytecode:\r
- if self.compile:\r
- outputs.append(filename + "c")\r
- if self.optimize > 0:\r
- outputs.append(filename + "o")\r
-\r
- outputs += [\r
- os.path.join(build_dir, filename)\r
- for package, src_dir, build_dir, filenames in self.data_files\r
- for filename in filenames\r
- ]\r
-\r
- return outputs\r
-\r
- def build_module(self, module, module_file, package):\r
- if isinstance(package, str):\r
- package = package.split('.')\r
- elif not isinstance(package, (list, tuple)):\r
- raise TypeError(\r
- "'package' must be a string (dot-separated), list, or tuple")\r
-\r
- # Now put the module source file into the "build" area -- this is\r
- # easy, we just copy it somewhere under self.build_lib (the build\r
- # directory for Python source).\r
- outfile = self.get_module_outfile(self.build_lib, package, module)\r
- dir = os.path.dirname(outfile)\r
- self.mkpath(dir)\r
- return self.copy_file(module_file, outfile, preserve_mode=0)\r
-\r
- def build_modules(self):\r
- modules = self.find_modules()\r
- for (package, module, module_file) in modules:\r
-\r
- # Now "build" the module -- ie. copy the source file to\r
- # self.build_lib (the build directory for Python source).\r
- # (Actually, it gets copied to the directory for this package\r
- # under self.build_lib.)\r
- self.build_module(module, module_file, package)\r
-\r
- def build_packages(self):\r
- for package in self.packages:\r
-\r
- # Get list of (package, module, module_file) tuples based on\r
- # scanning the package directory. 'package' is only included\r
- # in the tuple so that 'find_modules()' and\r
- # 'find_package_tuples()' have a consistent interface; it's\r
- # ignored here (apart from a sanity check). Also, 'module' is\r
- # the *unqualified* module name (ie. no dots, no package -- we\r
- # already know its package!), and 'module_file' is the path to\r
- # the .py file, relative to the current directory\r
- # (ie. including 'package_dir').\r
- package_dir = self.get_package_dir(package)\r
- modules = self.find_package_modules(package, package_dir)\r
-\r
- # Now loop over the modules we found, "building" each one (just\r
- # copy it to self.build_lib).\r
- for (package_, module, module_file) in modules:\r
- assert package == package_\r
- self.build_module(module, module_file, package)\r
-\r
- def byte_compile(self, files):\r
- if sys.dont_write_bytecode:\r
- self.warn('byte-compiling is disabled, skipping.')\r
- return\r
-\r
- from distutils.util import byte_compile\r
- prefix = self.build_lib\r
- if prefix[-1] != os.sep:\r
- prefix = prefix + os.sep\r
-\r
- # XXX this code is essentially the same as the 'byte_compile()\r
- # method of the "install_lib" command, except for the determination\r
- # of the 'prefix' string. Hmmm.\r
-\r
- if self.compile:\r
- byte_compile(files, optimize=0,\r
- force=self.force, prefix=prefix, dry_run=self.dry_run)\r
- if self.optimize > 0:\r
- byte_compile(files, optimize=self.optimize,\r
- force=self.force, prefix=prefix, dry_run=self.dry_run)\r