bdist_egg.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. """setuptools.command.bdist_egg
  2. Build .egg distributions"""
  3. from distutils.dir_util import remove_tree, mkpath
  4. from distutils import log
  5. from types import CodeType
  6. import sys
  7. import os
  8. import re
  9. import textwrap
  10. import marshal
  11. from setuptools.extension import Library
  12. from setuptools import Command
  13. from .._path import ensure_directory
  14. from sysconfig import get_path, get_python_version
  15. def _get_purelib():
  16. return get_path("purelib")
  17. def strip_module(filename):
  18. if '.' in filename:
  19. filename = os.path.splitext(filename)[0]
  20. if filename.endswith('module'):
  21. filename = filename[:-6]
  22. return filename
  23. def sorted_walk(dir):
  24. """Do os.walk in a reproducible way,
  25. independent of indeterministic filesystem readdir order
  26. """
  27. for base, dirs, files in os.walk(dir):
  28. dirs.sort()
  29. files.sort()
  30. yield base, dirs, files
  31. def write_stub(resource, pyfile):
  32. _stub_template = textwrap.dedent(
  33. """
  34. def __bootstrap__():
  35. global __bootstrap__, __loader__, __file__
  36. import sys, pkg_resources, importlib.util
  37. __file__ = pkg_resources.resource_filename(__name__, %r)
  38. __loader__ = None; del __bootstrap__, __loader__
  39. spec = importlib.util.spec_from_file_location(__name__,__file__)
  40. mod = importlib.util.module_from_spec(spec)
  41. spec.loader.exec_module(mod)
  42. __bootstrap__()
  43. """
  44. ).lstrip()
  45. with open(pyfile, 'w') as f:
  46. f.write(_stub_template % resource)
  47. class bdist_egg(Command):
  48. description = "create an \"egg\" distribution"
  49. user_options = [
  50. ('bdist-dir=', 'b', "temporary directory for creating the distribution"),
  51. (
  52. 'plat-name=',
  53. 'p',
  54. "platform name to embed in generated filenames "
  55. "(by default uses `pkg_resources.get_build_platform()`)",
  56. ),
  57. ('exclude-source-files', None, "remove all .py files from the generated egg"),
  58. (
  59. 'keep-temp',
  60. 'k',
  61. "keep the pseudo-installation tree around after "
  62. + "creating the distribution archive",
  63. ),
  64. ('dist-dir=', 'd', "directory to put final built distributions in"),
  65. ('skip-build', None, "skip rebuilding everything (for testing/debugging)"),
  66. ]
  67. boolean_options = ['keep-temp', 'skip-build', 'exclude-source-files']
  68. def initialize_options(self):
  69. self.bdist_dir = None
  70. self.plat_name = None
  71. self.keep_temp = 0
  72. self.dist_dir = None
  73. self.skip_build = 0
  74. self.egg_output = None
  75. self.exclude_source_files = None
  76. def finalize_options(self):
  77. ei_cmd = self.ei_cmd = self.get_finalized_command("egg_info")
  78. self.egg_info = ei_cmd.egg_info
  79. if self.bdist_dir is None:
  80. bdist_base = self.get_finalized_command('bdist').bdist_base
  81. self.bdist_dir = os.path.join(bdist_base, 'egg')
  82. if self.plat_name is None:
  83. from pkg_resources import get_build_platform
  84. self.plat_name = get_build_platform()
  85. self.set_undefined_options('bdist', ('dist_dir', 'dist_dir'))
  86. if self.egg_output is None:
  87. # Compute filename of the output egg
  88. basename = ei_cmd._get_egg_basename(
  89. py_version=get_python_version(),
  90. platform=self.distribution.has_ext_modules() and self.plat_name,
  91. )
  92. self.egg_output = os.path.join(self.dist_dir, basename + '.egg')
  93. def do_install_data(self):
  94. # Hack for packages that install data to install's --install-lib
  95. self.get_finalized_command('install').install_lib = self.bdist_dir
  96. site_packages = os.path.normcase(os.path.realpath(_get_purelib()))
  97. old, self.distribution.data_files = self.distribution.data_files, []
  98. for item in old:
  99. if isinstance(item, tuple) and len(item) == 2:
  100. if os.path.isabs(item[0]):
  101. realpath = os.path.realpath(item[0])
  102. normalized = os.path.normcase(realpath)
  103. if normalized == site_packages or normalized.startswith(
  104. site_packages + os.sep
  105. ):
  106. item = realpath[len(site_packages) + 1 :], item[1]
  107. # XXX else: raise ???
  108. self.distribution.data_files.append(item)
  109. try:
  110. log.info("installing package data to %s", self.bdist_dir)
  111. self.call_command('install_data', force=0, root=None)
  112. finally:
  113. self.distribution.data_files = old
  114. def get_outputs(self):
  115. return [self.egg_output]
  116. def call_command(self, cmdname, **kw):
  117. """Invoke reinitialized command `cmdname` with keyword args"""
  118. for dirname in INSTALL_DIRECTORY_ATTRS:
  119. kw.setdefault(dirname, self.bdist_dir)
  120. kw.setdefault('skip_build', self.skip_build)
  121. kw.setdefault('dry_run', self.dry_run)
  122. cmd = self.reinitialize_command(cmdname, **kw)
  123. self.run_command(cmdname)
  124. return cmd
  125. def run(self): # noqa: C901 # is too complex (14) # FIXME
  126. # Generate metadata first
  127. self.run_command("egg_info")
  128. # We run install_lib before install_data, because some data hacks
  129. # pull their data path from the install_lib command.
  130. log.info("installing library code to %s", self.bdist_dir)
  131. instcmd = self.get_finalized_command('install')
  132. old_root = instcmd.root
  133. instcmd.root = None
  134. if self.distribution.has_c_libraries() and not self.skip_build:
  135. self.run_command('build_clib')
  136. cmd = self.call_command('install_lib', warn_dir=0)
  137. instcmd.root = old_root
  138. all_outputs, ext_outputs = self.get_ext_outputs()
  139. self.stubs = []
  140. to_compile = []
  141. for p, ext_name in enumerate(ext_outputs):
  142. filename, ext = os.path.splitext(ext_name)
  143. pyfile = os.path.join(self.bdist_dir, strip_module(filename) + '.py')
  144. self.stubs.append(pyfile)
  145. log.info("creating stub loader for %s", ext_name)
  146. if not self.dry_run:
  147. write_stub(os.path.basename(ext_name), pyfile)
  148. to_compile.append(pyfile)
  149. ext_outputs[p] = ext_name.replace(os.sep, '/')
  150. if to_compile:
  151. cmd.byte_compile(to_compile)
  152. if self.distribution.data_files:
  153. self.do_install_data()
  154. # Make the EGG-INFO directory
  155. archive_root = self.bdist_dir
  156. egg_info = os.path.join(archive_root, 'EGG-INFO')
  157. self.mkpath(egg_info)
  158. if self.distribution.scripts:
  159. script_dir = os.path.join(egg_info, 'scripts')
  160. log.info("installing scripts to %s", script_dir)
  161. self.call_command('install_scripts', install_dir=script_dir, no_ep=1)
  162. self.copy_metadata_to(egg_info)
  163. native_libs = os.path.join(egg_info, "native_libs.txt")
  164. if all_outputs:
  165. log.info("writing %s", native_libs)
  166. if not self.dry_run:
  167. ensure_directory(native_libs)
  168. libs_file = open(native_libs, 'wt')
  169. libs_file.write('\n'.join(all_outputs))
  170. libs_file.write('\n')
  171. libs_file.close()
  172. elif os.path.isfile(native_libs):
  173. log.info("removing %s", native_libs)
  174. if not self.dry_run:
  175. os.unlink(native_libs)
  176. write_safety_flag(os.path.join(archive_root, 'EGG-INFO'), self.zip_safe())
  177. if os.path.exists(os.path.join(self.egg_info, 'depends.txt')):
  178. log.warn(
  179. "WARNING: 'depends.txt' will not be used by setuptools 0.6!\n"
  180. "Use the install_requires/extras_require setup() args instead."
  181. )
  182. if self.exclude_source_files:
  183. self.zap_pyfiles()
  184. # Make the archive
  185. make_zipfile(
  186. self.egg_output,
  187. archive_root,
  188. verbose=self.verbose,
  189. dry_run=self.dry_run,
  190. mode=self.gen_header(),
  191. )
  192. if not self.keep_temp:
  193. remove_tree(self.bdist_dir, dry_run=self.dry_run)
  194. # Add to 'Distribution.dist_files' so that the "upload" command works
  195. getattr(self.distribution, 'dist_files', []).append(
  196. ('bdist_egg', get_python_version(), self.egg_output)
  197. )
  198. def zap_pyfiles(self):
  199. log.info("Removing .py files from temporary directory")
  200. for base, dirs, files in walk_egg(self.bdist_dir):
  201. for name in files:
  202. path = os.path.join(base, name)
  203. if name.endswith('.py'):
  204. log.debug("Deleting %s", path)
  205. os.unlink(path)
  206. if base.endswith('__pycache__'):
  207. path_old = path
  208. pattern = r'(?P<name>.+)\.(?P<magic>[^.]+)\.pyc'
  209. m = re.match(pattern, name)
  210. path_new = os.path.join(base, os.pardir, m.group('name') + '.pyc')
  211. log.info("Renaming file from [%s] to [%s]" % (path_old, path_new))
  212. try:
  213. os.remove(path_new)
  214. except OSError:
  215. pass
  216. os.rename(path_old, path_new)
  217. def zip_safe(self):
  218. safe = getattr(self.distribution, 'zip_safe', None)
  219. if safe is not None:
  220. return safe
  221. log.warn("zip_safe flag not set; analyzing archive contents...")
  222. return analyze_egg(self.bdist_dir, self.stubs)
  223. def gen_header(self):
  224. return 'w'
  225. def copy_metadata_to(self, target_dir):
  226. "Copy metadata (egg info) to the target_dir"
  227. # normalize the path (so that a forward-slash in egg_info will
  228. # match using startswith below)
  229. norm_egg_info = os.path.normpath(self.egg_info)
  230. prefix = os.path.join(norm_egg_info, '')
  231. for path in self.ei_cmd.filelist.files:
  232. if path.startswith(prefix):
  233. target = os.path.join(target_dir, path[len(prefix) :])
  234. ensure_directory(target)
  235. self.copy_file(path, target)
  236. def get_ext_outputs(self):
  237. """Get a list of relative paths to C extensions in the output distro"""
  238. all_outputs = []
  239. ext_outputs = []
  240. paths = {self.bdist_dir: ''}
  241. for base, dirs, files in sorted_walk(self.bdist_dir):
  242. for filename in files:
  243. if os.path.splitext(filename)[1].lower() in NATIVE_EXTENSIONS:
  244. all_outputs.append(paths[base] + filename)
  245. for filename in dirs:
  246. paths[os.path.join(base, filename)] = paths[base] + filename + '/'
  247. if self.distribution.has_ext_modules():
  248. build_cmd = self.get_finalized_command('build_ext')
  249. for ext in build_cmd.extensions:
  250. if isinstance(ext, Library):
  251. continue
  252. fullname = build_cmd.get_ext_fullname(ext.name)
  253. filename = build_cmd.get_ext_filename(fullname)
  254. if not os.path.basename(filename).startswith('dl-'):
  255. if os.path.exists(os.path.join(self.bdist_dir, filename)):
  256. ext_outputs.append(filename)
  257. return all_outputs, ext_outputs
  258. NATIVE_EXTENSIONS = dict.fromkeys('.dll .so .dylib .pyd'.split())
  259. def walk_egg(egg_dir):
  260. """Walk an unpacked egg's contents, skipping the metadata directory"""
  261. walker = sorted_walk(egg_dir)
  262. base, dirs, files = next(walker)
  263. if 'EGG-INFO' in dirs:
  264. dirs.remove('EGG-INFO')
  265. yield base, dirs, files
  266. for bdf in walker:
  267. yield bdf
  268. def analyze_egg(egg_dir, stubs):
  269. # check for existing flag in EGG-INFO
  270. for flag, fn in safety_flags.items():
  271. if os.path.exists(os.path.join(egg_dir, 'EGG-INFO', fn)):
  272. return flag
  273. if not can_scan():
  274. return False
  275. safe = True
  276. for base, dirs, files in walk_egg(egg_dir):
  277. for name in files:
  278. if name.endswith('.py') or name.endswith('.pyw'):
  279. continue
  280. elif name.endswith('.pyc') or name.endswith('.pyo'):
  281. # always scan, even if we already know we're not safe
  282. safe = scan_module(egg_dir, base, name, stubs) and safe
  283. return safe
  284. def write_safety_flag(egg_dir, safe):
  285. # Write or remove zip safety flag file(s)
  286. for flag, fn in safety_flags.items():
  287. fn = os.path.join(egg_dir, fn)
  288. if os.path.exists(fn):
  289. if safe is None or bool(safe) != flag:
  290. os.unlink(fn)
  291. elif safe is not None and bool(safe) == flag:
  292. f = open(fn, 'wt')
  293. f.write('\n')
  294. f.close()
  295. safety_flags = {
  296. True: 'zip-safe',
  297. False: 'not-zip-safe',
  298. }
  299. def scan_module(egg_dir, base, name, stubs):
  300. """Check whether module possibly uses unsafe-for-zipfile stuff"""
  301. filename = os.path.join(base, name)
  302. if filename[:-1] in stubs:
  303. return True # Extension module
  304. pkg = base[len(egg_dir) + 1 :].replace(os.sep, '.')
  305. module = pkg + (pkg and '.' or '') + os.path.splitext(name)[0]
  306. if sys.version_info < (3, 7):
  307. skip = 12 # skip magic & date & file size
  308. else:
  309. skip = 16 # skip magic & reserved? & date & file size
  310. f = open(filename, 'rb')
  311. f.read(skip)
  312. code = marshal.load(f)
  313. f.close()
  314. safe = True
  315. symbols = dict.fromkeys(iter_symbols(code))
  316. for bad in ['__file__', '__path__']:
  317. if bad in symbols:
  318. log.warn("%s: module references %s", module, bad)
  319. safe = False
  320. if 'inspect' in symbols:
  321. for bad in [
  322. 'getsource',
  323. 'getabsfile',
  324. 'getsourcefile',
  325. 'getfile' 'getsourcelines',
  326. 'findsource',
  327. 'getcomments',
  328. 'getframeinfo',
  329. 'getinnerframes',
  330. 'getouterframes',
  331. 'stack',
  332. 'trace',
  333. ]:
  334. if bad in symbols:
  335. log.warn("%s: module MAY be using inspect.%s", module, bad)
  336. safe = False
  337. return safe
  338. def iter_symbols(code):
  339. """Yield names and strings used by `code` and its nested code objects"""
  340. for name in code.co_names:
  341. yield name
  342. for const in code.co_consts:
  343. if isinstance(const, str):
  344. yield const
  345. elif isinstance(const, CodeType):
  346. for name in iter_symbols(const):
  347. yield name
  348. def can_scan():
  349. if not sys.platform.startswith('java') and sys.platform != 'cli':
  350. # CPython, PyPy, etc.
  351. return True
  352. log.warn("Unable to analyze compiled code on this platform.")
  353. log.warn(
  354. "Please ask the author to include a 'zip_safe'"
  355. " setting (either True or False) in the package's setup.py"
  356. )
  357. # Attribute names of options for commands that might need to be convinced to
  358. # install to the egg build directory
  359. INSTALL_DIRECTORY_ATTRS = ['install_lib', 'install_dir', 'install_data', 'install_base']
  360. def make_zipfile(zip_filename, base_dir, verbose=0, dry_run=0, compress=True, mode='w'):
  361. """Create a zip file from all the files under 'base_dir'. The output
  362. zip file will be named 'base_dir' + ".zip". Uses either the "zipfile"
  363. Python module (if available) or the InfoZIP "zip" utility (if installed
  364. and found on the default search path). If neither tool is available,
  365. raises DistutilsExecError. Returns the name of the output zip file.
  366. """
  367. import zipfile
  368. mkpath(os.path.dirname(zip_filename), dry_run=dry_run)
  369. log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir)
  370. def visit(z, dirname, names):
  371. for name in names:
  372. path = os.path.normpath(os.path.join(dirname, name))
  373. if os.path.isfile(path):
  374. p = path[len(base_dir) + 1 :]
  375. if not dry_run:
  376. z.write(path, p)
  377. log.debug("adding '%s'", p)
  378. compression = zipfile.ZIP_DEFLATED if compress else zipfile.ZIP_STORED
  379. if not dry_run:
  380. z = zipfile.ZipFile(zip_filename, mode, compression=compression)
  381. for dirname, dirs, files in sorted_walk(base_dir):
  382. visit(z, dirname, files)
  383. z.close()
  384. else:
  385. for dirname, dirs, files in sorted_walk(base_dir):
  386. visit(None, dirname, files)
  387. return zip_filename