easy_install.py 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  1. """
  2. Easy Install
  3. ------------
  4. A tool for doing automatic download/extract/build of distutils-based Python
  5. packages. For detailed documentation, see the accompanying EasyInstall.txt
  6. file, or visit the `EasyInstall home page`__.
  7. __ https://setuptools.readthedocs.io/en/latest/easy_install.html
  8. """
  9. from glob import glob
  10. from distutils.util import get_platform
  11. from distutils.util import convert_path, subst_vars
  12. from distutils.errors import (
  13. DistutilsArgError, DistutilsOptionError,
  14. DistutilsError, DistutilsPlatformError,
  15. )
  16. from distutils.command.install import INSTALL_SCHEMES, SCHEME_KEYS
  17. from distutils import log, dir_util
  18. from distutils.command.build_scripts import first_line_re
  19. from distutils.spawn import find_executable
  20. import sys
  21. import os
  22. import zipimport
  23. import shutil
  24. import tempfile
  25. import zipfile
  26. import re
  27. import stat
  28. import random
  29. import textwrap
  30. import warnings
  31. import site
  32. import struct
  33. import contextlib
  34. import subprocess
  35. import shlex
  36. import io
  37. from sysconfig import get_config_vars, get_path
  38. from setuptools import SetuptoolsDeprecationWarning
  39. from setuptools.extern import six
  40. from setuptools.extern.six.moves import configparser, map
  41. from setuptools import Command
  42. from setuptools.sandbox import run_setup
  43. from setuptools.py27compat import rmtree_safe
  44. from setuptools.command import setopt
  45. from setuptools.archive_util import unpack_archive
  46. from setuptools.package_index import (
  47. PackageIndex, parse_requirement_arg, URL_SCHEME,
  48. )
  49. from setuptools.command import bdist_egg, egg_info
  50. from setuptools.wheel import Wheel
  51. from pkg_resources import (
  52. yield_lines, normalize_path, resource_string, ensure_directory,
  53. get_distribution, find_distributions, Environment, Requirement,
  54. Distribution, PathMetadata, EggMetadata, WorkingSet, DistributionNotFound,
  55. VersionConflict, DEVELOP_DIST,
  56. )
  57. import pkg_resources.py31compat
  58. __metaclass__ = type
  59. # Turn on PEP440Warnings
  60. warnings.filterwarnings("default", category=pkg_resources.PEP440Warning)
  61. __all__ = [
  62. 'samefile', 'easy_install', 'PthDistributions', 'extract_wininst_cfg',
  63. 'main', 'get_exe_prefixes',
  64. ]
  65. def is_64bit():
  66. return struct.calcsize("P") == 8
  67. def samefile(p1, p2):
  68. """
  69. Determine if two paths reference the same file.
  70. Augments os.path.samefile to work on Windows and
  71. suppresses errors if the path doesn't exist.
  72. """
  73. both_exist = os.path.exists(p1) and os.path.exists(p2)
  74. use_samefile = hasattr(os.path, 'samefile') and both_exist
  75. if use_samefile:
  76. return os.path.samefile(p1, p2)
  77. norm_p1 = os.path.normpath(os.path.normcase(p1))
  78. norm_p2 = os.path.normpath(os.path.normcase(p2))
  79. return norm_p1 == norm_p2
  80. if six.PY2:
  81. def _to_bytes(s):
  82. return s
  83. def isascii(s):
  84. try:
  85. six.text_type(s, 'ascii')
  86. return True
  87. except UnicodeError:
  88. return False
  89. else:
  90. def _to_bytes(s):
  91. return s.encode('utf8')
  92. def isascii(s):
  93. try:
  94. s.encode('ascii')
  95. return True
  96. except UnicodeError:
  97. return False
  98. def _one_liner(text):
  99. return textwrap.dedent(text).strip().replace('\n', '; ')
  100. class easy_install(Command):
  101. """Manage a download/build/install process"""
  102. description = "Find/get/install Python packages"
  103. command_consumes_arguments = True
  104. user_options = [
  105. ('prefix=', None, "installation prefix"),
  106. ("zip-ok", "z", "install package as a zipfile"),
  107. ("multi-version", "m", "make apps have to require() a version"),
  108. ("upgrade", "U", "force upgrade (searches PyPI for latest versions)"),
  109. ("install-dir=", "d", "install package to DIR"),
  110. ("script-dir=", "s", "install scripts to DIR"),
  111. ("exclude-scripts", "x", "Don't install scripts"),
  112. ("always-copy", "a", "Copy all needed packages to install dir"),
  113. ("index-url=", "i", "base URL of Python Package Index"),
  114. ("find-links=", "f", "additional URL(s) to search for packages"),
  115. ("build-directory=", "b",
  116. "download/extract/build in DIR; keep the results"),
  117. ('optimize=', 'O',
  118. "also compile with optimization: -O1 for \"python -O\", "
  119. "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
  120. ('record=', None,
  121. "filename in which to record list of installed files"),
  122. ('always-unzip', 'Z', "don't install as a zipfile, no matter what"),
  123. ('site-dirs=', 'S', "list of directories where .pth files work"),
  124. ('editable', 'e', "Install specified packages in editable form"),
  125. ('no-deps', 'N', "don't install dependencies"),
  126. ('allow-hosts=', 'H', "pattern(s) that hostnames must match"),
  127. ('local-snapshots-ok', 'l',
  128. "allow building eggs from local checkouts"),
  129. ('version', None, "print version information and exit"),
  130. ('no-find-links', None,
  131. "Don't load find-links defined in packages being installed"),
  132. ('user', None, "install in user site-package '%s'" % site.USER_SITE)
  133. ]
  134. boolean_options = [
  135. 'zip-ok', 'multi-version', 'exclude-scripts', 'upgrade', 'always-copy',
  136. 'editable',
  137. 'no-deps', 'local-snapshots-ok', 'version',
  138. 'user'
  139. ]
  140. negative_opt = {'always-unzip': 'zip-ok'}
  141. create_index = PackageIndex
  142. def initialize_options(self):
  143. # the --user option seems to be an opt-in one,
  144. # so the default should be False.
  145. self.user = 0
  146. self.zip_ok = self.local_snapshots_ok = None
  147. self.install_dir = self.script_dir = self.exclude_scripts = None
  148. self.index_url = None
  149. self.find_links = None
  150. self.build_directory = None
  151. self.args = None
  152. self.optimize = self.record = None
  153. self.upgrade = self.always_copy = self.multi_version = None
  154. self.editable = self.no_deps = self.allow_hosts = None
  155. self.root = self.prefix = self.no_report = None
  156. self.version = None
  157. self.install_purelib = None # for pure module distributions
  158. self.install_platlib = None # non-pure (dists w/ extensions)
  159. self.install_headers = None # for C/C++ headers
  160. self.install_lib = None # set to either purelib or platlib
  161. self.install_scripts = None
  162. self.install_data = None
  163. self.install_base = None
  164. self.install_platbase = None
  165. if site.ENABLE_USER_SITE:
  166. self.install_userbase = site.USER_BASE
  167. self.install_usersite = site.USER_SITE
  168. else:
  169. self.install_userbase = None
  170. self.install_usersite = None
  171. self.no_find_links = None
  172. # Options not specifiable via command line
  173. self.package_index = None
  174. self.pth_file = self.always_copy_from = None
  175. self.site_dirs = None
  176. self.installed_projects = {}
  177. self.sitepy_installed = False
  178. # Always read easy_install options, even if we are subclassed, or have
  179. # an independent instance created. This ensures that defaults will
  180. # always come from the standard configuration file(s)' "easy_install"
  181. # section, even if this is a "develop" or "install" command, or some
  182. # other embedding.
  183. self._dry_run = None
  184. self.verbose = self.distribution.verbose
  185. self.distribution._set_command_options(
  186. self, self.distribution.get_option_dict('easy_install')
  187. )
  188. def delete_blockers(self, blockers):
  189. extant_blockers = (
  190. filename for filename in blockers
  191. if os.path.exists(filename) or os.path.islink(filename)
  192. )
  193. list(map(self._delete_path, extant_blockers))
  194. def _delete_path(self, path):
  195. log.info("Deleting %s", path)
  196. if self.dry_run:
  197. return
  198. is_tree = os.path.isdir(path) and not os.path.islink(path)
  199. remover = rmtree if is_tree else os.unlink
  200. remover(path)
  201. @staticmethod
  202. def _render_version():
  203. """
  204. Render the Setuptools version and installation details, then exit.
  205. """
  206. ver = '{}.{}'.format(*sys.version_info)
  207. dist = get_distribution('setuptools')
  208. tmpl = 'setuptools {dist.version} from {dist.location} (Python {ver})'
  209. print(tmpl.format(**locals()))
  210. raise SystemExit()
  211. def finalize_options(self):
  212. self.version and self._render_version()
  213. py_version = sys.version.split()[0]
  214. prefix, exec_prefix = get_config_vars('prefix', 'exec_prefix')
  215. self.config_vars = {
  216. 'dist_name': self.distribution.get_name(),
  217. 'dist_version': self.distribution.get_version(),
  218. 'dist_fullname': self.distribution.get_fullname(),
  219. 'py_version': py_version,
  220. 'py_version_short': py_version[0:3],
  221. 'py_version_nodot': py_version[0] + py_version[2],
  222. 'sys_prefix': prefix,
  223. 'prefix': prefix,
  224. 'sys_exec_prefix': exec_prefix,
  225. 'exec_prefix': exec_prefix,
  226. # Only python 3.2+ has abiflags
  227. 'abiflags': getattr(sys, 'abiflags', ''),
  228. }
  229. if site.ENABLE_USER_SITE:
  230. self.config_vars['userbase'] = self.install_userbase
  231. self.config_vars['usersite'] = self.install_usersite
  232. elif self.user:
  233. log.warn("WARNING: The user site-packages directory is disabled.")
  234. self._fix_install_dir_for_user_site()
  235. self.expand_basedirs()
  236. self.expand_dirs()
  237. self._expand(
  238. 'install_dir', 'script_dir', 'build_directory',
  239. 'site_dirs',
  240. )
  241. # If a non-default installation directory was specified, default the
  242. # script directory to match it.
  243. if self.script_dir is None:
  244. self.script_dir = self.install_dir
  245. if self.no_find_links is None:
  246. self.no_find_links = False
  247. # Let install_dir get set by install_lib command, which in turn
  248. # gets its info from the install command, and takes into account
  249. # --prefix and --home and all that other crud.
  250. self.set_undefined_options(
  251. 'install_lib', ('install_dir', 'install_dir')
  252. )
  253. # Likewise, set default script_dir from 'install_scripts.install_dir'
  254. self.set_undefined_options(
  255. 'install_scripts', ('install_dir', 'script_dir')
  256. )
  257. if self.user and self.install_purelib:
  258. self.install_dir = self.install_purelib
  259. self.script_dir = self.install_scripts
  260. # default --record from the install command
  261. self.set_undefined_options('install', ('record', 'record'))
  262. # Should this be moved to the if statement below? It's not used
  263. # elsewhere
  264. normpath = map(normalize_path, sys.path)
  265. self.all_site_dirs = get_site_dirs()
  266. if self.site_dirs is not None:
  267. site_dirs = [
  268. os.path.expanduser(s.strip()) for s in
  269. self.site_dirs.split(',')
  270. ]
  271. for d in site_dirs:
  272. if not os.path.isdir(d):
  273. log.warn("%s (in --site-dirs) does not exist", d)
  274. elif normalize_path(d) not in normpath:
  275. raise DistutilsOptionError(
  276. d + " (in --site-dirs) is not on sys.path"
  277. )
  278. else:
  279. self.all_site_dirs.append(normalize_path(d))
  280. if not self.editable:
  281. self.check_site_dir()
  282. self.index_url = self.index_url or "https://pypi.org/simple/"
  283. self.shadow_path = self.all_site_dirs[:]
  284. for path_item in self.install_dir, normalize_path(self.script_dir):
  285. if path_item not in self.shadow_path:
  286. self.shadow_path.insert(0, path_item)
  287. if self.allow_hosts is not None:
  288. hosts = [s.strip() for s in self.allow_hosts.split(',')]
  289. else:
  290. hosts = ['*']
  291. if self.package_index is None:
  292. self.package_index = self.create_index(
  293. self.index_url, search_path=self.shadow_path, hosts=hosts,
  294. )
  295. self.local_index = Environment(self.shadow_path + sys.path)
  296. if self.find_links is not None:
  297. if isinstance(self.find_links, six.string_types):
  298. self.find_links = self.find_links.split()
  299. else:
  300. self.find_links = []
  301. if self.local_snapshots_ok:
  302. self.package_index.scan_egg_links(self.shadow_path + sys.path)
  303. if not self.no_find_links:
  304. self.package_index.add_find_links(self.find_links)
  305. self.set_undefined_options('install_lib', ('optimize', 'optimize'))
  306. if not isinstance(self.optimize, int):
  307. try:
  308. self.optimize = int(self.optimize)
  309. if not (0 <= self.optimize <= 2):
  310. raise ValueError
  311. except ValueError:
  312. raise DistutilsOptionError("--optimize must be 0, 1, or 2")
  313. if self.editable and not self.build_directory:
  314. raise DistutilsArgError(
  315. "Must specify a build directory (-b) when using --editable"
  316. )
  317. if not self.args:
  318. raise DistutilsArgError(
  319. "No urls, filenames, or requirements specified (see --help)")
  320. self.outputs = []
  321. def _fix_install_dir_for_user_site(self):
  322. """
  323. Fix the install_dir if "--user" was used.
  324. """
  325. if not self.user or not site.ENABLE_USER_SITE:
  326. return
  327. self.create_home_path()
  328. if self.install_userbase is None:
  329. msg = "User base directory is not specified"
  330. raise DistutilsPlatformError(msg)
  331. self.install_base = self.install_platbase = self.install_userbase
  332. scheme_name = os.name.replace('posix', 'unix') + '_user'
  333. self.select_scheme(scheme_name)
  334. def _expand_attrs(self, attrs):
  335. for attr in attrs:
  336. val = getattr(self, attr)
  337. if val is not None:
  338. if os.name == 'posix' or os.name == 'nt':
  339. val = os.path.expanduser(val)
  340. val = subst_vars(val, self.config_vars)
  341. setattr(self, attr, val)
  342. def expand_basedirs(self):
  343. """Calls `os.path.expanduser` on install_base, install_platbase and
  344. root."""
  345. self._expand_attrs(['install_base', 'install_platbase', 'root'])
  346. def expand_dirs(self):
  347. """Calls `os.path.expanduser` on install dirs."""
  348. dirs = [
  349. 'install_purelib',
  350. 'install_platlib',
  351. 'install_lib',
  352. 'install_headers',
  353. 'install_scripts',
  354. 'install_data',
  355. ]
  356. self._expand_attrs(dirs)
  357. def run(self, show_deprecation=True):
  358. if show_deprecation:
  359. self.announce(
  360. "WARNING: The easy_install command is deprecated "
  361. "and will be removed in a future version.",
  362. log.WARN,
  363. )
  364. if self.verbose != self.distribution.verbose:
  365. log.set_verbosity(self.verbose)
  366. try:
  367. for spec in self.args:
  368. self.easy_install(spec, not self.no_deps)
  369. if self.record:
  370. outputs = self.outputs
  371. if self.root: # strip any package prefix
  372. root_len = len(self.root)
  373. for counter in range(len(outputs)):
  374. outputs[counter] = outputs[counter][root_len:]
  375. from distutils import file_util
  376. self.execute(
  377. file_util.write_file, (self.record, outputs),
  378. "writing list of installed files to '%s'" %
  379. self.record
  380. )
  381. self.warn_deprecated_options()
  382. finally:
  383. log.set_verbosity(self.distribution.verbose)
  384. def pseudo_tempname(self):
  385. """Return a pseudo-tempname base in the install directory.
  386. This code is intentionally naive; if a malicious party can write to
  387. the target directory you're already in deep doodoo.
  388. """
  389. try:
  390. pid = os.getpid()
  391. except Exception:
  392. pid = random.randint(0, sys.maxsize)
  393. return os.path.join(self.install_dir, "test-easy-install-%s" % pid)
  394. def warn_deprecated_options(self):
  395. pass
  396. def check_site_dir(self):
  397. """Verify that self.install_dir is .pth-capable dir, if needed"""
  398. instdir = normalize_path(self.install_dir)
  399. pth_file = os.path.join(instdir, 'easy-install.pth')
  400. if not os.path.exists(instdir):
  401. try:
  402. os.makedirs(instdir)
  403. except (OSError, IOError):
  404. self.cant_write_to_target()
  405. # Is it a configured, PYTHONPATH, implicit, or explicit site dir?
  406. is_site_dir = instdir in self.all_site_dirs
  407. if not is_site_dir and not self.multi_version:
  408. # No? Then directly test whether it does .pth file processing
  409. is_site_dir = self.check_pth_processing()
  410. else:
  411. # make sure we can write to target dir
  412. testfile = self.pseudo_tempname() + '.write-test'
  413. test_exists = os.path.exists(testfile)
  414. try:
  415. if test_exists:
  416. os.unlink(testfile)
  417. open(testfile, 'w').close()
  418. os.unlink(testfile)
  419. except (OSError, IOError):
  420. self.cant_write_to_target()
  421. if not is_site_dir and not self.multi_version:
  422. # Can't install non-multi to non-site dir with easy_install
  423. pythonpath = os.environ.get('PYTHONPATH', '')
  424. log.warn(self.__no_default_msg, self.install_dir, pythonpath)
  425. if is_site_dir:
  426. if self.pth_file is None:
  427. self.pth_file = PthDistributions(pth_file, self.all_site_dirs)
  428. else:
  429. self.pth_file = None
  430. if instdir not in map(normalize_path, _pythonpath()):
  431. # only PYTHONPATH dirs need a site.py, so pretend it's there
  432. self.sitepy_installed = True
  433. elif self.multi_version and not os.path.exists(pth_file):
  434. self.sitepy_installed = True # don't need site.py in this case
  435. self.pth_file = None # and don't create a .pth file
  436. self.install_dir = instdir
  437. __cant_write_msg = textwrap.dedent("""
  438. can't create or remove files in install directory
  439. The following error occurred while trying to add or remove files in the
  440. installation directory:
  441. %s
  442. The installation directory you specified (via --install-dir, --prefix, or
  443. the distutils default setting) was:
  444. %s
  445. """).lstrip() # noqa
  446. __not_exists_id = textwrap.dedent("""
  447. This directory does not currently exist. Please create it and try again, or
  448. choose a different installation directory (using the -d or --install-dir
  449. option).
  450. """).lstrip() # noqa
  451. __access_msg = textwrap.dedent("""
  452. Perhaps your account does not have write access to this directory? If the
  453. installation directory is a system-owned directory, you may need to sign in
  454. as the administrator or "root" account. If you do not have administrative
  455. access to this machine, you may wish to choose a different installation
  456. directory, preferably one that is listed in your PYTHONPATH environment
  457. variable.
  458. For information on other options, you may wish to consult the
  459. documentation at:
  460. https://setuptools.readthedocs.io/en/latest/easy_install.html
  461. Please make the appropriate changes for your system and try again.
  462. """).lstrip() # noqa
  463. def cant_write_to_target(self):
  464. msg = self.__cant_write_msg % (sys.exc_info()[1], self.install_dir,)
  465. if not os.path.exists(self.install_dir):
  466. msg += '\n' + self.__not_exists_id
  467. else:
  468. msg += '\n' + self.__access_msg
  469. raise DistutilsError(msg)
  470. def check_pth_processing(self):
  471. """Empirically verify whether .pth files are supported in inst. dir"""
  472. instdir = self.install_dir
  473. log.info("Checking .pth file support in %s", instdir)
  474. pth_file = self.pseudo_tempname() + ".pth"
  475. ok_file = pth_file + '.ok'
  476. ok_exists = os.path.exists(ok_file)
  477. tmpl = _one_liner("""
  478. import os
  479. f = open({ok_file!r}, 'w')
  480. f.write('OK')
  481. f.close()
  482. """) + '\n'
  483. try:
  484. if ok_exists:
  485. os.unlink(ok_file)
  486. dirname = os.path.dirname(ok_file)
  487. pkg_resources.py31compat.makedirs(dirname, exist_ok=True)
  488. f = open(pth_file, 'w')
  489. except (OSError, IOError):
  490. self.cant_write_to_target()
  491. else:
  492. try:
  493. f.write(tmpl.format(**locals()))
  494. f.close()
  495. f = None
  496. executable = sys.executable
  497. if os.name == 'nt':
  498. dirname, basename = os.path.split(executable)
  499. alt = os.path.join(dirname, 'pythonw.exe')
  500. use_alt = (
  501. basename.lower() == 'python.exe' and
  502. os.path.exists(alt)
  503. )
  504. if use_alt:
  505. # use pythonw.exe to avoid opening a console window
  506. executable = alt
  507. from distutils.spawn import spawn
  508. spawn([executable, '-E', '-c', 'pass'], 0)
  509. if os.path.exists(ok_file):
  510. log.info(
  511. "TEST PASSED: %s appears to support .pth files",
  512. instdir
  513. )
  514. return True
  515. finally:
  516. if f:
  517. f.close()
  518. if os.path.exists(ok_file):
  519. os.unlink(ok_file)
  520. if os.path.exists(pth_file):
  521. os.unlink(pth_file)
  522. if not self.multi_version:
  523. log.warn("TEST FAILED: %s does NOT support .pth files", instdir)
  524. return False
  525. def install_egg_scripts(self, dist):
  526. """Write all the scripts for `dist`, unless scripts are excluded"""
  527. if not self.exclude_scripts and dist.metadata_isdir('scripts'):
  528. for script_name in dist.metadata_listdir('scripts'):
  529. if dist.metadata_isdir('scripts/' + script_name):
  530. # The "script" is a directory, likely a Python 3
  531. # __pycache__ directory, so skip it.
  532. continue
  533. self.install_script(
  534. dist, script_name,
  535. dist.get_metadata('scripts/' + script_name)
  536. )
  537. self.install_wrapper_scripts(dist)
  538. def add_output(self, path):
  539. if os.path.isdir(path):
  540. for base, dirs, files in os.walk(path):
  541. for filename in files:
  542. self.outputs.append(os.path.join(base, filename))
  543. else:
  544. self.outputs.append(path)
  545. def not_editable(self, spec):
  546. if self.editable:
  547. raise DistutilsArgError(
  548. "Invalid argument %r: you can't use filenames or URLs "
  549. "with --editable (except via the --find-links option)."
  550. % (spec,)
  551. )
  552. def check_editable(self, spec):
  553. if not self.editable:
  554. return
  555. if os.path.exists(os.path.join(self.build_directory, spec.key)):
  556. raise DistutilsArgError(
  557. "%r already exists in %s; can't do a checkout there" %
  558. (spec.key, self.build_directory)
  559. )
  560. @contextlib.contextmanager
  561. def _tmpdir(self):
  562. tmpdir = tempfile.mkdtemp(prefix=u"easy_install-")
  563. try:
  564. # cast to str as workaround for #709 and #710 and #712
  565. yield str(tmpdir)
  566. finally:
  567. os.path.exists(tmpdir) and rmtree(rmtree_safe(tmpdir))
  568. def easy_install(self, spec, deps=False):
  569. if not self.editable:
  570. self.install_site_py()
  571. with self._tmpdir() as tmpdir:
  572. if not isinstance(spec, Requirement):
  573. if URL_SCHEME(spec):
  574. # It's a url, download it to tmpdir and process
  575. self.not_editable(spec)
  576. dl = self.package_index.download(spec, tmpdir)
  577. return self.install_item(None, dl, tmpdir, deps, True)
  578. elif os.path.exists(spec):
  579. # Existing file or directory, just process it directly
  580. self.not_editable(spec)
  581. return self.install_item(None, spec, tmpdir, deps, True)
  582. else:
  583. spec = parse_requirement_arg(spec)
  584. self.check_editable(spec)
  585. dist = self.package_index.fetch_distribution(
  586. spec, tmpdir, self.upgrade, self.editable,
  587. not self.always_copy, self.local_index
  588. )
  589. if dist is None:
  590. msg = "Could not find suitable distribution for %r" % spec
  591. if self.always_copy:
  592. msg += " (--always-copy skips system and development eggs)"
  593. raise DistutilsError(msg)
  594. elif dist.precedence == DEVELOP_DIST:
  595. # .egg-info dists don't need installing, just process deps
  596. self.process_distribution(spec, dist, deps, "Using")
  597. return dist
  598. else:
  599. return self.install_item(spec, dist.location, tmpdir, deps)
  600. def install_item(self, spec, download, tmpdir, deps, install_needed=False):
  601. # Installation is also needed if file in tmpdir or is not an egg
  602. install_needed = install_needed or self.always_copy
  603. install_needed = install_needed or os.path.dirname(download) == tmpdir
  604. install_needed = install_needed or not download.endswith('.egg')
  605. install_needed = install_needed or (
  606. self.always_copy_from is not None and
  607. os.path.dirname(normalize_path(download)) ==
  608. normalize_path(self.always_copy_from)
  609. )
  610. if spec and not install_needed:
  611. # at this point, we know it's a local .egg, we just don't know if
  612. # it's already installed.
  613. for dist in self.local_index[spec.project_name]:
  614. if dist.location == download:
  615. break
  616. else:
  617. install_needed = True # it's not in the local index
  618. log.info("Processing %s", os.path.basename(download))
  619. if install_needed:
  620. dists = self.install_eggs(spec, download, tmpdir)
  621. for dist in dists:
  622. self.process_distribution(spec, dist, deps)
  623. else:
  624. dists = [self.egg_distribution(download)]
  625. self.process_distribution(spec, dists[0], deps, "Using")
  626. if spec is not None:
  627. for dist in dists:
  628. if dist in spec:
  629. return dist
  630. def select_scheme(self, name):
  631. """Sets the install directories by applying the install schemes."""
  632. # it's the caller's problem if they supply a bad name!
  633. scheme = INSTALL_SCHEMES[name]
  634. for key in SCHEME_KEYS:
  635. attrname = 'install_' + key
  636. if getattr(self, attrname) is None:
  637. setattr(self, attrname, scheme[key])
  638. def process_distribution(self, requirement, dist, deps=True, *info):
  639. self.update_pth(dist)
  640. self.package_index.add(dist)
  641. if dist in self.local_index[dist.key]:
  642. self.local_index.remove(dist)
  643. self.local_index.add(dist)
  644. self.install_egg_scripts(dist)
  645. self.installed_projects[dist.key] = dist
  646. log.info(self.installation_report(requirement, dist, *info))
  647. if (dist.has_metadata('dependency_links.txt') and
  648. not self.no_find_links):
  649. self.package_index.add_find_links(
  650. dist.get_metadata_lines('dependency_links.txt')
  651. )
  652. if not deps and not self.always_copy:
  653. return
  654. elif requirement is not None and dist.key != requirement.key:
  655. log.warn("Skipping dependencies for %s", dist)
  656. return # XXX this is not the distribution we were looking for
  657. elif requirement is None or dist not in requirement:
  658. # if we wound up with a different version, resolve what we've got
  659. distreq = dist.as_requirement()
  660. requirement = Requirement(str(distreq))
  661. log.info("Processing dependencies for %s", requirement)
  662. try:
  663. distros = WorkingSet([]).resolve(
  664. [requirement], self.local_index, self.easy_install
  665. )
  666. except DistributionNotFound as e:
  667. raise DistutilsError(str(e))
  668. except VersionConflict as e:
  669. raise DistutilsError(e.report())
  670. if self.always_copy or self.always_copy_from:
  671. # Force all the relevant distros to be copied or activated
  672. for dist in distros:
  673. if dist.key not in self.installed_projects:
  674. self.easy_install(dist.as_requirement())
  675. log.info("Finished processing dependencies for %s", requirement)
  676. def should_unzip(self, dist):
  677. if self.zip_ok is not None:
  678. return not self.zip_ok
  679. if dist.has_metadata('not-zip-safe'):
  680. return True
  681. if not dist.has_metadata('zip-safe'):
  682. return True
  683. return False
  684. def maybe_move(self, spec, dist_filename, setup_base):
  685. dst = os.path.join(self.build_directory, spec.key)
  686. if os.path.exists(dst):
  687. msg = (
  688. "%r already exists in %s; build directory %s will not be kept"
  689. )
  690. log.warn(msg, spec.key, self.build_directory, setup_base)
  691. return setup_base
  692. if os.path.isdir(dist_filename):
  693. setup_base = dist_filename
  694. else:
  695. if os.path.dirname(dist_filename) == setup_base:
  696. os.unlink(dist_filename) # get it out of the tmp dir
  697. contents = os.listdir(setup_base)
  698. if len(contents) == 1:
  699. dist_filename = os.path.join(setup_base, contents[0])
  700. if os.path.isdir(dist_filename):
  701. # if the only thing there is a directory, move it instead
  702. setup_base = dist_filename
  703. ensure_directory(dst)
  704. shutil.move(setup_base, dst)
  705. return dst
  706. def install_wrapper_scripts(self, dist):
  707. if self.exclude_scripts:
  708. return
  709. for args in ScriptWriter.best().get_args(dist):
  710. self.write_script(*args)
  711. def install_script(self, dist, script_name, script_text, dev_path=None):
  712. """Generate a legacy script wrapper and install it"""
  713. spec = str(dist.as_requirement())
  714. is_script = is_python_script(script_text, script_name)
  715. if is_script:
  716. body = self._load_template(dev_path) % locals()
  717. script_text = ScriptWriter.get_header(script_text) + body
  718. self.write_script(script_name, _to_bytes(script_text), 'b')
  719. @staticmethod
  720. def _load_template(dev_path):
  721. """
  722. There are a couple of template scripts in the package. This
  723. function loads one of them and prepares it for use.
  724. """
  725. # See https://github.com/pypa/setuptools/issues/134 for info
  726. # on script file naming and downstream issues with SVR4
  727. name = 'script.tmpl'
  728. if dev_path:
  729. name = name.replace('.tmpl', ' (dev).tmpl')
  730. raw_bytes = resource_string('setuptools', name)
  731. return raw_bytes.decode('utf-8')
  732. def write_script(self, script_name, contents, mode="t", blockers=()):
  733. """Write an executable file to the scripts directory"""
  734. self.delete_blockers( # clean up old .py/.pyw w/o a script
  735. [os.path.join(self.script_dir, x) for x in blockers]
  736. )
  737. log.info("Installing %s script to %s", script_name, self.script_dir)
  738. target = os.path.join(self.script_dir, script_name)
  739. self.add_output(target)
  740. if self.dry_run:
  741. return
  742. mask = current_umask()
  743. ensure_directory(target)
  744. if os.path.exists(target):
  745. os.unlink(target)
  746. with open(target, "w" + mode) as f:
  747. f.write(contents)
  748. chmod(target, 0o777 - mask)
  749. def install_eggs(self, spec, dist_filename, tmpdir):
  750. # .egg dirs or files are already built, so just return them
  751. if dist_filename.lower().endswith('.egg'):
  752. return [self.install_egg(dist_filename, tmpdir)]
  753. elif dist_filename.lower().endswith('.exe'):
  754. return [self.install_exe(dist_filename, tmpdir)]
  755. elif dist_filename.lower().endswith('.whl'):
  756. return [self.install_wheel(dist_filename, tmpdir)]
  757. # Anything else, try to extract and build
  758. setup_base = tmpdir
  759. if os.path.isfile(dist_filename) and not dist_filename.endswith('.py'):
  760. unpack_archive(dist_filename, tmpdir, self.unpack_progress)
  761. elif os.path.isdir(dist_filename):
  762. setup_base = os.path.abspath(dist_filename)
  763. if (setup_base.startswith(tmpdir) # something we downloaded
  764. and self.build_directory and spec is not None):
  765. setup_base = self.maybe_move(spec, dist_filename, setup_base)
  766. # Find the setup.py file
  767. setup_script = os.path.join(setup_base, 'setup.py')
  768. if not os.path.exists(setup_script):
  769. setups = glob(os.path.join(setup_base, '*', 'setup.py'))
  770. if not setups:
  771. raise DistutilsError(
  772. "Couldn't find a setup script in %s" %
  773. os.path.abspath(dist_filename)
  774. )
  775. if len(setups) > 1:
  776. raise DistutilsError(
  777. "Multiple setup scripts in %s" %
  778. os.path.abspath(dist_filename)
  779. )
  780. setup_script = setups[0]
  781. # Now run it, and return the result
  782. if self.editable:
  783. log.info(self.report_editable(spec, setup_script))
  784. return []
  785. else:
  786. return self.build_and_install(setup_script, setup_base)
  787. def egg_distribution(self, egg_path):
  788. if os.path.isdir(egg_path):
  789. metadata = PathMetadata(egg_path, os.path.join(egg_path,
  790. 'EGG-INFO'))
  791. else:
  792. metadata = EggMetadata(zipimport.zipimporter(egg_path))
  793. return Distribution.from_filename(egg_path, metadata=metadata)
  794. def install_egg(self, egg_path, tmpdir):
  795. destination = os.path.join(
  796. self.install_dir,
  797. os.path.basename(egg_path),
  798. )
  799. destination = os.path.abspath(destination)
  800. if not self.dry_run:
  801. ensure_directory(destination)
  802. dist = self.egg_distribution(egg_path)
  803. if not samefile(egg_path, destination):
  804. if os.path.isdir(destination) and not os.path.islink(destination):
  805. dir_util.remove_tree(destination, dry_run=self.dry_run)
  806. elif os.path.exists(destination):
  807. self.execute(
  808. os.unlink,
  809. (destination,),
  810. "Removing " + destination,
  811. )
  812. try:
  813. new_dist_is_zipped = False
  814. if os.path.isdir(egg_path):
  815. if egg_path.startswith(tmpdir):
  816. f, m = shutil.move, "Moving"
  817. else:
  818. f, m = shutil.copytree, "Copying"
  819. elif self.should_unzip(dist):
  820. self.mkpath(destination)
  821. f, m = self.unpack_and_compile, "Extracting"
  822. else:
  823. new_dist_is_zipped = True
  824. if egg_path.startswith(tmpdir):
  825. f, m = shutil.move, "Moving"
  826. else:
  827. f, m = shutil.copy2, "Copying"
  828. self.execute(
  829. f,
  830. (egg_path, destination),
  831. (m + " %s to %s") % (
  832. os.path.basename(egg_path),
  833. os.path.dirname(destination)
  834. ),
  835. )
  836. update_dist_caches(
  837. destination,
  838. fix_zipimporter_caches=new_dist_is_zipped,
  839. )
  840. except Exception:
  841. update_dist_caches(destination, fix_zipimporter_caches=False)
  842. raise
  843. self.add_output(destination)
  844. return self.egg_distribution(destination)
  845. def install_exe(self, dist_filename, tmpdir):
  846. # See if it's valid, get data
  847. cfg = extract_wininst_cfg(dist_filename)
  848. if cfg is None:
  849. raise DistutilsError(
  850. "%s is not a valid distutils Windows .exe" % dist_filename
  851. )
  852. # Create a dummy distribution object until we build the real distro
  853. dist = Distribution(
  854. None,
  855. project_name=cfg.get('metadata', 'name'),
  856. version=cfg.get('metadata', 'version'), platform=get_platform(),
  857. )
  858. # Convert the .exe to an unpacked egg
  859. egg_path = os.path.join(tmpdir, dist.egg_name() + '.egg')
  860. dist.location = egg_path
  861. egg_tmp = egg_path + '.tmp'
  862. _egg_info = os.path.join(egg_tmp, 'EGG-INFO')
  863. pkg_inf = os.path.join(_egg_info, 'PKG-INFO')
  864. ensure_directory(pkg_inf) # make sure EGG-INFO dir exists
  865. dist._provider = PathMetadata(egg_tmp, _egg_info) # XXX
  866. self.exe_to_egg(dist_filename, egg_tmp)
  867. # Write EGG-INFO/PKG-INFO
  868. if not os.path.exists(pkg_inf):
  869. f = open(pkg_inf, 'w')
  870. f.write('Metadata-Version: 1.0\n')
  871. for k, v in cfg.items('metadata'):
  872. if k != 'target_version':
  873. f.write('%s: %s\n' % (k.replace('_', '-').title(), v))
  874. f.close()
  875. script_dir = os.path.join(_egg_info, 'scripts')
  876. # delete entry-point scripts to avoid duping
  877. self.delete_blockers([
  878. os.path.join(script_dir, args[0])
  879. for args in ScriptWriter.get_args(dist)
  880. ])
  881. # Build .egg file from tmpdir
  882. bdist_egg.make_zipfile(
  883. egg_path, egg_tmp, verbose=self.verbose, dry_run=self.dry_run,
  884. )
  885. # install the .egg
  886. return self.install_egg(egg_path, tmpdir)
  887. def exe_to_egg(self, dist_filename, egg_tmp):
  888. """Extract a bdist_wininst to the directories an egg would use"""
  889. # Check for .pth file and set up prefix translations
  890. prefixes = get_exe_prefixes(dist_filename)
  891. to_compile = []
  892. native_libs = []
  893. top_level = {}
  894. def process(src, dst):
  895. s = src.lower()
  896. for old, new in prefixes:
  897. if s.startswith(old):
  898. src = new + src[len(old):]
  899. parts = src.split('/')
  900. dst = os.path.join(egg_tmp, *parts)
  901. dl = dst.lower()
  902. if dl.endswith('.pyd') or dl.endswith('.dll'):
  903. parts[-1] = bdist_egg.strip_module(parts[-1])
  904. top_level[os.path.splitext(parts[0])[0]] = 1
  905. native_libs.append(src)
  906. elif dl.endswith('.py') and old != 'SCRIPTS/':
  907. top_level[os.path.splitext(parts[0])[0]] = 1
  908. to_compile.append(dst)
  909. return dst
  910. if not src.endswith('.pth'):
  911. log.warn("WARNING: can't process %s", src)
  912. return None
  913. # extract, tracking .pyd/.dll->native_libs and .py -> to_compile
  914. unpack_archive(dist_filename, egg_tmp, process)
  915. stubs = []
  916. for res in native_libs:
  917. if res.lower().endswith('.pyd'): # create stubs for .pyd's
  918. parts = res.split('/')
  919. resource = parts[-1]
  920. parts[-1] = bdist_egg.strip_module(parts[-1]) + '.py'
  921. pyfile = os.path.join(egg_tmp, *parts)
  922. to_compile.append(pyfile)
  923. stubs.append(pyfile)
  924. bdist_egg.write_stub(resource, pyfile)
  925. self.byte_compile(to_compile) # compile .py's
  926. bdist_egg.write_safety_flag(
  927. os.path.join(egg_tmp, 'EGG-INFO'),
  928. bdist_egg.analyze_egg(egg_tmp, stubs)) # write zip-safety flag
  929. for name in 'top_level', 'native_libs':
  930. if locals()[name]:
  931. txt = os.path.join(egg_tmp, 'EGG-INFO', name + '.txt')
  932. if not os.path.exists(txt):
  933. f = open(txt, 'w')
  934. f.write('\n'.join(locals()[name]) + '\n')
  935. f.close()
  936. def install_wheel(self, wheel_path, tmpdir):
  937. wheel = Wheel(wheel_path)
  938. assert wheel.is_compatible()
  939. destination = os.path.join(self.install_dir, wheel.egg_name())
  940. destination = os.path.abspath(destination)
  941. if not self.dry_run:
  942. ensure_directory(destination)
  943. if os.path.isdir(destination) and not os.path.islink(destination):
  944. dir_util.remove_tree(destination, dry_run=self.dry_run)
  945. elif os.path.exists(destination):
  946. self.execute(
  947. os.unlink,
  948. (destination,),
  949. "Removing " + destination,
  950. )
  951. try:
  952. self.execute(
  953. wheel.install_as_egg,
  954. (destination,),
  955. ("Installing %s to %s") % (
  956. os.path.basename(wheel_path),
  957. os.path.dirname(destination)
  958. ),
  959. )
  960. finally:
  961. update_dist_caches(destination, fix_zipimporter_caches=False)
  962. self.add_output(destination)
  963. return self.egg_distribution(destination)
  964. __mv_warning = textwrap.dedent("""
  965. Because this distribution was installed --multi-version, before you can
  966. import modules from this package in an application, you will need to
  967. 'import pkg_resources' and then use a 'require()' call similar to one of
  968. these examples, in order to select the desired version:
  969. pkg_resources.require("%(name)s") # latest installed version
  970. pkg_resources.require("%(name)s==%(version)s") # this exact version
  971. pkg_resources.require("%(name)s>=%(version)s") # this version or higher
  972. """).lstrip() # noqa
  973. __id_warning = textwrap.dedent("""
  974. Note also that the installation directory must be on sys.path at runtime for
  975. this to work. (e.g. by being the application's script directory, by being on
  976. PYTHONPATH, or by being added to sys.path by your code.)
  977. """) # noqa
  978. def installation_report(self, req, dist, what="Installed"):
  979. """Helpful installation message for display to package users"""
  980. msg = "\n%(what)s %(eggloc)s%(extras)s"
  981. if self.multi_version and not self.no_report:
  982. msg += '\n' + self.__mv_warning
  983. if self.install_dir not in map(normalize_path, sys.path):
  984. msg += '\n' + self.__id_warning
  985. eggloc = dist.location
  986. name = dist.project_name
  987. version = dist.version
  988. extras = '' # TODO: self.report_extras(req, dist)
  989. return msg % locals()
  990. __editable_msg = textwrap.dedent("""
  991. Extracted editable version of %(spec)s to %(dirname)s
  992. If it uses setuptools in its setup script, you can activate it in
  993. "development" mode by going to that directory and running::
  994. %(python)s setup.py develop
  995. See the setuptools documentation for the "develop" command for more info.
  996. """).lstrip() # noqa
  997. def report_editable(self, spec, setup_script):
  998. dirname = os.path.dirname(setup_script)
  999. python = sys.executable
  1000. return '\n' + self.__editable_msg % locals()
  1001. def run_setup(self, setup_script, setup_base, args):
  1002. sys.modules.setdefault('distutils.command.bdist_egg', bdist_egg)
  1003. sys.modules.setdefault('distutils.command.egg_info', egg_info)
  1004. args = list(args)
  1005. if self.verbose > 2:
  1006. v = 'v' * (self.verbose - 1)
  1007. args.insert(0, '-' + v)
  1008. elif self.verbose < 2:
  1009. args.insert(0, '-q')
  1010. if self.dry_run:
  1011. args.insert(0, '-n')
  1012. log.info(
  1013. "Running %s %s", setup_script[len(setup_base) + 1:], ' '.join(args)
  1014. )
  1015. try:
  1016. run_setup(setup_script, args)
  1017. except SystemExit as v:
  1018. raise DistutilsError("Setup script exited with %s" % (v.args[0],))
  1019. def build_and_install(self, setup_script, setup_base):
  1020. args = ['bdist_egg', '--dist-dir']
  1021. dist_dir = tempfile.mkdtemp(
  1022. prefix='egg-dist-tmp-', dir=os.path.dirname(setup_script)
  1023. )
  1024. try:
  1025. self._set_fetcher_options(os.path.dirname(setup_script))
  1026. args.append(dist_dir)
  1027. self.run_setup(setup_script, setup_base, args)
  1028. all_eggs = Environment([dist_dir])
  1029. eggs = []
  1030. for key in all_eggs:
  1031. for dist in all_eggs[key]:
  1032. eggs.append(self.install_egg(dist.location, setup_base))
  1033. if not eggs and not self.dry_run:
  1034. log.warn("No eggs found in %s (setup script problem?)",
  1035. dist_dir)
  1036. return eggs
  1037. finally:
  1038. rmtree(dist_dir)
  1039. log.set_verbosity(self.verbose) # restore our log verbosity
  1040. def _set_fetcher_options(self, base):
  1041. """
  1042. When easy_install is about to run bdist_egg on a source dist, that
  1043. source dist might have 'setup_requires' directives, requiring
  1044. additional fetching. Ensure the fetcher options given to easy_install
  1045. are available to that command as well.
  1046. """
  1047. # find the fetch options from easy_install and write them out
  1048. # to the setup.cfg file.
  1049. ei_opts = self.distribution.get_option_dict('easy_install').copy()
  1050. fetch_directives = (
  1051. 'find_links', 'site_dirs', 'index_url', 'optimize', 'allow_hosts',
  1052. )
  1053. fetch_options = {}
  1054. for key, val in ei_opts.items():
  1055. if key not in fetch_directives:
  1056. continue
  1057. fetch_options[key.replace('_', '-')] = val[1]
  1058. # create a settings dictionary suitable for `edit_config`
  1059. settings = dict(easy_install=fetch_options)
  1060. cfg_filename = os.path.join(base, 'setup.cfg')
  1061. setopt.edit_config(cfg_filename, settings)
  1062. def update_pth(self, dist):
  1063. if self.pth_file is None:
  1064. return
  1065. for d in self.pth_file[dist.key]: # drop old entries
  1066. if self.multi_version or d.location != dist.location:
  1067. log.info("Removing %s from easy-install.pth file", d)
  1068. self.pth_file.remove(d)
  1069. if d.location in self.shadow_path:
  1070. self.shadow_path.remove(d.location)
  1071. if not self.multi_version:
  1072. if dist.location in self.pth_file.paths:
  1073. log.info(
  1074. "%s is already the active version in easy-install.pth",
  1075. dist,
  1076. )
  1077. else:
  1078. log.info("Adding %s to easy-install.pth file", dist)
  1079. self.pth_file.add(dist) # add new entry
  1080. if dist.location not in self.shadow_path:
  1081. self.shadow_path.append(dist.location)
  1082. if not self.dry_run:
  1083. self.pth_file.save()
  1084. if dist.key == 'setuptools':
  1085. # Ensure that setuptools itself never becomes unavailable!
  1086. # XXX should this check for latest version?
  1087. filename = os.path.join(self.install_dir, 'setuptools.pth')
  1088. if os.path.islink(filename):
  1089. os.unlink(filename)
  1090. f = open(filename, 'wt')
  1091. f.write(self.pth_file.make_relative(dist.location) + '\n')
  1092. f.close()
  1093. def unpack_progress(self, src, dst):
  1094. # Progress filter for unpacking
  1095. log.debug("Unpacking %s to %s", src, dst)
  1096. return dst # only unpack-and-compile skips files for dry run
  1097. def unpack_and_compile(self, egg_path, destination):
  1098. to_compile = []
  1099. to_chmod = []
  1100. def pf(src, dst):
  1101. if dst.endswith('.py') and not src.startswith('EGG-INFO/'):
  1102. to_compile.append(dst)
  1103. elif dst.endswith('.dll') or dst.endswith('.so'):
  1104. to_chmod.append(dst)
  1105. self.unpack_progress(src, dst)
  1106. return not self.dry_run and dst or None
  1107. unpack_archive(egg_path, destination, pf)
  1108. self.byte_compile(to_compile)
  1109. if not self.dry_run:
  1110. for f in to_chmod:
  1111. mode = ((os.stat(f)[stat.ST_MODE]) | 0o555) & 0o7755
  1112. chmod(f, mode)
  1113. def byte_compile(self, to_compile):
  1114. if sys.dont_write_bytecode:
  1115. return
  1116. from distutils.util import byte_compile
  1117. try:
  1118. # try to make the byte compile messages quieter
  1119. log.set_verbosity(self.verbose - 1)
  1120. byte_compile(to_compile, optimize=0, force=1, dry_run=self.dry_run)
  1121. if self.optimize:
  1122. byte_compile(
  1123. to_compile, optimize=self.optimize, force=1,
  1124. dry_run=self.dry_run,
  1125. )
  1126. finally:
  1127. log.set_verbosity(self.verbose) # restore original verbosity
  1128. __no_default_msg = textwrap.dedent("""
  1129. bad install directory or PYTHONPATH
  1130. You are attempting to install a package to a directory that is not
  1131. on PYTHONPATH and which Python does not read ".pth" files from. The
  1132. installation directory you specified (via --install-dir, --prefix, or
  1133. the distutils default setting) was:
  1134. %s
  1135. and your PYTHONPATH environment variable currently contains:
  1136. %r
  1137. Here are some of your options for correcting the problem:
  1138. * You can choose a different installation directory, i.e., one that is
  1139. on PYTHONPATH or supports .pth files
  1140. * You can add the installation directory to the PYTHONPATH environment
  1141. variable. (It must then also be on PYTHONPATH whenever you run
  1142. Python and want to use the package(s) you are installing.)
  1143. * You can set up the installation directory to support ".pth" files by
  1144. using one of the approaches described here:
  1145. https://setuptools.readthedocs.io/en/latest/easy_install.html#custom-installation-locations
  1146. Please make the appropriate changes for your system and try again.
  1147. """).strip()
  1148. def install_site_py(self):
  1149. """Make sure there's a site.py in the target dir, if needed"""
  1150. if self.sitepy_installed:
  1151. return # already did it, or don't need to
  1152. sitepy = os.path.join(self.install_dir, "site.py")
  1153. source = resource_string("setuptools", "site-patch.py")
  1154. source = source.decode('utf-8')
  1155. current = ""
  1156. if os.path.exists(sitepy):
  1157. log.debug("Checking existing site.py in %s", self.install_dir)
  1158. with io.open(sitepy) as strm:
  1159. current = strm.read()
  1160. if not current.startswith('def __boot():'):
  1161. raise DistutilsError(
  1162. "%s is not a setuptools-generated site.py; please"
  1163. " remove it." % sitepy
  1164. )
  1165. if current != source:
  1166. log.info("Creating %s", sitepy)
  1167. if not self.dry_run:
  1168. ensure_directory(sitepy)
  1169. with io.open(sitepy, 'w', encoding='utf-8') as strm:
  1170. strm.write(source)
  1171. self.byte_compile([sitepy])
  1172. self.sitepy_installed = True
  1173. def create_home_path(self):
  1174. """Create directories under ~."""
  1175. if not self.user:
  1176. return
  1177. home = convert_path(os.path.expanduser("~"))
  1178. for name, path in six.iteritems(self.config_vars):
  1179. if path.startswith(home) and not os.path.isdir(path):
  1180. self.debug_print("os.makedirs('%s', 0o700)" % path)
  1181. os.makedirs(path, 0o700)
  1182. INSTALL_SCHEMES = dict(
  1183. posix=dict(
  1184. install_dir='$base/lib/python$py_version_short/site-packages',
  1185. script_dir='$base/bin',
  1186. ),
  1187. )
  1188. DEFAULT_SCHEME = dict(
  1189. install_dir='$base/Lib/site-packages',
  1190. script_dir='$base/Scripts',
  1191. )
  1192. def _expand(self, *attrs):
  1193. config_vars = self.get_finalized_command('install').config_vars
  1194. if self.prefix:
  1195. # Set default install_dir/scripts from --prefix
  1196. config_vars = config_vars.copy()
  1197. config_vars['base'] = self.prefix
  1198. scheme = self.INSTALL_SCHEMES.get(os.name, self.DEFAULT_SCHEME)
  1199. for attr, val in scheme.items():
  1200. if getattr(self, attr, None) is None:
  1201. setattr(self, attr, val)
  1202. from distutils.util import subst_vars
  1203. for attr in attrs:
  1204. val = getattr(self, attr)
  1205. if val is not None:
  1206. val = subst_vars(val, config_vars)
  1207. if os.name == 'posix':
  1208. val = os.path.expanduser(val)
  1209. setattr(self, attr, val)
  1210. def _pythonpath():
  1211. items = os.environ.get('PYTHONPATH', '').split(os.pathsep)
  1212. return filter(None, items)
  1213. def get_site_dirs():
  1214. """
  1215. Return a list of 'site' dirs
  1216. """
  1217. sitedirs = []
  1218. # start with PYTHONPATH
  1219. sitedirs.extend(_pythonpath())
  1220. prefixes = [sys.prefix]
  1221. if sys.exec_prefix != sys.prefix:
  1222. prefixes.append(sys.exec_prefix)
  1223. for prefix in prefixes:
  1224. if prefix:
  1225. if sys.platform in ('os2emx', 'riscos'):
  1226. sitedirs.append(os.path.join(prefix, "Lib", "site-packages"))
  1227. elif os.sep == '/':
  1228. sitedirs.extend([
  1229. os.path.join(
  1230. prefix,
  1231. "lib",
  1232. "python{}.{}".format(*sys.version_info),
  1233. "site-packages",
  1234. ),
  1235. os.path.join(prefix, "lib", "site-python"),
  1236. ])
  1237. else:
  1238. sitedirs.extend([
  1239. prefix,
  1240. os.path.join(prefix, "lib", "site-packages"),
  1241. ])
  1242. if sys.platform == 'darwin':
  1243. # for framework builds *only* we add the standard Apple
  1244. # locations. Currently only per-user, but /Library and
  1245. # /Network/Library could be added too
  1246. if 'Python.framework' in prefix:
  1247. home = os.environ.get('HOME')
  1248. if home:
  1249. home_sp = os.path.join(
  1250. home,
  1251. 'Library',
  1252. 'Python',
  1253. '{}.{}'.format(*sys.version_info),
  1254. 'site-packages',
  1255. )
  1256. sitedirs.append(home_sp)
  1257. lib_paths = get_path('purelib'), get_path('platlib')
  1258. for site_lib in lib_paths:
  1259. if site_lib not in sitedirs:
  1260. sitedirs.append(site_lib)
  1261. if site.ENABLE_USER_SITE:
  1262. sitedirs.append(site.USER_SITE)
  1263. try:
  1264. sitedirs.extend(site.getsitepackages())
  1265. except AttributeError:
  1266. pass
  1267. sitedirs = list(map(normalize_path, sitedirs))
  1268. return sitedirs
  1269. def expand_paths(inputs):
  1270. """Yield sys.path directories that might contain "old-style" packages"""
  1271. seen = {}
  1272. for dirname in inputs:
  1273. dirname = normalize_path(dirname)
  1274. if dirname in seen:
  1275. continue
  1276. seen[dirname] = 1
  1277. if not os.path.isdir(dirname):
  1278. continue
  1279. files = os.listdir(dirname)
  1280. yield dirname, files
  1281. for name in files:
  1282. if not name.endswith('.pth'):
  1283. # We only care about the .pth files
  1284. continue
  1285. if name in ('easy-install.pth', 'setuptools.pth'):
  1286. # Ignore .pth files that we control
  1287. continue
  1288. # Read the .pth file
  1289. f = open(os.path.join(dirname, name))
  1290. lines = list(yield_lines(f))
  1291. f.close()
  1292. # Yield existing non-dupe, non-import directory lines from it
  1293. for line in lines:
  1294. if not line.startswith("import"):
  1295. line = normalize_path(line.rstrip())
  1296. if line not in seen:
  1297. seen[line] = 1
  1298. if not os.path.isdir(line):
  1299. continue
  1300. yield line, os.listdir(line)
  1301. def extract_wininst_cfg(dist_filename):
  1302. """Extract configuration data from a bdist_wininst .exe
  1303. Returns a configparser.RawConfigParser, or None
  1304. """
  1305. f = open(dist_filename, 'rb')
  1306. try:
  1307. endrec = zipfile._EndRecData(f)
  1308. if endrec is None:
  1309. return None
  1310. prepended = (endrec[9] - endrec[5]) - endrec[6]
  1311. if prepended < 12: # no wininst data here
  1312. return None
  1313. f.seek(prepended - 12)
  1314. tag, cfglen, bmlen = struct.unpack("<iii", f.read(12))
  1315. if tag not in (0x1234567A, 0x1234567B):
  1316. return None # not a valid tag
  1317. f.seek(prepended - (12 + cfglen))
  1318. init = {'version': '', 'target_version': ''}
  1319. cfg = configparser.RawConfigParser(init)
  1320. try:
  1321. part = f.read(cfglen)
  1322. # Read up to the first null byte.
  1323. config = part.split(b'\0', 1)[0]
  1324. # Now the config is in bytes, but for RawConfigParser, it should
  1325. # be text, so decode it.
  1326. config = config.decode(sys.getfilesystemencoding())
  1327. cfg.readfp(six.StringIO(config))
  1328. except configparser.Error:
  1329. return None
  1330. if not cfg.has_section('metadata') or not cfg.has_section('Setup'):
  1331. return None
  1332. return cfg
  1333. finally:
  1334. f.close()
  1335. def get_exe_prefixes(exe_filename):
  1336. """Get exe->egg path translations for a given .exe file"""
  1337. prefixes = [
  1338. ('PURELIB/', ''),
  1339. ('PLATLIB/pywin32_system32', ''),
  1340. ('PLATLIB/', ''),
  1341. ('SCRIPTS/', 'EGG-INFO/scripts/'),
  1342. ('DATA/lib/site-packages', ''),
  1343. ]
  1344. z = zipfile.ZipFile(exe_filename)
  1345. try:
  1346. for info in z.infolist():
  1347. name = info.filename
  1348. parts = name.split('/')
  1349. if len(parts) == 3 and parts[2] == 'PKG-INFO':
  1350. if parts[1].endswith('.egg-info'):
  1351. prefixes.insert(0, ('/'.join(parts[:2]), 'EGG-INFO/'))
  1352. break
  1353. if len(parts) != 2 or not name.endswith('.pth'):
  1354. continue
  1355. if name.endswith('-nspkg.pth'):
  1356. continue
  1357. if parts[0].upper() in ('PURELIB', 'PLATLIB'):
  1358. contents = z.read(name)
  1359. if not six.PY2:
  1360. contents = contents.decode()
  1361. for pth in yield_lines(contents):
  1362. pth = pth.strip().replace('\\', '/')
  1363. if not pth.startswith('import'):
  1364. prefixes.append((('%s/%s/' % (parts[0], pth)), ''))
  1365. finally:
  1366. z.close()
  1367. prefixes = [(x.lower(), y) for x, y in prefixes]
  1368. prefixes.sort()
  1369. prefixes.reverse()
  1370. return prefixes
  1371. class PthDistributions(Environment):
  1372. """A .pth file with Distribution paths in it"""
  1373. dirty = False
  1374. def __init__(self, filename, sitedirs=()):
  1375. self.filename = filename
  1376. self.sitedirs = list(map(normalize_path, sitedirs))
  1377. self.basedir = normalize_path(os.path.dirname(self.filename))
  1378. self._load()
  1379. Environment.__init__(self, [], None, None)
  1380. for path in yield_lines(self.paths):
  1381. list(map(self.add, find_distributions(path, True)))
  1382. def _load(self):
  1383. self.paths = []
  1384. saw_import = False
  1385. seen = dict.fromkeys(self.sitedirs)
  1386. if os.path.isfile(self.filename):
  1387. f = open(self.filename, 'rt')
  1388. for line in f:
  1389. if line.startswith('import'):
  1390. saw_import = True
  1391. continue
  1392. path = line.rstrip()
  1393. self.paths.append(path)
  1394. if not path.strip() or path.strip().startswith('#'):
  1395. continue
  1396. # skip non-existent paths, in case somebody deleted a package
  1397. # manually, and duplicate paths as well
  1398. path = self.paths[-1] = normalize_path(
  1399. os.path.join(self.basedir, path)
  1400. )
  1401. if not os.path.exists(path) or path in seen:
  1402. self.paths.pop() # skip it
  1403. self.dirty = True # we cleaned up, so we're dirty now :)
  1404. continue
  1405. seen[path] = 1
  1406. f.close()
  1407. if self.paths and not saw_import:
  1408. self.dirty = True # ensure anything we touch has import wrappers
  1409. while self.paths and not self.paths[-1].strip():
  1410. self.paths.pop()
  1411. def save(self):
  1412. """Write changed .pth file back to disk"""
  1413. if not self.dirty:
  1414. return
  1415. rel_paths = list(map(self.make_relative, self.paths))
  1416. if rel_paths:
  1417. log.debug("Saving %s", self.filename)
  1418. lines = self._wrap_lines(rel_paths)
  1419. data = '\n'.join(lines) + '\n'
  1420. if os.path.islink(self.filename):
  1421. os.unlink(self.filename)
  1422. with open(self.filename, 'wt') as f:
  1423. f.write(data)
  1424. elif os.path.exists(self.filename):
  1425. log.debug("Deleting empty %s", self.filename)
  1426. os.unlink(self.filename)
  1427. self.dirty = False
  1428. @staticmethod
  1429. def _wrap_lines(lines):
  1430. return lines
  1431. def add(self, dist):
  1432. """Add `dist` to the distribution map"""
  1433. new_path = (
  1434. dist.location not in self.paths and (
  1435. dist.location not in self.sitedirs or
  1436. # account for '.' being in PYTHONPATH
  1437. dist.location == os.getcwd()
  1438. )
  1439. )
  1440. if new_path:
  1441. self.paths.append(dist.location)
  1442. self.dirty = True
  1443. Environment.add(self, dist)
  1444. def remove(self, dist):
  1445. """Remove `dist` from the distribution map"""
  1446. while dist.location in self.paths:
  1447. self.paths.remove(dist.location)
  1448. self.dirty = True
  1449. Environment.remove(self, dist)
  1450. def make_relative(self, path):
  1451. npath, last = os.path.split(normalize_path(path))
  1452. baselen = len(self.basedir)
  1453. parts = [last]
  1454. sep = os.altsep == '/' and '/' or os.sep
  1455. while len(npath) >= baselen:
  1456. if npath == self.basedir:
  1457. parts.append(os.curdir)
  1458. parts.reverse()
  1459. return sep.join(parts)
  1460. npath, last = os.path.split(npath)
  1461. parts.append(last)
  1462. else:
  1463. return path
  1464. class RewritePthDistributions(PthDistributions):
  1465. @classmethod
  1466. def _wrap_lines(cls, lines):
  1467. yield cls.prelude
  1468. for line in lines:
  1469. yield line
  1470. yield cls.postlude
  1471. prelude = _one_liner("""
  1472. import sys
  1473. sys.__plen = len(sys.path)
  1474. """)
  1475. postlude = _one_liner("""
  1476. import sys
  1477. new = sys.path[sys.__plen:]
  1478. del sys.path[sys.__plen:]
  1479. p = getattr(sys, '__egginsert', 0)
  1480. sys.path[p:p] = new
  1481. sys.__egginsert = p + len(new)
  1482. """)
  1483. if os.environ.get('SETUPTOOLS_SYS_PATH_TECHNIQUE', 'raw') == 'rewrite':
  1484. PthDistributions = RewritePthDistributions
  1485. def _first_line_re():
  1486. """
  1487. Return a regular expression based on first_line_re suitable for matching
  1488. strings.
  1489. """
  1490. if isinstance(first_line_re.pattern, str):
  1491. return first_line_re
  1492. # first_line_re in Python >=3.1.4 and >=3.2.1 is a bytes pattern.
  1493. return re.compile(first_line_re.pattern.decode())
  1494. def auto_chmod(func, arg, exc):
  1495. if func in [os.unlink, os.remove] and os.name == 'nt':
  1496. chmod(arg, stat.S_IWRITE)
  1497. return func(arg)
  1498. et, ev, _ = sys.exc_info()
  1499. six.reraise(et, (ev[0], ev[1] + (" %s %s" % (func, arg))))
  1500. def update_dist_caches(dist_path, fix_zipimporter_caches):
  1501. """
  1502. Fix any globally cached `dist_path` related data
  1503. `dist_path` should be a path of a newly installed egg distribution (zipped
  1504. or unzipped).
  1505. sys.path_importer_cache contains finder objects that have been cached when
  1506. importing data from the original distribution. Any such finders need to be
  1507. cleared since the replacement distribution might be packaged differently,
  1508. e.g. a zipped egg distribution might get replaced with an unzipped egg
  1509. folder or vice versa. Having the old finders cached may then cause Python
  1510. to attempt loading modules from the replacement distribution using an
  1511. incorrect loader.
  1512. zipimport.zipimporter objects are Python loaders charged with importing
  1513. data packaged inside zip archives. If stale loaders referencing the
  1514. original distribution, are left behind, they can fail to load modules from
  1515. the replacement distribution. E.g. if an old zipimport.zipimporter instance
  1516. is used to load data from a new zipped egg archive, it may cause the
  1517. operation to attempt to locate the requested data in the wrong location -
  1518. one indicated by the original distribution's zip archive directory
  1519. information. Such an operation may then fail outright, e.g. report having
  1520. read a 'bad local file header', or even worse, it may fail silently &
  1521. return invalid data.
  1522. zipimport._zip_directory_cache contains cached zip archive directory
  1523. information for all existing zipimport.zipimporter instances and all such
  1524. instances connected to the same archive share the same cached directory
  1525. information.
  1526. If asked, and the underlying Python implementation allows it, we can fix
  1527. all existing zipimport.zipimporter instances instead of having to track
  1528. them down and remove them one by one, by updating their shared cached zip
  1529. archive directory information. This, of course, assumes that the
  1530. replacement distribution is packaged as a zipped egg.
  1531. If not asked to fix existing zipimport.zipimporter instances, we still do
  1532. our best to clear any remaining zipimport.zipimporter related cached data
  1533. that might somehow later get used when attempting to load data from the new
  1534. distribution and thus cause such load operations to fail. Note that when
  1535. tracking down such remaining stale data, we can not catch every conceivable
  1536. usage from here, and we clear only those that we know of and have found to
  1537. cause problems if left alive. Any remaining caches should be updated by
  1538. whomever is in charge of maintaining them, i.e. they should be ready to
  1539. handle us replacing their zip archives with new distributions at runtime.
  1540. """
  1541. # There are several other known sources of stale zipimport.zipimporter
  1542. # instances that we do not clear here, but might if ever given a reason to
  1543. # do so:
  1544. # * Global setuptools pkg_resources.working_set (a.k.a. 'master working
  1545. # set') may contain distributions which may in turn contain their
  1546. # zipimport.zipimporter loaders.
  1547. # * Several zipimport.zipimporter loaders held by local variables further
  1548. # up the function call stack when running the setuptools installation.
  1549. # * Already loaded modules may have their __loader__ attribute set to the
  1550. # exact loader instance used when importing them. Python 3.4 docs state
  1551. # that this information is intended mostly for introspection and so is
  1552. # not expected to cause us problems.
  1553. normalized_path = normalize_path(dist_path)
  1554. _uncache(normalized_path, sys.path_importer_cache)
  1555. if fix_zipimporter_caches:
  1556. _replace_zip_directory_cache_data(normalized_path)
  1557. else:
  1558. # Here, even though we do not want to fix existing and now stale
  1559. # zipimporter cache information, we still want to remove it. Related to
  1560. # Python's zip archive directory information cache, we clear each of
  1561. # its stale entries in two phases:
  1562. # 1. Clear the entry so attempting to access zip archive information
  1563. # via any existing stale zipimport.zipimporter instances fails.
  1564. # 2. Remove the entry from the cache so any newly constructed
  1565. # zipimport.zipimporter instances do not end up using old stale
  1566. # zip archive directory information.
  1567. # This whole stale data removal step does not seem strictly necessary,
  1568. # but has been left in because it was done before we started replacing
  1569. # the zip archive directory information cache content if possible, and
  1570. # there are no relevant unit tests that we can depend on to tell us if
  1571. # this is really needed.
  1572. _remove_and_clear_zip_directory_cache_data(normalized_path)
  1573. def _collect_zipimporter_cache_entries(normalized_path, cache):
  1574. """
  1575. Return zipimporter cache entry keys related to a given normalized path.
  1576. Alternative path spellings (e.g. those using different character case or
  1577. those using alternative path separators) related to the same path are
  1578. included. Any sub-path entries are included as well, i.e. those
  1579. corresponding to zip archives embedded in other zip archives.
  1580. """
  1581. result = []
  1582. prefix_len = len(normalized_path)
  1583. for p in cache:
  1584. np = normalize_path(p)
  1585. if (np.startswith(normalized_path) and
  1586. np[prefix_len:prefix_len + 1] in (os.sep, '')):
  1587. result.append(p)
  1588. return result
  1589. def _update_zipimporter_cache(normalized_path, cache, updater=None):
  1590. """
  1591. Update zipimporter cache data for a given normalized path.
  1592. Any sub-path entries are processed as well, i.e. those corresponding to zip
  1593. archives embedded in other zip archives.
  1594. Given updater is a callable taking a cache entry key and the original entry
  1595. (after already removing the entry from the cache), and expected to update
  1596. the entry and possibly return a new one to be inserted in its place.
  1597. Returning None indicates that the entry should not be replaced with a new
  1598. one. If no updater is given, the cache entries are simply removed without
  1599. any additional processing, the same as if the updater simply returned None.
  1600. """
  1601. for p in _collect_zipimporter_cache_entries(normalized_path, cache):
  1602. # N.B. pypy's custom zipimport._zip_directory_cache implementation does
  1603. # not support the complete dict interface:
  1604. # * Does not support item assignment, thus not allowing this function
  1605. # to be used only for removing existing cache entries.
  1606. # * Does not support the dict.pop() method, forcing us to use the
  1607. # get/del patterns instead. For more detailed information see the
  1608. # following links:
  1609. # https://github.com/pypa/setuptools/issues/202#issuecomment-202913420
  1610. # http://bit.ly/2h9itJX
  1611. old_entry = cache[p]
  1612. del cache[p]
  1613. new_entry = updater and updater(p, old_entry)
  1614. if new_entry is not None:
  1615. cache[p] = new_entry
  1616. def _uncache(normalized_path, cache):
  1617. _update_zipimporter_cache(normalized_path, cache)
  1618. def _remove_and_clear_zip_directory_cache_data(normalized_path):
  1619. def clear_and_remove_cached_zip_archive_directory_data(path, old_entry):
  1620. old_entry.clear()
  1621. _update_zipimporter_cache(
  1622. normalized_path, zipimport._zip_directory_cache,
  1623. updater=clear_and_remove_cached_zip_archive_directory_data)
  1624. # PyPy Python implementation does not allow directly writing to the
  1625. # zipimport._zip_directory_cache and so prevents us from attempting to correct
  1626. # its content. The best we can do there is clear the problematic cache content
  1627. # and have PyPy repopulate it as needed. The downside is that if there are any
  1628. # stale zipimport.zipimporter instances laying around, attempting to use them
  1629. # will fail due to not having its zip archive directory information available
  1630. # instead of being automatically corrected to use the new correct zip archive
  1631. # directory information.
  1632. if '__pypy__' in sys.builtin_module_names:
  1633. _replace_zip_directory_cache_data = \
  1634. _remove_and_clear_zip_directory_cache_data
  1635. else:
  1636. def _replace_zip_directory_cache_data(normalized_path):
  1637. def replace_cached_zip_archive_directory_data(path, old_entry):
  1638. # N.B. In theory, we could load the zip directory information just
  1639. # once for all updated path spellings, and then copy it locally and
  1640. # update its contained path strings to contain the correct
  1641. # spelling, but that seems like a way too invasive move (this cache
  1642. # structure is not officially documented anywhere and could in
  1643. # theory change with new Python releases) for no significant
  1644. # benefit.
  1645. old_entry.clear()
  1646. zipimport.zipimporter(path)
  1647. old_entry.update(zipimport._zip_directory_cache[path])
  1648. return old_entry
  1649. _update_zipimporter_cache(
  1650. normalized_path, zipimport._zip_directory_cache,
  1651. updater=replace_cached_zip_archive_directory_data)
  1652. def is_python(text, filename='<string>'):
  1653. "Is this string a valid Python script?"
  1654. try:
  1655. compile(text, filename, 'exec')
  1656. except (SyntaxError, TypeError):
  1657. return False
  1658. else:
  1659. return True
  1660. def is_sh(executable):
  1661. """Determine if the specified executable is a .sh (contains a #! line)"""
  1662. try:
  1663. with io.open(executable, encoding='latin-1') as fp:
  1664. magic = fp.read(2)
  1665. except (OSError, IOError):
  1666. return executable
  1667. return magic == '#!'
  1668. def nt_quote_arg(arg):
  1669. """Quote a command line argument according to Windows parsing rules"""
  1670. return subprocess.list2cmdline([arg])
  1671. def is_python_script(script_text, filename):
  1672. """Is this text, as a whole, a Python script? (as opposed to shell/bat/etc.
  1673. """
  1674. if filename.endswith('.py') or filename.endswith('.pyw'):
  1675. return True # extension says it's Python
  1676. if is_python(script_text, filename):
  1677. return True # it's syntactically valid Python
  1678. if script_text.startswith('#!'):
  1679. # It begins with a '#!' line, so check if 'python' is in it somewhere
  1680. return 'python' in script_text.splitlines()[0].lower()
  1681. return False # Not any Python I can recognize
  1682. try:
  1683. from os import chmod as _chmod
  1684. except ImportError:
  1685. # Jython compatibility
  1686. def _chmod(*args):
  1687. pass
  1688. def chmod(path, mode):
  1689. log.debug("changing mode of %s to %o", path, mode)
  1690. try:
  1691. _chmod(path, mode)
  1692. except os.error as e:
  1693. log.debug("chmod failed: %s", e)
  1694. class CommandSpec(list):
  1695. """
  1696. A command spec for a #! header, specified as a list of arguments akin to
  1697. those passed to Popen.
  1698. """
  1699. options = []
  1700. split_args = dict()
  1701. @classmethod
  1702. def best(cls):
  1703. """
  1704. Choose the best CommandSpec class based on environmental conditions.
  1705. """
  1706. return cls
  1707. @classmethod
  1708. def _sys_executable(cls):
  1709. _default = os.path.normpath(sys.executable)
  1710. return os.environ.get('__PYVENV_LAUNCHER__', _default)
  1711. @classmethod
  1712. def from_param(cls, param):
  1713. """
  1714. Construct a CommandSpec from a parameter to build_scripts, which may
  1715. be None.
  1716. """
  1717. if isinstance(param, cls):
  1718. return param
  1719. if isinstance(param, list):
  1720. return cls(param)
  1721. if param is None:
  1722. return cls.from_environment()
  1723. # otherwise, assume it's a string.
  1724. return cls.from_string(param)
  1725. @classmethod
  1726. def from_environment(cls):
  1727. return cls([cls._sys_executable()])
  1728. @classmethod
  1729. def from_string(cls, string):
  1730. """
  1731. Construct a command spec from a simple string representing a command
  1732. line parseable by shlex.split.
  1733. """
  1734. items = shlex.split(string, **cls.split_args)
  1735. return cls(items)
  1736. def install_options(self, script_text):
  1737. self.options = shlex.split(self._extract_options(script_text))
  1738. cmdline = subprocess.list2cmdline(self)
  1739. if not isascii(cmdline):
  1740. self.options[:0] = ['-x']
  1741. @staticmethod
  1742. def _extract_options(orig_script):
  1743. """
  1744. Extract any options from the first line of the script.
  1745. """
  1746. first = (orig_script + '\n').splitlines()[0]
  1747. match = _first_line_re().match(first)
  1748. options = match.group(1) or '' if match else ''
  1749. return options.strip()
  1750. def as_header(self):
  1751. return self._render(self + list(self.options))
  1752. @staticmethod
  1753. def _strip_quotes(item):
  1754. _QUOTES = '"\''
  1755. for q in _QUOTES:
  1756. if item.startswith(q) and item.endswith(q):
  1757. return item[1:-1]
  1758. return item
  1759. @staticmethod
  1760. def _render(items):
  1761. cmdline = subprocess.list2cmdline(
  1762. CommandSpec._strip_quotes(item.strip()) for item in items)
  1763. return '#!' + cmdline + '\n'
  1764. # For pbr compat; will be removed in a future version.
  1765. sys_executable = CommandSpec._sys_executable()
  1766. class WindowsCommandSpec(CommandSpec):
  1767. split_args = dict(posix=False)
  1768. class ScriptWriter:
  1769. """
  1770. Encapsulates behavior around writing entry point scripts for console and
  1771. gui apps.
  1772. """
  1773. template = textwrap.dedent(r"""
  1774. # EASY-INSTALL-ENTRY-SCRIPT: %(spec)r,%(group)r,%(name)r
  1775. __requires__ = %(spec)r
  1776. import re
  1777. import sys
  1778. from pkg_resources import load_entry_point
  1779. if __name__ == '__main__':
  1780. sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
  1781. sys.exit(
  1782. load_entry_point(%(spec)r, %(group)r, %(name)r)()
  1783. )
  1784. """).lstrip()
  1785. command_spec_class = CommandSpec
  1786. @classmethod
  1787. def get_script_args(cls, dist, executable=None, wininst=False):
  1788. # for backward compatibility
  1789. warnings.warn("Use get_args", EasyInstallDeprecationWarning)
  1790. writer = (WindowsScriptWriter if wininst else ScriptWriter).best()
  1791. header = cls.get_script_header("", executable, wininst)
  1792. return writer.get_args(dist, header)
  1793. @classmethod
  1794. def get_script_header(cls, script_text, executable=None, wininst=False):
  1795. # for backward compatibility
  1796. warnings.warn(
  1797. "Use get_header", EasyInstallDeprecationWarning, stacklevel=2)
  1798. if wininst:
  1799. executable = "python.exe"
  1800. return cls.get_header(script_text, executable)
  1801. @classmethod
  1802. def get_args(cls, dist, header=None):
  1803. """
  1804. Yield write_script() argument tuples for a distribution's
  1805. console_scripts and gui_scripts entry points.
  1806. """
  1807. if header is None:
  1808. header = cls.get_header()
  1809. spec = str(dist.as_requirement())
  1810. for type_ in 'console', 'gui':
  1811. group = type_ + '_scripts'
  1812. for name, ep in dist.get_entry_map(group).items():
  1813. cls._ensure_safe_name(name)
  1814. script_text = cls.template % locals()
  1815. args = cls._get_script_args(type_, name, header, script_text)
  1816. for res in args:
  1817. yield res
  1818. @staticmethod
  1819. def _ensure_safe_name(name):
  1820. """
  1821. Prevent paths in *_scripts entry point names.
  1822. """
  1823. has_path_sep = re.search(r'[\\/]', name)
  1824. if has_path_sep:
  1825. raise ValueError("Path separators not allowed in script names")
  1826. @classmethod
  1827. def get_writer(cls, force_windows):
  1828. # for backward compatibility
  1829. warnings.warn("Use best", EasyInstallDeprecationWarning)
  1830. return WindowsScriptWriter.best() if force_windows else cls.best()
  1831. @classmethod
  1832. def best(cls):
  1833. """
  1834. Select the best ScriptWriter for this environment.
  1835. """
  1836. if sys.platform == 'win32' or (os.name == 'java' and os._name == 'nt'):
  1837. return WindowsScriptWriter.best()
  1838. else:
  1839. return cls
  1840. @classmethod
  1841. def _get_script_args(cls, type_, name, header, script_text):
  1842. # Simply write the stub with no extension.
  1843. yield (name, header + script_text)
  1844. @classmethod
  1845. def get_header(cls, script_text="", executable=None):
  1846. """Create a #! line, getting options (if any) from script_text"""
  1847. cmd = cls.command_spec_class.best().from_param(executable)
  1848. cmd.install_options(script_text)
  1849. return cmd.as_header()
  1850. class WindowsScriptWriter(ScriptWriter):
  1851. command_spec_class = WindowsCommandSpec
  1852. @classmethod
  1853. def get_writer(cls):
  1854. # for backward compatibility
  1855. warnings.warn("Use best", EasyInstallDeprecationWarning)
  1856. return cls.best()
  1857. @classmethod
  1858. def best(cls):
  1859. """
  1860. Select the best ScriptWriter suitable for Windows
  1861. """
  1862. writer_lookup = dict(
  1863. executable=WindowsExecutableLauncherWriter,
  1864. natural=cls,
  1865. )
  1866. # for compatibility, use the executable launcher by default
  1867. launcher = os.environ.get('SETUPTOOLS_LAUNCHER', 'executable')
  1868. return writer_lookup[launcher]
  1869. @classmethod
  1870. def _get_script_args(cls, type_, name, header, script_text):
  1871. "For Windows, add a .py extension"
  1872. ext = dict(console='.pya', gui='.pyw')[type_]
  1873. if ext not in os.environ['PATHEXT'].lower().split(';'):
  1874. msg = (
  1875. "{ext} not listed in PATHEXT; scripts will not be "
  1876. "recognized as executables."
  1877. ).format(**locals())
  1878. warnings.warn(msg, UserWarning)
  1879. old = ['.pya', '.py', '-script.py', '.pyc', '.pyo', '.pyw', '.exe']
  1880. old.remove(ext)
  1881. header = cls._adjust_header(type_, header)
  1882. blockers = [name + x for x in old]
  1883. yield name + ext, header + script_text, 't', blockers
  1884. @classmethod
  1885. def _adjust_header(cls, type_, orig_header):
  1886. """
  1887. Make sure 'pythonw' is used for gui and and 'python' is used for
  1888. console (regardless of what sys.executable is).
  1889. """
  1890. pattern = 'pythonw.exe'
  1891. repl = 'python.exe'
  1892. if type_ == 'gui':
  1893. pattern, repl = repl, pattern
  1894. pattern_ob = re.compile(re.escape(pattern), re.IGNORECASE)
  1895. new_header = pattern_ob.sub(string=orig_header, repl=repl)
  1896. return new_header if cls._use_header(new_header) else orig_header
  1897. @staticmethod
  1898. def _use_header(new_header):
  1899. """
  1900. Should _adjust_header use the replaced header?
  1901. On non-windows systems, always use. On
  1902. Windows systems, only use the replaced header if it resolves
  1903. to an executable on the system.
  1904. """
  1905. clean_header = new_header[2:-1].strip('"')
  1906. return sys.platform != 'win32' or find_executable(clean_header)
  1907. class WindowsExecutableLauncherWriter(WindowsScriptWriter):
  1908. @classmethod
  1909. def _get_script_args(cls, type_, name, header, script_text):
  1910. """
  1911. For Windows, add a .py extension and an .exe launcher
  1912. """
  1913. if type_ == 'gui':
  1914. launcher_type = 'gui'
  1915. ext = '-script.pyw'
  1916. old = ['.pyw']
  1917. else:
  1918. launcher_type = 'cli'
  1919. ext = '-script.py'
  1920. old = ['.py', '.pyc', '.pyo']
  1921. hdr = cls._adjust_header(type_, header)
  1922. blockers = [name + x for x in old]
  1923. yield (name + ext, hdr + script_text, 't', blockers)
  1924. yield (
  1925. name + '.exe', get_win_launcher(launcher_type),
  1926. 'b' # write in binary mode
  1927. )
  1928. if not is_64bit():
  1929. # install a manifest for the launcher to prevent Windows
  1930. # from detecting it as an installer (which it will for
  1931. # launchers like easy_install.exe). Consider only
  1932. # adding a manifest for launchers detected as installers.
  1933. # See Distribute #143 for details.
  1934. m_name = name + '.exe.manifest'
  1935. yield (m_name, load_launcher_manifest(name), 't')
  1936. # for backward-compatibility
  1937. get_script_args = ScriptWriter.get_script_args
  1938. get_script_header = ScriptWriter.get_script_header
  1939. def get_win_launcher(type):
  1940. """
  1941. Load the Windows launcher (executable) suitable for launching a script.
  1942. `type` should be either 'cli' or 'gui'
  1943. Returns the executable as a byte string.
  1944. """
  1945. launcher_fn = '%s.exe' % type
  1946. if is_64bit():
  1947. launcher_fn = launcher_fn.replace(".", "-64.")
  1948. else:
  1949. launcher_fn = launcher_fn.replace(".", "-32.")
  1950. return resource_string('setuptools', launcher_fn)
  1951. def load_launcher_manifest(name):
  1952. manifest = pkg_resources.resource_string(__name__, 'launcher manifest.xml')
  1953. if six.PY2:
  1954. return manifest % vars()
  1955. else:
  1956. return manifest.decode('utf-8') % vars()
  1957. def rmtree(path, ignore_errors=False, onerror=auto_chmod):
  1958. return shutil.rmtree(path, ignore_errors, onerror)
  1959. def current_umask():
  1960. tmp = os.umask(0o022)
  1961. os.umask(tmp)
  1962. return tmp
  1963. def bootstrap():
  1964. # This function is called when setuptools*.egg is run using /bin/sh
  1965. import setuptools
  1966. argv0 = os.path.dirname(setuptools.__path__[0])
  1967. sys.argv[0] = argv0
  1968. sys.argv.append(argv0)
  1969. main()
  1970. def main(argv=None, **kw):
  1971. from setuptools import setup
  1972. from setuptools.dist import Distribution
  1973. class DistributionWithoutHelpCommands(Distribution):
  1974. common_usage = ""
  1975. def _show_help(self, *args, **kw):
  1976. with _patch_usage():
  1977. Distribution._show_help(self, *args, **kw)
  1978. if argv is None:
  1979. argv = sys.argv[1:]
  1980. with _patch_usage():
  1981. setup(
  1982. script_args=['-q', 'easy_install', '-v'] + argv,
  1983. script_name=sys.argv[0] or 'easy_install',
  1984. distclass=DistributionWithoutHelpCommands,
  1985. **kw
  1986. )
  1987. @contextlib.contextmanager
  1988. def _patch_usage():
  1989. import distutils.core
  1990. USAGE = textwrap.dedent("""
  1991. usage: %(script)s [options] requirement_or_url ...
  1992. or: %(script)s --help
  1993. """).lstrip()
  1994. def gen_usage(script_name):
  1995. return USAGE % dict(
  1996. script=os.path.basename(script_name),
  1997. )
  1998. saved = distutils.core.gen_usage
  1999. distutils.core.gen_usage = gen_usage
  2000. try:
  2001. yield
  2002. finally:
  2003. distutils.core.gen_usage = saved
  2004. class EasyInstallDeprecationWarning(SetuptoolsDeprecationWarning):
  2005. """
  2006. Warning for EasyInstall deprecations, bypassing suppression.
  2007. """