Skip to content

Commit 36b32b1

Browse files
committed
prefer pathlib
1 parent 401a0de commit 36b32b1

File tree

3 files changed

+24
-29
lines changed

3 files changed

+24
-29
lines changed

src/poetry/core/json/__init__.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
from __future__ import annotations
22

33
import json
4-
import os
4+
from pathlib import Path
55

66
from typing import Any
77

88

9-
SCHEMA_DIR = os.path.join(os.path.dirname(__file__), "schemas")
9+
SCHEMA_DIR = Path(__file__).parent / "schemas"
1010

1111

1212
class ValidationError(ValueError):
1313
pass
1414

1515

1616
def validate_object(obj: dict[str, Any], schema_name: str) -> list[str]:
17-
schema_file = os.path.join(SCHEMA_DIR, f"{schema_name}.json")
17+
schema_file = SCHEMA_DIR / f"{schema_name}.json"
1818

19-
if not os.path.exists(schema_file):
19+
if not schema_file.exists():
2020
raise ValueError(f"Schema {schema_name} does not exist.")
2121

22-
with open(schema_file, encoding="utf-8") as f:
22+
with schema_file.open(encoding="utf-8") as f:
2323
schema = json.loads(f.read())
2424

2525
from jsonschema import Draft7Validator

src/poetry/core/masonry/builders/wheel.py

+16-20
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,19 @@ def _build(self, wheel: zipfile.ZipFile) -> None:
163163
# we assume that the build script will build and copy the files
164164
# directly.
165165
# That way they will be picked up when adding files to the wheel.
166-
current_path = os.getcwd()
166+
current_path = Path.cwd()
167167
try:
168-
os.chdir(str(self._path))
168+
os.chdir(self._path)
169169
self._run_build_script(self._package.build_script)
170170
finally:
171171
os.chdir(current_path)
172172
else:
173173
with SdistBuilder(poetry=self._poetry).setup_py() as setup:
174174
# We need to place ourselves in the temporary
175175
# directory in order to build the package
176-
current_path = os.getcwd()
176+
current_path = Path.cwd()
177177
try:
178-
os.chdir(str(self._path))
178+
os.chdir(self._path)
179179
self._run_build_command(setup)
180180
finally:
181181
os.chdir(current_path)
@@ -194,9 +194,9 @@ def _build(self, wheel: zipfile.ZipFile) -> None:
194194
if pkg.is_dir() or self.is_excluded(pkg):
195195
continue
196196

197-
rel_path = str(pkg.relative_to(lib))
197+
rel_path = pkg.relative_to(lib)
198198

199-
if rel_path in wheel.namelist():
199+
if rel_path.as_posix() in wheel.namelist():
200200
continue
201201

202202
logger.debug(f"Adding: {rel_path}")
@@ -210,7 +210,7 @@ def _copy_file_scripts(self, wheel: zipfile.ZipFile) -> None:
210210
self._add_file(
211211
wheel,
212212
abs_path,
213-
Path.joinpath(Path(self.wheel_data_folder), "scripts", abs_path.name),
213+
Path(self.wheel_data_folder) / "scripts" / abs_path.name,
214214
)
215215

216216
def _run_build_command(self, setup: Path) -> None:
@@ -268,7 +268,7 @@ def prepare_metadata(self, metadata_directory: Path) -> Path:
268268
continue
269269

270270
dest = dist_info / license_file.relative_to(self._path)
271-
os.makedirs(dest.parent, exist_ok=True)
271+
dest.parent.mkdir(parents=True, exist_ok=True)
272272
shutil.copy(license_file, dest)
273273

274274
return dist_info
@@ -345,19 +345,15 @@ def tag(self) -> str:
345345
def _add_file(
346346
self,
347347
wheel: zipfile.ZipFile,
348-
full_path: Path | str,
349-
rel_path: Path | str,
348+
full_path: Path,
349+
rel_path: Path,
350350
) -> None:
351-
full_path, rel_path = str(full_path), str(rel_path)
352-
if os.sep != "/":
353-
# We always want to have /-separated paths in the zip file and in
354-
# RECORD
355-
rel_path = rel_path.replace(os.sep, "/")
356-
357-
zinfo = zipfile.ZipInfo(rel_path)
351+
# We always want to have /-separated paths in the zip file and in RECORD
352+
rel_path_name = rel_path.as_posix()
353+
zinfo = zipfile.ZipInfo(rel_path_name)
358354

359355
# Normalize permission bits to either 755 (executable) or 644
360-
st_mode = os.stat(full_path).st_mode
356+
st_mode = full_path.stat().st_mode
361357
new_mode = normalize_file_permissions(st_mode)
362358
zinfo.external_attr = (new_mode & 0xFFFF) << 16 # Unix attributes
363359

@@ -375,10 +371,10 @@ def _add_file(
375371
src.seek(0)
376372
wheel.writestr(zinfo, src.read(), compress_type=zipfile.ZIP_DEFLATED)
377373

378-
size = os.stat(full_path).st_size
374+
size = full_path.stat().st_size
379375
hash_digest = urlsafe_b64encode(hashsum.digest()).decode("ascii").rstrip("=")
380376

381-
self._records.append((rel_path, hash_digest, size))
377+
self._records.append((rel_path_name, hash_digest, size))
382378

383379
@contextlib.contextmanager
384380
def _write_to_zip(

src/poetry/core/version/markers.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -764,10 +764,7 @@ def _compact_markers(
764764
elif token.data == f"{tree_prefix}item":
765765
name, op, value = token.children
766766
if value.type == f"{tree_prefix}MARKER_NAME":
767-
name, value, = (
768-
value,
769-
name,
770-
)
767+
name, value = value, name
771768

772769
value = value[1:-1]
773770
sub_marker = SingleMarker(str(name), f"{op}{value}")
@@ -814,8 +811,10 @@ def dnf(marker: BaseMarker) -> BaseMarker:
814811
return MarkerUnion.of(
815812
*[MultiMarker.of(*c) for c in itertools.product(*sub_marker_lists)]
816813
)
814+
817815
if isinstance(marker, MarkerUnion):
818816
return MarkerUnion.of(*[dnf(m) for m in marker.markers])
817+
819818
return marker
820819

821820

0 commit comments

Comments
 (0)