Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit a6a0758

Browse files
authored
make manifest read only (#32)
2 parents edf0f76 + d35993e commit a6a0758

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/PythonProjectBootstrapper/ProjectGenerationUtils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import hashlib
1111
import itertools
1212
import os
13+
from stat import S_IWUSR
1314
import sys
1415
from pathlib import Path
1516

@@ -99,6 +100,26 @@ def CreateManifest(generated_dir: Path) -> dict[str, str]:
99100
return manifest_dict
100101

101102

103+
# ----------------------------------------------------------------------
104+
def _ChangeManifestWritePermissions(manifest_filepath: Path, read_only: bool) -> None:
105+
"""
106+
Change write permissions for manifest file
107+
108+
Args:
109+
manifest_filepath (Path): Filepath to manifest file
110+
read_only (bool): If true, set to read-only. If false, allow writing to file
111+
"""
112+
PathEx.EnsureFile(manifest_filepath)
113+
status = manifest_filepath.stat()
114+
115+
if read_only:
116+
manifest_filepath.chmod(status.st_mode & ~S_IWUSR)
117+
else:
118+
manifest_filepath.chmod(status.st_mode | S_IWUSR)
119+
120+
status = manifest_filepath.stat()
121+
122+
102123
# ----------------------------------------------------------------------
103124
def ConditionallyRemoveUnchangedTemplateFiles(
104125
new_manifest_dict: dict[str, str],
@@ -250,10 +271,15 @@ def CopyToOutputDir(
250271
# Please do not change the contents :)
251272
#####################################################################################\n\n"""
252273

274+
if potential_manifest.is_file():
275+
_ChangeManifestWritePermissions(manifest_filepath=potential_manifest, read_only=False)
276+
253277
with open(potential_manifest, "w") as manifest_file:
254278
manifest_file.write(yaml_comments)
255279
yaml.dump(merged_manifest, manifest_file)
256280

281+
_ChangeManifestWritePermissions(manifest_filepath=potential_manifest, read_only=True)
282+
257283
# copy temporary directory to final output directory and remove temporary directory
258284
shutil.copytree(
259285
src_dir,

tests/ProjectGenerationUtils_UnitTest.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# ----------------------------------------------------------------------
77
import pytest
88
import os
9+
from stat import S_IRUSR, S_IWUSR
10+
import sys
911
from unittest.mock import patch
1012

1113
from pathlib import Path
@@ -164,14 +166,22 @@ def test_CopyToOutputDir_overwritePrompt(fs, overwrite):
164166
with open(dest / path, "r") as destfile:
165167
assert content == destfile.read()
166168

167-
assert (dest / ".manifest.yml").is_file()
169+
manifest_filepath = dest / ".manifest.yml"
170+
assert manifest_filepath.is_file()
171+
172+
# check that manifest file has read-only permissions
173+
status = manifest_filepath.stat()
174+
assert status.st_mode & S_IWUSR == 0
175+
assert status.st_mode & S_IRUSR == S_IRUSR
168176

169177

170178
# ----------------------------------------------------------------------
171179
def test_CopyToOutputDir_no_prompt(fs):
172180
# Test for the following:
173181
# - All created files and directories are copied into the dest
174182
# - All contents of files are the same in src and dest
183+
# - Created manifest file is read-only
184+
175185
files = [(Path("test/file1"), "abc"), (Path("test/file2"), "def")]
176186
emptydir_path = Path("emptydir")
177187

@@ -189,10 +199,16 @@ def test_CopyToOutputDir_no_prompt(fs):
189199
fs.create_dir(dest)
190200

191201
directory_changes = CopyToOutputDir(src_dir=src, dest_dir=dest)
202+
manifest_filepath = dest / ".manifest.yml"
192203

193204
assert directory_changes.added_files == ["dest/test/file1", "dest/test/file2"]
194205
assert directory_changes.deleted_files == []
195206
assert directory_changes.overwritten_files == []
196207
assert directory_changes.modified_template_files == []
197208
assert _dirs_equal(expected, dest)
198-
assert (dest / ".manifest.yml").is_file()
209+
assert (manifest_filepath).is_file()
210+
211+
# check that manifest file has read-only permissions
212+
status = manifest_filepath.stat()
213+
assert status.st_mode & S_IWUSR == 0
214+
assert status.st_mode & S_IRUSR == S_IRUSR

0 commit comments

Comments
 (0)