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