Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 1 | """distutils.command.build_py |
| 2 | |
| 3 | Implements the Distutils 'build_py' command.""" |
| 4 | |
| 5 | # created 1999/03/08, Greg Ward |
| 6 | |
| 7 | __rcsid__ = "$Id$" |
| 8 | |
Greg Ward | 9b45443 | 1999-12-12 17:03:59 +0000 | [diff] [blame] | 9 | import sys, string, os |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 10 | from types import * |
| 11 | from glob import glob |
| 12 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 13 | from distutils.core import Command |
| 14 | from distutils.errors import * |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | class BuildPy (Command): |
| 18 | |
Greg Ward | 37bc815 | 2000-01-30 18:34:15 +0000 | [diff] [blame^] | 19 | description = "\"build\" pure Python modules (copy to build directory)" |
| 20 | |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 21 | options = [('build-dir=', 'd', "directory for platform-shared files"), |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 22 | ] |
| 23 | |
| 24 | |
| 25 | def set_default_options (self): |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 26 | self.build_dir = None |
Greg Ward | 71eb864 | 1999-09-08 02:42:30 +0000 | [diff] [blame] | 27 | self.modules = None |
| 28 | self.package = None |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 29 | self.package_dir = None |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 30 | |
| 31 | def set_final_options (self): |
| 32 | self.set_undefined_options ('build', |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 33 | ('build_lib', 'build_dir')) |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 34 | |
| 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def run (self): |
| 43 | |
Greg Ward | 9b45443 | 1999-12-12 17:03:59 +0000 | [diff] [blame] | 44 | # 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 59 | |
| 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 67 | infiles = [] |
| 68 | outfiles = [] |
| 69 | missing = [] |
| 70 | |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 71 | # 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 77 | |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 78 | # 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 Ward | 5d60fcf | 1999-08-29 18:19:01 +0000 | [diff] [blame] | 83 | return |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 84 | if self.modules and self.packages: |
| 85 | raise DistutilsOptionError, \ |
| 86 | "build_py: supplying both 'packages' and 'modules' " + \ |
Greg Ward | 9b45443 | 1999-12-12 17:03:59 +0000 | [diff] [blame] | 87 | "options is not allowed" |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 88 | |
| 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 Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 95 | # run () |
Greg Ward | 5d60fcf | 1999-08-29 18:19:01 +0000 | [diff] [blame] | 96 | |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 97 | |
| 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 Ward | 631e6a0 | 1999-12-03 16:18:56 +0000 | [diff] [blame] | 106 | path = list (package) |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 107 | else: |
| 108 | raise TypeError, "'package' must be a string, list, or tuple" |
| 109 | |
| 110 | if not self.package_dir: |
Greg Ward | 631e6a0 | 1999-12-03 16:18:56 +0000 | [diff] [blame] | 111 | if path: |
| 112 | return apply (os.path.join, path) |
| 113 | else: |
| 114 | return '' |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 115 | 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 Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 126 | else: |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 127 | # arg! everything failed, we might as well have not even |
| 128 | # looked in package_dir -- oh well |
Greg Ward | 631e6a0 | 1999-12-03 16:18:56 +0000 | [diff] [blame] | 129 | if tail: |
| 130 | return apply (os.path.join, tail) |
| 131 | else: |
| 132 | return '' |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 133 | |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 134 | # get_package_dir () |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 135 | |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 136 | |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 137 | 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 Ward | 631e6a0 | 1999-12-03 16:18:56 +0000 | [diff] [blame] | 153 | if package: |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 154 | 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 Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 172 | def find_package_modules (self, package, package_dir): |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 173 | module_files = glob (os.path.join (package_dir, "*.py")) |
| 174 | module_pairs = [] |
Greg Ward | 9b45443 | 1999-12-12 17:03:59 +0000 | [diff] [blame] | 175 | setup_script = os.path.abspath (sys.argv[0]) |
| 176 | |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 177 | for f in module_files: |
Greg Ward | 9b45443 | 1999-12-12 17:03:59 +0000 | [diff] [blame] | 178 | 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 Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 182 | return module_pairs |
| 183 | |
| 184 | |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 185 | def find_modules (self): |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 186 | # 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 Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 192 | packages = {} |
| 193 | |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 194 | # List of (module, package, filename) tuples to return |
| 195 | modules = [] |
| 196 | |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 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]) |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 205 | module_base = path[-1] |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 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) |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 220 | module_file = os.path.join (package_dir, module_base + ".py") |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 221 | if not self.check_module (module, module_file): |
| 222 | continue |
| 223 | |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 224 | 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 Ward | 631e6a0 | 1999-12-03 16:18:56 +0000 | [diff] [blame] | 256 | elif type (package) not in (ListType, TupleType): |
| 257 | raise TypeError, \ |
| 258 | "'package' must be a string (dot-separated), list, or tuple" |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 259 | |
| 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 Ward | 631e6a0 | 1999-12-03 16:18:56 +0000 | [diff] [blame] | 263 | outfile_path = list (package) |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 264 | 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 Ward | 9b45443 | 1999-12-12 17:03:59 +0000 | [diff] [blame] | 270 | self.copy_file (module_file, outfile, preserve_mode=0) |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | def build_modules (self): |
| 274 | |
| 275 | modules = self.find_modules() |
| 276 | for (module, package, module_file) in modules: |
| 277 | |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 278 | # Now "build" the module -- ie. copy the source file to |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 279 | # 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 Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 282 | 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 Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 299 | modules = self.find_package_modules (package, package_dir) |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 300 | |
| 301 | # Now loop over the modules we found, "building" each one (just |
Greg Ward | 2a61206 | 1999-09-29 12:44:57 +0000 | [diff] [blame] | 302 | # copy it to self.build_dir). |
Greg Ward | 17dc6e7 | 1999-09-21 18:22:34 +0000 | [diff] [blame] | 303 | for (module, module_file) in modules: |
| 304 | self.build_module (module, module_file, package) |
| 305 | |
| 306 | # build_packages () |
Greg Ward | 13ae1c8 | 1999-03-22 14:55:25 +0000 | [diff] [blame] | 307 | |
| 308 | # end class BuildPy |