codekingpro/portable-devtools
114k
1"""2Create a dist_info directory3As defined in the wheel specification4"""5 6import os7import shutil8from contextlib import contextmanager9from distutils import log10from distutils.core import Command11from pathlib import Path12from typing import cast13 14from .. import _normalization15from .egg_info import egg_info as egg_info_cls16 17 18class dist_info(Command):19 """20 This command is private and reserved for internal use of setuptools,21 users should rely on ``setuptools.build_meta`` APIs.22 """23 24 description = "DO NOT CALL DIRECTLY, INTERNAL ONLY: create .dist-info directory"25 26 user_options = [27 (28 'output-dir=',29 'o',30 "directory inside of which the .dist-info will be"31 "created (default: top of the source tree)",32 ),33 ('tag-date', 'd', "Add date stamp (e.g. 20050528) to version number"),34 ('tag-build=', 'b', "Specify explicit tag to add to version number"),35 ('no-date', 'D', "Don't include date stamp [default]"),36 ('keep-egg-info', None, "*TRANSITIONAL* will be removed in the future"),37 ]38 39 boolean_options = ['tag-date', 'keep-egg-info']40 negative_opt = {'no-date': 'tag-date'}41 42 def initialize_options(self):43 self.output_dir = None44 self.name = None45 self.dist_info_dir = None46 self.tag_date = None47 self.tag_build = None48 self.keep_egg_info = False49 50 def finalize_options(self):51 dist = self.distribution52 project_dir = dist.src_root or os.curdir53 self.output_dir = Path(self.output_dir or project_dir)54 55 egg_info = cast(egg_info_cls, self.reinitialize_command("egg_info"))56 egg_info.egg_base = str(self.output_dir)57 58 if self.tag_date:59 egg_info.tag_date = self.tag_date60 else:61 self.tag_date = egg_info.tag_date62 63 if self.tag_build:64 egg_info.tag_build = self.tag_build65 else:66 self.tag_build = egg_info.tag_build67 68 egg_info.finalize_options()69 self.egg_info = egg_info70 71 name = _normalization.safer_name(dist.get_name())72 version = _normalization.safer_best_effort_version(dist.get_version())73 self.name = f"{name}-{version}"74 self.dist_info_dir = os.path.join(self.output_dir, f"{self.name}.dist-info")75 76 @contextmanager77 def _maybe_bkp_dir(self, dir_path: str, requires_bkp: bool):78 if requires_bkp:79 bkp_name = f"{dir_path}.__bkp__"80 _rm(bkp_name, ignore_errors=True)81 shutil.copytree(dir_path, bkp_name, dirs_exist_ok=True, symlinks=True)82 try:83 yield84 finally:85 _rm(dir_path, ignore_errors=True)86 shutil.move(bkp_name, dir_path)87 else:88 yield89 90 def run(self):91 self.output_dir.mkdir(parents=True, exist_ok=True)92 self.egg_info.run()93 egg_info_dir = self.egg_info.egg_info94 assert os.path.isdir(egg_info_dir), ".egg-info dir should have been created"95 96 log.info("creating '{}'".format(os.path.abspath(self.dist_info_dir)))97 bdist_wheel = self.get_finalized_command('bdist_wheel')98 99 # TODO: if bdist_wheel if merged into setuptools, just add "keep_egg_info" there100 with self._maybe_bkp_dir(egg_info_dir, self.keep_egg_info):101 bdist_wheel.egg2dist(egg_info_dir, self.dist_info_dir)102 103 104def _rm(dir_name, **opts):105 if os.path.isdir(dir_name):106 shutil.rmtree(dir_name, **opts)107 