Quick update to the README file. For intros and books we now point to
[python/dscho.git] / Lib / distutils / command / build_py.py
blob3baddc62c7924aa49d048ae737ec4d4186e71ba5
1 """distutils.command.build_py
3 Implements the Distutils 'build_py' command."""
5 # created 1999/03/08, Greg Ward
7 __revision__ = "$Id$"
9 import sys, string, os
10 from types import *
11 from glob import glob
13 from distutils.core import Command
14 from distutils.errors import *
17 class build_py (Command):
19 description = "\"build\" pure Python modules (copy to build directory)"
21 user_options = [
22 ('build-lib=', 'd', "directory to \"build\" (copy) to"),
26 def initialize_options (self):
27 self.build_lib = None
28 self.modules = None
29 self.package = None
30 self.package_dir = None
32 def finalize_options (self):
33 self.set_undefined_options ('build',
34 ('build_lib', 'build_lib'))
36 # Get the distribution options that are aliases for build_py
37 # options -- list of packages and list of modules.
38 self.packages = self.distribution.packages
39 self.modules = self.distribution.py_modules
40 self.package_dir = self.distribution.package_dir
43 def run (self):
45 # XXX copy_file by default preserves atime and mtime. IMHO this is
46 # the right thing to do, but perhaps it should be an option -- in
47 # particular, a site administrator might want installed files to
48 # reflect the time of installation rather than the last
49 # modification time before the installed release.
51 # XXX copy_file by default preserves mode, which appears to be the
52 # wrong thing to do: if a file is read-only in the working
53 # directory, we want it to be installed read/write so that the next
54 # installation of the same module distribution can overwrite it
55 # without problems. (This might be a Unix-specific issue.) Thus
56 # we turn off 'preserve_mode' when copying to the build directory,
57 # since the build directory is supposed to be exactly what the
58 # installation will look like (ie. we preserve mode when
59 # installing).
61 # Two options control which modules will be installed: 'packages'
62 # and 'modules'. The former lets us work with whole packages, not
63 # specifying individual modules at all; the latter is for
64 # specifying modules one-at-a-time. Currently they are mutually
65 # exclusive: you can define one or the other (or neither), but not
66 # both. It remains to be seen how limiting this is.
68 # Dispose of the two "unusual" cases first: no pure Python modules
69 # at all (no problem, just return silently), and over-specified
70 # 'packages' and 'modules' options.
72 if not self.modules and not self.packages:
73 return
74 if self.modules and self.packages:
75 raise DistutilsOptionError, \
76 "build_py: supplying both 'packages' and 'modules' " + \
77 "options is not allowed"
79 # Now we're down to two cases: 'modules' only and 'packages' only.
80 if self.modules:
81 self.build_modules ()
82 else:
83 self.build_packages ()
85 # run ()
88 def get_package_dir (self, package):
89 """Return the directory, relative to the top of the source
90 distribution, where package 'package' should be found
91 (at least according to the 'package_dir' option, if any)."""
93 if type (package) is StringType:
94 path = string.split (package, '.')
95 elif type (package) in (TupleType, ListType):
96 path = list (package)
97 else:
98 raise TypeError, "'package' must be a string, list, or tuple"
100 if not self.package_dir:
101 if path:
102 return apply (os.path.join, path)
103 else:
104 return ''
105 else:
106 tail = []
107 while path:
108 try:
109 pdir = self.package_dir[string.join (path, '.')]
110 except KeyError:
111 tail.insert (0, path[-1])
112 del path[-1]
113 else:
114 tail.insert (0, pdir)
115 return apply (os.path.join, tail)
116 else:
117 # arg! everything failed, we might as well have not even
118 # looked in package_dir -- oh well
119 if tail:
120 return apply (os.path.join, tail)
121 else:
122 return ''
124 # get_package_dir ()
127 def check_package (self, package, package_dir):
129 # Empty dir name means current directory, which we can probably
130 # assume exists. Also, os.path.exists and isdir don't know about
131 # my "empty string means current dir" convention, so we have to
132 # circumvent them.
133 if package_dir != "":
134 if not os.path.exists (package_dir):
135 raise DistutilsFileError, \
136 "package directory '%s' does not exist" % package_dir
137 if not os.path.isdir (package_dir):
138 raise DistutilsFileError, \
139 ("supposed package directory '%s' exists, " +
140 "but is not a directory") % package_dir
142 # Require __init__.py for all but the "root package"
143 if package:
144 init_py = os.path.join (package_dir, "__init__.py")
145 if not os.path.isfile (init_py):
146 self.warn (("package init file '%s' not found " +
147 "(or not a regular file)") % init_py)
148 # check_package ()
151 def check_module (self, module, module_file):
152 if not os.path.isfile (module_file):
153 self.warn ("file %s (for module %s) not found" %
154 (module_file, module))
155 return 0
156 else:
157 return 1
159 # check_module ()
162 def find_package_modules (self, package, package_dir):
163 self.check_package (package, package_dir)
164 module_files = glob (os.path.join (package_dir, "*.py"))
165 modules = []
166 setup_script = os.path.abspath (sys.argv[0])
168 for f in module_files:
169 abs_f = os.path.abspath (f)
170 if abs_f != setup_script:
171 module = os.path.splitext (os.path.basename (f))[0]
172 modules.append ((package, module, f))
173 return modules
176 def find_modules (self):
177 # Map package names to tuples of useful info about the package:
178 # (package_dir, checked)
179 # package_dir - the directory where we'll find source files for
180 # this package
181 # checked - true if we have checked that the package directory
182 # is valid (exists, contains __init__.py, ... ?)
183 packages = {}
185 # List of (module, package, filename) tuples to return
186 modules = []
188 # We treat modules-in-packages almost the same as toplevel modules,
189 # just the "package" for a toplevel is empty (either an empty
190 # string or empty list, depending on context). Differences:
191 # - don't check for __init__.py in directory for empty package
193 for module in self.modules:
194 path = string.split (module, '.')
195 package = tuple (path[0:-1])
196 module_base = path[-1]
198 try:
199 (package_dir, checked) = packages[package]
200 except KeyError:
201 package_dir = self.get_package_dir (package)
202 checked = 0
204 if not checked:
205 self.check_package (package, package_dir)
206 packages[package] = (package_dir, 1)
208 # XXX perhaps we should also check for just .pyc files
209 # (so greedy closed-source bastards can distribute Python
210 # modules too)
211 module_file = os.path.join (package_dir, module_base + ".py")
212 if not self.check_module (module, module_file):
213 continue
215 modules.append ((package, module, module_file))
217 return modules
219 # find_modules ()
222 def find_all_modules (self):
223 """Compute the list of all modules that will be built, whether
224 they are specified one-module-at-a-time ('self.modules') or
225 by whole packages ('self.packages'). Return a list of tuples
226 (package, module, module_file), just like 'find_modules()' and
227 'find_package_modules()' do."""
229 if self.modules:
230 modules = self.find_modules ()
231 else:
232 modules = []
233 for package in self.packages:
234 package_dir = self.get_package_dir (package)
235 m = self.find_package_modules (package, package_dir)
236 modules.extend (m)
238 return modules
240 # find_all_modules ()
243 def get_source_files (self):
245 modules = self.find_all_modules ()
246 filenames = []
247 for module in modules:
248 filenames.append (module[-1])
250 return filenames
253 def get_module_outfile (self, build_dir, package, module):
254 outfile_path = [build_dir] + list(package) + [module + ".py"]
255 return apply (os.path.join, outfile_path)
258 def get_outputs (self):
259 modules = self.find_all_modules ()
260 outputs = []
261 for (package, module, module_file) in modules:
262 package = string.split (package, '.')
263 outputs.append (self.get_module_outfile (self.build_lib,
264 package, module))
265 return outputs
268 def build_module (self, module, module_file, package):
269 if type (package) is StringType:
270 package = string.split (package, '.')
271 elif type (package) not in (ListType, TupleType):
272 raise TypeError, \
273 "'package' must be a string (dot-separated), list, or tuple"
275 # Now put the module source file into the "build" area -- this is
276 # easy, we just copy it somewhere under self.build_lib (the build
277 # directory for Python source).
278 outfile = self.get_module_outfile (self.build_lib, package, module)
279 dir = os.path.dirname (outfile)
280 self.mkpath (dir)
281 self.copy_file (module_file, outfile, preserve_mode=0)
284 def build_modules (self):
286 modules = self.find_modules()
287 for (package, module, module_file) in modules:
289 # Now "build" the module -- ie. copy the source file to
290 # self.build_lib (the build directory for Python source).
291 # (Actually, it gets copied to the directory for this package
292 # under self.build_lib.)
293 self.build_module (module, module_file, package)
295 # build_modules ()
298 def build_packages (self):
300 for package in self.packages:
302 # Get list of (package, module, module_file) tuples based on
303 # scanning the package directory. 'package' is only included
304 # in the tuple so that 'find_modules()' and
305 # 'find_package_tuples()' have a consistent interface; it's
306 # ignored here (apart from a sanity check). Also, 'module' is
307 # the *unqualified* module name (ie. no dots, no package -- we
308 # already know its package!), and 'module_file' is the path to
309 # the .py file, relative to the current directory
310 # (ie. including 'package_dir').
311 package_dir = self.get_package_dir (package)
312 modules = self.find_package_modules (package, package_dir)
314 # Now loop over the modules we found, "building" each one (just
315 # copy it to self.build_lib).
316 for (package_, module, module_file) in modules:
317 assert package == package_
318 self.build_module (module, module_file, package)
320 # build_packages ()
322 # end class BuildPy