8
8
import six
9
9
from attr import attrs , attrib , Factory , validators
10
10
import attr
11
- from ._compat import Link , path_to_url , _strip_extras
11
+ from ._compat import Link , path_to_url , _strip_extras , InstallRequirement , Wheel
12
12
from distlib .markers import Evaluator
13
13
from packaging .markers import Marker , InvalidMarker
14
14
from packaging .specifiers import SpecifierSet , InvalidSpecifier
30
30
except ImportError :
31
31
from pathlib2 import Path
32
32
33
+ try :
34
+ from urllib .parse import urlparse
35
+ except ImportError :
36
+ from urlparse import urlparse
37
+
33
38
HASH_STRING = " --hash={0}"
34
39
35
40
@@ -261,7 +266,7 @@ def line_part(self):
261
266
262
267
@property
263
268
def pipfile_part (self ):
264
- pipfile_dict = attr .asdict (self , filter = _filter_none )
269
+ pipfile_dict = attr .asdict (self , filter = _filter_none ). copy ()
265
270
if "version" not in pipfile_dict :
266
271
pipfile_dict ["version" ] = "*"
267
272
name = pipfile_dict .pop ("name" )
@@ -301,20 +306,23 @@ def get_name(self):
301
306
@link .default
302
307
def get_link (self ):
303
308
target = "{0}#egg={1}" .format (self .uri , self .name )
304
- return Link (target )
309
+ link = Link (target )
310
+ if link .is_wheel and self ._has_hashed_name :
311
+ self .name = os .path .basename (Wheel (link .path ).name )
312
+ return link
305
313
306
314
@req .default
307
315
def get_requirement (self ):
308
- base = "{0}" .format (self .link )
309
- req = first (requirements .parse (base ))
316
+ prefix = "-e " if self .editable else ""
317
+ line = "{0}{1}" .format (prefix , self .link .url )
318
+ req = first (requirements .parse (line ))
319
+ if self .path and self .link and self .link .scheme .startswith ("file" ):
320
+ req .local_file = True
321
+ req .path = self .path
322
+ req .uri = None
323
+ self ._uri_scheme = "file"
310
324
if self .editable :
311
325
req .editable = True
312
- if self .link and self .link .scheme .startswith ("file" ):
313
- if self .path :
314
- req .path = self .path
315
- req .local_file = True
316
- self ._uri_scheme = "file"
317
- req .uri = None
318
326
req .link = self .link
319
327
return req
320
328
@@ -338,15 +346,24 @@ def from_line(cls, line):
338
346
"Supplied requirement is not installable: {0!r}" .format (line )
339
347
)
340
348
341
- if is_valid_url (line ):
349
+ if is_valid_url (line ) and not is_installable_file ( line ) :
342
350
link = Link (line )
343
351
else :
344
- _path = Path (line )
345
- link = Link (_path .absolute ().as_uri ())
346
- if _path .is_absolute () or _path .as_posix () == "." :
347
- path = _path .as_posix ()
352
+ if is_valid_url (line ):
353
+ parsed = urlparse (line )
354
+ link = Link ('{0}' .format (line ))
355
+ if parsed .scheme == "file" :
356
+ path = Path (parsed .path ).absolute ().as_posix ()
357
+ if get_converted_relative_path (path ) == "." :
358
+ path = "."
359
+ line = path
348
360
else :
349
- path = get_converted_relative_path (line )
361
+ _path = Path (line )
362
+ link = Link (_path .absolute ().as_uri ())
363
+ if _path .is_absolute () or _path .as_posix () == "." :
364
+ path = _path .as_posix ()
365
+ else :
366
+ path = get_converted_relative_path (line )
350
367
arg_dict = {
351
368
"path" : path ,
352
369
"uri" : link .url_without_fragment ,
@@ -571,6 +588,7 @@ class Requirement(object):
571
588
editable = attrib (default = None )
572
589
hashes = attrib (default = Factory (list ), converter = list )
573
590
extras = attrib (default = Factory (list ))
591
+ _ireq = None
574
592
_INCLUDE_FIELDS = ("name" , "markers" , "index" , "editable" , "hashes" , "extras" )
575
593
576
594
@name .default
@@ -749,6 +767,17 @@ def as_pipfile(self, include_index=False):
749
767
def pipfile_entry (self ):
750
768
return self .as_pipfile ().copy ().popitem ()
751
769
770
+ @property
771
+ def ireq (self ):
772
+ if not self ._ireq :
773
+ ireq_line = self .as_line ()
774
+ if ireq_line .startswith ("-e " ):
775
+ ireq_line = ireq_line [len ("-e " ):]
776
+ self ._ireq = InstallRequirement .from_editable (ireq_line )
777
+ else :
778
+ self ._ireq = InstallRequirement .from_line (ireq_line )
779
+ return self ._ireq
780
+
752
781
753
782
def _extras_to_string (extras ):
754
783
"""Turn a list of extras into a string"""
0 commit comments