wppm.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright © 2012 Pierre Raybaut
  4. # Licensed under the terms of the MIT License
  5. # (see winpython/__init__.py for details)
  6. """
  7. WinPython Package Manager
  8. Created on Fri Aug 03 14:32:26 2012
  9. """
  10. from __future__ import print_function
  11. import os
  12. import os.path as osp
  13. import shutil
  14. import re
  15. import sys
  16. import subprocess
  17. # Local imports
  18. from winpython import utils
  19. from winpython.config import DATA_PATH
  20. from winpython.py3compat import configparser as cp
  21. # from former wppm separate script launcher
  22. from argparse import ArgumentParser
  23. from winpython import py3compat
  24. # import information reader
  25. # importlib_metadata before Python 3.8
  26. try:
  27. from importlib import metadata as metadata # Python-3.8
  28. metadata = metadata.metadata
  29. except:
  30. try:
  31. from importlib_metadata import metadata # <Python-3.8
  32. except:
  33. metadata = None # nothing available
  34. # Workaround for installing PyVISA on Windows from source:
  35. os.environ['HOME'] = os.environ['USERPROFILE']
  36. # pep503 defines normalized package names: www.python.org/dev/peps/pep-0503
  37. def normalize(name):
  38. """ return normalized (unique) name of a package"""
  39. return re.sub(r"[-_.]+", "-", name).lower()
  40. def get_official_description(name):
  41. """Extract package Summary description from pypi.org"""
  42. from winpython import utils
  43. this = normalize(name)
  44. this_len = len(this)
  45. pip_ask = ['pip', 'search', this, '--retries', '0']
  46. if len(this)<2: # don't ask stupid things
  47. return ''
  48. try:
  49. # .run work when .popen fails when no internet
  50. pip_res = (utils.exec_run_cmd(pip_ask)+'\n').splitlines()
  51. pip_filter = [l for l in pip_res if this + " (" ==
  52. normalize(l[:this_len])+l[this_len:this_len+2]]
  53. pip_desc = (pip_filter[0][len(this)+1:]).split(" - ", 1)[1]
  54. return pip_desc.replace("://", " ")
  55. except:
  56. return ''
  57. def get_package_metadata(database, name, gotoWWW=False, update=False):
  58. """Extract infos (description, url) from the local database"""
  59. # Note: we could use the PyPI database but this has been written on
  60. # machine which is not connected to the internet
  61. # we store only normalized names now (PEP 503)
  62. db = cp.ConfigParser()
  63. db.readfp(open(osp.join(DATA_PATH, database)))
  64. my_metadata = dict(
  65. description='',
  66. url='https://pypi.org/project/' + name,
  67. )
  68. for key in my_metadata:
  69. # wheel replace '-' per '_' in key
  70. for name2 in (name, normalize(name)):
  71. try:
  72. my_metadata[key] = db.get(name2, key)
  73. break
  74. except (cp.NoSectionError, cp.NoOptionError):
  75. pass
  76. db_desc = my_metadata.get('description')
  77. if my_metadata.get('description') == '' and metadata:
  78. # nothing in package.ini, we look in our installed packages
  79. try:
  80. my_metadata['description']=(
  81. metadata(name)['Summary']+'\n').splitlines()[0]
  82. except:
  83. pass
  84. if my_metadata['description'] == '' and gotoWWW:
  85. # still nothing, try look on pypi
  86. the_official = get_official_description(name)
  87. if the_official != '':
  88. my_metadata['description'] = the_official
  89. if update == True and db_desc == '' and my_metadata['description'] != '':
  90. # we add new findings in our packgages.ini list, if it's required
  91. try:
  92. db[normalize(name)] = {}
  93. db[normalize(name)]['description'] = my_metadata['description']
  94. with open(osp.join(DATA_PATH, database), 'w') as configfile:
  95. db.write(configfile)
  96. except:
  97. pass
  98. return my_metadata
  99. class BasePackage(object):
  100. def __init__(self, fname):
  101. self.fname = fname
  102. self.name = None
  103. self.version = None
  104. self.architecture = None
  105. self.pyversion = None
  106. self.description = None
  107. self.url = None
  108. def __str__(self):
  109. text = "%s %s" % (self.name, self.version)
  110. pytext = ""
  111. if self.pyversion is not None:
  112. pytext = " for Python %s" % self.pyversion
  113. if self.architecture is not None:
  114. if not pytext:
  115. pytext = " for Python"
  116. pytext += " %dbits" % self.architecture
  117. text += "%s\n%s\nWebsite: %s\n[%s]" % (
  118. pytext,
  119. self.description,
  120. self.url,
  121. osp.basename(self.fname),
  122. )
  123. return text
  124. def is_compatible_with(self, distribution):
  125. """Return True if package is compatible with distribution in terms of
  126. architecture and Python version (if applyable)"""
  127. iscomp = True
  128. if self.architecture is not None:
  129. # Source distributions (not yet supported though)
  130. iscomp = (
  131. iscomp
  132. and self.architecture
  133. == distribution.architecture
  134. )
  135. if self.pyversion is not None:
  136. # Non-pure Python package
  137. iscomp = (
  138. iscomp
  139. and self.pyversion == distribution.version
  140. )
  141. return iscomp
  142. def extract_optional_infos(self, update=False):
  143. """Extract package optional infos (description, url)
  144. from the package database"""
  145. metadata = get_package_metadata(
  146. 'packages.ini', self.name, True, update=update
  147. )
  148. for key, value in list(metadata.items()):
  149. setattr(self, key, value)
  150. class Package(BasePackage):
  151. def __init__(self, fname, update=False):
  152. BasePackage.__init__(self, fname)
  153. self.files = []
  154. self.extract_infos()
  155. self.extract_optional_infos(update=update)
  156. def extract_infos(self):
  157. """Extract package infos (name, version, architecture)
  158. from filename (installer basename)"""
  159. bname = osp.basename(self.fname)
  160. if bname.endswith(('32.whl', '64.whl')):
  161. # {name}[-{bloat}]-{version}-{python tag}-{abi tag}-{platform tag}.whl
  162. # ['sounddevice','0.3.5','py2.py3.cp34.cp35','none','win32']
  163. # PyQt5-5.7.1-5.7.1-cp34.cp35.cp36-none-win_amd64.whl
  164. bname2 = bname[:-4].split("-")
  165. self.name = bname2[0]
  166. self.version = '-'.join(list(bname2[1:-3]))
  167. self.pywheel, abi, arch = bname2[-3:]
  168. self.pyversion = (
  169. None
  170. ) # Let's ignore this self.pywheel
  171. # wheel arch is 'win32' or 'win_amd64'
  172. self.architecture = (
  173. 32 if arch == 'win32' else 64
  174. )
  175. return
  176. elif bname.endswith(('.zip', '.tar.gz', '.whl')):
  177. # distutils sdist
  178. infos = utils.get_source_package_infos(bname)
  179. if infos is not None:
  180. self.name, self.version = infos
  181. return
  182. raise NotImplementedError(
  183. "Not supported package type %s" % bname
  184. )
  185. class WininstPackage(BasePackage):
  186. def __init__(self, fname, distribution):
  187. BasePackage.__init__(self, fname)
  188. self.logname = None
  189. self.distribution = distribution
  190. self.architecture = distribution.architecture
  191. self.pyversion = distribution.version
  192. self.extract_infos()
  193. self.extract_optional_infos()
  194. def extract_infos(self):
  195. """Extract package infos (name, version, architecture)"""
  196. match = re.match(
  197. r'Remove([a-zA-Z0-9\-\_\.]*)\.exe', self.fname
  198. )
  199. if match is None:
  200. return
  201. self.name = match.groups()[0]
  202. self.logname = '%s-wininst.log' % self.name
  203. fd = open(
  204. osp.join(
  205. self.distribution.target, self.logname
  206. ),
  207. 'U',
  208. )
  209. searchtxt = 'DisplayName='
  210. for line in fd.readlines():
  211. pos = line.find(searchtxt)
  212. if pos != -1:
  213. break
  214. else:
  215. return
  216. fd.close()
  217. match = re.match(
  218. r'Python %s %s-([0-9\.]*)'
  219. % (self.pyversion, self.name),
  220. line[pos + len(searchtxt) :],
  221. )
  222. if match is None:
  223. return
  224. self.version = match.groups()[0]
  225. def uninstall(self):
  226. """Uninstall package"""
  227. subprocess.call(
  228. [self.fname, '-u', self.logname],
  229. cwd=self.distribution.target,
  230. )
  231. class Distribution(object):
  232. def __init__(
  233. self, target=None, verbose=False, indent=False
  234. ):
  235. self.target = target
  236. self.verbose = verbose
  237. self.indent = indent
  238. # if no target path given, take the current python interpreter one
  239. if self.target is None:
  240. self.target = os.path.dirname(sys.executable)
  241. self.to_be_removed = (
  242. []
  243. ) # list of directories to be removed later
  244. self.version, self.architecture = utils.get_python_infos(
  245. target
  246. )
  247. def clean_up(self):
  248. """Remove directories which couldn't be removed when building"""
  249. for path in self.to_be_removed:
  250. try:
  251. shutil.rmtree(path, onerror=utils.onerror)
  252. except WindowsError:
  253. print(
  254. "Directory %s could not be removed"
  255. % path,
  256. file=sys.stderr,
  257. )
  258. def remove_directory(self, path):
  259. """Try to remove directory -- on WindowsError, remove it later"""
  260. try:
  261. shutil.rmtree(path)
  262. except WindowsError:
  263. self.to_be_removed.append(path)
  264. def copy_files(
  265. self,
  266. package,
  267. targetdir,
  268. srcdir,
  269. dstdir,
  270. create_bat_files=False,
  271. ):
  272. """Add copy task"""
  273. srcdir = osp.join(targetdir, srcdir)
  274. if not osp.isdir(srcdir):
  275. return
  276. offset = len(srcdir) + len(os.pathsep)
  277. for dirpath, dirnames, filenames in os.walk(srcdir):
  278. for dname in dirnames:
  279. t_dname = osp.join(dirpath, dname)[offset:]
  280. src = osp.join(srcdir, t_dname)
  281. dst = osp.join(dstdir, t_dname)
  282. if self.verbose:
  283. print("mkdir: %s" % dst)
  284. full_dst = osp.join(self.target, dst)
  285. if not osp.exists(full_dst):
  286. os.mkdir(full_dst)
  287. package.files.append(dst)
  288. for fname in filenames:
  289. t_fname = osp.join(dirpath, fname)[offset:]
  290. src = osp.join(srcdir, t_fname)
  291. if dirpath.endswith('_system32'):
  292. # Files that should be copied in %WINDIR%\system32
  293. dst = fname
  294. else:
  295. dst = osp.join(dstdir, t_fname)
  296. if self.verbose:
  297. print("file: %s" % dst)
  298. full_dst = osp.join(self.target, dst)
  299. shutil.move(src, full_dst)
  300. package.files.append(dst)
  301. name, ext = osp.splitext(dst)
  302. if create_bat_files and ext in ('', '.py'):
  303. dst = name + '.bat'
  304. if self.verbose:
  305. print("file: %s" % dst)
  306. full_dst = osp.join(self.target, dst)
  307. fd = open(full_dst, 'w')
  308. fd.write(
  309. """@echo off
  310. python "%~dpn0"""
  311. + ext
  312. + """" %*"""
  313. )
  314. fd.close()
  315. package.files.append(dst)
  316. def create_file(self, package, name, dstdir, contents):
  317. """Generate data file -- path is relative to distribution root dir"""
  318. dst = osp.join(dstdir, name)
  319. if self.verbose:
  320. print("create: %s" % dst)
  321. full_dst = osp.join(self.target, dst)
  322. open(full_dst, 'w').write(contents)
  323. package.files.append(dst)
  324. def get_installed_packages(self, update=False):
  325. """Return installed packages"""
  326. # Include package installed via pip (not via WPPM)
  327. wppm = []
  328. try:
  329. if (
  330. os.path.dirname(sys.executable)
  331. == self.target
  332. ):
  333. # direct way: we interrogate ourself, using official API
  334. import pkg_resources, imp
  335. imp.reload(pkg_resources)
  336. pip_list = [
  337. (i.key, i.version)
  338. for i in pkg_resources.working_set
  339. ]
  340. else:
  341. # indirect way: we interrogate something else
  342. cmdx = [
  343. osp.join(self.target, 'python.exe'),
  344. '-c',
  345. "import pip;from pip._internal.utils.misc import get_installed_distributions as pip_get_installed_distributions ;print('+!+'.join(['%s@+@%s@+@' % (i.key,i.version) for i in pip_get_installed_distributions()]))",
  346. ]
  347. p = subprocess.Popen(
  348. cmdx,
  349. shell=True,
  350. stdout=subprocess.PIPE,
  351. cwd=self.target,
  352. )
  353. stdout, stderr = p.communicate()
  354. start_at = (
  355. 2 if sys.version_info >= (3, 0) else 0
  356. )
  357. pip_list = [
  358. line.split("@+@")[:2]
  359. for line in ("%s" % stdout)[
  360. start_at:
  361. ].split("+!+")
  362. ]
  363. # there are only Packages installed with pip now
  364. # create pip package list
  365. wppm = [
  366. Package(
  367. '%s-%s-py2.py3-none-any.whl'
  368. % (i[0].replace('-', '_').lower(), i[1])
  369. , update=update)
  370. for i in pip_list
  371. ]
  372. except:
  373. pass
  374. return sorted(
  375. wppm, key=lambda tup: tup.name.lower()
  376. )
  377. def find_package(self, name):
  378. """Find installed package"""
  379. for pack in self.get_installed_packages():
  380. if normalize(pack.name) == normalize(name):
  381. return pack
  382. def uninstall_existing(self, package):
  383. """Uninstall existing package (or package name)"""
  384. if isinstance(package, str):
  385. pack = self.find_package(package)
  386. else:
  387. pack = self.find_package(package.name)
  388. if pack is not None:
  389. self.uninstall(pack)
  390. def patch_all_shebang(
  391. self,
  392. to_movable=True,
  393. max_exe_size=999999,
  394. targetdir="",
  395. ):
  396. """make all python launchers relatives"""
  397. import glob
  398. import os
  399. for ffname in glob.glob(
  400. r'%s\Scripts\*.exe' % self.target
  401. ):
  402. size = os.path.getsize(ffname)
  403. if size <= max_exe_size:
  404. utils.patch_shebang_line(
  405. ffname,
  406. to_movable=to_movable,
  407. targetdir=targetdir,
  408. )
  409. for ffname in glob.glob(
  410. r'%s\Scripts\*.py' % self.target
  411. ):
  412. utils.patch_shebang_line_py(
  413. ffname,
  414. to_movable=to_movable,
  415. targetdir=targetdir,
  416. )
  417. def install(self, package, install_options=None):
  418. """Install package in distribution"""
  419. assert package.is_compatible_with(self)
  420. # wheel addition
  421. if package.fname.endswith(
  422. ('.whl', '.tar.gz', '.zip')
  423. ):
  424. self.install_bdist_direct(
  425. package, install_options=install_options
  426. )
  427. self.handle_specific_packages(package)
  428. # minimal post-install actions
  429. self.patch_standard_packages(package.name)
  430. def do_pip_action(
  431. self, actions=None, install_options=None
  432. ):
  433. """Do pip action in a distribution"""
  434. my_list = install_options
  435. if my_list is None:
  436. my_list = []
  437. my_actions = actions
  438. if my_actions is None:
  439. my_actions = []
  440. executing = osp.join(
  441. self.target, '..', 'scripts', 'env.bat'
  442. )
  443. if osp.isfile(executing):
  444. complement = [
  445. r'&&',
  446. 'cd',
  447. '/D',
  448. self.target,
  449. r'&&',
  450. osp.join(self.target, 'python.exe'),
  451. ]
  452. complement += ['-m', 'pip']
  453. else:
  454. executing = osp.join(self.target, 'python.exe')
  455. complement = ['-m', 'pip']
  456. try:
  457. fname = utils.do_script(
  458. this_script=None,
  459. python_exe=executing,
  460. architecture=self.architecture,
  461. verbose=self.verbose,
  462. install_options=complement
  463. + my_actions
  464. + my_list,
  465. )
  466. except RuntimeError:
  467. if not self.verbose:
  468. print("Failed!")
  469. raise
  470. def patch_standard_packages(
  471. self, package_name='', to_movable=True
  472. ):
  473. """patch Winpython packages in need"""
  474. import filecmp
  475. # 'pywin32' minimal post-install (pywin32_postinstall.py do too much)
  476. if (
  477. package_name.lower() == "pywin32"
  478. or package_name == ''
  479. ):
  480. origin = self.target + (
  481. r"\Lib\site-packages\pywin32_system32"
  482. )
  483. destin = self.target
  484. if osp.isdir(origin):
  485. for name in os.listdir(origin):
  486. here, there = (
  487. osp.join(origin, name),
  488. osp.join(destin, name),
  489. )
  490. if not os.path.exists(
  491. there
  492. ) or not filecmp.cmp(here, there):
  493. shutil.copyfile(here, there)
  494. # 'pip' to do movable launchers (around line 100) !!!!
  495. # rational: https://github.com/pypa/pip/issues/2328
  496. if (
  497. package_name.lower() == "pip"
  498. or package_name == ''
  499. ):
  500. # ensure pip will create movable launchers
  501. # sheb_mov1 = classic way up to WinPython 2016-01
  502. # sheb_mov2 = tried way, but doesn't work for pip (at least)
  503. sheb_fix = " executable = get_executable()"
  504. sheb_mov1 = " executable = os.path.join(os.path.basename(get_executable()))"
  505. sheb_mov2 = " executable = os.path.join('..',os.path.basename(get_executable()))"
  506. if to_movable:
  507. utils.patch_sourcefile(
  508. self.target
  509. + r"\Lib\site-packages\pip\_vendor\distlib\scripts.py",
  510. sheb_fix,
  511. sheb_mov1,
  512. )
  513. utils.patch_sourcefile(
  514. self.target
  515. + r"\Lib\site-packages\pip\_vendor\distlib\scripts.py",
  516. sheb_mov2,
  517. sheb_mov1,
  518. )
  519. else:
  520. utils.patch_sourcefile(
  521. self.target
  522. + r"\Lib\site-packages\pip\_vendor\distlib\scripts.py",
  523. sheb_mov1,
  524. sheb_fix,
  525. )
  526. utils.patch_sourcefile(
  527. self.target
  528. + r"\Lib\site-packages\pip\_vendor\distlib\scripts.py",
  529. sheb_mov2,
  530. sheb_fix,
  531. )
  532. # ensure pip wheel will register relative PATH in 'RECORD' files
  533. # will be in standard pip 8.0.3
  534. utils.patch_sourcefile(
  535. self.target
  536. + (r"\Lib\site-packages\pip\wheel.py"),
  537. " writer.writerow((f, h, l))",
  538. " writer.writerow((normpath(f, lib_dir), h, l))",
  539. )
  540. # create movable launchers for previous package installations
  541. self.patch_all_shebang(to_movable=to_movable)
  542. if (
  543. package_name.lower() == "spyder"
  544. or package_name == ''
  545. ):
  546. # spyder don't goes on internet without I ask
  547. utils.patch_sourcefile(
  548. self.target
  549. + (
  550. r"\Lib\site-packages\spyderlib\config\main.py"
  551. ),
  552. "'check_updates_on_startup': True,",
  553. "'check_updates_on_startup': False,",
  554. )
  555. utils.patch_sourcefile(
  556. self.target
  557. + (
  558. r"\Lib\site-packages\spyder\config\main.py"
  559. ),
  560. "'check_updates_on_startup': True,",
  561. "'check_updates_on_startup': False,",
  562. )
  563. # workaround bad installers
  564. if package_name.lower() == "numba":
  565. self.create_pybat(['numba', 'pycc'])
  566. else:
  567. self.create_pybat(package_name.lower())
  568. def create_pybat(
  569. self,
  570. names='',
  571. contents=r"""@echo off
  572. ..\python "%~dpn0" %*""",
  573. ):
  574. """Create launcher batch script when missing"""
  575. scriptpy = osp.join(
  576. self.target, 'Scripts'
  577. ) # std Scripts of python
  578. if not list(names) == names:
  579. my_list = [
  580. f
  581. for f in os.listdir(scriptpy)
  582. if '.' not in f and f.startswith(names)
  583. ]
  584. else:
  585. my_list = names
  586. for name in my_list:
  587. if osp.isdir(scriptpy) and osp.isfile(
  588. osp.join(scriptpy, name)
  589. ):
  590. if not osp.isfile(
  591. osp.join(scriptpy, name + '.exe')
  592. ) and not osp.isfile(
  593. osp.join(scriptpy, name + '.bat')
  594. ):
  595. fd = open(
  596. osp.join(scriptpy, name + '.bat'),
  597. 'w',
  598. )
  599. fd.write(contents)
  600. fd.close()
  601. def handle_specific_packages(self, package):
  602. """Packages requiring additional configuration"""
  603. if package.name.lower() in (
  604. 'pyqt4',
  605. 'pyqt5',
  606. 'pyside2',
  607. ):
  608. # Qt configuration file (where to find Qt)
  609. name = 'qt.conf'
  610. contents = """[Paths]
  611. Prefix = .
  612. Binaries = ."""
  613. self.create_file(
  614. package,
  615. name,
  616. osp.join(
  617. 'Lib', 'site-packages', package.name
  618. ),
  619. contents,
  620. )
  621. self.create_file(
  622. package,
  623. name,
  624. '.',
  625. contents.replace(
  626. '.',
  627. './Lib/site-packages/%s' % package.name,
  628. ),
  629. )
  630. # pyuic script
  631. if package.name.lower() == 'pyqt5':
  632. # see http://code.activestate.com/lists/python-list/666469/
  633. tmp_string = r'''@echo off
  634. if "%WINPYDIR%"=="" call "%~dp0..\..\scripts\env.bat"
  635. "%WINPYDIR%\python.exe" -m PyQt5.uic.pyuic %1 %2 %3 %4 %5 %6 %7 %8 %9'''
  636. else:
  637. tmp_string = r'''@echo off
  638. if "%WINPYDIR%"=="" call "%~dp0..\..\scripts\env.bat"
  639. "%WINPYDIR%\python.exe" "%WINPYDIR%\Lib\site-packages\package.name\uic\pyuic.py" %1 %2 %3 %4 %5 %6 %7 %8 %9'''
  640. self.create_file(
  641. package,
  642. 'pyuic%s.bat' % package.name[-1],
  643. 'Scripts',
  644. tmp_string.replace(
  645. 'package.name', package.name
  646. ),
  647. )
  648. # Adding missing __init__.py files (fixes Issue 8)
  649. uic_path = osp.join(
  650. 'Lib', 'site-packages', package.name, 'uic'
  651. )
  652. for dirname in ('Loader', 'port_v2', 'port_v3'):
  653. self.create_file(
  654. package,
  655. '__init__.py',
  656. osp.join(uic_path, dirname),
  657. '',
  658. )
  659. def _print(self, package, action):
  660. """Print package-related action text (e.g. 'Installing')
  661. indicating progress"""
  662. text = " ".join(
  663. [action, package.name, package.version]
  664. )
  665. if self.verbose:
  666. utils.print_box(text)
  667. else:
  668. if self.indent:
  669. text = (' ' * 4) + text
  670. print(text + '...', end=" ")
  671. def _print_done(self):
  672. """Print OK at the end of a process"""
  673. if not self.verbose:
  674. print("OK")
  675. def uninstall(self, package):
  676. """Uninstall package from distribution"""
  677. self._print(package, "Uninstalling")
  678. if not package.name == 'pip':
  679. # trick to get true target (if not current)
  680. this_executable_path = self.target
  681. subprocess.call(
  682. [
  683. this_executable_path + r'\python.exe',
  684. '-m',
  685. 'pip',
  686. 'uninstall',
  687. package.name,
  688. '-y',
  689. ],
  690. cwd=this_executable_path,
  691. )
  692. # no more legacy, no package are installed by old non-pip means
  693. self._print_done()
  694. def install_bdist_direct(
  695. self, package, install_options=None
  696. ):
  697. """Install a package directly !"""
  698. self._print(
  699. package,
  700. "Installing %s" % package.fname.split(".")[-1],
  701. )
  702. try:
  703. fname = utils.direct_pip_install(
  704. package.fname,
  705. python_exe=osp.join(
  706. self.target, 'python.exe'
  707. ),
  708. architecture=self.architecture,
  709. verbose=self.verbose,
  710. install_options=install_options,
  711. )
  712. except RuntimeError:
  713. if not self.verbose:
  714. print("Failed!")
  715. raise
  716. package = Package(fname)
  717. self._print_done()
  718. def install_script(self, script, install_options=None):
  719. try:
  720. fname = utils.do_script(
  721. script,
  722. python_exe=osp.join(
  723. self.target, 'python.exe'
  724. ),
  725. architecture=self.architecture,
  726. verbose=self.verbose,
  727. install_options=install_options,
  728. )
  729. except RuntimeError:
  730. if not self.verbose:
  731. print("Failed!")
  732. raise
  733. def main(test=False):
  734. if test:
  735. sbdir = osp.join(
  736. osp.dirname(__file__),
  737. os.pardir,
  738. os.pardir,
  739. os.pardir,
  740. 'sandbox',
  741. )
  742. tmpdir = osp.join(sbdir, 'tobedeleted')
  743. # fname = osp.join(tmpdir, 'scipy-0.10.1.win-amd64-py2.7.exe')
  744. fname = osp.join(
  745. sbdir, 'VTK-5.10.0-Qt-4.7.4.win32-py2.7.exe'
  746. )
  747. print(Package(fname))
  748. sys.exit()
  749. target = osp.join(
  750. utils.BASE_DIR,
  751. 'build',
  752. 'winpython-2.7.3',
  753. 'python-2.7.3',
  754. )
  755. fname = osp.join(
  756. utils.BASE_DIR,
  757. 'packages.src',
  758. 'docutils-0.9.1.tar.gz',
  759. )
  760. dist = Distribution(target, verbose=True)
  761. pack = Package(fname)
  762. print(pack.description)
  763. # dist.install(pack)
  764. # dist.uninstall(pack)
  765. else:
  766. parser = ArgumentParser(
  767. description="WinPython Package Manager: install, "
  768. "uninstall or upgrade Python packages on a Windows "
  769. "Python distribution like WinPython."
  770. )
  771. parser.add_argument(
  772. 'fname',
  773. metavar='package',
  774. type=str if py3compat.PY3 else unicode,
  775. help='path to a Python package',
  776. )
  777. parser.add_argument(
  778. '-t',
  779. '--target',
  780. dest='target',
  781. default=sys.prefix,
  782. help='path to target Python distribution '
  783. '(default: "%s")' % sys.prefix,
  784. )
  785. parser.add_argument(
  786. '-i',
  787. '--install',
  788. dest='install',
  789. action='store_const',
  790. const=True,
  791. default=False,
  792. help='install package (this is the default action)',
  793. )
  794. parser.add_argument(
  795. '-u',
  796. '--uninstall',
  797. dest='uninstall',
  798. action='store_const',
  799. const=True,
  800. default=False,
  801. help='uninstall package',
  802. )
  803. args = parser.parse_args()
  804. if args.install and args.uninstall:
  805. raise RuntimeError(
  806. "Incompatible arguments: --install and --uninstall"
  807. )
  808. if not args.install and not args.uninstall:
  809. args.install = True
  810. if not osp.isfile(args.fname) and args.install:
  811. raise IOError("File not found: %s" % args.fname)
  812. if utils.is_python_distribution(args.target):
  813. dist = Distribution(args.target)
  814. try:
  815. if args.uninstall:
  816. package = dist.find_package(args.fname)
  817. dist.uninstall(package)
  818. else:
  819. package = Package(args.fname)
  820. if (
  821. args.install
  822. and package.is_compatible_with(dist)
  823. ):
  824. dist.install(package)
  825. else:
  826. raise RuntimeError(
  827. "Package is not compatible with Python "
  828. "%s %dbit"
  829. % (
  830. dist.version,
  831. dist.architecture,
  832. )
  833. )
  834. except NotImplementedError:
  835. raise RuntimeError(
  836. "Package is not (yet) supported by WPPM"
  837. )
  838. else:
  839. raise WindowsError(
  840. "Invalid Python distribution %s"
  841. % args.target
  842. )
  843. if __name__ == '__main__':
  844. main()