1 """distutils.command.build_py
3 Implements the Distutils 'build_py' command."""
5 # created 1999/03/08, Greg Ward
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)"
22 ('build-lib=', 'd', "directory to \"build\" (copy) to"),
26 def initialize_options (self
):
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
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
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
:
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.
83 self
.build_packages ()
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
):
98 raise TypeError, "'package' must be a string, list, or tuple"
100 if not self
.package_dir
:
102 return apply (os
.path
.join
, path
)
109 pdir
= self
.package_dir
[string
.join (path
, '.')]
111 tail
.insert (0, path
[-1])
114 tail
.insert (0, pdir
)
115 return apply (os
.path
.join
, tail
)
117 # arg! everything failed, we might as well have not even
118 # looked in package_dir -- oh well
120 return apply (os
.path
.join
, tail
)
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
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"
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
)
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
))
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"))
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
))
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
181 # checked - true if we have checked that the package directory
182 # is valid (exists, contains __init__.py, ... ?)
185 # List of (module, package, filename) tuples to return
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]
199 (package_dir
, checked
) = packages
[package
]
201 package_dir
= self
.get_package_dir (package
)
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
211 module_file
= os
.path
.join (package_dir
, module_base
+ ".py")
212 if not self
.check_module (module
, module_file
):
215 modules
.append ((package
, module
, module_file
))
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."""
230 modules
= self
.find_modules ()
233 for package
in self
.packages
:
234 package_dir
= self
.get_package_dir (package
)
235 m
= self
.find_package_modules (package
, package_dir
)
240 # find_all_modules ()
243 def get_source_files (self
):
245 modules
= self
.find_all_modules ()
247 for module
in modules
:
248 filenames
.append (module
[-1])
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 ()
261 for (package
, module
, module_file
) in modules
:
262 package
= string
.split (package
, '.')
263 outputs
.append (self
.get_module_outfile (self
.build_lib
,
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
):
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
)
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
)
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
)