blob: 57ddf7e7ea653d5ddd814307e2af33680b69243e [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 Ward13ae1c81999-03-22 14:55:25 +000015
16
17class BuildPy (Command):
18
Greg Ward37bc8152000-01-30 18:34:15 +000019 description = "\"build\" pure Python modules (copy to build directory)"
20
Greg Ward2a612061999-09-29 12:44:57 +000021 options = [('build-dir=', 'd', "directory for platform-shared files"),
Greg Ward13ae1c81999-03-22 14:55:25 +000022 ]
23
24
25 def set_default_options (self):
Greg Ward2a612061999-09-29 12:44:57 +000026 self.build_dir = None
Greg Ward71eb8641999-09-08 02:42:30 +000027 self.modules = None
28 self.package = None
Greg Ward17dc6e71999-09-21 18:22:34 +000029 self.package_dir = None
Greg Ward13ae1c81999-03-22 14:55:25 +000030
31 def set_final_options (self):
32 self.set_undefined_options ('build',
Greg Ward2a612061999-09-29 12:44:57 +000033 ('build_lib', 'build_dir'))
Greg Ward17dc6e71999-09-21 18:22:34 +000034
35 # Get the distribution options that are aliases for build_py
36 # options -- list of packages and list of modules.
37 self.packages = self.distribution.packages
38 self.modules = self.distribution.py_modules
39 self.package_dir = self.distribution.package_dir
Greg Ward13ae1c81999-03-22 14:55:25 +000040
41
42 def run (self):
43
Greg Ward9b454431999-12-12 17:03:59 +000044 # XXX copy_file by default preserves atime and mtime. IMHO this is
45 # the right thing to do, but perhaps it should be an option -- in
46 # particular, a site administrator might want installed files to
47 # reflect the time of installation rather than the last
48 # modification time before the installed release.
49
50 # XXX copy_file by default preserves mode, which appears to be the
51 # wrong thing to do: if a file is read-only in the working
52 # directory, we want it to be installed read/write so that the next
53 # installation of the same module distribution can overwrite it
54 # without problems. (This might be a Unix-specific issue.) Thus
55 # we turn off 'preserve_mode' when copying to the build directory,
56 # since the build directory is supposed to be exactly what the
57 # installation will look like (ie. we preserve mode when
58 # installing).
Greg Ward13ae1c81999-03-22 14:55:25 +000059
60 # XXX copy_file does *not* preserve MacOS-specific file metadata.
61 # If this is a problem for building/installing Python modules, then
62 # we'll have to fix copy_file. (And what about installing scripts,
63 # when the time comes for that -- does MacOS use its special
64 # metadata to know that a file is meant to be interpreted by
65 # Python?)
66
Greg Ward13ae1c81999-03-22 14:55:25 +000067 infiles = []
68 outfiles = []
69 missing = []
70
Greg Ward17dc6e71999-09-21 18:22:34 +000071 # Two options control which modules will be installed: 'packages'
72 # and 'modules'. The former lets us work with whole packages, not
73 # specifying individual modules at all; the latter is for
74 # specifying modules one-at-a-time. Currently they are mutually
75 # exclusive: you can define one or the other (or neither), but not
76 # both. It remains to be seen how limiting this is.
Greg Ward13ae1c81999-03-22 14:55:25 +000077
Greg Ward17dc6e71999-09-21 18:22:34 +000078 # Dispose of the two "unusual" cases first: no pure Python modules
79 # at all (no problem, just return silently), and over-specified
80 # 'packages' and 'modules' options.
81
82 if not self.modules and not self.packages:
Greg Ward5d60fcf1999-08-29 18:19:01 +000083 return
Greg Ward17dc6e71999-09-21 18:22:34 +000084 if self.modules and self.packages:
85 raise DistutilsOptionError, \
86 "build_py: supplying both 'packages' and 'modules' " + \
Greg Ward9b454431999-12-12 17:03:59 +000087 "options is not allowed"
Greg Ward17dc6e71999-09-21 18:22:34 +000088
89 # Now we're down to two cases: 'modules' only and 'packages' only.
90 if self.modules:
91 self.build_modules ()
92 else:
93 self.build_packages ()
94
Greg Ward17dc6e71999-09-21 18:22:34 +000095 # run ()
Greg Ward5d60fcf1999-08-29 18:19:01 +000096
Greg Ward17dc6e71999-09-21 18:22:34 +000097
98 def get_package_dir (self, package):
99 """Return the directory, relative to the top of the source
100 distribution, where package 'package' should be found
101 (at least according to the 'package_dir' option, if any)."""
102
103 if type (package) is StringType:
104 path = string.split (package, '.')
105 elif type (package) in (TupleType, ListType):
Greg Ward631e6a01999-12-03 16:18:56 +0000106 path = list (package)
Greg Ward17dc6e71999-09-21 18:22:34 +0000107 else:
108 raise TypeError, "'package' must be a string, list, or tuple"
109
110 if not self.package_dir:
Greg Ward631e6a01999-12-03 16:18:56 +0000111 if path:
112 return apply (os.path.join, path)
113 else:
114 return ''
Greg Ward17dc6e71999-09-21 18:22:34 +0000115 else:
116 tail = []
117 while path:
118 try:
119 pdir = self.package_dir[string.join (path, '.')]
120 except KeyError:
121 tail.insert (0, path[-1])
122 del path[-1]
123 else:
124 tail.insert (0, pdir)
125 return apply (os.path.join, tail)
Greg Ward13ae1c81999-03-22 14:55:25 +0000126 else:
Greg Ward17dc6e71999-09-21 18:22:34 +0000127 # arg! everything failed, we might as well have not even
128 # looked in package_dir -- oh well
Greg Ward631e6a01999-12-03 16:18:56 +0000129 if tail:
130 return apply (os.path.join, tail)
131 else:
132 return ''
Greg Ward13ae1c81999-03-22 14:55:25 +0000133
Greg Ward17dc6e71999-09-21 18:22:34 +0000134 # get_package_dir ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000135
Greg Ward13ae1c81999-03-22 14:55:25 +0000136
Greg Ward17dc6e71999-09-21 18:22:34 +0000137 def check_package (self, package, package_dir):
138
139 # Empty dir name means current directory, which we can probably
140 # assume exists. Also, os.path.exists and isdir don't know about
141 # my "empty string means current dir" convention, so we have to
142 # circumvent them.
143 if package_dir != "":
144 if not os.path.exists (package_dir):
145 raise DistutilsFileError, \
146 "package directory '%s' does not exist" % package_dir
147 if not os.path.isdir (package_dir):
148 raise DistutilsFileErorr, \
149 ("supposed package directory '%s' exists, " +
150 "but is not a directory") % package_dir
151
152 # Require __init__.py for all but the "root package"
Greg Ward631e6a01999-12-03 16:18:56 +0000153 if package:
Greg Ward17dc6e71999-09-21 18:22:34 +0000154 init_py = os.path.join (package_dir, "__init__.py")
155 if not os.path.isfile (init_py):
156 self.warn (("package init file '%s' not found " +
157 "(or not a regular file)") % init_py)
158 # check_package ()
159
160
161 def check_module (self, module, module_file):
162 if not os.path.isfile (module_file):
163 self.warn ("file %s (for module %s) not found" %
164 module_file, module)
165 return 0
166 else:
167 return 1
168
169 # check_module ()
170
171
Greg Ward2a612061999-09-29 12:44:57 +0000172 def find_package_modules (self, package, package_dir):
Greg Ward17dc6e71999-09-21 18:22:34 +0000173 module_files = glob (os.path.join (package_dir, "*.py"))
174 module_pairs = []
Greg Ward9b454431999-12-12 17:03:59 +0000175 setup_script = os.path.abspath (sys.argv[0])
176
Greg Ward17dc6e71999-09-21 18:22:34 +0000177 for f in module_files:
Greg Ward9b454431999-12-12 17:03:59 +0000178 abs_f = os.path.abspath (f)
179 if abs_f != setup_script:
180 module = os.path.splitext (os.path.basename (f))[0]
181 module_pairs.append ((module, f))
Greg Ward17dc6e71999-09-21 18:22:34 +0000182 return module_pairs
183
184
Greg Ward2a612061999-09-29 12:44:57 +0000185 def find_modules (self):
Greg Ward17dc6e71999-09-21 18:22:34 +0000186 # Map package names to tuples of useful info about the package:
187 # (package_dir, checked)
188 # package_dir - the directory where we'll find source files for
189 # this package
190 # checked - true if we have checked that the package directory
191 # is valid (exists, contains __init__.py, ... ?)
Greg Ward17dc6e71999-09-21 18:22:34 +0000192 packages = {}
193
Greg Ward2a612061999-09-29 12:44:57 +0000194 # List of (module, package, filename) tuples to return
195 modules = []
196
Greg Ward17dc6e71999-09-21 18:22:34 +0000197 # 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])
Greg Ward2a612061999-09-29 12:44:57 +0000205 module_base = path[-1]
Greg Ward17dc6e71999-09-21 18:22:34 +0000206
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)
Greg Ward2a612061999-09-29 12:44:57 +0000220 module_file = os.path.join (package_dir, module_base + ".py")
Greg Ward17dc6e71999-09-21 18:22:34 +0000221 if not self.check_module (module, module_file):
222 continue
223
Greg Ward2a612061999-09-29 12:44:57 +0000224 modules.append ((module, package, module_file))
225
226 return modules
227
228 # find_modules ()
229
230
231 def get_source_files (self):
232
233 if self.modules:
234 modules = self.find_modules ()
235 else:
236 modules = []
237 for package in self.packages:
238 package_dir = self.get_package_dir (package)
239 m = self.find_package_modules (package, package_dir)
240 modules.extend (m)
241
242 # Both find_modules() and find_package_modules() return a list of
243 # tuples where the last element of each tuple is the filename --
244 # what a happy coincidence!
245 filenames = []
246 for module in modules:
247 filenames.append (module[-1])
248
249 return filenames
250
251
252 def build_module (self, module, module_file, package):
253
254 if type (package) is StringType:
255 package = string.split (package, '.')
Greg Ward631e6a01999-12-03 16:18:56 +0000256 elif type (package) not in (ListType, TupleType):
257 raise TypeError, \
258 "'package' must be a string (dot-separated), list, or tuple"
Greg Ward2a612061999-09-29 12:44:57 +0000259
260 # Now put the module source file into the "build" area -- this is
261 # easy, we just copy it somewhere under self.build_dir (the build
262 # directory for Python source).
Greg Ward631e6a01999-12-03 16:18:56 +0000263 outfile_path = list (package)
Greg Ward2a612061999-09-29 12:44:57 +0000264 outfile_path.append (module + ".py")
265 outfile_path.insert (0, self.build_dir)
266 outfile = apply (os.path.join, outfile_path)
267
268 dir = os.path.dirname (outfile)
269 self.mkpath (dir)
Greg Ward9b454431999-12-12 17:03:59 +0000270 self.copy_file (module_file, outfile, preserve_mode=0)
Greg Ward2a612061999-09-29 12:44:57 +0000271
272
273 def build_modules (self):
274
275 modules = self.find_modules()
276 for (module, package, module_file) in modules:
277
Greg Ward17dc6e71999-09-21 18:22:34 +0000278 # Now "build" the module -- ie. copy the source file to
Greg Ward2a612061999-09-29 12:44:57 +0000279 # self.build_dir (the build directory for Python source).
280 # (Actually, it gets copied to the directory for this package
281 # under self.build_dir.)
Greg Ward17dc6e71999-09-21 18:22:34 +0000282 self.build_module (module, module_file, package)
283
284 # build_modules ()
285
286
287 def build_packages (self):
288
289 for package in self.packages:
290 package_dir = self.get_package_dir (package)
291 self.check_package (package, package_dir)
292
293 # Get list of (module, module_file) tuples based on scanning
294 # the package directory. Here, 'module' is the *unqualified*
295 # module name (ie. no dots, no package -- we already know its
296 # package!), and module_file is the path to the .py file,
297 # relative to the current directory (ie. including
298 # 'package_dir').
Greg Ward2a612061999-09-29 12:44:57 +0000299 modules = self.find_package_modules (package, package_dir)
Greg Ward17dc6e71999-09-21 18:22:34 +0000300
301 # Now loop over the modules we found, "building" each one (just
Greg Ward2a612061999-09-29 12:44:57 +0000302 # copy it to self.build_dir).
Greg Ward17dc6e71999-09-21 18:22:34 +0000303 for (module, module_file) in modules:
304 self.build_module (module, module_file, package)
305
306 # build_packages ()
Greg Ward13ae1c81999-03-22 14:55:25 +0000307
308# end class BuildPy