util.py 53 KB

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