annotate Lib/distutils/command/build_py.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children 0a1cf947eff6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
1 """distutils.command.build_py
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
2
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
3 Implements the Distutils 'build_py' command."""
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
4
11897
017e592c0fd2 Changed '__rcsid__' to '__revision__'.
Greg Ward <gward@python.net>
parents: 11884
diff changeset
5 __revision__ = "$Id$"
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
6
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
7 import os
58205
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 48260
diff changeset
8 import sys
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
9 from glob import glob
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
10
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
11 from distutils.core import Command
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
12 from distutils.errors import DistutilsOptionError, DistutilsFileError
21856
2f616474eaec package_dir must be converted from the distutils path conventions to
Thomas Heller <theller@ctypes.org>
parents: 21268
diff changeset
13 from distutils.util import convert_path
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22833
diff changeset
14 from distutils import log
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
15
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
16 class build_py(Command):
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
17
11683
47f75a17b103 Added 'description' class attribute to every command class (to help the
Greg Ward <gward@python.net>
parents: 11658
diff changeset
18 description = "\"build\" pure Python modules (copy to build directory)"
47f75a17b103 Added 'description' class attribute to every command class (to help the
Greg Ward <gward@python.net>
parents: 11658
diff changeset
19
11815
03902bc0214a Renamed all 'options' class attributes to 'user_options'.
Greg Ward <gward@python.net>
parents: 11813
diff changeset
20 user_options = [
11884
758a87f4cb2c Build reorg: change 'build_dir' option to 'build_lib'.
Greg Ward <gward@python.net>
parents: 11817
diff changeset
21 ('build-lib=', 'd', "directory to \"build\" (copy) to"),
14963
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
22 ('compile', 'c', "compile .py to .pyc"),
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
23 ('no-compile', None, "don't compile .py files [default]"),
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
24 ('optimize=', 'O',
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
25 "also compile with optimization: -O1 for \"python -O\", "
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
26 "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
14434
34379127ee3b Typo fix.
Greg Ward <gward@python.net>
parents: 14237
diff changeset
27 ('force', 'f', "forcibly build everything (ignore file timestamps)"),
11815
03902bc0214a Renamed all 'options' class attributes to 'user_options'.
Greg Ward <gward@python.net>
parents: 11813
diff changeset
28 ]
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
29
14963
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
30 boolean_options = ['compile', 'force']
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
31 negative_opt = {'no-compile' : 'compile'}
14829
b1f220f636ed Added 'boolean_options' list to support config file parsing.
Greg Ward <gward@python.net>
parents: 14434
diff changeset
32
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
33 def initialize_options(self):
11884
758a87f4cb2c Build reorg: change 'build_dir' option to 'build_lib'.
Greg Ward <gward@python.net>
parents: 11817
diff changeset
34 self.build_lib = None
12847
4916d40d9677 Renamed 'modules' option to 'py_modules', for consistency with Distribution
Greg Ward <gward@python.net>
parents: 12760
diff changeset
35 self.py_modules = None
11262
810c25d5041c Changed to reflect the new "command options" regime -- in particular,
Greg Ward <gward@python.net>
parents: 11247
diff changeset
36 self.package = None
32328
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
37 self.package_data = None
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
38 self.package_dir = None
14963
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
39 self.compile = 0
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
40 self.optimize = 0
12314
20a5cbf9fcc0 Added '--force' option -- very clear what it means for building (ignore
Greg Ward <gward@python.net>
parents: 12094
diff changeset
41 self.force = None
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
42
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
43 def finalize_options(self):
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
44 self.set_undefined_options('build',
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
45 ('build_lib', 'build_lib'),
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
46 ('force', 'force'))
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
47
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
48 # Get the distribution options that are aliases for build_py
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
49 # options -- list of packages and list of modules.
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
50 self.packages = self.distribution.packages
12847
4916d40d9677 Renamed 'modules' option to 'py_modules', for consistency with Distribution
Greg Ward <gward@python.net>
parents: 12760
diff changeset
51 self.py_modules = self.distribution.py_modules
32328
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
52 self.package_data = self.distribution.package_data
21856
2f616474eaec package_dir must be converted from the distutils path conventions to
Thomas Heller <theller@ctypes.org>
parents: 21268
diff changeset
53 self.package_dir = {}
2f616474eaec package_dir must be converted from the distutils path conventions to
Thomas Heller <theller@ctypes.org>
parents: 21268
diff changeset
54 if self.distribution.package_dir:
2f616474eaec package_dir must be converted from the distutils path conventions to
Thomas Heller <theller@ctypes.org>
parents: 21268
diff changeset
55 for name, path in self.distribution.package_dir.items():
2f616474eaec package_dir must be converted from the distutils path conventions to
Thomas Heller <theller@ctypes.org>
parents: 21268
diff changeset
56 self.package_dir[name] = convert_path(path)
32366
509072c3fa26 fix bug: list of data files was initialized too soon in build_py
Fred Drake <fdrake@acm.org>
parents: 32328
diff changeset
57 self.data_files = self.get_data_files()
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
58
14963
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
59 # Ick, copied straight from install_lib.py (fancy_getopt needs a
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
60 # type system! Hell, *everything* needs a type system!!!)
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
61 if not isinstance(self.optimize, int):
14963
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
62 try:
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
63 self.optimize = int(self.optimize)
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
64 assert 0 <= self.optimize <= 2
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
65 except (ValueError, AssertionError):
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
66 raise DistutilsOptionError("optimize must be 0, 1, or 2")
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
67
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
68 def run(self):
11545
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
69 # XXX copy_file by default preserves atime and mtime. IMHO this is
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
70 # the right thing to do, but perhaps it should be an option -- in
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
71 # particular, a site administrator might want installed files to
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
72 # reflect the time of installation rather than the last
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
73 # modification time before the installed release.
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
74
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
75 # XXX copy_file by default preserves mode, which appears to be the
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
76 # wrong thing to do: if a file is read-only in the working
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
77 # directory, we want it to be installed read/write so that the next
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
78 # installation of the same module distribution can overwrite it
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
79 # without problems. (This might be a Unix-specific issue.) Thus
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
80 # we turn off 'preserve_mode' when copying to the build directory,
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
81 # since the build directory is supposed to be exactly what the
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
82 # installation will look like (ie. we preserve mode when
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
83 # installing).
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
84
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
85 # Two options control which modules will be installed: 'packages'
12847
4916d40d9677 Renamed 'modules' option to 'py_modules', for consistency with Distribution
Greg Ward <gward@python.net>
parents: 12760
diff changeset
86 # and 'py_modules'. The former lets us work with whole packages, not
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
87 # specifying individual modules at all; the latter is for
28114
14463f478240 [Patch #695090 from Bernhard Herzog] Allow specifying both py_modules and packages
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
88 # specifying modules one-at-a-time.
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
89
12847
4916d40d9677 Renamed 'modules' option to 'py_modules', for consistency with Distribution
Greg Ward <gward@python.net>
parents: 12760
diff changeset
90 if self.py_modules:
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
91 self.build_modules()
28114
14463f478240 [Patch #695090 from Bernhard Herzog] Allow specifying both py_modules and packages
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
92 if self.packages:
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
93 self.build_packages()
32328
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
94 self.build_package_data()
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
95
14963
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
96 self.byte_compile(self.get_outputs(include_bytecode=0))
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
97
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
98 def get_data_files(self):
32328
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
99 """Generate list of '(package,src_dir,build_dir,filenames)' tuples"""
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
100 data = []
32378
a9a4b0df512a If self.packages is None (this can happen, I saw it), return
Sjoerd Mullender <sjoerd@acm.org>
parents: 32366
diff changeset
101 if not self.packages:
a9a4b0df512a If self.packages is None (this can happen, I saw it), return
Sjoerd Mullender <sjoerd@acm.org>
parents: 32366
diff changeset
102 return data
32328
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
103 for package in self.packages:
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
104 # Locate package source directory
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
105 src_dir = self.get_package_dir(package)
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
106
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
107 # Compute package build directory
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
108 build_dir = os.path.join(*([self.build_lib] + package.split('.')))
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
109
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
110 # Length of path to strip from found files
41929
24efa2e448bc SF 1668596/1720897: distutils now copies data files
Neal Norwitz <nnorwitz@gmail.com>
parents: 33965
diff changeset
111 plen = 0
24efa2e448bc SF 1668596/1720897: distutils now copies data files
Neal Norwitz <nnorwitz@gmail.com>
parents: 33965
diff changeset
112 if src_dir:
24efa2e448bc SF 1668596/1720897: distutils now copies data files
Neal Norwitz <nnorwitz@gmail.com>
parents: 33965
diff changeset
113 plen = len(src_dir)+1
32328
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
114
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
115 # Strip directory from globbed filenames
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
116 filenames = [
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
117 file[plen:] for file in self.find_data_files(package, src_dir)
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
118 ]
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
119 data.append((package, src_dir, build_dir, filenames))
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
120 return data
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
121
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
122 def find_data_files(self, package, src_dir):
32328
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
123 """Return filenames for package's data files in 'src_dir'"""
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
124 globs = (self.package_data.get('', [])
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
125 + self.package_data.get(package, []))
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
126 files = []
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
127 for pattern in globs:
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
128 # Each pattern has to be converted to a platform-specific path
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
129 filelist = glob(os.path.join(src_dir, convert_path(pattern)))
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
130 # Files that match more than one pattern are only added once
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
131 files.extend([fn for fn in filelist if fn not in files])
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
132 return files
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
133
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
134 def build_package_data(self):
32328
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
135 """Copy data files into build directory"""
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
136 for package, src_dir, build_dir, filenames in self.data_files:
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
137 for filename in filenames:
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
138 target = os.path.join(build_dir, filename)
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
139 self.mkpath(os.path.dirname(target))
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
140 self.copy_file(os.path.join(src_dir, filename), target,
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
141 preserve_mode=False)
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
142
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
143 def get_package_dir(self, package):
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
144 """Return the directory, relative to the top of the source
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
145 distribution, where package 'package' should be found
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
146 (at least according to the 'package_dir' option, if any)."""
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
147
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
148 path = package.split('.')
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
149
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
150 if not self.package_dir:
11521
ba07fff72511 [from 1999-11-04]
Greg Ward <gward@python.net>
parents: 11365
diff changeset
151 if path:
63663
cdf6a3a50407 Merged revisions 79191 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58205
diff changeset
152 return os.path.join(*path)
11521
ba07fff72511 [from 1999-11-04]
Greg Ward <gward@python.net>
parents: 11365
diff changeset
153 else:
ba07fff72511 [from 1999-11-04]
Greg Ward <gward@python.net>
parents: 11365
diff changeset
154 return ''
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
155 else:
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
156 tail = []
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
157 while path:
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
158 try:
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
159 pdir = self.package_dir['.'.join(path)]
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
160 except KeyError:
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
161 tail.insert(0, path[-1])
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
162 del path[-1]
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
163 else:
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
164 tail.insert(0, pdir)
48260
2be17d368a9c Update distutils so that it triggers no warnings when run under -3.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
165 return os.path.join(*tail)
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
166 else:
12760
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
167 # Oops, got all the way through 'path' without finding a
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
168 # match in package_dir. If package_dir defines a directory
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
169 # for the root (nameless) package, then fallback on it;
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
170 # otherwise, we might as well have not consulted
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
171 # package_dir at all, as we just use the directory implied
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
172 # by 'tail' (which should be the same as the original value
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
173 # of 'path' at this point).
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
174 pdir = self.package_dir.get('')
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
175 if pdir is not None:
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
176 tail.insert(0, pdir)
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
177
11521
ba07fff72511 [from 1999-11-04]
Greg Ward <gward@python.net>
parents: 11365
diff changeset
178 if tail:
63663
cdf6a3a50407 Merged revisions 79191 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58205
diff changeset
179 return os.path.join(*tail)
11521
ba07fff72511 [from 1999-11-04]
Greg Ward <gward@python.net>
parents: 11365
diff changeset
180 else:
ba07fff72511 [from 1999-11-04]
Greg Ward <gward@python.net>
parents: 11365
diff changeset
181 return ''
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
182
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
183 def check_package(self, package, package_dir):
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
184 # Empty dir name means current directory, which we can probably
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
185 # assume exists. Also, os.path.exists and isdir don't know about
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
186 # my "empty string means current dir" convention, so we have to
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
187 # circumvent them.
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
188 if package_dir != "":
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
189 if not os.path.exists(package_dir):
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
190 raise DistutilsFileError(
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
191 "package directory '%s' does not exist" % package_dir)
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
192 if not os.path.isdir(package_dir):
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
193 raise DistutilsFileError(
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
194 "supposed package directory '%s' exists, "
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
195 "but is not a directory" % package_dir)
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
196
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
197 # Require __init__.py for all but the "root package"
11521
ba07fff72511 [from 1999-11-04]
Greg Ward <gward@python.net>
parents: 11365
diff changeset
198 if package:
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
199 init_py = os.path.join(package_dir, "__init__.py")
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
200 if os.path.isfile(init_py):
12760
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
201 return init_py
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
202 else:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22833
diff changeset
203 log.warn(("package init file '%s' not found " +
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22833
diff changeset
204 "(or not a regular file)"), init_py)
12760
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
205
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
206 # Either not in a package at all (__init__.py not expected), or
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
207 # __init__.py doesn't exist -- so don't return the filename.
23679
b18348bdca08 Make None return explicit
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23668
diff changeset
208 return None
21268
d296e6bb17e5 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14963
diff changeset
209
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
210 def check_module(self, module, module_file):
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
211 if not os.path.isfile(module_file):
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22833
diff changeset
212 log.warn("file %s (for module %s) not found", module_file, module)
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
213 return False
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
214 else:
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
215 return True
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
216
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
217 def find_package_modules(self, package, package_dir):
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
218 self.check_package(package, package_dir)
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
219 module_files = glob(os.path.join(package_dir, "*.py"))
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
220 modules = []
14237
d9643e9c90eb Added 'script_name' and 'script_args' instance attributes to Distribution.
Greg Ward <gward@python.net>
parents: 13987
diff changeset
221 setup_script = os.path.abspath(self.distribution.script_name)
11545
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
222
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
223 for f in module_files:
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
224 abs_f = os.path.abspath(f)
11545
7555990e8db9 Fixed 'find_package_modules()' to ensure that we never build (and thus
Greg Ward <gward@python.net>
parents: 11521
diff changeset
225 if abs_f != setup_script:
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
226 module = os.path.splitext(os.path.basename(f))[0]
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
227 modules.append((package, module, f))
14237
d9643e9c90eb Added 'script_name' and 'script_args' instance attributes to Distribution.
Greg Ward <gward@python.net>
parents: 13987
diff changeset
228 else:
d9643e9c90eb Added 'script_name' and 'script_args' instance attributes to Distribution.
Greg Ward <gward@python.net>
parents: 13987
diff changeset
229 self.debug_print("excluding %s" % setup_script)
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
230 return modules
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
231
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
232 def find_modules(self):
12760
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
233 """Finds individually-specified Python modules, ie. those listed by
12847
4916d40d9677 Renamed 'modules' option to 'py_modules', for consistency with Distribution
Greg Ward <gward@python.net>
parents: 12760
diff changeset
234 module name in 'self.py_modules'. Returns a list of tuples (package,
12760
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
235 module_base, filename): 'package' is a tuple of the path through
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
236 package-space to the module; 'module_base' is the bare (no
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
237 packages, no dots) module name, and 'filename' is the path to the
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
238 ".py" file (relative to the distribution root) that implements the
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
239 module.
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
240 """
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
241 # Map package names to tuples of useful info about the package:
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
242 # (package_dir, checked)
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
243 # package_dir - the directory where we'll find source files for
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
244 # this package
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
245 # checked - true if we have checked that the package directory
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
246 # is valid (exists, contains __init__.py, ... ?)
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
247 packages = {}
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
248
12760
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
249 # List of (package, module, filename) tuples to return
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
250 modules = []
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
251
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
252 # We treat modules-in-packages almost the same as toplevel modules,
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
253 # just the "package" for a toplevel is empty (either an empty
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
254 # string or empty list, depending on context). Differences:
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
255 # - don't check for __init__.py in directory for empty package
12847
4916d40d9677 Renamed 'modules' option to 'py_modules', for consistency with Distribution
Greg Ward <gward@python.net>
parents: 12760
diff changeset
256 for module in self.py_modules:
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
257 path = module.split('.')
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
258 package = '.'.join(path[0:-1])
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
259 module_base = path[-1]
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
260
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
261 try:
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
262 (package_dir, checked) = packages[package]
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
263 except KeyError:
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
264 package_dir = self.get_package_dir(package)
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
265 checked = 0
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
266
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
267 if not checked:
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
268 init_py = self.check_package(package, package_dir)
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
269 packages[package] = (package_dir, 1)
12760
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
270 if init_py:
0f1e4e3935d7 Fixed a couple of long-hidden bugs (amazing what you find when you
Greg Ward <gward@python.net>
parents: 12743
diff changeset
271 modules.append((package, "__init__", init_py))
10437
3bc8f53041c0 First checkin of real Distutils command modules.
Greg Ward <gward@python.net>
parents:
diff changeset
272
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
273 # XXX perhaps we should also check for just .pyc files
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
274 # (so greedy closed-source bastards can distribute Python
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
275 # modules too)
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
276 module_file = os.path.join(package_dir, module_base + ".py")
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
277 if not self.check_module(module, module_file):
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
278 continue
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
279
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
280 modules.append((package, module_base, module_file))
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
281
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
282 return modules
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
283
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
284 def find_all_modules(self):
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
285 """Compute the list of all modules that will be built, whether
12847
4916d40d9677 Renamed 'modules' option to 'py_modules', for consistency with Distribution
Greg Ward <gward@python.net>
parents: 12760
diff changeset
286 they are specified one-module-at-a-time ('self.py_modules') or
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
287 by whole packages ('self.packages'). Return a list of tuples
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
288 (package, module, module_file), just like 'find_modules()' and
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
289 'find_package_modules()' do."""
28114
14463f478240 [Patch #695090 from Bernhard Herzog] Allow specifying both py_modules and packages
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
290 modules = []
12847
4916d40d9677 Renamed 'modules' option to 'py_modules', for consistency with Distribution
Greg Ward <gward@python.net>
parents: 12760
diff changeset
291 if self.py_modules:
28114
14463f478240 [Patch #695090 from Bernhard Herzog] Allow specifying both py_modules and packages
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
292 modules.extend(self.find_modules())
14463f478240 [Patch #695090 from Bernhard Herzog] Allow specifying both py_modules and packages
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
293 if self.packages:
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
294 for package in self.packages:
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
295 package_dir = self.get_package_dir(package)
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
296 m = self.find_package_modules(package, package_dir)
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
297 modules.extend(m)
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
298 return modules
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
299
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
300 def get_source_files(self):
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
301 return [module[-1] for module in self.find_all_modules()]
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
302
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
303 def get_module_outfile(self, build_dir, package, module):
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
304 outfile_path = [build_dir] + list(package) + [module + ".py"]
48260
2be17d368a9c Update distutils so that it triggers no warnings when run under -3.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
305 return os.path.join(*outfile_path)
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
306
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
307 def get_outputs(self, include_bytecode=1):
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
308 modules = self.find_all_modules()
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
309 outputs = []
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
310 for (package, module, module_file) in modules:
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
311 package = package.split('.')
14963
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
312 filename = self.get_module_outfile(self.build_lib, package, module)
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
313 outputs.append(filename)
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
314 if include_bytecode:
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
315 if self.compile:
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
316 outputs.append(filename + "c")
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
317 if self.optimize > 0:
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
318 outputs.append(filename + "o")
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
319
32328
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
320 outputs += [
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
321 os.path.join(build_dir, filename)
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
322 for package, src_dir, build_dir, filenames in self.data_files
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
323 for filename in filenames
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
324 ]
f49471953689 Add support for package data.
Fred Drake <fdrake@acm.org>
parents: 28114
diff changeset
325
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
326 return outputs
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
327
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
328 def build_module(self, module, module_file, package):
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
329 if isinstance(package, str):
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
330 package = package.split('.')
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
331 elif not isinstance(package, (list, tuple)):
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
332 raise TypeError(
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
333 "'package' must be a string (dot-separated), list, or tuple")
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
334
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
335 # Now put the module source file into the "build" area -- this is
11884
758a87f4cb2c Build reorg: change 'build_dir' option to 'build_lib'.
Greg Ward <gward@python.net>
parents: 11817
diff changeset
336 # easy, we just copy it somewhere under self.build_lib (the build
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
337 # directory for Python source).
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
338 outfile = self.get_module_outfile(self.build_lib, package, module)
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
339 dir = os.path.dirname(outfile)
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
340 self.mkpath(dir)
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
341 return self.copy_file(module_file, outfile, preserve_mode=0)
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
342
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
343 def build_modules(self):
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
344 modules = self.find_modules()
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
345 for (package, module, module_file) in modules:
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
346
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
347 # Now "build" the module -- ie. copy the source file to
11884
758a87f4cb2c Build reorg: change 'build_dir' option to 'build_lib'.
Greg Ward <gward@python.net>
parents: 11817
diff changeset
348 # self.build_lib (the build directory for Python source).
11339
6f7507ef2b11 Renamed 'dir' option to be consistent with other commands.
Greg Ward <gward@python.net>
parents: 11299
diff changeset
349 # (Actually, it gets copied to the directory for this package
11884
758a87f4cb2c Build reorg: change 'build_dir' option to 'build_lib'.
Greg Ward <gward@python.net>
parents: 11817
diff changeset
350 # under self.build_lib.)
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
351 self.build_module(module, module_file, package)
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
352
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
353 def build_packages(self):
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
354 for package in self.packages:
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
355
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
356 # Get list of (package, module, module_file) tuples based on
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
357 # scanning the package directory. 'package' is only included
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
358 # in the tuple so that 'find_modules()' and
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
359 # 'find_package_tuples()' have a consistent interface; it's
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
360 # ignored here (apart from a sanity check). Also, 'module' is
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
361 # the *unqualified* module name (ie. no dots, no package -- we
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
362 # already know its package!), and 'module_file' is the path to
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
363 # the .py file, relative to the current directory
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
364 # (ie. including 'package_dir').
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
365 package_dir = self.get_package_dir(package)
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
366 modules = self.find_package_modules(package, package_dir)
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
367
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
368 # Now loop over the modules we found, "building" each one (just
11884
758a87f4cb2c Build reorg: change 'build_dir' option to 'build_lib'.
Greg Ward <gward@python.net>
parents: 11817
diff changeset
369 # copy it to self.build_lib).
12094
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
370 for (package_, module, module_file) in modules:
8d1c69bdc4f2 Deleted some crufty comments and code.
Greg Ward <gward@python.net>
parents: 11903
diff changeset
371 assert package == package_
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14943
diff changeset
372 self.build_module(module, module_file, package)
11299
6d646f7e1a1c Basically a complete rewrite to support dealing with modules in whole
Greg Ward <gward@python.net>
parents: 11280
diff changeset
373
55070
a501573c23f2 cleaned up distutils.command.build_py
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
374 def byte_compile(self, files):
58205
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 48260
diff changeset
375 if sys.dont_write_bytecode:
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 48260
diff changeset
376 self.warn('byte-compiling is disabled, skipping.')
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 48260
diff changeset
377 return
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 48260
diff changeset
378
14963
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
379 from distutils.util import byte_compile
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
380 prefix = self.build_lib
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
381 if prefix[-1] != os.sep:
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
382 prefix = prefix + os.sep
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
383
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
384 # XXX this code is essentially the same as the 'byte_compile()
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
385 # method of the "install_lib" command, except for the determination
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
386 # of the 'prefix' string. Hmmm.
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
387
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
388 if self.compile:
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
389 byte_compile(files, optimize=0,
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22833
diff changeset
390 force=self.force, prefix=prefix, dry_run=self.dry_run)
14963
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
391 if self.optimize > 0:
73d09322f2c3 Added the ability to do byte-compilation at build time, currently off
Greg Ward <gward@python.net>
parents: 14948
diff changeset
392 byte_compile(files, optimize=self.optimize,
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22833
diff changeset
393 force=self.force, prefix=prefix, dry_run=self.dry_run)