Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions specfile/specfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ def _read_lines(cls, file: IO) -> Tuple[List[str], bool]:
content = raw_content.decode(**cls.ENCODING_ARGS)
return content.splitlines(), content.endswith("\n")

def _reopen_named_file(self) -> None:
if not self.path:
return
self._file.close()
self._file = self.path.open("r+", **self.ENCODING_ARGS)

@property
def path(self) -> Optional[Path]:
"""Path to the spec file."""
Expand Down Expand Up @@ -263,21 +269,17 @@ def rpm_spec(self) -> rpm.spec:

def reload(self) -> None:
"""Reloads the spec file content."""
try:
path = Path(cast(FileIO, self._file).name)
except AttributeError:
pass
else:
# reopen the path in case the original file has been deleted/replaced
self._file.close()
self._file = path.open("r+", **self.ENCODING_ARGS)
# reopen the path in case the original file has been deleted/replaced
self._reopen_named_file()
self._lines, self._trailing_newline = self._read_lines(self._file)

def save(self) -> None:
"""Saves the spec file content."""
content = str(self)
# reopen the path in case the original file has been deleted/replaced
self._reopen_named_file()
self._file.seek(0)
self._file.truncate(0)
content = str(self)
try:
self._file.write(content)
except TypeError:
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/test_specfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,3 +710,21 @@ def test_reload(specfile_factory, spec_minimal, spec_traditional, remove_spec):
after_reload = spec

assert str(before_reload) != str(after_reload)


def test_save_after_inode_change(specfile_factory, spec_minimal):
spec = specfile_factory(spec_minimal)
if spec.path is None:
return
inode = spec_minimal.stat().st_ino
content = spec_minimal.read_bytes()
spec_minimal.unlink()
spec_minimal.write_bytes(content)
assert spec_minimal.stat().st_ino != inode
spec.version = "0.2"
spec.save()
assert all(
line.endswith("0.2")
for line in spec_minimal.read_text().splitlines()
if line.startswith("Version:")
)
Loading