1
0
Fork 0
mirror of https://github.com/tldr-pages/tldr.git synced 2025-04-29 23:24:55 +02:00

scripts: use pathlib and avoid page iteration (#12183)

* scripts: use pathlib and simplify page iteration

* scripts: remove unnecessary loop

* scripts: remove obsolete condition

* scripts: raise SystemError and readd coloring

* scripts: remove unused "sys" import

* scripts: use Path instead of str for git add

* scripts: revert condition removal

---------

Co-authored-by: K.B.Dharun Krishna <kbdharunkrishna@gmail.com>
This commit is contained in:
Vitor Henrique 2024-02-13 10:44:50 -03:00 committed by GitHub
parent e9345d68da
commit f4d1a1d775
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -45,7 +45,7 @@ import argparse
import os import os
import re import re
import subprocess import subprocess
import sys from pathlib import Path
labels = { labels = {
"en": "More information:", "en": "More information:",
@ -90,22 +90,19 @@ labels = {
IGNORE_FILES = (".DS_Store",) IGNORE_FILES = (".DS_Store",)
def get_tldr_root(): def get_tldr_root() -> Path:
# if this script is running from tldr/scripts, the parent's parent is the root f = Path(__file__).resolve()
f = os.path.normpath(__file__) return next(path for path in f.parents if path.name == "tldr")
if f.endswith("tldr/scripts/set-more-info-link.py"):
return os.path.dirname(os.path.dirname(f))
if "TLDR_ROOT" in os.environ: if "TLDR_ROOT" in os.environ:
return os.environ["TLDR_ROOT"] return Path(os.environ["TLDR_ROOT"])
print( raise SystemError(
"\x1b[31mPlease set TLDR_ROOT to the location of a clone of https://github.com/tldr-pages/tldr." "\x1b[31mPlease set TLDR_ROOT to the location of a clone of https://github.com/tldr-pages/tldr."
) )
sys.exit(1)
def set_link(file, link, dry_run=False): def set_link(path: Path, link: str, dry_run=False) -> str:
with open(file, encoding="utf-8") as f: with path.open(encoding="utf-8") as f:
lines = f.readlines() lines = f.readlines()
desc_start = 0 desc_start = 0
@ -120,9 +117,9 @@ def set_link(file, link, dry_run=False):
break break
# compute locale # compute locale
pages_dir = os.path.basename(os.path.dirname(os.path.dirname(file))) pages_dirname = path.parents[1].name
if "." in pages_dir: if "." in pages_dirname:
_, locale = pages_dir.split(".") _, locale = pages_dirname.split(".")
else: else:
locale = "en" locale = "en"
@ -161,14 +158,14 @@ def set_link(file, link, dry_run=False):
status = f"{status_prefix}{status}\x1b[0m" status = f"{status_prefix}{status}\x1b[0m"
if not dry_run: if not dry_run:
with open(file, "w", encoding="utf-8") as f: with path.open("w", encoding="utf-8") as f:
f.writelines(lines) f.writelines(lines)
return status return status
def get_link(file): def get_link(path: Path) -> str:
with open(file, encoding="utf-8") as f: with path.open(encoding="utf-8") as f:
lines = f.readlines() lines = f.readlines()
desc_start = 0 desc_start = 0
@ -189,17 +186,19 @@ def get_link(file):
return "" return ""
def sync(root, pages_dirs, command, link, dry_run=False): def sync(
rel_paths = [] root: Path, pages_dirs: list[str], command: str, link: str, dry_run=False
) -> list[str]:
paths = []
for page_dir in pages_dirs: for page_dir in pages_dirs:
path = os.path.join(root, page_dir, command) path = root / page_dir / command
if os.path.exists(path): if path.exists():
rel_path = path.replace(f"{root}/", "") rel_path = "/".join(path.parts[-3:])
rel_paths.append(rel_path)
status = set_link(path, link, dry_run) status = set_link(path, link, dry_run)
if status != "": if status != "":
paths.append(path)
print(f"\x1b[32m{rel_path} {status}\x1b[0m") print(f"\x1b[32m{rel_path} {status}\x1b[0m")
return rel_paths return paths
def main(): def main():
@ -239,59 +238,49 @@ def main():
args = parser.parse_args() args = parser.parse_args()
root = get_tldr_root() root = get_tldr_root()
pages_dirs = [d for d in os.listdir(root) if d.startswith("pages")] pages_dirs = [d for d in root.iterdir() if d.name.startswith("pages")]
rel_paths = [] target_paths = []
# Use '--page' option # Use '--page' option
if args.page != "": if args.page != "":
target_paths = []
if not args.page.lower().endswith(".md"): if not args.page.lower().endswith(".md"):
args.page = f"{args.page}.md" args.page = f"{args.page}.md"
arg_platform, arg_page = args.page.split("/")
for pages_dir in pages_dirs: for pages_dir in pages_dirs:
pages_dir_path = os.path.join(root, pages_dir) page_path = pages_dir / arg_platform / arg_page
platforms = [i for i in os.listdir(pages_dir_path) if i not in IGNORE_FILES] if not page_path.exists():
for platform in platforms: continue
platform_path = os.path.join(pages_dir_path, platform) target_paths.append(page_path)
commands = [
f"{platform}/{p}"
for p in os.listdir(platform_path)
if p not in IGNORE_FILES
]
if args.page in commands:
path = os.path.join(pages_dir_path, args.page)
target_paths.append(path)
target_paths.sort() target_paths.sort()
for path in target_paths: for path in target_paths:
rel_path = path.replace(f"{root}/", "") rel_path = "/".join(path.parts[-3:])
rel_paths.append(rel_path)
status = set_link(path, args.link) status = set_link(path, args.link)
if status != "": if status != "":
print(f"\x1b[32m{rel_path} {status}\x1b[0m") print(f"\x1b[32m{rel_path} {status}\x1b[0m")
# Use '--sync' option # Use '--sync' option
elif args.sync: elif args.sync:
pages_dirs.remove("pages") pages_dirs.remove(root / "pages")
en_page = os.path.join(root, "pages") en_path = root / "pages"
platforms = [i for i in os.listdir(en_page) if i not in IGNORE_FILES] platforms = [i.name for i in en_path.iterdir() if i.name not in IGNORE_FILES]
for platform in platforms: for platform in platforms:
platform_path = os.path.join(en_page, platform) platform_path = en_path / platform
commands = [ commands = [
f"{platform}/{p}" f"{platform}/{page.name}"
for p in os.listdir(platform_path) for page in platform_path.iterdir()
if p not in IGNORE_FILES if page not in IGNORE_FILES
] ]
for command in commands: for command in commands:
link = get_link(os.path.join(root, "pages", command)) link = get_link(root / "pages" / command)
if link != "": if link != "":
rel_paths += sync(root, pages_dirs, command, link, args.dry_run) target_paths += sync(root, pages_dirs, command, link, args.dry_run)
if args.stage and not args.dry_run: if args.stage and not args.dry_run and len(target_paths) > 0:
subprocess.call(["git", "add", *rel_paths], cwd=root) subprocess.call(["git", "add", *target_paths], cwd=root)
if __name__ == "__main__": if __name__ == "__main__":