blob: 187e93b58c6658da6cfc4ce9bf03aeb1682125c1 [file] [log] [blame]
Greg Ward13ae1c81999-03-22 14:55:25 +00001"""distutils.command.build_py
2
3Implements the Distutils 'build_py' command."""
4
5# created 1999/03/08, Greg Ward
6
7__rcsid__ = "$Id$"
8
9import string, os
Greg Ward17dc6e71999-09-21 18:22:34 +000010from types import *
11from glob import glob
12
Greg Ward13ae1c81999-03-22 14:55:25 +000013from distutils.core import Command
14from distutils.errors import *
15from distutils.util import mkpath, newer, make_file, copy_file
16
17
18class BuildPy (Command):
19
20 options = [('dir=', 'd', "directory for platform-shared files"),
Greg Ward13ae1c81999-03-22 14:55:25 +000021 ]
22
23
24 def set_default_options (self):
25 self.dir = None
Greg Ward71eb8641999-09-08 02:42:30 +000026 self.modules = None
27 self.package = None
Greg Ward17dc6e71999-09-21 18:22:34 +000028 self.package_dir = None
Greg Ward13ae1c81999-03-22 14:55:25 +000029
30 def set_final_options (self):
31 self.set_undefined_options ('build',
Greg Ward0f726951999-05-02 21:39:13 +000032 ('libdir', 'dir'))
Greg Ward17dc6e71999-09-21 18:22:34 +000033
34 # Get the distribution options that are aliases for build_py
35 # options -- list of packages and list of modules.
36 self.packages = self.distribution.packages
37 self.modules = self.distribution.py_modules
38 self.package_dir = self.distribution.package_dir
Greg Ward13ae1c81999-03-22 14:55:25 +000039
40
41 def run (self):
42
43 # XXX copy_file by default preserves all stat info -- mode, atime,
44 # and mtime. IMHO this is the right thing to do, but perhaps it
45 # should be an option -- in particular, a site administrator might
46 # want installed files to reflect the time of installation rather
47 # than the last modification time before the installed release.
48
49 # XXX copy_file does *not* preserve MacOS-specific file metadata.
50 # If this is a problem for building/installing Python modules, then
51 # we'll have to fix copy_file. (And what about installing scripts,
52 # when the time comes for that -- does MacOS use its special
53 # metadata to know that a file is meant to be interpreted by
54 # Python?)
55
56 self.set_final_options ()
57
Greg Ward13ae1c81999-03-22 14:55:25 +000058 infiles = []
59 outfiles = []
60 missing = []
61
Greg Ward17dc6e71999-09-21 18:22:34 +000062 # Two options control which modules will be installed: 'packages'
63 # and 'modules'. The former lets us work with whole packages, not
64 # specifying individual modules at all; the latter is for
65 # specifying modules one-at-a-time. Currently they are mutually
66 # exclusive: you can define one or the other (or neither), but not
67 # both. It remains to be seen how limiting this is.
Greg Ward13ae1c81999-03-22 14:55:25 +000068
Greg Ward17dc6e71999-09-21 18:22:34 +000069 # Dispose of the two "unusual" cases first: no pure Python modules
70 # at all (no problem, just return silently), and over-specified
71 # 'packages' and 'modules' options.
72
73 if not self.modules and not self.packages:
Greg Ward5d60fcf1999-08-29 18:19:01 +000074 return
Greg Ward17dc6e71999-09-21 18:22:34 +000075 if self.modules and self.packages:
76 raise DistutilsOptionError, \
77 "build_py: supplying both 'packages' and 'modules' " + \
78 "options not allowed"
79
80 # Now we're down to two cases: 'modules' only and 'packages' only.
81 if self.modules:
82 self.build_modules ()
83 else:
84 self.build_packages ()
85
86
87 # run ()
Greg Ward5d60fcf1999-08-29 18:19:01 +000088
Greg Ward17dc6e71999-09-21 18:22:34 +000089
90 def get_package_dir (self, package):
91 """Return the directory, relative to the top of the source
92 distribution, where package 'package' should be found
93 (at least according to the 'package_dir' option, if any)."""
94
95 if type (package) is StringType:
96 path = string.split (package, '.')
97 elif type (package) in (TupleType, ListType):
98 path = list (path)
99 else:
100 raise TypeError, "'package' must be a string, list, or tuple"
101
102 if not self.package_dir:
103 return apply (os.path.join, path)
104 else:
105 tail = []
106 while path:
107 try:
108 pdir = self.package_dir[string.join (path, '.')]
109 except KeyError:
110 tail.insert (0, path[-1])
111 del path[-1]
112 else:
113 tail.insert (0, pdir)
114 return apply (os.path.join, tail)
Greg Ward13ae1c81999-03-22 14:55:25 +0000115 else:
Greg Ward17dc6e71999-09-21 18:22:34 +0000116 # arg! everything failed, we might as well have not even
117 # looked in package_dir -- oh well
118 return apply (os.path.join, tail)
Greg Ward13ae1c81999-03-22 14:55:25 +0000119
Greg Ward17dc6e71999-09-21 18:22:34 +0000120 # get_package_dir ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000121
Greg Ward13ae1c81999-03-22 14:55:25 +0000122
Greg Ward17dc6e71999-09-21 18:22:34 +0000123 def check_package (self, package, package_dir):
124
125 # Empty dir name means current directory, which we can probably
126 # assume exists. Also, os.path.exists and isdir don't know about
127 # my "empty string means current dir" convention, so we have to
128 # circumvent them.
129 if package_dir != "":
130 if not os.path.exists (package_dir):
131 raise DistutilsFileError, \
132 "package directory '%s' does not exist" % package_dir
133 if not os.path.isdir (package_dir):
134 raise DistutilsFileErorr, \
135 ("supposed package directory '%s' exists, " +
136 "but is not a directory") % package_dir
137
138 # Require __init__.py for all but the "root package"
139 if package != "":
140 init_py = os.path.join (package_dir, "__init__.py")
141 if not os.path.isfile (init_py):
142 self.warn (("package init file '%s' not found " +
143 "(or not a regular file)") % init_py)
144 # check_package ()
145
146
147 def check_module (self, module, module_file):
148 if not os.path.isfile (module_file):
149 self.warn ("file %s (for module %s) not found" %
150 module_file, module)
151 return 0
152 else:
153 return 1
154
155 # check_module ()
156
157
158 def find_modules (self, package, package_dir):
159 module_files = glob (os.path.join (package_dir, "*.py"))
160 module_pairs = []
161 for f in module_files:
162 module = os.path.splitext (os.path.basename (f))[0]
163 module_pairs.append (module, f)
164 return module_pairs
165
166
167 def build_module (self, module, module_file, package):
168
169 if type (package) is StringType:
170 package = string.split (package, '.')
171
172 # Now put the module source file into the "build" area -- this
173 # is easy, we just copy it somewhere under self.dir (the build
174 # directory for Python source).
175 outfile_path = package
176 outfile_path.append (module + ".py")
177 outfile_path.insert (0, self.dir)
178 outfile = apply (os.path.join, outfile_path)
179
180 dir = os.path.dirname (outfile)
181 self.mkpath (dir)
182 self.copy_file (module_file, outfile)
183
184
185 def build_modules (self):
186
187 # Map package names to tuples of useful info about the package:
188 # (package_dir, checked)
189 # package_dir - the directory where we'll find source files for
190 # this package
191 # checked - true if we have checked that the package directory
192 # is valid (exists, contains __init__.py, ... ?)
193
194
195 packages = {}
196
197 # We treat modules-in-packages almost the same as toplevel modules,
198 # just the "package" for a toplevel is empty (either an empty
199 # string or empty list, depending on context). Differences:
200 # - don't check for __init__.py in directory for empty package
201
202 for module in self.modules:
203 path = string.split (module, '.')
204 package = tuple (path[0:-1])
205 module = path[-1]
206
207 try:
208 (package_dir, checked) = packages[package]
209 except KeyError:
210 package_dir = self.get_package_dir (package)
211 checked = 0
212
213 if not checked:
214 self.check_package (package, package_dir)
215 packages[package] = (package_dir, 1)
216
217 # XXX perhaps we should also check for just .pyc files
218 # (so greedy closed-source bastards can distribute Python
219 # modules too)
220 module_file = os.path.join (package_dir, module + ".py")
221 if not self.check_module (module, module_file):
222 continue
223
224 # Now "build" the module -- ie. copy the source file to
225 # self.dir (the build directory for Python source). (Actually,
226 # it gets copied to the directory for this package under
227 # self.dir.)
228 self.build_module (module, module_file, package)
229
230 # build_modules ()
231
232
233 def build_packages (self):
234
235 for package in self.packages:
236 package_dir = self.get_package_dir (package)
237 self.check_package (package, package_dir)
238
239 # Get list of (module, module_file) tuples based on scanning
240 # the package directory. Here, 'module' is the *unqualified*
241 # module name (ie. no dots, no package -- we already know its
242 # package!), and module_file is the path to the .py file,
243 # relative to the current directory (ie. including
244 # 'package_dir').
245 modules = self.find_modules (package, package_dir)
246
247 # Now loop over the modules we found, "building" each one (just
248 # copy it to self.dir).
249 for (module, module_file) in modules:
250 self.build_module (module, module_file, package)
251
252 # build_packages ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000253
254# end class BuildPy