Skip to content

Commit 544c1d2

Browse files
committed
Create a function to perform chgrp operations
1 parent 9bcde43 commit 544c1d2

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

build_docs.py

+39-26
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,8 @@ def build(self) -> None:
670670
"SPHINXERRORHANDLING=",
671671
maketarget,
672672
])
673-
run(["mkdir", "-p", self.log_directory])
674-
run(["chgrp", "-R", self.group, self.log_directory])
673+
self.log_directory.mkdir(parents=True, exist_ok=True)
674+
chgrp(self.log_directory, group=self.group, recursive=True)
675675
if self.includes_html:
676676
setup_switchers(
677677
self.switchers_content, self.checkout / "Doc" / "build" / "html"
@@ -722,10 +722,7 @@ def copy_build_to_webroot(self, http: urllib3.PoolManager) -> None:
722722
else:
723723
language_dir = self.www_root / self.language.tag
724724
language_dir.mkdir(parents=True, exist_ok=True)
725-
try:
726-
run(["chgrp", "-R", self.group, language_dir])
727-
except subprocess.CalledProcessError as err:
728-
logging.warning("Can't change group of %s: %s", language_dir, str(err))
725+
chgrp(language_dir, group=self.group, recursive=True)
729726
language_dir.chmod(0o775)
730727
target = language_dir / self.version.name
731728

@@ -734,22 +731,18 @@ def copy_build_to_webroot(self, http: urllib3.PoolManager) -> None:
734731
target.chmod(0o775)
735732
except PermissionError as err:
736733
logging.warning("Can't change mod of %s: %s", target, str(err))
737-
try:
738-
run(["chgrp", "-R", self.group, target])
739-
except subprocess.CalledProcessError as err:
740-
logging.warning("Can't change group of %s: %s", target, str(err))
734+
chgrp(target, group=self.group, recursive=True)
741735

742736
changed = []
743737
if self.includes_html:
744738
# Copy built HTML files to webroot (default /srv/docs.python.org)
745739
changed = changed_files(self.checkout / "Doc" / "build" / "html", target)
746740
logging.info("Copying HTML files to %s", target)
747-
run([
748-
"chown",
749-
"-R",
750-
":" + self.group,
741+
chgrp(
751742
self.checkout / "Doc" / "build" / "html/",
752-
])
743+
group=self.group,
744+
recursive=True,
745+
)
753746
run(["chmod", "-R", "o+r", self.checkout / "Doc" / "build" / "html"])
754747
run([
755748
"find",
@@ -775,20 +768,17 @@ def copy_build_to_webroot(self, http: urllib3.PoolManager) -> None:
775768
if not self.quick and (self.checkout / "Doc" / "dist").is_dir():
776769
# Copy archive files to /archives/
777770
logging.debug("Copying dist files.")
778-
run([
779-
"chown",
780-
"-R",
781-
":" + self.group,
782-
self.checkout / "Doc" / "dist",
783-
])
771+
chgrp(
772+
self.checkout / "Doc" / "dist", group=self.group, recursive=True
773+
)
784774
run([
785775
"chmod",
786776
"-R",
787777
"o+r",
788778
self.checkout / "Doc" / "dist",
789779
])
790780
run(["mkdir", "-m", "o+rx", "-p", target / "archives"])
791-
run(["chown", ":" + self.group, target / "archives"])
781+
chgrp(target / "archives", group=self.group)
792782
run([
793783
"cp",
794784
"-a",
@@ -888,6 +878,29 @@ def save_state(
888878
logging.info("Saved new rebuild state for %s: %s", key, table.as_string())
889879

890880

881+
def chgrp(path: Path, /, group: int | str | None, *, recursive: bool = False) -> None:
882+
if sys.platform == "win32":
883+
return
884+
885+
from grp import getgrnam
886+
887+
try:
888+
try:
889+
group_id = int(group)
890+
except ValueError:
891+
group_id = getgrnam(group)[2]
892+
except (LookupError, TypeError, ValueError):
893+
return
894+
895+
try:
896+
os.chown(path, -1, group_id)
897+
if recursive:
898+
for p in path.rglob("*"):
899+
os.chown(p, -1, group_id)
900+
except OSError as err:
901+
logging.warning("Can't change group of %s: %s", path, str(err))
902+
903+
891904
def format_seconds(seconds: float) -> str:
892905
hours, remainder = divmod(seconds, 3600)
893906
minutes, seconds = divmod(remainder, 60)
@@ -1212,7 +1225,7 @@ def build_sitemap(
12121225
sitemap_path = www_root / "sitemap.xml"
12131226
sitemap_path.write_text(rendered_template + "\n", encoding="UTF-8")
12141227
sitemap_path.chmod(0o664)
1215-
run(["chgrp", group, sitemap_path])
1228+
chgrp(sitemap_path, group=group)
12161229

12171230

12181231
def build_404(www_root: Path, group: str) -> None:
@@ -1224,7 +1237,7 @@ def build_404(www_root: Path, group: str) -> None:
12241237
not_found_file = www_root / "404.html"
12251238
shutil.copyfile(HERE / "templates" / "404.html", not_found_file)
12261239
not_found_file.chmod(0o664)
1227-
run(["chgrp", group, not_found_file])
1240+
chgrp(not_found_file, group=group)
12281241

12291242

12301243
def copy_robots_txt(
@@ -1242,7 +1255,7 @@ def copy_robots_txt(
12421255
robots_path = www_root / "robots.txt"
12431256
shutil.copyfile(template_path, robots_path)
12441257
robots_path.chmod(0o775)
1245-
run(["chgrp", group, robots_path])
1258+
chgrp(robots_path, group=group)
12461259
if not skip_cache_invalidation:
12471260
purge(http, "robots.txt")
12481261

@@ -1310,7 +1323,7 @@ def symlink(
13101323
# Link does not exist or points to the wrong target.
13111324
link.unlink(missing_ok=True)
13121325
link.symlink_to(directory)
1313-
run(["chown", "-h", f":{group}", str(link)])
1326+
chgrp(link, group=group) # --no-dereference
13141327
if not skip_cache_invalidation:
13151328
surrogate_key = f"{language_tag}/{name}"
13161329
purge_surrogate_key(http, surrogate_key)

0 commit comments

Comments
 (0)