Skip to content

Commit

Permalink
Csv fixes (#5836)
Browse files Browse the repository at this point in the history
* open RECORD file with newline=""

* use csv module to write RECORD from editable builder

* Unit test for csv RECORD file
  • Loading branch information
dimbleby committed Jun 13, 2022
1 parent c33e3c6 commit 79694ae
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/poetry/installation/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ def _save_url_reference(self, operation: Operation) -> None:

record = dist_path / "RECORD"
if record.exists():
with record.open(mode="a", encoding="utf-8") as f:
with record.open(mode="a", encoding="utf-8", newline="") as f:
writer = csv.writer(f)
path = url.relative_to(record.parent.parent)
writer.writerow([str(path), "", ""])
Expand Down
8 changes: 5 additions & 3 deletions src/poetry/masonry/builders/editable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import csv
import hashlib
import json
import os
Expand Down Expand Up @@ -255,14 +256,15 @@ def _add_dist_info(self, added_files: list[Path]) -> None:
added_files.append(direct_url_json)

record = dist_info.joinpath("RECORD")
with record.open("w", encoding="utf-8") as f:
with record.open("w", encoding="utf-8", newline="") as f:
csv_writer = csv.writer(f)
for path in added_files:
hash = self._get_file_hash(path)
size = path.stat().st_size
f.write(f"{path!s},sha256={hash},{size}\n")
csv_writer.writerow((path, f"sha256={hash}", size))

# RECORD itself is recorded with no hash or size
f.write(f"{record},,\n")
csv_writer.writerow((record, "", ""))

def _get_file_hash(self, filepath: Path) -> str:
hashsum = hashlib.sha256()
Expand Down
11 changes: 7 additions & 4 deletions tests/installation/test_executor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import csv
import json
import re
import shutil
Expand Down Expand Up @@ -400,12 +401,14 @@ def verify_installed_distribution(

if url_reference is not None:
record_file = distribution._path.joinpath("RECORD")
with open(record_file, encoding="utf-8", newline="") as f:
reader = csv.reader(f)
rows = list(reader)
assert all(len(row) == 3 for row in rows)
record_entries = {row[0] for row in rows}
direct_url_entry = direct_url_file.relative_to(record_file.parent.parent)
assert direct_url_file.exists()
assert str(direct_url_entry) in {
row.split(",")[0]
for row in record_file.read_text(encoding="utf-8").splitlines()
}
assert str(direct_url_entry) in record_entries
assert json.loads(direct_url_file.read_text(encoding="utf-8")) == url_reference
else:
assert not direct_url_file.exists()
Expand Down
24 changes: 15 additions & 9 deletions tests/masonry/builders/test_editable_builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import csv
import json
import os
import shutil
Expand Down Expand Up @@ -158,17 +159,22 @@ def test_builder_installs_proper_files_for_standard_packages(
"""
assert metadata == dist_info.joinpath("METADATA").read_text(encoding="utf-8")

records = dist_info.joinpath("RECORD").read_text()
with open(dist_info.joinpath("RECORD"), encoding="utf-8", newline="") as f:
reader = csv.reader(f)
records = list(reader)

assert all(len(row) == 3 for row in records)
record_entries = {row[0] for row in records}
pth_file = "simple_project.pth"
assert tmp_venv.site_packages.exists(pth_file)
assert str(tmp_venv.site_packages.find(pth_file)[0]) in records
assert str(tmp_venv._bin_dir.joinpath("foo")) in records
assert str(tmp_venv._bin_dir.joinpath("baz")) in records
assert str(dist_info.joinpath("METADATA")) in records
assert str(dist_info.joinpath("INSTALLER")) in records
assert str(dist_info.joinpath("entry_points.txt")) in records
assert str(dist_info.joinpath("RECORD")) in records
assert str(dist_info.joinpath("direct_url.json")) in records
assert str(tmp_venv.site_packages.find(pth_file)[0]) in record_entries
assert str(tmp_venv._bin_dir.joinpath("foo")) in record_entries
assert str(tmp_venv._bin_dir.joinpath("baz")) in record_entries
assert str(dist_info.joinpath("METADATA")) in record_entries
assert str(dist_info.joinpath("INSTALLER")) in record_entries
assert str(dist_info.joinpath("entry_points.txt")) in record_entries
assert str(dist_info.joinpath("RECORD")) in record_entries
assert str(dist_info.joinpath("direct_url.json")) in record_entries

baz_script = f"""\
#!{tmp_venv.python}
Expand Down

0 comments on commit 79694ae

Please sign in to comment.