@@ -152,7 +152,7 @@ def __init__(self, python_version=None, chdir=True):
152
152
self ._pipfile_newlines = DEFAULT_NEWLINES
153
153
self ._lockfile_newlines = DEFAULT_NEWLINES
154
154
self ._requirements_location = None
155
- self ._original_dir = os . path . abspath ( os . curdir )
155
+ self ._original_dir = Path . cwd (). resolve ( )
156
156
self ._environment = None
157
157
self ._build_system = {"requires" : ["setuptools" , "wheel" ]}
158
158
self .python_version = python_version
@@ -692,11 +692,12 @@ def _parse_pipfile(
692
692
return toml .loads (contents )
693
693
694
694
def _read_pyproject (self ) -> None :
695
- pyproject = self .path_to ("pyproject.toml" )
696
- if os . path . exists (pyproject ):
697
- self ._pyproject = toml .load (pyproject )
695
+ pyproject_path = Path ( self .path_to ("pyproject.toml" ) )
696
+ if pyproject_path . exists ():
697
+ self ._pyproject = toml .load (pyproject_path )
698
698
build_system = self ._pyproject .get ("build-system" , None )
699
- if not os .path .exists (self .path_to ("setup.py" )):
699
+ setup_py_path = Path (self .path_to ("setup.py" ))
700
+ if not setup_py_path .exists ():
700
701
if not build_system or not build_system .get ("requires" ):
701
702
build_system = {
702
703
"requires" : ["setuptools>=40.8.0" , "wheel" ],
@@ -783,7 +784,7 @@ def lockfile_location(self):
783
784
784
785
@property
785
786
def lockfile_exists (self ):
786
- return os . path . isfile (self .lockfile_location )
787
+ return Path (self .lockfile_location ). is_file ( )
787
788
788
789
@property
789
790
def lockfile_content (self ):
@@ -987,9 +988,7 @@ def pipfile_sources(self, expand_vars=True):
987
988
if os .environ .get ("PIPENV_PYPI_MIRROR" ):
988
989
sources [0 ]["url" ] = os .environ ["PIPENV_PYPI_MIRROR" ]
989
990
return sources
990
- # We need to make copies of the source info so we don't
991
- # accidentally modify the cache. See #2100 where values are
992
- # written after the os.path.expandvars() call.
991
+ # Expand environment variables in the source URLs.
993
992
sources = [
994
993
{k : safe_expandvars (v ) if expand_vars else v for k , v in source .items ()}
995
994
for source in self .parsed_pipfile ["source" ]
@@ -1320,25 +1319,34 @@ def recase_pipfile(self):
1320
1319
1321
1320
def load_lockfile (self , expand_env_vars = True ):
1322
1321
lockfile_modified = False
1323
- with open (self .lockfile_location , encoding = "utf-8" ) as lock :
1324
- try :
1325
- j = json .load (lock )
1326
- self ._lockfile_newlines = preferred_newlines (lock )
1327
- except JSONDecodeError :
1328
- err .print (
1329
- "[bold yellow]Pipfile.lock is corrupted; ignoring contents.[/bold yellow]"
1330
- )
1331
- j = {}
1322
+ lockfile_path = Path (self .lockfile_location )
1323
+ pipfile_path = Path (self .pipfile_location )
1324
+
1325
+ try :
1326
+ with lockfile_path .open (encoding = "utf-8" ) as lock :
1327
+ try :
1328
+ j = json .load (lock )
1329
+ self ._lockfile_newlines = preferred_newlines (lock )
1330
+ except JSONDecodeError :
1331
+ err .print (
1332
+ "[bold yellow]Pipfile.lock is corrupted; ignoring contents.[/bold yellow]"
1333
+ )
1334
+ j = {}
1335
+ except FileNotFoundError :
1336
+ j = {}
1337
+
1332
1338
if not j .get ("_meta" ):
1333
- with open (self . pipfile_location ) as pf :
1339
+ with pipfile_path . open () as pf :
1334
1340
default_lockfile = plette .Lockfile .with_meta_from (
1335
1341
plette .Pipfile .load (pf ), categories = []
1336
1342
)
1337
1343
j ["_meta" ] = default_lockfile ._data ["_meta" ]
1338
1344
lockfile_modified = True
1345
+
1339
1346
if j .get ("default" ) is None :
1340
1347
j ["default" ] = {}
1341
1348
lockfile_modified = True
1349
+
1342
1350
if j .get ("develop" ) is None :
1343
1351
j ["develop" ] = {}
1344
1352
lockfile_modified = True
@@ -1349,14 +1357,16 @@ def load_lockfile(self, expand_env_vars=True):
1349
1357
if expand_env_vars :
1350
1358
# Expand environment variables in Pipfile.lock at runtime.
1351
1359
for i , _ in enumerate (j ["_meta" ].get ("sources" , {})):
1360
+ # Path doesn't have expandvars method, so we need to use os.path.expandvars
1352
1361
j ["_meta" ]["sources" ][i ]["url" ] = os .path .expandvars (
1353
1362
j ["_meta" ]["sources" ][i ]["url" ]
1354
1363
)
1355
1364
1356
1365
return j
1357
1366
1358
1367
def get_lockfile_hash (self ):
1359
- if not os .path .exists (self .lockfile_location ):
1368
+ lockfile_path = Path (self .lockfile_location )
1369
+ if not lockfile_path .exists ():
1360
1370
return
1361
1371
1362
1372
try :
@@ -1443,22 +1453,35 @@ def _which(self, command, location=None, allow_global=False):
1443
1453
location = self .virtualenv_location
1444
1454
else :
1445
1455
location = os .environ .get ("VIRTUAL_ENV" , None )
1446
- if not (location and Path (location ).exists ()) and not allow_global :
1456
+
1457
+ location_path = Path (location ) if location else None
1458
+
1459
+ if not (location_path and location_path .exists ()) and not allow_global :
1447
1460
raise RuntimeError ("location not created nor specified" )
1448
1461
1449
- version_str = "python{}" .format ("." .join ([str (v ) for v in sys .version_info [:2 ]]))
1450
- is_python = command in ("python" , os .path .basename (sys .executable ), version_str )
1462
+ version_str = f"python{ '.' .join ([str (v ) for v in sys .version_info [:2 ]])} "
1463
+ is_python = command in ("python" , Path (sys .executable ).name , version_str )
1464
+
1451
1465
if not allow_global :
1452
1466
if os .name == "nt" :
1453
- p = find_windows_executable (os .path .join (location , "Scripts" ), command )
1467
+ p = find_windows_executable (str (location_path / "Scripts" ), command )
1468
+ # Convert to Path object if it's a string
1469
+ p = Path (p ) if isinstance (p , str ) else p
1454
1470
else :
1455
- p = Path ( location ) / "bin" / command
1471
+ p = location_path / "bin" / command
1456
1472
elif is_python :
1457
1473
p = Path (sys .executable )
1458
- p_str = str (p ) if isinstance (p , Path ) else p
1459
- if not os .path .exists (p_str ):
1474
+ else :
1475
+ p = None
1476
+
1477
+ if p is None or not p .exists ():
1460
1478
if is_python :
1461
- p = sys .executable or system_which ("python" )
1479
+ p = (
1480
+ Path (sys .executable )
1481
+ if sys .executable
1482
+ else Path (system_which ("python" ))
1483
+ )
1462
1484
else :
1463
- p = system_which (command )
1485
+ p = Path (system_which (command )) if system_which (command ) else None
1486
+
1464
1487
return p
0 commit comments