blob: 6bc5aa8007934f4537dfe7f342ee90991b5814f3 [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
Greg Ward9b454431999-12-12 17:03:59 +00009import sys, 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 *
Greg Ward455eb611999-10-03 21:07:21 +000015from distutils.util import mkpath, copy_file
Greg Ward13ae1c81999-03-22 14:55:25 +000016
17
18class BuildPy (Command):
19
Greg Ward2a612061999-09-29 12:44:57 +000020 options = [('build-dir=', 'd', "directory for platform-shared files"),
Greg Ward13ae1c81999-03-22 14:55:25 +000021 ]
22
23
24 def set_default_options (self):
Greg Ward2a612061999-09-29 12:44:57 +000025 self.build_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 Ward2a612061999-09-29 12:44:57 +000032 ('build_lib', 'build_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
Greg Ward9b454431999-12-12 17:03:59 +000043 # XXX copy_file by default preserves atime and mtime. IMHO this is
44 # the right thing to do, but perhaps it should be an option -- in
45 # particular, a site administrator might want installed files to
46 # reflect the time of installation rather than the last
47 # modification time before the installed release.
48
49 # XXX copy_file by default preserves mode, which appears to be the
50 # wrong thing to do: if a file is read-only in the working
51 # directory, we want it to be installed read/write so that the next
52 # installation of the same module distribution can overwrite it
53 # without problems. (This might be a Unix-specific issue.) Thus
54 # we turn off 'preserve_mode' when copying to the build directory,
55 # since the build directory is supposed to be exactly what the
56 # installation will look like (ie. we preserve mode when
57 # installing).
Greg Ward13ae1c81999-03-22 14:55:25 +000058
59 # XXX copy_file does *not* preserve MacOS-specific file metadata.
60 # If this is a problem for building/installing Python modules, then
61 # we'll have to fix copy_file. (And what about installing scripts,
62 # when the time comes for that -- does MacOS use its special
63 # metadata to know that a file is meant to be interpreted by
64 # Python?)
65
Greg Ward13ae1c81999-03-22 14:55:25 +000066 infiles = []
67 outfiles = []
68 missing = []
69
Greg Ward17dc6e71999-09-21 18:22:34 +000070 # Two options control which modules will be installed: 'packages'
71 # and 'modules'. The former lets us work with whole packages, not
72 # specifying individual modules at all; the latter is for
73 # specifying modules one-at-a-time. Currently they are mutually
74 # exclusive: you can define one or the other (or neither), but not
75 # both. It remains to be seen how limiting this is.
Greg Ward13ae1c81999-03-22 14:55:25 +000076
Greg Ward17dc6e71999-09-21 18:22:34 +000077 # Dispose of the two "unusual" cases first: no pure Python modules
78 # at all (no problem, just return silently), and over-specified
79 # 'packages' and 'modules' options.
80
81 if not self.modules and not self.packages:
Greg Ward5d60fcf1999-08-29 18:19:01 +000082 return
Greg Ward17dc6e71999-09-21 18:22:34 +000083 if self.modules and self.packages:
84 raise DistutilsOptionError, \
85 "build_py: supplying both 'packages' and 'modules' " + \
Greg Ward9b454431999-12-12 17:03:59 +000086 "options is not allowed"
Greg Ward17dc6e71999-09-21 18:22:34 +000087
88 # Now we're down to two cases: 'modules' only and 'packages' only.
89 if self.modules:
90 self.build_modules ()
91 else:
92 self.build_packages ()
93
Greg Ward17dc6e71999-09-21 18:22:34 +000094 # run ()
Greg Ward5d60fcf1999-08-29 18:19:01 +000095
Greg Ward17dc6e71999-09-21 18:22:34 +000096
97 def get_package_dir (self, package):
98 """Return the directory, relative to the top of the source
99 distribution, where package 'package' should be found
100 (at least according to the 'package_dir' option, if any)."""
101
102 if type (package) is StringType:
103 path = string.split (package, '.')
104 elif type (package) in (TupleType, ListType):
Greg Ward631e6a01999-12-03 16:18:56 +0000105 path = list (package)
Greg Ward17dc6e71999-09-21 18:22:34 +0000106 else:
107 raise TypeError, "'package' must be a string, list, or tuple"
108
109 if not self.package_dir:
Greg Ward631e6a01999-12-03 16:18:56 +0000110 if path:
111 return apply (os.path.join, path)
112 else:
113 return ''
Greg Ward17dc6e71999-09-21 18:22:34 +0000114 else:
115 tail = []
116 while path:
117 try:
118 pdir = self.package_dir[string.join (path, '.')]
119 except KeyError:
120 tail.insert (0, path[-1])
121 del path[-1]
122 else:
123 tail.insert (0, pdir)
124 return apply (os.path.join, tail)
Greg Ward13ae1c81999-03-22 14:55:25 +0000125 else:
Greg Ward17dc6e71999-09-21 18:22:34 +0000126 # arg! everything failed, we might as well have not even
127 # looked in package_dir -- oh well
Greg Ward631e6a01999-12-03 16:18:56 +0000128 if tail:
129 return apply (os.path.join, tail)
130 else:
131 return ''
Greg Ward13ae1c81999-03-22 14:55:25 +0000132
Greg Ward17dc6e71999-09-21 18:22:34 +0000133 # get_package_dir ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000134
Greg Ward13ae1c81999-03-22 14:55:25 +0000135
Greg Ward17dc6e71999-09-21 18:22:34 +0000136 def check_package (self, package, package_dir):
137
138 # Empty dir name means current directory, which we can probably
139 # assume exists. Also, os.path.exists and isdir don't know about
140 # my "empty string means current dir" convention, so we have to
141 # circumvent them.
142 if package_dir != "":
143 if not os.path.exists (package_dir):
144 raise DistutilsFileError, \
145 "package directory '%s' does not exist" % package_dir
146 if not os.path.isdir (package_dir):
147 raise DistutilsFileErorr, \
148 ("supposed package directory '%s' exists, " +
149 "but is not a directory") % package_dir
150
151 # Require __init__.py for all but the "root package"
Greg Ward631e6a01999-12-03 16:18:56 +0000152 if package:
Greg Ward17dc6e71999-09-21 18:22:34 +0000153 init_py = os.path.join (package_dir, "__init__.py")
154 if not os.path.isfile (init_py):
155 self.warn (("package init file '%s' not found " +
156 "(or not a regular file)") % init_py)
157 # check_package ()
158
159
160 def check_module (self, module, module_file):
161 if not os.path.isfile (module_file):
162 self.warn ("file %s (for module %s) not found" %
163 module_file, module)
164 return 0
165 else:
166 return 1
167
168 # check_module ()
169
170
Greg Ward2a612061999-09-29 12:44:57 +0000171 def find_package_modules (self, package, package_dir):
Greg Ward17dc6e71999-09-21 18:22:34 +0000172 module_files = glob (os.path.join (package_dir, "*.py"))
173 module_pairs = []
Greg Ward9b454431999-12-12 17:03:59 +0000174 setup_script = os.path.abspath (sys.argv[0])
175
Greg Ward17dc6e71999-09-21 18:22:34 +0000176 for f in module_files:
Greg Ward9b454431999-12-12 17:03:59 +0000177 abs_f = os.path.abspath (f)
178 if abs_f != setup_script:
179 module = os.path.splitext (os.path.basename (f))[0]
180 module_pairs.append ((module, f))
Greg Ward17dc6e71999-09-21 18:22:34 +0000181 return module_pairs
182
183
Greg Ward2a612061999-09-29 12:44:57 +0000184 def find_modules (self):
Greg Ward17dc6e71999-09-21 18:22:34 +0000185 # Map package names to tuples of useful info about the package:
186 # (package_dir, checked)
187 # package_dir - the directory where we'll find source files for
188 # this package
189 # checked - true if we have checked that the package directory
190 # is valid (exists, contains __init__.py, ... ?)
Greg Ward17dc6e71999-09-21 18:22:34 +0000191 packages = {}
192
Greg Ward2a612061999-09-29 12:44:57 +0000193 # List of (module, package, filename) tuples to return
194 modules = []
195
Greg Ward17dc6e71999-09-21 18:22:34 +0000196 # We treat modules-in-packages almost the same as toplevel modules,
197 # just the "package" for a toplevel is empty (either an empty
198 # string or empty list, depending on context). Differences:
199 # - don't check for __init__.py in directory for empty package
200
201 for module in self.modules:
202 path = string.split (module, '.')
203 package = tuple (path[0:-1])
Greg Ward2a612061999-09-29 12:44:57 +0000204 module_base = path[-1]
Greg Ward17dc6e71999-09-21 18:22:34 +0000205
206 try:
207 (package_dir, checked) = packages[package]
208 except KeyError:
209 package_dir = self.get_package_dir (package)
210 checked = 0
211
212 if not checked:
213 self.check_package (package, package_dir)
214 packages[package] = (package_dir, 1)
215
216 # XXX perhaps we should also check for just .pyc files
217 # (so greedy closed-source bastards can distribute Python
218 # modules too)
Greg Ward2a612061999-09-29 12:44:57 +0000219 module_file = os.path.join (package_dir, module_base + ".py")
Greg Ward17dc6e71999-09-21 18:22:34 +0000220 if not self.check_module (module, module_file):
221 continue
222
Greg Ward2a612061999-09-29 12:44:57 +0000223 modules.append ((module, package, module_file))
224
225 return modules
226
227 # find_modules ()
228
229
230 def get_source_files (self):
231
232 if self.modules:
233 modules = self.find_modules ()
234 else:
235 modules = []
236 for package in self.packages:
237 package_dir = self.get_package_dir (package)
238 m = self.find_package_modules (package, package_dir)
239 modules.extend (m)
240
241 # Both find_modules() and find_package_modules() return a list of
242 # tuples where the last element of each tuple is the filename --
243 # what a happy coincidence!
244 filenames = []
245 for module in modules:
246 filenames.append (module[-1])
247
248 return filenames
249
250
251 def build_module (self, module, module_file, package):
252
253 if type (package) is StringType:
254 package = string.split (package, '.')
Greg Ward631e6a01999-12-03 16:18:56 +0000255 elif type (package) not in (ListType, TupleType):
256 raise TypeError, \
257 "'package' must be a string (dot-separated), list, or tuple"
Greg Ward2a612061999-09-29 12:44:57 +0000258
259 # Now put the module source file into the "build" area -- this is
260 # easy, we just copy it somewhere under self.build_dir (the build
261 # directory for Python source).
Greg Ward631e6a01999-12-03 16:18:56 +0000262 outfile_path = list (package)
Greg Ward2a612061999-09-29 12:44:57 +0000263 outfile_path.append (module + ".py")
264 outfile_path.insert (0, self.build_dir)
265 outfile = apply (os.path.join, outfile_path)
266
267 dir = os.path.dirname (outfile)
268 self.mkpath (dir)
Greg Ward9b454431999-12-12 17:03:59 +0000269 self.copy_file (module_file, outfile, preserve_mode=0)
Greg Ward2a612061999-09-29 12:44:57 +0000270
271
272 def build_modules (self):
273
274 modules = self.find_modules()
275 for (module, package, module_file) in modules:
276
Greg Ward17dc6e71999-09-21 18:22:34 +0000277 # Now "build" the module -- ie. copy the source file to
Greg Ward2a612061999-09-29 12:44:57 +0000278 # self.build_dir (the build directory for Python source).
279 # (Actually, it gets copied to the directory for this package
280 # under self.build_dir.)
Greg Ward17dc6e71999-09-21 18:22:34 +0000281 self.build_module (module, module_file, package)
282
283 # build_modules ()
284
285
286 def build_packages (self):
287
288 for package in self.packages:
289 package_dir = self.get_package_dir (package)
290 self.check_package (package, package_dir)
291
292 # Get list of (module, module_file) tuples based on scanning
293 # the package directory. Here, 'module' is the *unqualified*
294 # module name (ie. no dots, no package -- we already know its
295 # package!), and module_file is the path to the .py file,
296 # relative to the current directory (ie. including
297 # 'package_dir').
Greg Ward2a612061999-09-29 12:44:57 +0000298 modules = self.find_package_modules (package, package_dir)
Greg Ward17dc6e71999-09-21 18:22:34 +0000299
300 # Now loop over the modules we found, "building" each one (just
Greg Ward2a612061999-09-29 12:44:57 +0000301 # copy it to self.build_dir).
Greg Ward17dc6e71999-09-21 18:22:34 +0000302 for (module, module_file) in modules:
303 self.build_module (module, module_file, package)
304
305 # build_packages ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000306
307# end class BuildPy