1"""distutils.command.install_egg_info 2 3Implements the Distutils 'install_egg_info' command, for installing 4a package's PKG-INFO metadata.""" 5 6 7from distutils.cmd import Command 8from distutils import log, dir_util 9import os, sys, re 10 11class 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 ] 18 19 def initialize_options(self): 20 self.install_dir = None 21 22 def finalize_options(self): 23 self.set_undefined_options('install_lib',('install_dir','install_dir')) 24 basename = "%s-%s-py%d.%d.egg-info" % ( 25 to_filename(safe_name(self.distribution.get_name())), 26 to_filename(safe_version(self.distribution.get_version())), 27 *sys.version_info[:2] 28 ) 29 self.target = os.path.join(self.install_dir, basename) 30 self.outputs = [self.target] 31 32 def run(self): 33 target = self.target 34 if os.path.isdir(target) and not os.path.islink(target): 35 dir_util.remove_tree(target, dry_run=self.dry_run) 36 elif os.path.exists(target): 37 self.execute(os.unlink,(self.target,),"Removing "+target) 38 elif not os.path.isdir(self.install_dir): 39 self.execute(os.makedirs, (self.install_dir,), 40 "Creating "+self.install_dir) 41 log.info("Writing %s", target) 42 if not self.dry_run: 43 with open(target, 'w', encoding='UTF-8') as f: 44 self.distribution.metadata.write_pkg_file(f) 45 46 def get_outputs(self): 47 return self.outputs 48 49 50# The following routines are taken from setuptools' pkg_resources module and 51# can be replaced by importing them from pkg_resources once it is included 52# in the stdlib. 53 54def safe_name(name): 55 """Convert an arbitrary string to a standard distribution name 56 57 Any runs of non-alphanumeric/. characters are replaced with a single '-'. 58 """ 59 return re.sub('[^A-Za-z0-9.]+', '-', name) 60 61 62def safe_version(version): 63 """Convert an arbitrary string to a standard version string 64 65 Spaces become dots, and all other non-alphanumeric characters become 66 dashes, with runs of multiple dashes condensed to a single dash. 67 """ 68 version = version.replace(' ','.') 69 return re.sub('[^A-Za-z0-9.]+', '-', version) 70 71 72def to_filename(name): 73 """Convert a project or version name to its filename-escaped form 74 75 Any '-' characters are currently replaced with '_'. 76 """ 77 return name.replace('-','_') 78