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