forked from ywangd/stash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpip.py
1541 lines (1298 loc) · 57.9 KB
/
pip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Install and manage python packages
usage: pip.py [-h] [--verbose] sub-command ...
optional arguments:
-h, --help show this help message and exit
--verbose be more chatty
List of sub-commands:
sub-command "pip sub-command -h" for more help on a sub-command
list list packages installed
install install packages
download download packages
search search with the given word fragment
versions find versions available for the given package
uninstall uninstall packages
update update an installed package
"""
from __future__ import print_function
import sys
import os
import ast
import shutil
import types
import contextlib
import requests
import operator
import traceback
import platform
import six
from distutils.util import convert_path
from fnmatch import fnmatchcase
# noinspection PyUnresolvedReferences
from six.moves import filterfalse
from stashutils.extensions import create_command
from stashutils.wheels import Wheel, wheel_is_compatible
from stash.system.shcommon import IN_PYTHONISTA
_stash = globals()['_stash']
VersionSpecifier = _stash.libversion.VersionSpecifier # alias for readability
try:
unicode
except NameError:
unicode = str
if IN_PYTHONISTA:
PYTHONISTA_BUNDLED_MODULES = [
'bottle', 'beautifulsoup4', 'pycrypto', 'py-dateutil',
'dropbox', 'ecdsa', 'evernote', 'Faker', 'feedparser', 'flask', 'html2text',
'html5lib', 'httplib2', 'itsdangerous', 'jedi', 'jinja2', 'markdown', 'markdown2',
'matplotlib', 'mechanize', 'midiutil', 'mpmath', 'numpy', 'oauth2', 'paramiko',
'parsedatetime', 'Pillow', 'pycparser', 'pyflakes', 'pygments', 'pyparsing',
'PyPDF2', 'pytz', 'qrcode', 'reportlab', 'requests', 'simpy', 'six', 'sqlalchemy',
'pysqlite', 'sympy', 'thrift', 'werkzeug', 'wsgiref', 'pisa', 'xmltodict', 'PyYAML',
]
if _stash.PY3:
SITE_PACKAGES_DIR_NAME = 'site-packages-3'
else:
SITE_PACKAGES_DIR_NAME = 'site-packages-2'
OLD_SITE_PACKAGES_DIR_NAME = 'site-packages'
SITE_PACKAGES_FOLDER = os.path.expanduser('~/Documents/{}'.format(SITE_PACKAGES_DIR_NAME))
OLD_SITE_PACKAGES_FOLDER = os.path.expanduser('~/Documents/{}'.format(OLD_SITE_PACKAGES_DIR_NAME))
else:
PYTHONISTA_BUNDLED_MODULES = []
SITE_PACKAGES_FOLDER = os.path.expandvars("$STASH_ROOT/lib/")
OLD_SITE_PACKAGES_FOLDER = os.path.expandvars("$STASH_ROOT/lib/")
SITE_PACKAGES_DIR_NAME = os.path.basename(SITE_PACKAGES_FOLDER)
OLD_SITE_PACKAGES_DIR_NAME = os.path.basename(OLD_SITE_PACKAGES_FOLDER)
# Some packages use wrong name for their dependencies
PACKAGE_NAME_FIXER = {
'lazy_object_proxy': 'lazy-object-proxy',
}
NO_OVERWRITE = False
# Utility constants
DIST_ALLOW_SRC = 1
DIST_ALLOW_WHL = 2
DIST_PREFER_SRC = 4
DIST_PREFER_WHL = 8
DIST_DEFAULT = DIST_ALLOW_SRC | DIST_ALLOW_WHL | DIST_PREFER_WHL
def _setup_stub_(*args, **kwargs):
setuptools = sys.modules['setuptools']
setuptools._setup_params_ = (args, kwargs)
class PipError(Exception):
pass
class PackageAlreadyInstalled(PipError):
"""Error raised when a package is already installed."""
pass
class OmniClass(object):
def __init__(self, *args, **kwargs):
pass
def __call__(self, *args, **kwargs):
return OmniClass()
def __getattr__(self, item):
return OmniClass()
def __getitem__(self, item):
return OmniClass()
class PackageFinder(object):
"""
This class is copied from setuptools
"""
@classmethod
def find(cls, where='.', exclude=(), include=('*',)):
"""Return a list all Python packages found within directory 'where'
'where' should be supplied as a "cross-platform" (i.e. URL-style)
path; it will be converted to the appropriate local path syntax.
'exclude' is a sequence of package names to exclude; '*' can be used
as a wildcard in the names, such that 'foo.*' will exclude all
subpackages of 'foo' (but not 'foo' itself).
'include' is a sequence of package names to include. If it's
specified, only the named packages will be included. If it's not
specified, all found packages will be included. 'include' can contain
shell style wildcard patterns just like 'exclude'.
The list of included packages is built up first and then any
explicitly excluded packages are removed from it.
"""
out = cls._find_packages_iter(convert_path(where))
out = cls.require_parents(out)
includes = cls._build_filter(*include)
excludes = cls._build_filter('ez_setup', '*__pycache__', *exclude)
out = filter(includes, out)
out = filterfalse(excludes, out)
return list(out)
@staticmethod
def require_parents(packages):
"""
Exclude any apparent package that apparently doesn't include its
parent.
For example, exclude 'foo.bar' if 'foo' is not present.
"""
found = []
for pkg in packages:
base, sep, child = pkg.rpartition('.')
if base and base not in found:
continue
found.append(pkg)
yield pkg
@staticmethod
def _candidate_dirs(base_path):
"""
Return all dirs in base_path that might be packages.
"""
has_dot = lambda name: '.' in name
for root, dirs, files in os.walk(base_path, followlinks=True):
# Exclude directories that contain a period, as they cannot be
# packages. Mutate the list to avoid traversal.
dirs[:] = filterfalse(has_dot, dirs)
for dir in dirs:
yield os.path.relpath(os.path.join(root, dir), base_path)
@classmethod
def _find_packages_iter(cls, base_path):
candidates = cls._candidate_dirs(base_path)
return (
path.replace(os.path.sep, '.')
for path in candidates
if cls._looks_like_package(os.path.join(base_path, path))
)
@staticmethod
def _looks_like_package(path):
return os.path.isfile(os.path.join(path, '__init__.py'))
@staticmethod
def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
class SetuptoolsStub(types.ModuleType):
"""
Approximate setuptools by providing and empty module and objects
"""
def __init__(self, *args, **kwargs):
super(SetuptoolsStub, self).__init__(*args, **kwargs)
self._installed_requirements_ = []
def __getattr__(self, item):
self_name = object.__getattribute__(self, '__name__')
# print('{} getting {!r}'.format(self_name, item))
if self_name == 'setuptools':
if item == '_setup_params_':
return (), {}
elif item == 'find_packages':
return PackageFinder.find
return OmniClass()
def fake_module(new_module):
"""
Created dummy empty modules
:param new_module: The name of a module to be faked
"""
module_names = []
for name in new_module.split('.'):
parent_name = '.'.join(module_names)
module_names.append(name)
full_name = '.'.join(module_names)
m = SetuptoolsStub(full_name, '')
if parent_name != '':
parent_module = sys.modules[parent_name]
if name not in parent_module.__dict__: # cannot use getattr
# print('setting {}'.format(name))
setattr(sys.modules[parent_name], name, m)
if full_name not in list(sys.modules.keys()):
sys.modules[full_name] = m
def fake_setuptools_modules():
"""
Created a bunch of stub setuptools modules
"""
setuptools_modules = [
'setuptools',
'setuptools.command',
'setuptools.command.alias',
'setuptools.command.bdist_egg',
'setuptools.command.bdist_rpm',
'setuptools.command.bdist_wininst',
'setuptools.command.build_ext',
'setuptools.command.build_py',
'setuptools.command.develop',
'setuptools.command.easy_install',
'setuptools.command.egg_info',
'setuptools.command.install',
'setuptools.depends.install_egg_info',
'setuptools.command.install_lib',
'setuptools.command.install_scripts',
'setuptools.command.register',
'setuptools.command.rotate',
'setuptools.command.saveopts',
'setuptools.command.sdist',
'setuptools.command.setopt',
'setuptools.command.test',
'setuptools.command.upload',
'setuptools.command.upload_docs',
'setuptools.extern',
'setuptools.dist',
'setuptools.extension',
'setuptools.launch',
'setuptools.lib2to3_ex',
'setuptools.msvc9_support',
'setuptools.package_index',
'setuptools.py26compat',
'setuptools.py27compat',
'setuptools.py31compat',
'setuptools.sandbox',
'setuptools.site-patch',
'setuptools.ssl_support',
'setuptools.unicode_utils',
'setuptools.utils',
'setuptools.version',
'setuptools.windows_support',
# 'pkg_resources',
# 'pkg_resources.extern',
]
for m in setuptools_modules:
fake_module(m)
# First import importable distutils
import distutils.command
import distutils.core
import distutils.util
distutils_command_modules = [
'distutils.command.bdist'
'distutils.command.bdist_dumb',
'distutils.command.bdist_msi',
'distutils.command.bdist_rpm',
'distutils.command.bdist_wininst',
'distutils.command.build',
'distutils.command.build_clib',
'distutils.command.build_ext',
'distutils.command.build_py',
'distutils.command.build_scripts',
]
for m in distutils_command_modules:
fake_module(m)
sys.modules['distutils.util'].get_platform = OmniClass()
# fix for new problem in issue 169
sys.modules['distutils.command.build_ext'].sub_commands = []
sys.modules['setuptools.command.build_ext'].sub_commands = []
def ensure_pkg_resources():
try:
import pkg_resources
except ImportError:
try:
print('Approximating pkg_resources ...')
GitHubRepository().install('ywangd/pkg_resources', None)
except: # silently fail as it may not be important or necessary
pass
@contextlib.contextmanager
def save_current_sys_modules():
"""
Save the current sys modules and restore them when processing is over
"""
# saved_sys_modules = dict(sys.modules)
save_setuptools = {}
for name in sorted(sys.modules.keys()):
if name == 'setuptools' or name.startswith('setuptools.'):
save_setuptools[name] = sys.modules.pop(name)
yield
# sys.modules = saved_sys_modules
for name in sorted(sys.modules.keys()):
if name == 'setuptools' or name.startswith('setuptools.'):
sys.modules.pop(name)
for k, v in save_setuptools.items():
sys.modules[k] = v
# noinspection PyUnresolvedReferences
from six.moves.configparser import SafeConfigParser, NoSectionError
class CIConfigParer(SafeConfigParser):
"""
This config parser is case insensitive for section names so that
the behaviour matches pypi queries.
"""
def _get_section_name(self, name):
for section_name in self.sections():
if section_name.lower() == name.lower():
return section_name
else:
raise NoSectionError(name)
def has_section(self, name):
names = [n.lower() for n in self.sections()]
return name.lower() in names
def has_option(self, name, option_name):
section_name = self._get_section_name(name)
return SafeConfigParser.has_option(self, section_name, option_name)
def items(self, name):
section_name = self._get_section_name(name)
return SafeConfigParser.items(self, section_name)
def get(self, name, option_name, *args, **kwargs):
section_name = self._get_section_name(name)
return SafeConfigParser.get(self, section_name, option_name, *args, **kwargs)
def set(self, name, option_name, value):
section_name = self._get_section_name(name)
return SafeConfigParser.set(self, section_name, option_name, value.replace('%', '%%'))
def remove_section(self, name):
section_name = self._get_section_name(name)
return SafeConfigParser.remove_section(self, section_name)
class PackageConfigHandler(object):
"""
Manager class for packages files for tracking installation of modules
"""
def __init__(self, site_packages=SITE_PACKAGES_FOLDER, verbose=False):
self.verbose = verbose
self.site_packages = site_packages
self.package_cfg = os.path.join(site_packages, '.pypi_packages')
if not os.path.isfile(self.package_cfg):
if self.verbose:
print('Creating package file...')
with open(self.package_cfg, 'w') as outs:
outs.close()
self.parser = CIConfigParer()
self.parser.read(self.package_cfg)
def save(self):
with open(self.package_cfg, 'w') as outs:
self.parser.write(outs)
def add_module(self, pkg_info):
"""
:param pkg_info: A dict that has name, url, version, summary
:return:
"""
if not self.parser.has_section(pkg_info['name']):
self.parser.add_section(pkg_info['name'])
self.parser.set(pkg_info['name'], 'url', pkg_info['url'])
self.parser.set(pkg_info['name'], 'version', pkg_info['version'])
self.parser.set(pkg_info['name'], 'summary', pkg_info['summary'])
self.parser.set(pkg_info['name'], 'files', pkg_info['files'])
self.parser.set(pkg_info['name'], 'dependency', pkg_info['dependency'])
self.save()
def list_modules(self):
return [module for module in self.parser.sections()]
def module_exists(self, name):
return self.parser.has_section(name)
def get_info(self, name):
if self.parser.has_section(name):
tbl = {}
for opt, value in self.parser.items(name):
tbl[opt] = value
return tbl
def remove_module(self, name):
self.parser.remove_section(name)
self.save()
def get_files_installed(self, section_name):
if self.parser.has_option(section_name, 'files'):
files = self.parser.get(section_name, 'files').strip()
return files.split(',')
else:
return None
def get_dependencies(self, section_name):
if self.parser.has_option(section_name, 'dependency'):
dependencies = self.parser.get(section_name, 'dependency').strip()
return set(dependencies.split(',')) if dependencies != '' else set()
else:
return None
def get_all_dependencies(self, exclude_module=()):
all_dependencies = set()
for section_name in self.parser.sections():
if section_name not in exclude_module and self.parser.has_option(section_name, 'dependency'):
dependencies = self.parser.get(section_name, 'dependency').strip()
if dependencies != '':
for dep in dependencies.split(','):
all_dependencies.add(dep)
return all_dependencies
# noinspection PyPep8Naming,PyProtectedMember
class ArchiveFileInstaller(object):
"""
Package Installer for archive files, e.g. zip, gz, bz2
"""
class SetupTransformer(ast.NodeTransformer):
"""
Analyze and Transform AST of a setup file.
1. Create empty modules for any setuptools imports (if it is not already covered)
2. replace setup calls with a stub one to get values of its arguments
"""
def visit_Import(self, node):
for idx, alias in enumerate(node.names):
if alias.name.startswith('setuptools'):
fake_module(alias.name)
return node
def vist_ImportFrom(self, node):
if node.module.startswith('setuptools'):
fake_module(node.module)
return node
def visit_Call(self, node):
func_name = self._get_possible_setup_name(node)
if func_name is not None and \
(func_name == 'setup' or func_name.endswith('.setup')):
node.func = ast.copy_location(
ast.Name('_setup_stub_', ast.Load()),
node.func)
return node
def _get_possible_setup_name(self, node):
names = []
func = node.func
while isinstance(func, ast.Attribute):
names.append(func.attr)
func = func.value
if isinstance(func, ast.Name):
names.append(func.id)
return '.'.join(reversed(names))
else:
return None
def __init__(self, site_packages=SITE_PACKAGES_FOLDER, verbose=False):
self.site_packages = site_packages
self.verbose = verbose
def run(self, pkg_name, archive_filename):
"""
Main method for Installer to do its job.
"""
extracted_folder = self._unzip(pkg_name, archive_filename)
try:
# locate the setup file
src_dir = os.path.join(extracted_folder, os.listdir(extracted_folder)[0])
setup_filename = os.path.join(src_dir, 'setup.py')
try:
print('Running setup file ...')
return self._run_setup_file(setup_filename)
except Exception as e:
print('{!r}'.format(e))
print('Failed to run setup.py')
if self.verbose:
# print traceback
print("")
traceback.print_exc()
print("")
print('Fall back to directory guessing ...')
pkg_name = pkg_name.lower().replace('-', '_')
if os.path.isdir(os.path.join(src_dir, pkg_name)):
ArchiveFileInstaller._safe_move(
os.path.join(src_dir, pkg_name),
os.path.join(self.site_packages, pkg_name)
)
return [os.path.join(self.site_packages, pkg_name)], []
elif os.path.isfile(os.path.join(src_dir, pkg_name + '.py')):
ArchiveFileInstaller._safe_move(
os.path.join(src_dir, pkg_name + '.py'),
os.path.join(self.site_packages, pkg_name + '.py')
)
return [os.path.join(self.site_packages, pkg_name + '.py')], []
elif os.path.isdir(os.path.join(src_dir, 'src', pkg_name)):
ArchiveFileInstaller._safe_move(
os.path.join(src_dir, 'src', pkg_name),
os.path.join(self.site_packages, pkg_name)
)
return [os.path.join(self.site_packages, pkg_name)], []
else:
raise PipError('Cannot locate packages. Manual installation required.')
finally:
shutil.rmtree(extracted_folder)
os.remove(archive_filename)
def _unzip(self, pkg_name, archive_filename):
import uuid
print('Extracting archive file ...')
extracted_folder = os.path.join(os.getenv('TMPDIR'), uuid.uuid4().hex)
os.mkdir(extracted_folder)
if '.zip' in archive_filename:
d = os.path.join(extracted_folder, pkg_name)
os.mkdir(d)
_stash('unzip -d {} {}'.format(d, archive_filename))
elif '.bz2' in archive_filename:
_stash('tar -C {} -jxf {}'.format(extracted_folder, archive_filename))
else: # gzip
_stash('tar -C {} -zxf {}'.format(extracted_folder, archive_filename))
return extracted_folder
def _run_setup_file(self, filename):
"""
Transform and Run AST of the setup file
"""
try:
import pkg_resources
except ImportError:
# pkg_resources install may be in progress
pkg_resources = None
namespace = {
'_setup_stub_': _setup_stub_,
'__file__': filename,
'__name__': '__main__',
'setup_args': None,
'setup_kwargs': None,
}
source_folder = os.path.dirname(filename)
tree = ArchiveFileInstaller._get_cooked_ast(filename)
codeobj = compile(tree, filename, 'exec')
# Some setup files import the package to be installed and sometimes opens a file
# in the source folder. So we modify sys.path and change directory into source folder.
saved_cwd = os.getcwd()
saved_sys_path = sys.path[:]
os.chdir(source_folder)
sys.path.insert(0, source_folder)
try:
exec(codeobj, namespace, namespace)
finally:
os.chdir(saved_cwd)
sys.path = saved_sys_path
args, kwargs = sys.modules['setuptools']._setup_params_
# for k in sorted(kwargs.keys()): print('{}: {!r}'.format(k, kwargs[k]))
if 'ext_modules' in kwargs:
ext = kwargs['ext_modules']
if (ext is not None) and (len(ext) > 0):
print('WARNING: Extension modules are skipped: {}'.format(ext))
packages = kwargs['packages'] if 'packages' in kwargs else []
py_modules = kwargs['py_modules'] if 'py_modules' in kwargs else []
if not packages and not py_modules:
raise PipError('failed to find packages or py_modules arguments in setup call')
package_dirs = kwargs.get('package_dir', {})
use_2to3 = kwargs.get('use_2to3', False) and six.PY3
files_installed = []
# handle scripts
# we handle them before the packages because they may be moved
# while handling the packages
scripts = kwargs.get("scripts", [])
for script in scripts:
if self.verbose:
print("Handling commandline script: {s}".format(s=script))
cmdname = script.replace(os.path.dirname(script), "").replace("/", "")
if not "." in cmdname:
cmdname += ".py"
scriptpath = os.path.join(source_folder, script)
with open(scriptpath, "r") as fin:
content = fin.read()
cmdpath = create_command(cmdname, content)
files_installed.append(cmdpath)
packages = ArchiveFileInstaller._consolidated_packages(packages)
for p in sorted(packages): # folders or files under source root
if p == '': # no packages just files
from_folder = os.path.join(source_folder, package_dirs.get(p, ''))
for f in ArchiveFileInstaller._find_package_files(from_folder):
target_file = os.path.join(self.site_packages, f)
ArchiveFileInstaller._safe_move(
os.path.join(from_folder, f),
target_file
)
files_installed.append(target_file)
if use_2to3:
_stash('2to3 -w {} > /dev/null'.format(target_file))
else: # packages
target_dir = os.path.join(self.site_packages, p)
if p in package_dirs:
ArchiveFileInstaller._safe_move(
os.path.join(source_folder, package_dirs[p]),
target_dir
)
elif '' in package_dirs:
ArchiveFileInstaller._safe_move(
os.path.join(source_folder, package_dirs[''], p),
target_dir
)
else:
ArchiveFileInstaller._safe_move(
os.path.join(source_folder, p),
target_dir
)
files_installed.append(target_dir)
if use_2to3:
_stash("""find {} --name '.py' | xargs -n 1 -I %% 2to3 -w %% > /dev/null""".format(target_dir))
py_modules = ArchiveFileInstaller._consolidated_packages(py_modules)
for p in sorted(py_modules): # files or folders where the file resides, e.g. ['file', 'folder.file']
if '' in package_dirs:
p = os.path.join(package_dirs[''], p)
if os.path.isdir(os.path.join(source_folder, p)): # folder
target_dir = os.path.join(self.site_packages, p)
ArchiveFileInstaller._safe_move(
os.path.join(source_folder, p),
target_dir
)
files_installed.append(target_dir)
if use_2to3:
_stash("""find {} --name '.py' | xargs -n 1 -I %% 2to3 -w %% > /dev/null""".format(target_dir))
else: # file
target_file = os.path.join(self.site_packages, p + '.py')
ArchiveFileInstaller._safe_move(
os.path.join(source_folder, p + '.py'),
target_file
)
files_installed.append(target_file)
if use_2to3:
_stash('2to3 -w {} > /dev/null'.format(target_file))
# handle entry points
entry_points = kwargs.get("entry_points", {})
if isinstance(entry_points, (str, unicode)):
if pkg_resources is not None:
entry_points = {s: c for s, c in pkg_resources.split_sections(entry_points)}
else:
print("Warning: pkg_resources not available, skipping entry_points definitions.")
entry_points = {}
for epn in entry_points:
if self.verbose:
print("Handling entrypoints for: " + epn)
ep = entry_points[epn]
if isinstance(ep, (str, unicode)):
ep = [ep]
if epn == "console_scripts":
for dec in ep:
name, loc = dec.replace(" ", "").split("=")
modname, funcname = loc.split(":")
if not name.endswith(".py"):
name += ".py"
desc = kwargs.get("description", "")
path = create_command(
name,
(u"""'''%s'''
from %s import %s
if __name__ == "__main__":
%s()
""" % (desc, modname, funcname, funcname)).encode("utf-8"))
files_installed.append(path)
else:
print("Warning: passing entry points for '{n}'.".format(n=epn))
# Recursively Handle dependencies
dependencies = kwargs.get('install_requires', [])
return files_installed, dependencies
@staticmethod
def _get_cooked_ast(filename):
"""
Get AST of the setup file and also transform it for fake setuptools
and stub setup calls.
"""
# with codecs.open(filename, mode="r", encoding="UTF-8") as ins:
# s = ins.read()
with open(filename, "r") as ins:
s = ins.read()
tree = ast.parse(s, filename=filename, mode='exec')
ArchiveFileInstaller.SetupTransformer().visit(tree)
return tree
@staticmethod
def _consolidated_packages(packages):
packages = sorted(packages)
consolidated = set()
for pkg in packages:
if '.' in pkg:
consolidated.add(pkg.split('.')[0]) # append the root folder
elif '/' in pkg:
consolidated.add(pkg.split('/')[0])
else:
consolidated.add(pkg)
return consolidated
@staticmethod
def _find_package_files(directory):
files = []
for f in os.listdir(directory):
if f.endswith('.py') and f != 'setup.py' and not os.path.isdir(f):
files.append(f)
return files
@staticmethod
def _safe_move(src, dest):
if not os.path.exists(src):
raise PipError('cannot locate source folder/file: {}'.format(src))
if os.path.exists(dest):
if NO_OVERWRITE:
raise PipError('cannot overwrite existing target, manual removal required: {}'.format(dest))
else:
if os.path.isdir(dest):
shutil.rmtree(dest)
else:
os.remove(dest)
shutil.move(src, dest)
# package_config_handler = PackageConfigHandler()
# archive_file_installer = ArchiveFileInstaller()
class PackageRepository(object):
"""
A Package Repository is a manager class to perform various actions
related to a package.
This is a base class providing basic layout of a Repository.
"""
def __init__(self, site_packages=SITE_PACKAGES_FOLDER, verbose=False):
self.site_packages = site_packages
self.verbose = verbose
self.config = PackageConfigHandler(site_packages=self.site_packages, verbose=self.verbose)
self.installer = ArchiveFileInstaller(site_packages=self.site_packages, verbose=self.verbose)
def versions(self, pkg_name):
raise PipError('versions only available for PyPI packages')
def download(self, pkg_name, ver_spec):
raise PipError('Action Not Available: download')
def install(self, pkg_name, ver_spec, dist=DIST_DEFAULT):
raise PipError('Action Not Available: install')
def _install(self, pkg_name, pkg_info, archive_filename, dependency_dist=DIST_DEFAULT):
if archive_filename.endswith(".whl"):
print("Installing wheel: {}...".format(os.path.basename(archive_filename)))
wheel = Wheel(archive_filename, verbose=self.verbose)
files_installed, dependencies = wheel.install(self.site_packages)
else:
files_installed, dependencies = self.installer.run(pkg_name, archive_filename)
# never install setuptools as dependency
dependencies = [dependency for dependency in dependencies if dependency != 'setuptools']
name_versions = [VersionSpecifier.parse_requirement(requirement)
for requirement in dependencies]
# filter (None, ...)
name_versions = list(filter(lambda e: e[0] is not None, name_versions))
sys.modules['setuptools']._installed_requirements_.append(pkg_name)
pkg_info['files'] = ','.join(files_installed)
pkg_info['dependency'] = ','.join(name_version[0] for name_version in name_versions)
self.config.add_module(pkg_info)
print('Package installed: {}'.format(pkg_name))
for dep_name, ver_spec in name_versions:
if dep_name == 'setuptools': # do not install setuptools
continue
# Some packages have error on dependency names
dep_name = PACKAGE_NAME_FIXER.get(dep_name, dep_name)
# If this dependency is installed before, skipping
if dep_name in sys.modules['setuptools']._installed_requirements_:
print('Dependency already installed: {}'.format(dep_name))
continue
if dep_name in PYTHONISTA_BUNDLED_MODULES:
print('Dependency available in Pythonista bundle : {}'.format(dep_name))
continue
print('Installing dependency: {} (required by: {})'.format('{}{}'.format(dep_name, ver_spec if ver_spec else ''), pkg_name))
repository = get_repository(dep_name, verbose=self.verbose)
try:
repository.install(dep_name, ver_spec, dist=dependency_dist)
except PackageAlreadyInstalled:
# well, it is already installed...
# TODO: maybe update package if required?
pass
def search(self, name_fragment):
raise PipError('search only available for PyPI packages')
def list(self):
modules = self.config.list_modules()
return [(module, self.config.get_info(module)) for module in modules]
def remove(self, pkg_name):
if self.config.module_exists(pkg_name):
dependencies = self.config.get_dependencies(pkg_name)
other_dependencies = self.config.get_all_dependencies(exclude_module=(pkg_name,))
files_installed = self.config.get_files_installed(pkg_name)
if files_installed:
for f in files_installed:
if os.path.isdir(f):
shutil.rmtree(f)
elif os.path.isfile(f):
os.remove(f)
else:
print('Package may have been removed externally without using pip. Deleting from registry ...')
else:
if os.path.isdir(os.path.expanduser('~/Documents/site-packages/{}'.format(pkg_name.lower()))):
shutil.rmtree(os.path.expanduser('~/Documents/site-packages/{}'.format(pkg_name.lower())))
elif os.path.isfile(os.path.expanduser('~/Documents/site-packages/{}.py'.format(pkg_name.lower()))):
os.remove(os.path.expanduser('~/Documents/site-packages/{}.py'.format(pkg_name.lower())))
else:
print('Package may have been removed externally without using pip. Deleting from registry ...')
self.config.remove_module(pkg_name)
print('Package removed.')
if dependencies:
for dependency in dependencies:
# If not other packages depend on it, it may be subject to removal
if dependency not in other_dependencies:
# Only remove the module if it exists in the registry. Otherwise
# it is possibly a builtin module.
# For backwards compatibility, we do not remove any entries
# that do not have a dependency option (since they are manually
# installed before).
if self.config.module_exists(dependency) \
and self.config.get_dependencies(dependency) is not None:
print('Removing dependency: {}'.format(dependency))
self.remove(dependency)
else:
raise PipError('package not installed: {}'.format(pkg_name))
def update(self, pkg_name):
if self.config.module_exists(pkg_name):
raise PipError('update only available for packages installed from PyPI')
else:
PipError('package not installed: {}'.format(pkg_name))
class PyPIRepository(PackageRepository):
"""
This repository performs its actions using the PyPI as a backend store.
"""
def __init__(self, *args, **kwargs):
super(PyPIRepository, self).__init__(*args, **kwargs)
try:
import xmlrpclib
except ImportError:
# py3
import xmlrpc.client as xmlrpclib
# DO NOT USE self.pypi, it's there just for search, it's obsolete/legacy
self.pypi = xmlrpclib.ServerProxy('https://pypi.python.org/pypi')
self.standard_package_names = {}
def get_standard_package_name(self, pkg_name):
if pkg_name not in self.standard_package_names:
try:
r = requests.get('https://pypi.python.org/pypi/{}/json'.format(pkg_name))
self.standard_package_names[pkg_name] = r.json()['info']['name']
except:
return pkg_name
return self.standard_package_names[pkg_name]
def search(self, pkg_name):
pkg_name = self.get_standard_package_name(pkg_name)
# XML-RPC replacement would be tricky, because we probably
# have to use simplified / cached index to search in, can't
# find JSON API to search for packages
hits = self.pypi.search({'name': pkg_name}, 'and')
if not hits:
raise PipError('No matches found: {}'.format(pkg_name))
hits = sorted(hits, key=lambda pkg: pkg['_pypi_ordering'], reverse=True)
return hits
def _package_data(self, pkg_name):
r = requests.get('https://pypi.python.org/pypi/{}/json'.format(pkg_name))
if not r.status_code == requests.codes.ok:
raise PipError('Failed to fetch package release urls')
return r.json()
def _package_releases(self, pkg_data):
return pkg_data['releases'].keys()
def _package_latest_release(self, pkg_data):
return pkg_data['info']['version']
def _package_downloads(self, pkg_data, hit):
return pkg_data['releases'][hit]
def _package_info(self, pkg_data):