Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/virtualenv/seed/wheels/util.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 from __future__ import absolute_import, unicode_literals | |
2 | |
3 from operator import attrgetter | |
4 from zipfile import ZipFile | |
5 | |
6 from virtualenv.util.six import ensure_text | |
7 | |
8 | |
9 class Wheel(object): | |
10 def __init__(self, path): | |
11 # https://www.python.org/dev/peps/pep-0427/#file-name-convention | |
12 # The wheel filename is {distribution}-{version}(-{build tag})?-{python tag}-{abi tag}-{platform tag}.whl | |
13 self.path = path | |
14 self._parts = path.stem.split("-") | |
15 | |
16 @classmethod | |
17 def from_path(cls, path): | |
18 if path is not None and path.suffix == ".whl" and len(path.stem.split("-")) >= 5: | |
19 return cls(path) | |
20 return None | |
21 | |
22 @property | |
23 def distribution(self): | |
24 return self._parts[0] | |
25 | |
26 @property | |
27 def version(self): | |
28 return self._parts[1] | |
29 | |
30 @property | |
31 def version_tuple(self): | |
32 return self.as_version_tuple(self.version) | |
33 | |
34 @staticmethod | |
35 def as_version_tuple(version): | |
36 result = [] | |
37 for part in version.split(".")[0:3]: | |
38 try: | |
39 result.append(int(part)) | |
40 except ValueError: | |
41 break | |
42 if not result: | |
43 raise ValueError(version) | |
44 return tuple(result) | |
45 | |
46 @property | |
47 def name(self): | |
48 return self.path.name | |
49 | |
50 def support_py(self, py_version): | |
51 name = "{}.dist-info/METADATA".format("-".join(self.path.stem.split("-")[0:2])) | |
52 with ZipFile(ensure_text(str(self.path)), "r") as zip_file: | |
53 metadata = zip_file.read(name).decode("utf-8") | |
54 marker = "Requires-Python:" | |
55 requires = next((i[len(marker) :] for i in metadata.splitlines() if i.startswith(marker)), None) | |
56 if requires is None: # if it does not specify a python requires the assumption is compatible | |
57 return True | |
58 py_version_int = tuple(int(i) for i in py_version.split(".")) | |
59 for require in (i.strip() for i in requires.split(",")): | |
60 # https://www.python.org/dev/peps/pep-0345/#version-specifiers | |
61 for operator, check in [ | |
62 ("!=", lambda v: py_version_int != v), | |
63 ("==", lambda v: py_version_int == v), | |
64 ("<=", lambda v: py_version_int <= v), | |
65 (">=", lambda v: py_version_int >= v), | |
66 ("<", lambda v: py_version_int < v), | |
67 (">", lambda v: py_version_int > v), | |
68 ]: | |
69 if require.startswith(operator): | |
70 ver_str = require[len(operator) :].strip() | |
71 version = tuple((int(i) if i != "*" else None) for i in ver_str.split("."))[0:2] | |
72 if not check(version): | |
73 return False | |
74 break | |
75 return True | |
76 | |
77 def __repr__(self): | |
78 return "{}({})".format(self.__class__.__name__, self.path) | |
79 | |
80 def __str__(self): | |
81 return str(self.path) | |
82 | |
83 | |
84 def discover_wheels(from_folder, distribution, version, for_py_version): | |
85 wheels = [] | |
86 for filename in from_folder.iterdir(): | |
87 wheel = Wheel.from_path(filename) | |
88 if wheel and wheel.distribution == distribution: | |
89 if version is None or wheel.version == version: | |
90 if wheel.support_py(for_py_version): | |
91 wheels.append(wheel) | |
92 return sorted(wheels, key=attrgetter("version_tuple", "distribution"), reverse=True) | |
93 | |
94 | |
95 class Version: | |
96 #: the version bundled with virtualenv | |
97 bundle = "bundle" | |
98 embed = "embed" | |
99 #: custom version handlers | |
100 non_version = ( | |
101 bundle, | |
102 embed, | |
103 ) | |
104 | |
105 @staticmethod | |
106 def of_version(value): | |
107 return None if value in Version.non_version else value | |
108 | |
109 @staticmethod | |
110 def as_pip_req(distribution, version): | |
111 return "{}{}".format(distribution, Version.as_version_spec(version)) | |
112 | |
113 @staticmethod | |
114 def as_version_spec(version): | |
115 of_version = Version.of_version(version) | |
116 return "" if of_version is None else "=={}".format(of_version) |