util.py 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  1. #
  2. # Copyright (C) 2012-2023 The Python Software Foundation.
  3. # See LICENSE.txt and CONTRIBUTORS.txt.
  4. #
  5. import codecs
  6. from collections import deque
  7. import contextlib
  8. import csv
  9. from glob import iglob as std_iglob
  10. import io
  11. import json
  12. import logging
  13. import os
  14. import py_compile
  15. import re
  16. import socket
  17. try:
  18. import ssl
  19. except ImportError: # pragma: no cover
  20. ssl = None
  21. import subprocess
  22. import sys
  23. import tarfile
  24. import tempfile
  25. import textwrap
  26. try:
  27. import threading
  28. except ImportError: # pragma: no cover
  29. import dummy_threading as threading
  30. import time
  31. from . import DistlibException
  32. from .compat import (string_types, text_type, shutil, raw_input, StringIO, cache_from_source, urlopen, urljoin, httplib,
  33. xmlrpclib, HTTPHandler, BaseConfigurator, valid_ident, Container, configparser, URLError, ZipFile,
  34. fsdecode, unquote, urlparse)
  35. logger = logging.getLogger(__name__)
  36. #
  37. # Requirement parsing code as per PEP 508
  38. #
  39. IDENTIFIER = re.compile(r'^([\w\.-]+)\s*')
  40. VERSION_IDENTIFIER = re.compile(r'^([\w\.*+-]+)\s*')
  41. COMPARE_OP = re.compile(r'^(<=?|>=?|={2,3}|[~!]=)\s*')
  42. MARKER_OP = re.compile(r'^((<=?)|(>=?)|={2,3}|[~!]=|in|not\s+in)\s*')
  43. OR = re.compile(r'^or\b\s*')
  44. AND = re.compile(r'^and\b\s*')
  45. NON_SPACE = re.compile(r'(\S+)\s*')
  46. STRING_CHUNK = re.compile(r'([\s\w\.{}()*+#:;,/?!~`@$%^&=|<>\[\]-]+)')
  47. def parse_marker(marker_string):
  48. """
  49. Parse a marker string and return a dictionary containing a marker expression.
  50. The dictionary will contain keys "op", "lhs" and "rhs" for non-terminals in
  51. the expression grammar, or strings. A string contained in quotes is to be
  52. interpreted as a literal string, and a string not contained in quotes is a
  53. variable (such as os_name).
  54. """
  55. def marker_var(remaining):
  56. # either identifier, or literal string
  57. m = IDENTIFIER.match(remaining)
  58. if m:
  59. result = m.groups()[0]
  60. remaining = remaining[m.end():]
  61. elif not remaining:
  62. raise SyntaxError('unexpected end of input')
  63. else:
  64. q = remaining[0]
  65. if q not in '\'"':
  66. raise SyntaxError('invalid expression: %s' % remaining)
  67. oq = '\'"'.replace(q, '')
  68. remaining = remaining[1:]
  69. parts = [q]
  70. while remaining:
  71. # either a string chunk, or oq, or q to terminate
  72. if remaining[0] == q:
  73. break
  74. elif remaining[0] == oq:
  75. parts.append(oq)
  76. remaining = remaining[1:]
  77. else:
  78. m = STRING_CHUNK.match(remaining)
  79. if not m:
  80. raise SyntaxError('error in string literal: %s' % remaining)
  81. parts.append(m.groups()[0])
  82. remaining = remaining[m.end():]
  83. else:
  84. s = ''.join(parts)
  85. raise SyntaxError('unterminated string: %s' % s)
  86. parts.append(q)
  87. result = ''.join(parts)
  88. remaining = remaining[1:].lstrip() # skip past closing quote
  89. return result, remaining
  90. def marker_expr(remaining):
  91. if remaining and remaining[0] == '(':
  92. result, remaining = marker(remaining[1:].lstrip())
  93. if remaining[0] != ')':
  94. raise SyntaxError('unterminated parenthesis: %s' % remaining)
  95. remaining = remaining[1:].lstrip()
  96. else:
  97. lhs, remaining = marker_var(remaining)
  98. while remaining:
  99. m = MARKER_OP.match(remaining)
  100. if not m:
  101. break
  102. op = m.groups()[0]
  103. remaining = remaining[m.end():]
  104. rhs, remaining = marker_var(remaining)
  105. lhs = {'op': op, 'lhs': lhs, 'rhs': rhs}
  106. result = lhs
  107. return result, remaining
  108. def marker_and(remaining):
  109. lhs, remaining = marker_expr(remaining)
  110. while remaining:
  111. m = AND.match(remaining)
  112. if not m:
  113. break
  114. remaining = remaining[m.end():]
  115. rhs, remaining = marker_expr(remaining)
  116. lhs = {'op': 'and', 'lhs': lhs, 'rhs': rhs}
  117. return lhs, remaining
  118. def marker(remaining):
  119. lhs, remaining = marker_and(remaining)
  120. while remaining:
  121. m = OR.match(remaining)
  122. if not m:
  123. break
  124. remaining = remaining[m.end():]
  125. rhs, remaining = marker_and(remaining)
  126. lhs = {'op': 'or', 'lhs': lhs, 'rhs': rhs}
  127. return lhs, remaining
  128. return marker(marker_string)
  129. def parse_requirement(req):
  130. """
  131. Parse a requirement passed in as a string. Return a Container
  132. whose attributes contain the various parts of the requirement.
  133. """
  134. remaining = req.strip()
  135. if not remaining or remaining.startswith('#'):
  136. return None
  137. m = IDENTIFIER.match(remaining)
  138. if not m:
  139. raise SyntaxError('name expected: %s' % remaining)
  140. distname = m.groups()[0]
  141. remaining = remaining[m.end():]
  142. extras = mark_expr = versions = uri = None
  143. if remaining and remaining[0] == '[':
  144. i = remaining.find(']', 1)
  145. if i < 0:
  146. raise SyntaxError('unterminated extra: %s' % remaining)
  147. s = remaining[1:i]
  148. remaining = remaining[i + 1:].lstrip()
  149. extras = []
  150. while s:
  151. m = IDENTIFIER.match(s)
  152. if not m:
  153. raise SyntaxError('malformed extra: %s' % s)
  154. extras.append(m.groups()[0])
  155. s = s[m.end():]
  156. if not s:
  157. break
  158. if s[0] != ',':
  159. raise SyntaxError('comma expected in extras: %s' % s)
  160. s = s[1:].lstrip()
  161. if not extras:
  162. extras = None
  163. if remaining:
  164. if remaining[0] == '@':
  165. # it's a URI
  166. remaining = remaining[1:].lstrip()
  167. m = NON_SPACE.match(remaining)
  168. if not m:
  169. raise SyntaxError('invalid URI: %s' % remaining)
  170. uri = m.groups()[0]
  171. t = urlparse(uri)
  172. # there are issues with Python and URL parsing, so this test
  173. # is a bit crude. See bpo-20271, bpo-23505. Python doesn't
  174. # always parse invalid URLs correctly - it should raise
  175. # exceptions for malformed URLs
  176. if not (t.scheme and t.netloc):
  177. raise SyntaxError('Invalid URL: %s' % uri)
  178. remaining = remaining[m.end():].lstrip()
  179. else:
  180. def get_versions(ver_remaining):
  181. """
  182. Return a list of operator, version tuples if any are
  183. specified, else None.
  184. """
  185. m = COMPARE_OP.match(ver_remaining)
  186. versions = None
  187. if m:
  188. versions = []
  189. while True:
  190. op = m.groups()[0]
  191. ver_remaining = ver_remaining[m.end():]
  192. m = VERSION_IDENTIFIER.match(ver_remaining)
  193. if not m:
  194. raise SyntaxError('invalid version: %s' % ver_remaining)
  195. v = m.groups()[0]
  196. versions.append((op, v))
  197. ver_remaining = ver_remaining[m.end():]
  198. if not ver_remaining or ver_remaining[0] != ',':
  199. break
  200. ver_remaining = ver_remaining[1:].lstrip()
  201. # Some packages have a trailing comma which would break things
  202. # See issue #148
  203. if not ver_remaining:
  204. break
  205. m = COMPARE_OP.match(ver_remaining)
  206. if not m:
  207. raise SyntaxError('invalid constraint: %s' % ver_remaining)
  208. if not versions:
  209. versions = None
  210. return versions, ver_remaining
  211. if remaining[0] != '(':
  212. versions, remaining = get_versions(remaining)
  213. else:
  214. i = remaining.find(')', 1)
  215. if i < 0:
  216. raise SyntaxError('unterminated parenthesis: %s' % remaining)
  217. s = remaining[1:i]
  218. remaining = remaining[i + 1:].lstrip()
  219. # As a special diversion from PEP 508, allow a version number
  220. # a.b.c in parentheses as a synonym for ~= a.b.c (because this
  221. # is allowed in earlier PEPs)
  222. if COMPARE_OP.match(s):
  223. versions, _ = get_versions(s)
  224. else:
  225. m = VERSION_IDENTIFIER.match(s)
  226. if not m:
  227. raise SyntaxError('invalid constraint: %s' % s)
  228. v = m.groups()[0]
  229. s = s[m.end():].lstrip()
  230. if s:
  231. raise SyntaxError('invalid constraint: %s' % s)
  232. versions = [('~=', v)]
  233. if remaining:
  234. if remaining[0] != ';':
  235. raise SyntaxError('invalid requirement: %s' % remaining)
  236. remaining = remaining[1:].lstrip()
  237. mark_expr, remaining = parse_marker(remaining)
  238. if remaining and remaining[0] != '#':
  239. raise SyntaxError('unexpected trailing data: %s' % remaining)
  240. if not versions:
  241. rs = distname
  242. else:
  243. rs = '%s %s' % (distname, ', '.join(['%s %s' % con for con in versions]))
  244. return Container(name=distname, extras=extras, constraints=versions, marker=mark_expr, url=uri, requirement=rs)
  245. def get_resources_dests(resources_root, rules):
  246. """Find destinations for resources files"""
  247. def get_rel_path(root, path):
  248. # normalizes and returns a lstripped-/-separated path
  249. root = root.replace(os.path.sep, '/')
  250. path = path.replace(os.path.sep, '/')
  251. assert path.startswith(root)
  252. return path[len(root):].lstrip('/')
  253. destinations = {}
  254. for base, suffix, dest in rules:
  255. prefix = os.path.join(resources_root, base)
  256. for abs_base in iglob(prefix):
  257. abs_glob = os.path.join(abs_base, suffix)
  258. for abs_path in iglob(abs_glob):
  259. resource_file = get_rel_path(resources_root, abs_path)
  260. if dest is None: # remove the entry if it was here
  261. destinations.pop(resource_file, None)
  262. else:
  263. rel_path = get_rel_path(abs_base, abs_path)
  264. rel_dest = dest.replace(os.path.sep, '/').rstrip('/')
  265. destinations[resource_file] = rel_dest + '/' + rel_path
  266. return destinations
  267. def in_venv():
  268. if hasattr(sys, 'real_prefix'):
  269. # virtualenv venvs
  270. result = True
  271. else:
  272. # PEP 405 venvs
  273. result = sys.prefix != getattr(sys, 'base_prefix', sys.prefix)
  274. return result
  275. def get_executable():
  276. # The __PYVENV_LAUNCHER__ dance is apparently no longer needed, as
  277. # changes to the stub launcher mean that sys.executable always points
  278. # to the stub on OS X
  279. # if sys.platform == 'darwin' and ('__PYVENV_LAUNCHER__'
  280. # in os.environ):
  281. # result = os.environ['__PYVENV_LAUNCHER__']
  282. # else:
  283. # result = sys.executable
  284. # return result
  285. # Avoid normcasing: see issue #143
  286. # result = os.path.normcase(sys.executable)
  287. result = sys.executable
  288. if not isinstance(result, text_type):
  289. result = fsdecode(result)
  290. return result
  291. def proceed(prompt, allowed_chars, error_prompt=None, default=None):
  292. p = prompt
  293. while True:
  294. s = raw_input(p)
  295. p = prompt
  296. if not s and default:
  297. s = default
  298. if s:
  299. c = s[0].lower()
  300. if c in allowed_chars:
  301. break
  302. if error_prompt:
  303. p = '%c: %s\n%s' % (c, error_prompt, prompt)
  304. return c
  305. def extract_by_key(d, keys):
  306. if isinstance(keys, string_types):
  307. keys = keys.split()
  308. result = {}
  309. for key in keys:
  310. if key in d:
  311. result[key] = d[key]
  312. return result
  313. def read_exports(stream):
  314. if sys.version_info[0] >= 3:
  315. # needs to be a text stream
  316. stream = codecs.getreader('utf-8')(stream)
  317. # Try to load as JSON, falling back on legacy format
  318. data = stream.read()
  319. stream = StringIO(data)
  320. try:
  321. jdata = json.load(stream)
  322. result = jdata['extensions']['python.exports']['exports']
  323. for group, entries in result.items():
  324. for k, v in entries.items():
  325. s = '%s = %s' % (k, v)
  326. entry = get_export_entry(s)
  327. assert entry is not None
  328. entries[k] = entry
  329. return result
  330. except Exception:
  331. stream.seek(0, 0)
  332. def read_stream(cp, stream):
  333. if hasattr(cp, 'read_file'):
  334. cp.read_file(stream)
  335. else:
  336. cp.readfp(stream)
  337. cp = configparser.ConfigParser()
  338. try:
  339. read_stream(cp, stream)
  340. except configparser.MissingSectionHeaderError:
  341. stream.close()
  342. data = textwrap.dedent(data)
  343. stream = StringIO(data)
  344. read_stream(cp, stream)
  345. result = {}
  346. for key in cp.sections():
  347. result[key] = entries = {}
  348. for name, value in cp.items(key):
  349. s = '%s = %s' % (name, value)
  350. entry = get_export_entry(s)
  351. assert entry is not None
  352. # entry.dist = self
  353. entries[name] = entry
  354. return result
  355. def write_exports(exports, stream):
  356. if sys.version_info[0] >= 3:
  357. # needs to be a text stream
  358. stream = codecs.getwriter('utf-8')(stream)
  359. cp = configparser.ConfigParser()
  360. for k, v in exports.items():
  361. # TODO check k, v for valid values
  362. cp.add_section(k)
  363. for entry in v.values():
  364. if entry.suffix is None:
  365. s = entry.prefix
  366. else:
  367. s = '%s:%s' % (entry.prefix, entry.suffix)
  368. if entry.flags:
  369. s = '%s [%s]' % (s, ', '.join(entry.flags))
  370. cp.set(k, entry.name, s)
  371. cp.write(stream)
  372. @contextlib.contextmanager
  373. def tempdir():
  374. td = tempfile.mkdtemp()
  375. try:
  376. yield td
  377. finally:
  378. shutil.rmtree(td)
  379. @contextlib.contextmanager
  380. def chdir(d):
  381. cwd = os.getcwd()
  382. try:
  383. os.chdir(d)
  384. yield
  385. finally:
  386. os.chdir(cwd)
  387. @contextlib.contextmanager
  388. def socket_timeout(seconds=15):
  389. cto = socket.getdefaulttimeout()
  390. try:
  391. socket.setdefaulttimeout(seconds)
  392. yield
  393. finally:
  394. socket.setdefaulttimeout(cto)
  395. class cached_property(object):
  396. def __init__(self, func):
  397. self.func = func
  398. # for attr in ('__name__', '__module__', '__doc__'):
  399. # setattr(self, attr, getattr(func, attr, None))
  400. def __get__(self, obj, cls=None):
  401. if obj is None:
  402. return self
  403. value = self.func(obj)
  404. object.__setattr__(obj, self.func.__name__, value)
  405. # obj.__dict__[self.func.__name__] = value = self.func(obj)
  406. return value
  407. def convert_path(pathname):
  408. """Return 'pathname' as a name that will work on the native filesystem.
  409. The path is split on '/' and put back together again using the current
  410. directory separator. Needed because filenames in the setup script are
  411. always supplied in Unix style, and have to be converted to the local
  412. convention before we can actually use them in the filesystem. Raises
  413. ValueError on non-Unix-ish systems if 'pathname' either starts or
  414. ends with a slash.
  415. """
  416. if os.sep == '/':
  417. return pathname
  418. if not pathname:
  419. return pathname
  420. if pathname[0] == '/':
  421. raise ValueError("path '%s' cannot be absolute" % pathname)
  422. if pathname[-1] == '/':
  423. raise ValueError("path '%s' cannot end with '/'" % pathname)
  424. paths = pathname.split('/')
  425. while os.curdir in paths:
  426. paths.remove(os.curdir)
  427. if not paths:
  428. return os.curdir
  429. return os.path.join(*paths)
  430. class FileOperator(object):
  431. def __init__(self, dry_run=False):
  432. self.dry_run = dry_run
  433. self.ensured = set()
  434. self._init_record()
  435. def _init_record(self):
  436. self.record = False
  437. self.files_written = set()
  438. self.dirs_created = set()
  439. def record_as_written(self, path):
  440. if self.record:
  441. self.files_written.add(path)
  442. def newer(self, source, target):
  443. """Tell if the target is newer than the source.
  444. Returns true if 'source' exists and is more recently modified than
  445. 'target', or if 'source' exists and 'target' doesn't.
  446. Returns false if both exist and 'target' is the same age or younger
  447. than 'source'. Raise PackagingFileError if 'source' does not exist.
  448. Note that this test is not very accurate: files created in the same
  449. second will have the same "age".
  450. """
  451. if not os.path.exists(source):
  452. raise DistlibException("file '%r' does not exist" % os.path.abspath(source))
  453. if not os.path.exists(target):
  454. return True
  455. return os.stat(source).st_mtime > os.stat(target).st_mtime
  456. def copy_file(self, infile, outfile, check=True):
  457. """Copy a file respecting dry-run and force flags.
  458. """
  459. self.ensure_dir(os.path.dirname(outfile))
  460. logger.info('Copying %s to %s', infile, outfile)
  461. if not self.dry_run:
  462. msg = None
  463. if check:
  464. if os.path.islink(outfile):
  465. msg = '%s is a symlink' % outfile
  466. elif os.path.exists(outfile) and not os.path.isfile(outfile):
  467. msg = '%s is a non-regular file' % outfile
  468. if msg:
  469. raise ValueError(msg + ' which would be overwritten')
  470. shutil.copyfile(infile, outfile)
  471. self.record_as_written(outfile)
  472. def copy_stream(self, instream, outfile, encoding=None):
  473. assert not os.path.isdir(outfile)
  474. self.ensure_dir(os.path.dirname(outfile))
  475. logger.info('Copying stream %s to %s', instream, outfile)
  476. if not self.dry_run:
  477. if encoding is None:
  478. outstream = open(outfile, 'wb')
  479. else:
  480. outstream = codecs.open(outfile, 'w', encoding=encoding)
  481. try:
  482. shutil.copyfileobj(instream, outstream)
  483. finally:
  484. outstream.close()
  485. self.record_as_written(outfile)
  486. def write_binary_file(self, path, data):
  487. self.ensure_dir(os.path.dirname(path))
  488. if not self.dry_run:
  489. if os.path.exists(path):
  490. os.remove(path)
  491. with open(path, 'wb') as f:
  492. f.write(data)
  493. self.record_as_written(path)
  494. def write_text_file(self, path, data, encoding):
  495. self.write_binary_file(path, data.encode(encoding))
  496. def set_mode(self, bits, mask, files):
  497. if os.name == 'posix' or (os.name == 'java' and os._name == 'posix'):
  498. # Set the executable bits (owner, group, and world) on
  499. # all the files specified.
  500. for f in files:
  501. if self.dry_run:
  502. logger.info("changing mode of %s", f)
  503. else:
  504. mode = (os.stat(f).st_mode | bits) & mask
  505. logger.info("changing mode of %s to %o", f, mode)
  506. os.chmod(f, mode)
  507. set_executable_mode = lambda s, f: s.set_mode(0o555, 0o7777, f)
  508. def ensure_dir(self, path):
  509. path = os.path.abspath(path)
  510. if path not in self.ensured and not os.path.exists(path):
  511. self.ensured.add(path)
  512. d, f = os.path.split(path)
  513. self.ensure_dir(d)
  514. logger.info('Creating %s' % path)
  515. if not self.dry_run:
  516. os.mkdir(path)
  517. if self.record:
  518. self.dirs_created.add(path)
  519. def byte_compile(self, path, optimize=False, force=False, prefix=None, hashed_invalidation=False):
  520. dpath = cache_from_source(path, not optimize)
  521. logger.info('Byte-compiling %s to %s', path, dpath)
  522. if not self.dry_run:
  523. if force or self.newer(path, dpath):
  524. if not prefix:
  525. diagpath = None
  526. else:
  527. assert path.startswith(prefix)
  528. diagpath = path[len(prefix):]
  529. compile_kwargs = {}
  530. if hashed_invalidation and hasattr(py_compile, 'PycInvalidationMode'):
  531. if not isinstance(hashed_invalidation, py_compile.PycInvalidationMode):
  532. hashed_invalidation = py_compile.PycInvalidationMode.CHECKED_HASH
  533. compile_kwargs['invalidation_mode'] = hashed_invalidation
  534. py_compile.compile(path, dpath, diagpath, True, **compile_kwargs) # raise error
  535. self.record_as_written(dpath)
  536. return dpath
  537. def ensure_removed(self, path):
  538. if os.path.exists(path):
  539. if os.path.isdir(path) and not os.path.islink(path):
  540. logger.debug('Removing directory tree at %s', path)
  541. if not self.dry_run:
  542. shutil.rmtree(path)
  543. if self.record:
  544. if path in self.dirs_created:
  545. self.dirs_created.remove(path)
  546. else:
  547. if os.path.islink(path):
  548. s = 'link'
  549. else:
  550. s = 'file'
  551. logger.debug('Removing %s %s', s, path)
  552. if not self.dry_run:
  553. os.remove(path)
  554. if self.record:
  555. if path in self.files_written:
  556. self.files_written.remove(path)
  557. def is_writable(self, path):
  558. result = False
  559. while not result:
  560. if os.path.exists(path):
  561. result = os.access(path, os.W_OK)
  562. break
  563. parent = os.path.dirname(path)
  564. if parent == path:
  565. break
  566. path = parent
  567. return result
  568. def commit(self):
  569. """
  570. Commit recorded changes, turn off recording, return
  571. changes.
  572. """
  573. assert self.record
  574. result = self.files_written, self.dirs_created
  575. self._init_record()
  576. return result
  577. def rollback(self):
  578. if not self.dry_run:
  579. for f in list(self.files_written):
  580. if os.path.exists(f):
  581. os.remove(f)
  582. # dirs should all be empty now, except perhaps for
  583. # __pycache__ subdirs
  584. # reverse so that subdirs appear before their parents
  585. dirs = sorted(self.dirs_created, reverse=True)
  586. for d in dirs:
  587. flist = os.listdir(d)
  588. if flist:
  589. assert flist == ['__pycache__']
  590. sd = os.path.join(d, flist[0])
  591. os.rmdir(sd)
  592. os.rmdir(d) # should fail if non-empty
  593. self._init_record()
  594. def resolve(module_name, dotted_path):
  595. if module_name in sys.modules:
  596. mod = sys.modules[module_name]
  597. else:
  598. mod = __import__(module_name)
  599. if dotted_path is None:
  600. result = mod
  601. else:
  602. parts = dotted_path.split('.')
  603. result = getattr(mod, parts.pop(0))
  604. for p in parts:
  605. result = getattr(result, p)
  606. return result
  607. class ExportEntry(object):
  608. def __init__(self, name, prefix, suffix, flags):
  609. self.name = name
  610. self.prefix = prefix
  611. self.suffix = suffix
  612. self.flags = flags
  613. @cached_property
  614. def value(self):
  615. return resolve(self.prefix, self.suffix)
  616. def __repr__(self): # pragma: no cover
  617. return '<ExportEntry %s = %s:%s %s>' % (self.name, self.prefix, self.suffix, self.flags)
  618. def __eq__(self, other):
  619. if not isinstance(other, ExportEntry):
  620. result = False
  621. else:
  622. result = (self.name == other.name and self.prefix == other.prefix and self.suffix == other.suffix and
  623. self.flags == other.flags)
  624. return result
  625. __hash__ = object.__hash__
  626. ENTRY_RE = re.compile(
  627. r'''(?P<name>([^\[]\S*))
  628. \s*=\s*(?P<callable>(\w+)([:\.]\w+)*)
  629. \s*(\[\s*(?P<flags>[\w-]+(=\w+)?(,\s*\w+(=\w+)?)*)\s*\])?
  630. ''', re.VERBOSE)
  631. def get_export_entry(specification):
  632. m = ENTRY_RE.search(specification)
  633. if not m:
  634. result = None
  635. if '[' in specification or ']' in specification:
  636. raise DistlibException("Invalid specification "
  637. "'%s'" % specification)
  638. else:
  639. d = m.groupdict()
  640. name = d['name']
  641. path = d['callable']
  642. colons = path.count(':')
  643. if colons == 0:
  644. prefix, suffix = path, None
  645. else:
  646. if colons != 1:
  647. raise DistlibException("Invalid specification "
  648. "'%s'" % specification)
  649. prefix, suffix = path.split(':')
  650. flags = d['flags']
  651. if flags is None:
  652. if '[' in specification or ']' in specification:
  653. raise DistlibException("Invalid specification "
  654. "'%s'" % specification)
  655. flags = []
  656. else:
  657. flags = [f.strip() for f in flags.split(',')]
  658. result = ExportEntry(name, prefix, suffix, flags)
  659. return result
  660. def get_cache_base(suffix=None):
  661. """
  662. Return the default base location for distlib caches. If the directory does
  663. not exist, it is created. Use the suffix provided for the base directory,
  664. and default to '.distlib' if it isn't provided.
  665. On Windows, if LOCALAPPDATA is defined in the environment, then it is
  666. assumed to be a directory, and will be the parent directory of the result.
  667. On POSIX, and on Windows if LOCALAPPDATA is not defined, the user's home
  668. directory - using os.expanduser('~') - will be the parent directory of
  669. the result.
  670. The result is just the directory '.distlib' in the parent directory as
  671. determined above, or with the name specified with ``suffix``.
  672. """
  673. if suffix is None:
  674. suffix = '.distlib'
  675. if os.name == 'nt' and 'LOCALAPPDATA' in os.environ:
  676. result = os.path.expandvars('$localappdata')
  677. else:
  678. # Assume posix, or old Windows
  679. result = os.path.expanduser('~')
  680. # we use 'isdir' instead of 'exists', because we want to
  681. # fail if there's a file with that name
  682. if os.path.isdir(result):
  683. usable = os.access(result, os.W_OK)
  684. if not usable:
  685. logger.warning('Directory exists but is not writable: %s', result)
  686. else:
  687. try:
  688. os.makedirs(result)
  689. usable = True
  690. except OSError:
  691. logger.warning('Unable to create %s', result, exc_info=True)
  692. usable = False
  693. if not usable:
  694. result = tempfile.mkdtemp()
  695. logger.warning('Default location unusable, using %s', result)
  696. return os.path.join(result, suffix)
  697. def path_to_cache_dir(path, use_abspath=True):
  698. """
  699. Convert an absolute path to a directory name for use in a cache.
  700. The algorithm used is:
  701. #. On Windows, any ``':'`` in the drive is replaced with ``'---'``.
  702. #. Any occurrence of ``os.sep`` is replaced with ``'--'``.
  703. #. ``'.cache'`` is appended.
  704. """
  705. d, p = os.path.splitdrive(os.path.abspath(path) if use_abspath else path)
  706. if d:
  707. d = d.replace(':', '---')
  708. p = p.replace(os.sep, '--')
  709. return d + p + '.cache'
  710. def ensure_slash(s):
  711. if not s.endswith('/'):
  712. return s + '/'
  713. return s
  714. def parse_credentials(netloc):
  715. username = password = None
  716. if '@' in netloc:
  717. prefix, netloc = netloc.rsplit('@', 1)
  718. if ':' not in prefix:
  719. username = prefix
  720. else:
  721. username, password = prefix.split(':', 1)
  722. if username:
  723. username = unquote(username)
  724. if password:
  725. password = unquote(password)
  726. return username, password, netloc
  727. def get_process_umask():
  728. result = os.umask(0o22)
  729. os.umask(result)
  730. return result
  731. def is_string_sequence(seq):
  732. result = True
  733. i = None
  734. for i, s in enumerate(seq):
  735. if not isinstance(s, string_types):
  736. result = False
  737. break
  738. assert i is not None
  739. return result
  740. PROJECT_NAME_AND_VERSION = re.compile('([a-z0-9_]+([.-][a-z_][a-z0-9_]*)*)-'
  741. '([a-z0-9_.+-]+)', re.I)
  742. PYTHON_VERSION = re.compile(r'-py(\d\.?\d?)')
  743. def split_filename(filename, project_name=None):
  744. """
  745. Extract name, version, python version from a filename (no extension)
  746. Return name, version, pyver or None
  747. """
  748. result = None
  749. pyver = None
  750. filename = unquote(filename).replace(' ', '-')
  751. m = PYTHON_VERSION.search(filename)
  752. if m:
  753. pyver = m.group(1)
  754. filename = filename[:m.start()]
  755. if project_name and len(filename) > len(project_name) + 1:
  756. m = re.match(re.escape(project_name) + r'\b', filename)
  757. if m:
  758. n = m.end()
  759. result = filename[:n], filename[n + 1:], pyver
  760. if result is None:
  761. m = PROJECT_NAME_AND_VERSION.match(filename)
  762. if m:
  763. result = m.group(1), m.group(3), pyver
  764. return result
  765. # Allow spaces in name because of legacy dists like "Twisted Core"
  766. NAME_VERSION_RE = re.compile(r'(?P<name>[\w .-]+)\s*'
  767. r'\(\s*(?P<ver>[^\s)]+)\)$')
  768. def parse_name_and_version(p):
  769. """
  770. A utility method used to get name and version from a string.
  771. From e.g. a Provides-Dist value.
  772. :param p: A value in a form 'foo (1.0)'
  773. :return: The name and version as a tuple.
  774. """
  775. m = NAME_VERSION_RE.match(p)
  776. if not m:
  777. raise DistlibException('Ill-formed name/version string: \'%s\'' % p)
  778. d = m.groupdict()
  779. return d['name'].strip().lower(), d['ver']
  780. def get_extras(requested, available):
  781. result = set()
  782. requested = set(requested or [])
  783. available = set(available or [])
  784. if '*' in requested:
  785. requested.remove('*')
  786. result |= available
  787. for r in requested:
  788. if r == '-':
  789. result.add(r)
  790. elif r.startswith('-'):
  791. unwanted = r[1:]
  792. if unwanted not in available:
  793. logger.warning('undeclared extra: %s' % unwanted)
  794. if unwanted in result:
  795. result.remove(unwanted)
  796. else:
  797. if r not in available:
  798. logger.warning('undeclared extra: %s' % r)
  799. result.add(r)
  800. return result
  801. #
  802. # Extended metadata functionality
  803. #
  804. def _get_external_data(url):
  805. result = {}
  806. try:
  807. # urlopen might fail if it runs into redirections,
  808. # because of Python issue #13696. Fixed in locators
  809. # using a custom redirect handler.
  810. resp = urlopen(url)
  811. headers = resp.info()
  812. ct = headers.get('Content-Type')
  813. if not ct.startswith('application/json'):
  814. logger.debug('Unexpected response for JSON request: %s', ct)
  815. else:
  816. reader = codecs.getreader('utf-8')(resp)
  817. # data = reader.read().decode('utf-8')
  818. # result = json.loads(data)
  819. result = json.load(reader)
  820. except Exception as e:
  821. logger.exception('Failed to get external data for %s: %s', url, e)
  822. return result
  823. _external_data_base_url = 'https://www.red-dove.com/pypi/projects/'
  824. def get_project_data(name):
  825. url = '%s/%s/project.json' % (name[0].upper(), name)
  826. url = urljoin(_external_data_base_url, url)
  827. result = _get_external_data(url)
  828. return result
  829. def get_package_data(name, version):
  830. url = '%s/%s/package-%s.json' % (name[0].upper(), name, version)
  831. url = urljoin(_external_data_base_url, url)
  832. return _get_external_data(url)
  833. class Cache(object):
  834. """
  835. A class implementing a cache for resources that need to live in the file system
  836. e.g. shared libraries. This class was moved from resources to here because it
  837. could be used by other modules, e.g. the wheel module.
  838. """
  839. def __init__(self, base):
  840. """
  841. Initialise an instance.
  842. :param base: The base directory where the cache should be located.
  843. """
  844. # we use 'isdir' instead of 'exists', because we want to
  845. # fail if there's a file with that name
  846. if not os.path.isdir(base): # pragma: no cover
  847. os.makedirs(base)
  848. if (os.stat(base).st_mode & 0o77) != 0:
  849. logger.warning('Directory \'%s\' is not private', base)
  850. self.base = os.path.abspath(os.path.normpath(base))
  851. def prefix_to_dir(self, prefix, use_abspath=True):
  852. """
  853. Converts a resource prefix to a directory name in the cache.
  854. """
  855. return path_to_cache_dir(prefix, use_abspath=use_abspath)
  856. def clear(self):
  857. """
  858. Clear the cache.
  859. """
  860. not_removed = []
  861. for fn in os.listdir(self.base):
  862. fn = os.path.join(self.base, fn)
  863. try:
  864. if os.path.islink(fn) or os.path.isfile(fn):
  865. os.remove(fn)
  866. elif os.path.isdir(fn):
  867. shutil.rmtree(fn)
  868. except Exception:
  869. not_removed.append(fn)
  870. return not_removed
  871. class EventMixin(object):
  872. """
  873. A very simple publish/subscribe system.
  874. """
  875. def __init__(self):
  876. self._subscribers = {}
  877. def add(self, event, subscriber, append=True):
  878. """
  879. Add a subscriber for an event.
  880. :param event: The name of an event.
  881. :param subscriber: The subscriber to be added (and called when the
  882. event is published).
  883. :param append: Whether to append or prepend the subscriber to an
  884. existing subscriber list for the event.
  885. """
  886. subs = self._subscribers
  887. if event not in subs:
  888. subs[event] = deque([subscriber])
  889. else:
  890. sq = subs[event]
  891. if append:
  892. sq.append(subscriber)
  893. else:
  894. sq.appendleft(subscriber)
  895. def remove(self, event, subscriber):
  896. """
  897. Remove a subscriber for an event.
  898. :param event: The name of an event.
  899. :param subscriber: The subscriber to be removed.
  900. """
  901. subs = self._subscribers
  902. if event not in subs:
  903. raise ValueError('No subscribers: %r' % event)
  904. subs[event].remove(subscriber)
  905. def get_subscribers(self, event):
  906. """
  907. Return an iterator for the subscribers for an event.
  908. :param event: The event to return subscribers for.
  909. """
  910. return iter(self._subscribers.get(event, ()))
  911. def publish(self, event, *args, **kwargs):
  912. """
  913. Publish a event and return a list of values returned by its
  914. subscribers.
  915. :param event: The event to publish.
  916. :param args: The positional arguments to pass to the event's
  917. subscribers.
  918. :param kwargs: The keyword arguments to pass to the event's
  919. subscribers.
  920. """
  921. result = []
  922. for subscriber in self.get_subscribers(event):
  923. try:
  924. value = subscriber(event, *args, **kwargs)
  925. except Exception:
  926. logger.exception('Exception during event publication')
  927. value = None
  928. result.append(value)
  929. logger.debug('publish %s: args = %s, kwargs = %s, result = %s', event, args, kwargs, result)
  930. return result
  931. #
  932. # Simple sequencing
  933. #
  934. class Sequencer(object):
  935. def __init__(self):
  936. self._preds = {}
  937. self._succs = {}
  938. self._nodes = set() # nodes with no preds/succs
  939. def add_node(self, node):
  940. self._nodes.add(node)
  941. def remove_node(self, node, edges=False):
  942. if node in self._nodes:
  943. self._nodes.remove(node)
  944. if edges:
  945. for p in set(self._preds.get(node, ())):
  946. self.remove(p, node)
  947. for s in set(self._succs.get(node, ())):
  948. self.remove(node, s)
  949. # Remove empties
  950. for k, v in list(self._preds.items()):
  951. if not v:
  952. del self._preds[k]
  953. for k, v in list(self._succs.items()):
  954. if not v:
  955. del self._succs[k]
  956. def add(self, pred, succ):
  957. assert pred != succ
  958. self._preds.setdefault(succ, set()).add(pred)
  959. self._succs.setdefault(pred, set()).add(succ)
  960. def remove(self, pred, succ):
  961. assert pred != succ
  962. try:
  963. preds = self._preds[succ]
  964. succs = self._succs[pred]
  965. except KeyError: # pragma: no cover
  966. raise ValueError('%r not a successor of anything' % succ)
  967. try:
  968. preds.remove(pred)
  969. succs.remove(succ)
  970. except KeyError: # pragma: no cover
  971. raise ValueError('%r not a successor of %r' % (succ, pred))
  972. def is_step(self, step):
  973. return (step in self._preds or step in self._succs or step in self._nodes)
  974. def get_steps(self, final):
  975. if not self.is_step(final):
  976. raise ValueError('Unknown: %r' % final)
  977. result = []
  978. todo = []
  979. seen = set()
  980. todo.append(final)
  981. while todo:
  982. step = todo.pop(0)
  983. if step in seen:
  984. # if a step was already seen,
  985. # move it to the end (so it will appear earlier
  986. # when reversed on return) ... but not for the
  987. # final step, as that would be confusing for
  988. # users
  989. if step != final:
  990. result.remove(step)
  991. result.append(step)
  992. else:
  993. seen.add(step)
  994. result.append(step)
  995. preds = self._preds.get(step, ())
  996. todo.extend(preds)
  997. return reversed(result)
  998. @property
  999. def strong_connections(self):
  1000. # http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
  1001. index_counter = [0]
  1002. stack = []
  1003. lowlinks = {}
  1004. index = {}
  1005. result = []
  1006. graph = self._succs
  1007. def strongconnect(node):
  1008. # set the depth index for this node to the smallest unused index
  1009. index[node] = index_counter[0]
  1010. lowlinks[node] = index_counter[0]
  1011. index_counter[0] += 1
  1012. stack.append(node)
  1013. # Consider successors
  1014. try:
  1015. successors = graph[node]
  1016. except Exception:
  1017. successors = []
  1018. for successor in successors:
  1019. if successor not in lowlinks:
  1020. # Successor has not yet been visited
  1021. strongconnect(successor)
  1022. lowlinks[node] = min(lowlinks[node], lowlinks[successor])
  1023. elif successor in stack:
  1024. # the successor is in the stack and hence in the current
  1025. # strongly connected component (SCC)
  1026. lowlinks[node] = min(lowlinks[node], index[successor])
  1027. # If `node` is a root node, pop the stack and generate an SCC
  1028. if lowlinks[node] == index[node]:
  1029. connected_component = []
  1030. while True:
  1031. successor = stack.pop()
  1032. connected_component.append(successor)
  1033. if successor == node:
  1034. break
  1035. component = tuple(connected_component)
  1036. # storing the result
  1037. result.append(component)
  1038. for node in graph:
  1039. if node not in lowlinks:
  1040. strongconnect(node)
  1041. return result
  1042. @property
  1043. def dot(self):
  1044. result = ['digraph G {']
  1045. for succ in self._preds:
  1046. preds = self._preds[succ]
  1047. for pred in preds:
  1048. result.append(' %s -> %s;' % (pred, succ))
  1049. for node in self._nodes:
  1050. result.append(' %s;' % node)
  1051. result.append('}')
  1052. return '\n'.join(result)
  1053. #
  1054. # Unarchiving functionality for zip, tar, tgz, tbz, whl
  1055. #
  1056. ARCHIVE_EXTENSIONS = ('.tar.gz', '.tar.bz2', '.tar', '.zip', '.tgz', '.tbz', '.whl')
  1057. def unarchive(archive_filename, dest_dir, format=None, check=True):
  1058. def check_path(path):
  1059. if not isinstance(path, text_type):
  1060. path = path.decode('utf-8')
  1061. p = os.path.abspath(os.path.join(dest_dir, path))
  1062. if not p.startswith(dest_dir) or p[plen] != os.sep:
  1063. raise ValueError('path outside destination: %r' % p)
  1064. dest_dir = os.path.abspath(dest_dir)
  1065. plen = len(dest_dir)
  1066. archive = None
  1067. if format is None:
  1068. if archive_filename.endswith(('.zip', '.whl')):
  1069. format = 'zip'
  1070. elif archive_filename.endswith(('.tar.gz', '.tgz')):
  1071. format = 'tgz'
  1072. mode = 'r:gz'
  1073. elif archive_filename.endswith(('.tar.bz2', '.tbz')):
  1074. format = 'tbz'
  1075. mode = 'r:bz2'
  1076. elif archive_filename.endswith('.tar'):
  1077. format = 'tar'
  1078. mode = 'r'
  1079. else: # pragma: no cover
  1080. raise ValueError('Unknown format for %r' % archive_filename)
  1081. try:
  1082. if format == 'zip':
  1083. archive = ZipFile(archive_filename, 'r')
  1084. if check:
  1085. names = archive.namelist()
  1086. for name in names:
  1087. check_path(name)
  1088. else:
  1089. archive = tarfile.open(archive_filename, mode)
  1090. if check:
  1091. names = archive.getnames()
  1092. for name in names:
  1093. check_path(name)
  1094. if format != 'zip' and sys.version_info[0] < 3:
  1095. # See Python issue 17153. If the dest path contains Unicode,
  1096. # tarfile extraction fails on Python 2.x if a member path name
  1097. # contains non-ASCII characters - it leads to an implicit
  1098. # bytes -> unicode conversion using ASCII to decode.
  1099. for tarinfo in archive.getmembers():
  1100. if not isinstance(tarinfo.name, text_type):
  1101. tarinfo.name = tarinfo.name.decode('utf-8')
  1102. # Limit extraction of dangerous items, if this Python
  1103. # allows it easily. If not, just trust the input.
  1104. # See: https://docs.python.org/3/library/tarfile.html#extraction-filters
  1105. def extraction_filter(member, path):
  1106. """Run tarfile.tar_filter, but raise the expected ValueError"""
  1107. # This is only called if the current Python has tarfile filters
  1108. try:
  1109. return tarfile.tar_filter(member, path)
  1110. except tarfile.FilterError as exc:
  1111. raise ValueError(str(exc))
  1112. archive.extraction_filter = extraction_filter
  1113. archive.extractall(dest_dir)
  1114. finally:
  1115. if archive:
  1116. archive.close()
  1117. def zip_dir(directory):
  1118. """zip a directory tree into a BytesIO object"""
  1119. result = io.BytesIO()
  1120. dlen = len(directory)
  1121. with ZipFile(result, "w") as zf:
  1122. for root, dirs, files in os.walk(directory):
  1123. for name in files:
  1124. full = os.path.join(root, name)
  1125. rel = root[dlen:]
  1126. dest = os.path.join(rel, name)
  1127. zf.write(full, dest)
  1128. return result
  1129. #
  1130. # Simple progress bar
  1131. #
  1132. UNITS = ('', 'K', 'M', 'G', 'T', 'P')
  1133. class Progress(object):
  1134. unknown = 'UNKNOWN'
  1135. def __init__(self, minval=0, maxval=100):
  1136. assert maxval is None or maxval >= minval
  1137. self.min = self.cur = minval
  1138. self.max = maxval
  1139. self.started = None
  1140. self.elapsed = 0
  1141. self.done = False
  1142. def update(self, curval):
  1143. assert self.min <= curval
  1144. assert self.max is None or curval <= self.max
  1145. self.cur = curval
  1146. now = time.time()
  1147. if self.started is None:
  1148. self.started = now
  1149. else:
  1150. self.elapsed = now - self.started
  1151. def increment(self, incr):
  1152. assert incr >= 0
  1153. self.update(self.cur + incr)
  1154. def start(self):
  1155. self.update(self.min)
  1156. return self
  1157. def stop(self):
  1158. if self.max is not None:
  1159. self.update(self.max)
  1160. self.done = True
  1161. @property
  1162. def maximum(self):
  1163. return self.unknown if self.max is None else self.max
  1164. @property
  1165. def percentage(self):
  1166. if self.done:
  1167. result = '100 %'
  1168. elif self.max is None:
  1169. result = ' ?? %'
  1170. else:
  1171. v = 100.0 * (self.cur - self.min) / (self.max - self.min)
  1172. result = '%3d %%' % v
  1173. return result
  1174. def format_duration(self, duration):
  1175. if (duration <= 0) and self.max is None or self.cur == self.min:
  1176. result = '??:??:??'
  1177. # elif duration < 1:
  1178. # result = '--:--:--'
  1179. else:
  1180. result = time.strftime('%H:%M:%S', time.gmtime(duration))
  1181. return result
  1182. @property
  1183. def ETA(self):
  1184. if self.done:
  1185. prefix = 'Done'
  1186. t = self.elapsed
  1187. # import pdb; pdb.set_trace()
  1188. else:
  1189. prefix = 'ETA '
  1190. if self.max is None:
  1191. t = -1
  1192. elif self.elapsed == 0 or (self.cur == self.min):
  1193. t = 0
  1194. else:
  1195. # import pdb; pdb.set_trace()
  1196. t = float(self.max - self.min)
  1197. t /= self.cur - self.min
  1198. t = (t - 1) * self.elapsed
  1199. return '%s: %s' % (prefix, self.format_duration(t))
  1200. @property
  1201. def speed(self):
  1202. if self.elapsed == 0:
  1203. result = 0.0
  1204. else:
  1205. result = (self.cur - self.min) / self.elapsed
  1206. for unit in UNITS:
  1207. if result < 1000:
  1208. break
  1209. result /= 1000.0
  1210. return '%d %sB/s' % (result, unit)
  1211. #
  1212. # Glob functionality
  1213. #
  1214. RICH_GLOB = re.compile(r'\{([^}]*)\}')
  1215. _CHECK_RECURSIVE_GLOB = re.compile(r'[^/\\,{]\*\*|\*\*[^/\\,}]')
  1216. _CHECK_MISMATCH_SET = re.compile(r'^[^{]*\}|\{[^}]*$')
  1217. def iglob(path_glob):
  1218. """Extended globbing function that supports ** and {opt1,opt2,opt3}."""
  1219. if _CHECK_RECURSIVE_GLOB.search(path_glob):
  1220. msg = """invalid glob %r: recursive glob "**" must be used alone"""
  1221. raise ValueError(msg % path_glob)
  1222. if _CHECK_MISMATCH_SET.search(path_glob):
  1223. msg = """invalid glob %r: mismatching set marker '{' or '}'"""
  1224. raise ValueError(msg % path_glob)
  1225. return _iglob(path_glob)
  1226. def _iglob(path_glob):
  1227. rich_path_glob = RICH_GLOB.split(path_glob, 1)
  1228. if len(rich_path_glob) > 1:
  1229. assert len(rich_path_glob) == 3, rich_path_glob
  1230. prefix, set, suffix = rich_path_glob
  1231. for item in set.split(','):
  1232. for path in _iglob(''.join((prefix, item, suffix))):
  1233. yield path
  1234. else:
  1235. if '**' not in path_glob:
  1236. for item in std_iglob(path_glob):
  1237. yield item
  1238. else:
  1239. prefix, radical = path_glob.split('**', 1)
  1240. if prefix == '':
  1241. prefix = '.'
  1242. if radical == '':
  1243. radical = '*'
  1244. else:
  1245. # we support both
  1246. radical = radical.lstrip('/')
  1247. radical = radical.lstrip('\\')
  1248. for path, dir, files in os.walk(prefix):
  1249. path = os.path.normpath(path)
  1250. for fn in _iglob(os.path.join(path, radical)):
  1251. yield fn
  1252. if ssl:
  1253. from .compat import (HTTPSHandler as BaseHTTPSHandler, match_hostname, CertificateError)
  1254. #
  1255. # HTTPSConnection which verifies certificates/matches domains
  1256. #
  1257. class HTTPSConnection(httplib.HTTPSConnection):
  1258. ca_certs = None # set this to the path to the certs file (.pem)
  1259. check_domain = True # only used if ca_certs is not None
  1260. # noinspection PyPropertyAccess
  1261. def connect(self):
  1262. sock = socket.create_connection((self.host, self.port), self.timeout)
  1263. if getattr(self, '_tunnel_host', False):
  1264. self.sock = sock
  1265. self._tunnel()
  1266. context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
  1267. if hasattr(ssl, 'OP_NO_SSLv2'):
  1268. context.options |= ssl.OP_NO_SSLv2
  1269. if getattr(self, 'cert_file', None):
  1270. context.load_cert_chain(self.cert_file, self.key_file)
  1271. kwargs = {}
  1272. if self.ca_certs:
  1273. context.verify_mode = ssl.CERT_REQUIRED
  1274. context.load_verify_locations(cafile=self.ca_certs)
  1275. if getattr(ssl, 'HAS_SNI', False):
  1276. kwargs['server_hostname'] = self.host
  1277. self.sock = context.wrap_socket(sock, **kwargs)
  1278. if self.ca_certs and self.check_domain:
  1279. try:
  1280. match_hostname(self.sock.getpeercert(), self.host)
  1281. logger.debug('Host verified: %s', self.host)
  1282. except CertificateError: # pragma: no cover
  1283. self.sock.shutdown(socket.SHUT_RDWR)
  1284. self.sock.close()
  1285. raise
  1286. class HTTPSHandler(BaseHTTPSHandler):
  1287. def __init__(self, ca_certs, check_domain=True):
  1288. BaseHTTPSHandler.__init__(self)
  1289. self.ca_certs = ca_certs
  1290. self.check_domain = check_domain
  1291. def _conn_maker(self, *args, **kwargs):
  1292. """
  1293. This is called to create a connection instance. Normally you'd
  1294. pass a connection class to do_open, but it doesn't actually check for
  1295. a class, and just expects a callable. As long as we behave just as a
  1296. constructor would have, we should be OK. If it ever changes so that
  1297. we *must* pass a class, we'll create an UnsafeHTTPSConnection class
  1298. which just sets check_domain to False in the class definition, and
  1299. choose which one to pass to do_open.
  1300. """
  1301. result = HTTPSConnection(*args, **kwargs)
  1302. if self.ca_certs:
  1303. result.ca_certs = self.ca_certs
  1304. result.check_domain = self.check_domain
  1305. return result
  1306. def https_open(self, req):
  1307. try:
  1308. return self.do_open(self._conn_maker, req)
  1309. except URLError as e:
  1310. if 'certificate verify failed' in str(e.reason):
  1311. raise CertificateError('Unable to verify server certificate '
  1312. 'for %s' % req.host)
  1313. else:
  1314. raise
  1315. #
  1316. # To prevent against mixing HTTP traffic with HTTPS (examples: A Man-In-The-
  1317. # Middle proxy using HTTP listens on port 443, or an index mistakenly serves
  1318. # HTML containing a http://xyz link when it should be https://xyz),
  1319. # you can use the following handler class, which does not allow HTTP traffic.
  1320. #
  1321. # It works by inheriting from HTTPHandler - so build_opener won't add a
  1322. # handler for HTTP itself.
  1323. #
  1324. class HTTPSOnlyHandler(HTTPSHandler, HTTPHandler):
  1325. def http_open(self, req):
  1326. raise URLError('Unexpected HTTP request on what should be a secure '
  1327. 'connection: %s' % req)
  1328. #
  1329. # XML-RPC with timeouts
  1330. #
  1331. class Transport(xmlrpclib.Transport):
  1332. def __init__(self, timeout, use_datetime=0):
  1333. self.timeout = timeout
  1334. xmlrpclib.Transport.__init__(self, use_datetime)
  1335. def make_connection(self, host):
  1336. h, eh, x509 = self.get_host_info(host)
  1337. if not self._connection or host != self._connection[0]:
  1338. self._extra_headers = eh
  1339. self._connection = host, httplib.HTTPConnection(h)
  1340. return self._connection[1]
  1341. if ssl:
  1342. class SafeTransport(xmlrpclib.SafeTransport):
  1343. def __init__(self, timeout, use_datetime=0):
  1344. self.timeout = timeout
  1345. xmlrpclib.SafeTransport.__init__(self, use_datetime)
  1346. def make_connection(self, host):
  1347. h, eh, kwargs = self.get_host_info(host)
  1348. if not kwargs:
  1349. kwargs = {}
  1350. kwargs['timeout'] = self.timeout
  1351. if not self._connection or host != self._connection[0]:
  1352. self._extra_headers = eh
  1353. self._connection = host, httplib.HTTPSConnection(h, None, **kwargs)
  1354. return self._connection[1]
  1355. class ServerProxy(xmlrpclib.ServerProxy):
  1356. def __init__(self, uri, **kwargs):
  1357. self.timeout = timeout = kwargs.pop('timeout', None)
  1358. # The above classes only come into play if a timeout
  1359. # is specified
  1360. if timeout is not None:
  1361. # scheme = splittype(uri) # deprecated as of Python 3.8
  1362. scheme = urlparse(uri)[0]
  1363. use_datetime = kwargs.get('use_datetime', 0)
  1364. if scheme == 'https':
  1365. tcls = SafeTransport
  1366. else:
  1367. tcls = Transport
  1368. kwargs['transport'] = t = tcls(timeout, use_datetime=use_datetime)
  1369. self.transport = t
  1370. xmlrpclib.ServerProxy.__init__(self, uri, **kwargs)
  1371. #
  1372. # CSV functionality. This is provided because on 2.x, the csv module can't
  1373. # handle Unicode. However, we need to deal with Unicode in e.g. RECORD files.
  1374. #
  1375. def _csv_open(fn, mode, **kwargs):
  1376. if sys.version_info[0] < 3:
  1377. mode += 'b'
  1378. else:
  1379. kwargs['newline'] = ''
  1380. # Python 3 determines encoding from locale. Force 'utf-8'
  1381. # file encoding to match other forced utf-8 encoding
  1382. kwargs['encoding'] = 'utf-8'
  1383. return open(fn, mode, **kwargs)
  1384. class CSVBase(object):
  1385. defaults = {
  1386. 'delimiter': str(','), # The strs are used because we need native
  1387. 'quotechar': str('"'), # str in the csv API (2.x won't take
  1388. 'lineterminator': str('\n') # Unicode)
  1389. }
  1390. def __enter__(self):
  1391. return self
  1392. def __exit__(self, *exc_info):
  1393. self.stream.close()
  1394. class CSVReader(CSVBase):
  1395. def __init__(self, **kwargs):
  1396. if 'stream' in kwargs:
  1397. stream = kwargs['stream']
  1398. if sys.version_info[0] >= 3:
  1399. # needs to be a text stream
  1400. stream = codecs.getreader('utf-8')(stream)
  1401. self.stream = stream
  1402. else:
  1403. self.stream = _csv_open(kwargs['path'], 'r')
  1404. self.reader = csv.reader(self.stream, **self.defaults)
  1405. def __iter__(self):
  1406. return self
  1407. def next(self):
  1408. result = next(self.reader)
  1409. if sys.version_info[0] < 3:
  1410. for i, item in enumerate(result):
  1411. if not isinstance(item, text_type):
  1412. result[i] = item.decode('utf-8')
  1413. return result
  1414. __next__ = next
  1415. class CSVWriter(CSVBase):
  1416. def __init__(self, fn, **kwargs):
  1417. self.stream = _csv_open(fn, 'w')
  1418. self.writer = csv.writer(self.stream, **self.defaults)
  1419. def writerow(self, row):
  1420. if sys.version_info[0] < 3:
  1421. r = []
  1422. for item in row:
  1423. if isinstance(item, text_type):
  1424. item = item.encode('utf-8')
  1425. r.append(item)
  1426. row = r
  1427. self.writer.writerow(row)
  1428. #
  1429. # Configurator functionality
  1430. #
  1431. class Configurator(BaseConfigurator):
  1432. value_converters = dict(BaseConfigurator.value_converters)
  1433. value_converters['inc'] = 'inc_convert'
  1434. def __init__(self, config, base=None):
  1435. super(Configurator, self).__init__(config)
  1436. self.base = base or os.getcwd()
  1437. def configure_custom(self, config):
  1438. def convert(o):
  1439. if isinstance(o, (list, tuple)):
  1440. result = type(o)([convert(i) for i in o])
  1441. elif isinstance(o, dict):
  1442. if '()' in o:
  1443. result = self.configure_custom(o)
  1444. else:
  1445. result = {}
  1446. for k in o:
  1447. result[k] = convert(o[k])
  1448. else:
  1449. result = self.convert(o)
  1450. return result
  1451. c = config.pop('()')
  1452. if not callable(c):
  1453. c = self.resolve(c)
  1454. props = config.pop('.', None)
  1455. # Check for valid identifiers
  1456. args = config.pop('[]', ())
  1457. if args:
  1458. args = tuple([convert(o) for o in args])
  1459. items = [(k, convert(config[k])) for k in config if valid_ident(k)]
  1460. kwargs = dict(items)
  1461. result = c(*args, **kwargs)
  1462. if props:
  1463. for n, v in props.items():
  1464. setattr(result, n, convert(v))
  1465. return result
  1466. def __getitem__(self, key):
  1467. result = self.config[key]
  1468. if isinstance(result, dict) and '()' in result:
  1469. self.config[key] = result = self.configure_custom(result)
  1470. return result
  1471. def inc_convert(self, value):
  1472. """Default converter for the inc:// protocol."""
  1473. if not os.path.isabs(value):
  1474. value = os.path.join(self.base, value)
  1475. with codecs.open(value, 'r', encoding='utf-8') as f:
  1476. result = json.load(f)
  1477. return result
  1478. class SubprocessMixin(object):
  1479. """
  1480. Mixin for running subprocesses and capturing their output
  1481. """
  1482. def __init__(self, verbose=False, progress=None):
  1483. self.verbose = verbose
  1484. self.progress = progress
  1485. def reader(self, stream, context):
  1486. """
  1487. Read lines from a subprocess' output stream and either pass to a progress
  1488. callable (if specified) or write progress information to sys.stderr.
  1489. """
  1490. progress = self.progress
  1491. verbose = self.verbose
  1492. while True:
  1493. s = stream.readline()
  1494. if not s:
  1495. break
  1496. if progress is not None:
  1497. progress(s, context)
  1498. else:
  1499. if not verbose:
  1500. sys.stderr.write('.')
  1501. else:
  1502. sys.stderr.write(s.decode('utf-8'))
  1503. sys.stderr.flush()
  1504. stream.close()
  1505. def run_command(self, cmd, **kwargs):
  1506. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
  1507. t1 = threading.Thread(target=self.reader, args=(p.stdout, 'stdout'))
  1508. t1.start()
  1509. t2 = threading.Thread(target=self.reader, args=(p.stderr, 'stderr'))
  1510. t2.start()
  1511. p.wait()
  1512. t1.join()
  1513. t2.join()
  1514. if self.progress is not None:
  1515. self.progress('done.', 'main')
  1516. elif self.verbose:
  1517. sys.stderr.write('done.\n')
  1518. return p
  1519. def normalize_name(name):
  1520. """Normalize a python package name a la PEP 503"""
  1521. # https://www.python.org/dev/peps/pep-0503/#normalized-names
  1522. return re.sub('[-_.]+', '-', name).lower()
  1523. # def _get_pypirc_command():
  1524. # """
  1525. # Get the distutils command for interacting with PyPI configurations.
  1526. # :return: the command.
  1527. # """
  1528. # from distutils.core import Distribution
  1529. # from distutils.config import PyPIRCCommand
  1530. # d = Distribution()
  1531. # return PyPIRCCommand(d)
  1532. class PyPIRCFile(object):
  1533. DEFAULT_REPOSITORY = 'https://upload.pypi.org/legacy/'
  1534. DEFAULT_REALM = 'pypi'
  1535. def __init__(self, fn=None, url=None):
  1536. if fn is None:
  1537. fn = os.path.join(os.path.expanduser('~'), '.pypirc')
  1538. self.filename = fn
  1539. self.url = url
  1540. def read(self):
  1541. result = {}
  1542. if os.path.exists(self.filename):
  1543. repository = self.url or self.DEFAULT_REPOSITORY
  1544. config = configparser.RawConfigParser()
  1545. config.read(self.filename)
  1546. sections = config.sections()
  1547. if 'distutils' in sections:
  1548. # let's get the list of servers
  1549. index_servers = config.get('distutils', 'index-servers')
  1550. _servers = [server.strip() for server in index_servers.split('\n') if server.strip() != '']
  1551. if _servers == []:
  1552. # nothing set, let's try to get the default pypi
  1553. if 'pypi' in sections:
  1554. _servers = ['pypi']
  1555. else:
  1556. for server in _servers:
  1557. result = {'server': server}
  1558. result['username'] = config.get(server, 'username')
  1559. # optional params
  1560. for key, default in (('repository', self.DEFAULT_REPOSITORY), ('realm', self.DEFAULT_REALM),
  1561. ('password', None)):
  1562. if config.has_option(server, key):
  1563. result[key] = config.get(server, key)
  1564. else:
  1565. result[key] = default
  1566. # work around people having "repository" for the "pypi"
  1567. # section of their config set to the HTTP (rather than
  1568. # HTTPS) URL
  1569. if (server == 'pypi' and repository in (self.DEFAULT_REPOSITORY, 'pypi')):
  1570. result['repository'] = self.DEFAULT_REPOSITORY
  1571. elif (result['server'] != repository and result['repository'] != repository):
  1572. result = {}
  1573. elif 'server-login' in sections:
  1574. # old format
  1575. server = 'server-login'
  1576. if config.has_option(server, 'repository'):
  1577. repository = config.get(server, 'repository')
  1578. else:
  1579. repository = self.DEFAULT_REPOSITORY
  1580. result = {
  1581. 'username': config.get(server, 'username'),
  1582. 'password': config.get(server, 'password'),
  1583. 'repository': repository,
  1584. 'server': server,
  1585. 'realm': self.DEFAULT_REALM
  1586. }
  1587. return result
  1588. def update(self, username, password):
  1589. # import pdb; pdb.set_trace()
  1590. config = configparser.RawConfigParser()
  1591. fn = self.filename
  1592. config.read(fn)
  1593. if not config.has_section('pypi'):
  1594. config.add_section('pypi')
  1595. config.set('pypi', 'username', username)
  1596. config.set('pypi', 'password', password)
  1597. with open(fn, 'w') as f:
  1598. config.write(f)
  1599. def _load_pypirc(index):
  1600. """
  1601. Read the PyPI access configuration as supported by distutils.
  1602. """
  1603. return PyPIRCFile(url=index.url).read()
  1604. def _store_pypirc(index):
  1605. PyPIRCFile().update(index.username, index.password)
  1606. #
  1607. # get_platform()/get_host_platform() copied from Python 3.10.a0 source, with some minor
  1608. # tweaks
  1609. #
  1610. def get_host_platform():
  1611. """Return a string that identifies the current platform. This is used mainly to
  1612. distinguish platform-specific build directories and platform-specific built
  1613. distributions. Typically includes the OS name and version and the
  1614. architecture (as supplied by 'os.uname()'), although the exact information
  1615. included depends on the OS; eg. on Linux, the kernel version isn't
  1616. particularly important.
  1617. Examples of returned values:
  1618. linux-i586
  1619. linux-alpha (?)
  1620. solaris-2.6-sun4u
  1621. Windows will return one of:
  1622. win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
  1623. win32 (all others - specifically, sys.platform is returned)
  1624. For other non-POSIX platforms, currently just returns 'sys.platform'.
  1625. """
  1626. if os.name == 'nt':
  1627. if 'amd64' in sys.version.lower():
  1628. return 'win-amd64'
  1629. if '(arm)' in sys.version.lower():
  1630. return 'win-arm32'
  1631. if '(arm64)' in sys.version.lower():
  1632. return 'win-arm64'
  1633. return sys.platform
  1634. # Set for cross builds explicitly
  1635. if "_PYTHON_HOST_PLATFORM" in os.environ:
  1636. return os.environ["_PYTHON_HOST_PLATFORM"]
  1637. if os.name != 'posix' or not hasattr(os, 'uname'):
  1638. # XXX what about the architecture? NT is Intel or Alpha,
  1639. # Mac OS is M68k or PPC, etc.
  1640. return sys.platform
  1641. # Try to distinguish various flavours of Unix
  1642. (osname, host, release, version, machine) = os.uname()
  1643. # Convert the OS name to lowercase, remove '/' characters, and translate
  1644. # spaces (for "Power Macintosh")
  1645. osname = osname.lower().replace('/', '')
  1646. machine = machine.replace(' ', '_').replace('/', '-')
  1647. if osname[:5] == 'linux':
  1648. # At least on Linux/Intel, 'machine' is the processor --
  1649. # i386, etc.
  1650. # XXX what about Alpha, SPARC, etc?
  1651. return "%s-%s" % (osname, machine)
  1652. elif osname[:5] == 'sunos':
  1653. if release[0] >= '5': # SunOS 5 == Solaris 2
  1654. osname = 'solaris'
  1655. release = '%d.%s' % (int(release[0]) - 3, release[2:])
  1656. # We can't use 'platform.architecture()[0]' because a
  1657. # bootstrap problem. We use a dict to get an error
  1658. # if some suspicious happens.
  1659. bitness = {2147483647: '32bit', 9223372036854775807: '64bit'}
  1660. machine += '.%s' % bitness[sys.maxsize]
  1661. # fall through to standard osname-release-machine representation
  1662. elif osname[:3] == 'aix':
  1663. from _aix_support import aix_platform
  1664. return aix_platform()
  1665. elif osname[:6] == 'cygwin':
  1666. osname = 'cygwin'
  1667. rel_re = re.compile(r'[\d.]+', re.ASCII)
  1668. m = rel_re.match(release)
  1669. if m:
  1670. release = m.group()
  1671. elif osname[:6] == 'darwin':
  1672. import _osx_support
  1673. try:
  1674. from distutils import sysconfig
  1675. except ImportError:
  1676. import sysconfig
  1677. osname, release, machine = _osx_support.get_platform_osx(sysconfig.get_config_vars(), osname, release, machine)
  1678. return '%s-%s-%s' % (osname, release, machine)
  1679. _TARGET_TO_PLAT = {
  1680. 'x86': 'win32',
  1681. 'x64': 'win-amd64',
  1682. 'arm': 'win-arm32',
  1683. }
  1684. def get_platform():
  1685. if os.name != 'nt':
  1686. return get_host_platform()
  1687. cross_compilation_target = os.environ.get('VSCMD_ARG_TGT_ARCH')
  1688. if cross_compilation_target not in _TARGET_TO_PLAT:
  1689. return get_host_platform()
  1690. return _TARGET_TO_PLAT[cross_compilation_target]