22
22
from itertools import chain
23
23
from os .path import dirname , isfile
24
24
from pathlib import Path
25
- from typing import Any , Dict , Iterable , Iterator , List , Optional , Sequence , Tuple , Union
25
+ from typing import Any , Dict , Iterable , Iterator , List , Optional , Sequence , Tuple
26
26
27
- from pkg_resources import Requirement , parse_requirements , yield_lines
27
+ from packaging . requirements import Requirement
28
28
29
29
REQUIREMENT_FILES = {
30
30
"pytorch" : (
@@ -80,14 +80,15 @@ def adjust(self, unfreeze: str) -> str:
80
80
out = str (self )
81
81
if self .strict :
82
82
return f"{ out } { self .strict_string } "
83
+ specs = [(spec .operator , spec .version ) for spec in self .specifier ]
83
84
if unfreeze == "major" :
84
- for operator , version in self . specs :
85
+ for operator , version in specs :
85
86
if operator in ("<" , "<=" ):
86
87
major = LooseVersion (version ).version [0 ]
87
88
# replace upper bound with major version increased by one
88
89
return out .replace (f"{ operator } { version } " , f"<{ major + 1 } .0" )
89
90
elif unfreeze == "all" :
90
- for operator , version in self . specs :
91
+ for operator , version in specs :
91
92
if operator in ("<" , "<=" ):
92
93
# drop upper bound
93
94
return out .replace (f"{ operator } { version } ," , "" )
@@ -96,33 +97,25 @@ def adjust(self, unfreeze: str) -> str:
96
97
return out
97
98
98
99
99
- def _parse_requirements (strs : Union [ str , Iterable [str ] ]) -> Iterator [_RequirementWithComment ]:
100
+ def _parse_requirements (lines : Iterable [str ]) -> Iterator [_RequirementWithComment ]:
100
101
"""Adapted from `pkg_resources.parse_requirements` to include comments.
101
102
102
103
>>> txt = ['# ignored', '', 'this # is an', '--piparg', 'example', 'foo # strict', 'thing', '-r different/file.txt']
103
104
>>> [r.adjust('none') for r in _parse_requirements(txt)]
104
105
['this', 'example', 'foo # strict', 'thing']
105
- >>> txt = '\\ n'.join(txt)
106
- >>> [r.adjust('none') for r in _parse_requirements(txt)]
107
- ['this', 'example', 'foo # strict', 'thing']
108
106
109
107
"""
110
- lines = yield_lines (strs )
111
108
pip_argument = None
112
109
for line in lines :
110
+ line = line .strip ()
111
+ if not line or line .startswith ("#" ):
112
+ continue
113
113
# Drop comments -- a hash without a space may be in a URL.
114
114
if " #" in line :
115
115
comment_pos = line .find (" #" )
116
116
line , comment = line [:comment_pos ], line [comment_pos :]
117
117
else :
118
118
comment = ""
119
- # If there is a line continuation, drop it, and append the next line.
120
- if line .endswith ("\\ " ):
121
- line = line [:- 2 ].strip ()
122
- try :
123
- line += next (lines )
124
- except StopIteration :
125
- return
126
119
# If there's a pip argument, save it
127
120
if line .startswith ("--" ):
128
121
pip_argument = line
@@ -148,7 +141,7 @@ def load_requirements(path_dir: str, file_name: str = "base.txt", unfreeze: str
148
141
logging .warning (f"Folder { path_dir } does not have any base requirements." )
149
142
return []
150
143
assert path .exists (), (path_dir , file_name , path )
151
- text = path .read_text ()
144
+ text = path .read_text (). splitlines ()
152
145
return [req .adjust (unfreeze ) for req in _parse_requirements (text )]
153
146
154
147
@@ -368,7 +361,7 @@ def _prune_packages(req_file: str, packages: Sequence[str]) -> None:
368
361
if not ln_ or ln_ .startswith ("#" ):
369
362
final .append (line )
370
363
continue
371
- req = list (parse_requirements ( ln_ ))[0 ]
364
+ req = list (_parse_requirements ([ ln_ ] ))[0 ]
372
365
if req .name not in packages :
373
366
final .append (line )
374
367
print (final )
0 commit comments