Skip to content

Create a function to perform chgrp operations #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Changes from all commits
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
70 changes: 44 additions & 26 deletions build_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,8 @@
"SPHINXERRORHANDLING=",
maketarget,
])
run(["mkdir", "-p", self.log_directory])
run(["chgrp", "-R", self.group, self.log_directory])
self.log_directory.mkdir(parents=True, exist_ok=True)
chgrp(self.log_directory, group=self.group, recursive=True)

Check warning on line 674 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L673-L674

Added lines #L673 - L674 were not covered by tests
if self.includes_html:
setup_switchers(
self.switchers_content, self.checkout / "Doc" / "build" / "html"
Expand Down Expand Up @@ -722,10 +722,7 @@
else:
language_dir = self.www_root / self.language.tag
language_dir.mkdir(parents=True, exist_ok=True)
try:
run(["chgrp", "-R", self.group, language_dir])
except subprocess.CalledProcessError as err:
logging.warning("Can't change group of %s: %s", language_dir, str(err))
chgrp(language_dir, group=self.group, recursive=True)

Check warning on line 725 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L725

Added line #L725 was not covered by tests
language_dir.chmod(0o775)
target = language_dir / self.version.name

Expand All @@ -734,22 +731,18 @@
target.chmod(0o775)
except PermissionError as err:
logging.warning("Can't change mod of %s: %s", target, str(err))
try:
run(["chgrp", "-R", self.group, target])
except subprocess.CalledProcessError as err:
logging.warning("Can't change group of %s: %s", target, str(err))
chgrp(target, group=self.group, recursive=True)

Check warning on line 734 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L734

Added line #L734 was not covered by tests

changed = []
if self.includes_html:
# Copy built HTML files to webroot (default /srv/docs.python.org)
changed = changed_files(self.checkout / "Doc" / "build" / "html", target)
logging.info("Copying HTML files to %s", target)
run([
"chown",
"-R",
":" + self.group,
chgrp(

Check warning on line 741 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L741

Added line #L741 was not covered by tests
self.checkout / "Doc" / "build" / "html/",
])
group=self.group,
recursive=True,
)
run(["chmod", "-R", "o+r", self.checkout / "Doc" / "build" / "html"])
run([
"find",
Expand All @@ -775,20 +768,15 @@
if not self.quick and (self.checkout / "Doc" / "dist").is_dir():
# Copy archive files to /archives/
logging.debug("Copying dist files.")
run([
"chown",
"-R",
":" + self.group,
self.checkout / "Doc" / "dist",
])
chgrp(self.checkout / "Doc" / "dist", group=self.group, recursive=True)

Check warning on line 771 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L771

Added line #L771 was not covered by tests
run([
"chmod",
"-R",
"o+r",
self.checkout / "Doc" / "dist",
])
run(["mkdir", "-m", "o+rx", "-p", target / "archives"])
run(["chown", ":" + self.group, target / "archives"])
chgrp(target / "archives", group=self.group)

Check warning on line 779 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L779

Added line #L779 was not covered by tests
run([
"cp",
"-a",
Expand Down Expand Up @@ -888,6 +876,36 @@
logging.info("Saved new rebuild state for %s: %s", key, table.as_string())


def chgrp(
path: Path,
/,
group: int | str | None,
*,
recursive: bool = False,
follow_symlinks: bool = True,
) -> None:
if sys.platform == "win32":
return

from grp import getgrnam

try:
try:
group_id = int(group)
except ValueError:
group_id = getgrnam(group)[2]
except (LookupError, TypeError, ValueError):
return

try:
os.chown(path, -1, group_id, follow_symlinks=follow_symlinks)
if recursive:
for p in path.rglob("*"):
os.chown(p, -1, group_id, follow_symlinks=follow_symlinks)
except OSError as err:
logging.warning("Can't change group of %s: %s", path, str(err))

Check warning on line 907 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L887-L907

Added lines #L887 - L907 were not covered by tests

def format_seconds(seconds: float) -> str:
hours, remainder = divmod(seconds, 3600)
minutes, seconds = divmod(remainder, 60)
Expand Down Expand Up @@ -1212,7 +1230,7 @@
sitemap_path = www_root / "sitemap.xml"
sitemap_path.write_text(rendered_template + "\n", encoding="UTF-8")
sitemap_path.chmod(0o664)
run(["chgrp", group, sitemap_path])
chgrp(sitemap_path, group=group)

Check warning on line 1233 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L1233

Added line #L1233 was not covered by tests


def build_404(www_root: Path, group: str) -> None:
Expand All @@ -1224,7 +1242,7 @@
not_found_file = www_root / "404.html"
shutil.copyfile(HERE / "templates" / "404.html", not_found_file)
not_found_file.chmod(0o664)
run(["chgrp", group, not_found_file])
chgrp(not_found_file, group=group)

Check warning on line 1245 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L1245

Added line #L1245 was not covered by tests


def copy_robots_txt(
Expand All @@ -1242,7 +1260,7 @@
robots_path = www_root / "robots.txt"
shutil.copyfile(template_path, robots_path)
robots_path.chmod(0o775)
run(["chgrp", group, robots_path])
chgrp(robots_path, group=group)

Check warning on line 1263 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L1263

Added line #L1263 was not covered by tests
if not skip_cache_invalidation:
purge(http, "robots.txt")

Expand Down Expand Up @@ -1310,7 +1328,7 @@
# Link does not exist or points to the wrong target.
link.unlink(missing_ok=True)
link.symlink_to(directory)
run(["chown", "-h", f":{group}", str(link)])
chgrp(link, group=group, follow_symlinks=False)

Check warning on line 1331 in build_docs.py

View check run for this annotation

Codecov / codecov/patch

build_docs.py#L1331

Added line #L1331 was not covered by tests
if not skip_cache_invalidation:
surrogate_key = f"{language_tag}/{name}"
purge_surrogate_key(http, surrogate_key)
Expand Down