[Python-checkins] python/dist/src/Lib/distutils/command build_py.py, 1.42, 1.43

fdrake at users.sourceforge.net fdrake at users.sourceforge.net
Fri Jun 11 17:50:35 EDT 2004


Update of /cvsroot/python/python/dist/src/Lib/distutils/command
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27213/Lib/distutils/command

Modified Files:
	build_py.py 
Log Message:
Add support for package data.

This is basically the support for package data from Phillip Eby's
setuptools package.  I've changed it only to fit it into the core
implementation rather than to live in subclasses, and added
documentation.


Index: build_py.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/distutils/command/build_py.py,v
retrieving revision 1.42
retrieving revision 1.43
diff -C2 -d -r1.42 -r1.43
*** build_py.py	28 Feb 2003 22:03:04 -0000	1.42
--- build_py.py	11 Jun 2004 21:50:33 -0000	1.43
***************
*** 38,41 ****
--- 38,42 ----
          self.py_modules = None
          self.package = None
+         self.package_data = None
          self.package_dir = None
          self.compile = 0
***************
*** 52,55 ****
--- 53,58 ----
          self.packages = self.distribution.packages
          self.py_modules = self.distribution.py_modules
+         self.package_data = self.distribution.package_data
+         self.data_files = self.get_data_files()
          self.package_dir = {}
          if self.distribution.package_dir:
***************
*** 93,96 ****
--- 96,100 ----
          if self.packages:
              self.build_packages()
+             self.build_package_data()
  
          self.byte_compile(self.get_outputs(include_bytecode=0))
***************
*** 98,101 ****
--- 102,146 ----
      # run ()
  
+     def get_data_files (self):
+         """Generate list of '(package,src_dir,build_dir,filenames)' tuples"""
+         data = []
+         for package in self.packages:
+             # Locate package source directory
+             src_dir = self.get_package_dir(package)
+ 
+             # Compute package build directory
+             build_dir = os.path.join(*([self.build_lib] + package.split('.')))
+ 
+             # Length of path to strip from found files
+             plen = len(src_dir)+1
+ 
+             # Strip directory from globbed filenames
+             filenames = [
+                 file[plen:] for file in self.find_data_files(package, src_dir)
+                 ]
+             data.append((package, src_dir, build_dir, filenames))
+         return data
+ 
+     def find_data_files (self, package, src_dir):
+         """Return filenames for package's data files in 'src_dir'"""
+         globs = (self.package_data.get('', [])
+                  + self.package_data.get(package, []))
+         files = []
+         for pattern in globs:
+             # Each pattern has to be converted to a platform-specific path
+             filelist = glob(os.path.join(src_dir, convert_path(pattern)))
+             # Files that match more than one pattern are only added once
+             files.extend([fn for fn in filelist if fn not in files])
+         return files
+ 
+     def build_package_data (self):
+         """Copy data files into build directory"""
+         lastdir = None
+         for package, src_dir, build_dir, filenames in self.data_files:
+             for filename in filenames:
+                 target = os.path.join(build_dir, filename)
+                 self.mkpath(os.path.dirname(target))
+                 self.copy_file(os.path.join(src_dir, filename), target,
+                                preserve_mode=False)
  
      def get_package_dir (self, package):
***************
*** 305,308 ****
--- 350,359 ----
                      outputs.append(filename + "o")
  
+         outputs += [
+             os.path.join(build_dir, filename)
+             for package, src_dir, build_dir, filenames in self.data_files
+             for filename in filenames
+             ]
+ 
          return outputs
  




More information about the Python-checkins mailing list