home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / command / install_egg_info.py < prev    next >
Encoding:
Python Source  |  2009-04-18  |  3.5 KB  |  98 lines

  1. """distutils.command.install_egg_info
  2.  
  3. Implements the Distutils 'install_egg_info' command, for installing
  4. a package's PKG-INFO metadata."""
  5.  
  6.  
  7. from distutils.cmd import Command
  8. from distutils import log, dir_util
  9. import os, sys, re
  10.  
  11. class install_egg_info(Command):
  12.     """Install an .egg-info file for the package"""
  13.  
  14.     description = "Install package's PKG-INFO metadata as an .egg-info file"
  15.     user_options = [
  16.         ('install-dir=', 'd', "directory to install to"),
  17.         ('install-layout', None, "custom installation layout"),
  18.     ]
  19.  
  20.     def initialize_options(self):
  21.         self.install_dir = None
  22.         self.install_layout = None
  23.         self.prefix_option = None
  24.  
  25.     def finalize_options(self):
  26.         self.set_undefined_options('install_lib',('install_dir','install_dir'))
  27.         self.set_undefined_options('install',('install_layout','install_layout'))
  28.         self.set_undefined_options('install',('prefix_option','prefix_option'))
  29.         if self.install_layout:
  30.             basename = "%s-%s.egg-info" % (
  31.                 to_filename(safe_name(self.distribution.get_name())),
  32.                 to_filename(safe_version(self.distribution.get_version()))
  33.                 )
  34.             if not self.install_layout.lower() in ['deb']:
  35.                 raise DistutilsOptionError(
  36.                     "unknown value for --install-layout")
  37.         elif self.prefix_option or 'real_prefix' in sys.__dict__:
  38.             basename = "%s-%s-py%s.egg-info" % (
  39.                 to_filename(safe_name(self.distribution.get_name())),
  40.                 to_filename(safe_version(self.distribution.get_version())),
  41.                 sys.version[:3]
  42.                 )
  43.         else:
  44.             basename = "%s-%s.egg-info" % (
  45.                 to_filename(safe_name(self.distribution.get_name())),
  46.                 to_filename(safe_version(self.distribution.get_version()))
  47.                 )
  48.         self.target = os.path.join(self.install_dir, basename)
  49.         self.outputs = [self.target]
  50.  
  51.     def run(self):
  52.         target = self.target
  53.         if os.path.isdir(target) and not os.path.islink(target):
  54.             dir_util.remove_tree(target, dry_run=self.dry_run)
  55.         elif os.path.exists(target):
  56.             self.execute(os.unlink,(self.target,),"Removing "+target)
  57.         elif not os.path.isdir(self.install_dir):
  58.             self.execute(os.makedirs, (self.install_dir,),
  59.                          "Creating "+self.install_dir)
  60.         log.info("Writing %s", target)
  61.         if not self.dry_run:
  62.             f = open(target, 'w')
  63.             self.distribution.metadata.write_pkg_file(f)
  64.             f.close()
  65.  
  66.     def get_outputs(self):
  67.         return self.outputs
  68.  
  69.  
  70. # The following routines are taken from setuptools' pkg_resources module and
  71. # can be replaced by importing them from pkg_resources once it is included
  72. # in the stdlib.
  73.  
  74. def safe_name(name):
  75.     """Convert an arbitrary string to a standard distribution name
  76.  
  77.     Any runs of non-alphanumeric/. characters are replaced with a single '-'.
  78.     """
  79.     return re.sub('[^A-Za-z0-9.]+', '-', name)
  80.  
  81.  
  82. def safe_version(version):
  83.     """Convert an arbitrary string to a standard version string
  84.  
  85.     Spaces become dots, and all other non-alphanumeric characters become
  86.     dashes, with runs of multiple dashes condensed to a single dash.
  87.     """
  88.     version = version.replace(' ','.')
  89.     return re.sub('[^A-Za-z0-9.]+', '-', version)
  90.  
  91.  
  92. def to_filename(name):
  93.     """Convert a project or version name to its filename-escaped form
  94.  
  95.     Any '-' characters are currently replaced with '_'.
  96.     """
  97.     return name.replace('-','_')
  98.