Skip to content

Commit

Permalink
prefer pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Feb 12, 2023
1 parent 401a0de commit 36b32b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/poetry/core/json/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
from __future__ import annotations

import json
import os
from pathlib import Path

from typing import Any


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


class ValidationError(ValueError):
pass


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

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

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

from jsonschema import Draft7Validator
Expand Down
36 changes: 16 additions & 20 deletions src/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,19 @@ def _build(self, wheel: zipfile.ZipFile) -> None:
# we assume that the build script will build and copy the files
# directly.
# That way they will be picked up when adding files to the wheel.
current_path = os.getcwd()
current_path = Path.cwd()
try:
os.chdir(str(self._path))
os.chdir(self._path)
self._run_build_script(self._package.build_script)
finally:
os.chdir(current_path)
else:
with SdistBuilder(poetry=self._poetry).setup_py() as setup:
# We need to place ourselves in the temporary
# directory in order to build the package
current_path = os.getcwd()
current_path = Path.cwd()
try:
os.chdir(str(self._path))
os.chdir(self._path)
self._run_build_command(setup)
finally:
os.chdir(current_path)
Expand All @@ -194,9 +194,9 @@ def _build(self, wheel: zipfile.ZipFile) -> None:
if pkg.is_dir() or self.is_excluded(pkg):
continue

rel_path = str(pkg.relative_to(lib))
rel_path = pkg.relative_to(lib)

if rel_path in wheel.namelist():
if rel_path.as_posix() in wheel.namelist():
continue

logger.debug(f"Adding: {rel_path}")
Expand All @@ -210,7 +210,7 @@ def _copy_file_scripts(self, wheel: zipfile.ZipFile) -> None:
self._add_file(
wheel,
abs_path,
Path.joinpath(Path(self.wheel_data_folder), "scripts", abs_path.name),
Path(self.wheel_data_folder) / "scripts" / abs_path.name,
)

def _run_build_command(self, setup: Path) -> None:
Expand Down Expand Up @@ -268,7 +268,7 @@ def prepare_metadata(self, metadata_directory: Path) -> Path:
continue

dest = dist_info / license_file.relative_to(self._path)
os.makedirs(dest.parent, exist_ok=True)
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(license_file, dest)

return dist_info
Expand Down Expand Up @@ -345,19 +345,15 @@ def tag(self) -> str:
def _add_file(
self,
wheel: zipfile.ZipFile,
full_path: Path | str,
rel_path: Path | str,
full_path: Path,
rel_path: Path,
) -> None:
full_path, rel_path = str(full_path), str(rel_path)
if os.sep != "/":
# We always want to have /-separated paths in the zip file and in
# RECORD
rel_path = rel_path.replace(os.sep, "/")

zinfo = zipfile.ZipInfo(rel_path)
# We always want to have /-separated paths in the zip file and in RECORD
rel_path_name = rel_path.as_posix()
zinfo = zipfile.ZipInfo(rel_path_name)

# Normalize permission bits to either 755 (executable) or 644
st_mode = os.stat(full_path).st_mode
st_mode = full_path.stat().st_mode
new_mode = normalize_file_permissions(st_mode)
zinfo.external_attr = (new_mode & 0xFFFF) << 16 # Unix attributes

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

size = os.stat(full_path).st_size
size = full_path.stat().st_size
hash_digest = urlsafe_b64encode(hashsum.digest()).decode("ascii").rstrip("=")

self._records.append((rel_path, hash_digest, size))
self._records.append((rel_path_name, hash_digest, size))

@contextlib.contextmanager
def _write_to_zip(
Expand Down
7 changes: 3 additions & 4 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,7 @@ def _compact_markers(
elif token.data == f"{tree_prefix}item":
name, op, value = token.children
if value.type == f"{tree_prefix}MARKER_NAME":
name, value, = (
value,
name,
)
name, value = value, name

value = value[1:-1]
sub_marker = SingleMarker(str(name), f"{op}{value}")
Expand Down Expand Up @@ -814,8 +811,10 @@ def dnf(marker: BaseMarker) -> BaseMarker:
return MarkerUnion.of(
*[MultiMarker.of(*c) for c in itertools.product(*sub_marker_lists)]
)

if isinstance(marker, MarkerUnion):
return MarkerUnion.of(*[dnf(m) for m in marker.markers])

return marker


Expand Down

0 comments on commit 36b32b1

Please sign in to comment.