scripts/_common.py
: add a shared Python file to move duplicated code (#12755)
* `scripts/_common.py`: add a shared Python file to move duplicated code --------- Co-authored-by: Vítor Henrique <87824454+vitorhcl@users.noreply.github.com> Co-authored-by: K.B.Dharun Krishna <kbdharunkrishna@gmail.com>
This commit is contained in:
@@ -41,57 +41,56 @@ Examples:
|
||||
python3 scripts/set-alias-page.py --sync --language pt_BR
|
||||
|
||||
4. Read English alias pages, synchronize them into all translations and stage modified pages for commit:
|
||||
python3 scripts/set-more-info-link.py -Ss
|
||||
python3 scripts/set-more-info-link.py --sync --stage
|
||||
python3 scripts/set-alias-page.py -Ss
|
||||
python3 scripts/set-alias-page.py --sync --stage
|
||||
|
||||
5. Read English alias pages and show what changes would be made:
|
||||
python3 scripts/set-alias-page.py -Sn
|
||||
python3 scripts/set-alias-page.py --sync --dry-run
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from _common import (
|
||||
IGNORE_FILES,
|
||||
Colors,
|
||||
get_tldr_root,
|
||||
get_pages_dir,
|
||||
get_target_paths,
|
||||
get_locale,
|
||||
get_status,
|
||||
stage,
|
||||
create_colored_line,
|
||||
create_argument_parser,
|
||||
)
|
||||
|
||||
IGNORE_FILES = (".DS_Store", "tldr.md", "aria2.md")
|
||||
IGNORE_FILES += ("tldr.md", "aria2.md")
|
||||
|
||||
|
||||
def get_tldr_root():
|
||||
"""
|
||||
Get the path of local tldr repository for environment variable TLDR_ROOT.
|
||||
"""
|
||||
|
||||
# If this script is running from tldr/scripts, the parent's parent is the root
|
||||
f = Path(__file__).resolve()
|
||||
if (
|
||||
tldr_root := next((path for path in f.parents if path.name == "tldr"), None)
|
||||
) is not None:
|
||||
return tldr_root
|
||||
elif "TLDR_ROOT" in os.environ:
|
||||
return Path(os.environ["TLDR_ROOT"])
|
||||
raise SystemExit(
|
||||
"\x1b[31mPlease set TLDR_ROOT to the location of a clone of https://github.com/tldr-pages/tldr."
|
||||
def test_ignore_files():
|
||||
assert IGNORE_FILES == (
|
||||
".DS_Store",
|
||||
"tldr.md",
|
||||
"aria2.md",
|
||||
)
|
||||
assert ".DS_Store" in IGNORE_FILES
|
||||
assert "tldr.md" in IGNORE_FILES
|
||||
|
||||
|
||||
def get_templates(root):
|
||||
def get_templates(root: Path):
|
||||
"""
|
||||
Get all alias page translation templates from
|
||||
TLDR_ROOT/contributing-guides/translation-templates/alias-pages.md.
|
||||
|
||||
Parameters:
|
||||
root (string): The path of local tldr repository, i.e., TLDR_ROOT.
|
||||
root (Path): The path of local tldr repository, i.e., TLDR_ROOT.
|
||||
|
||||
Returns:
|
||||
dict of (str, str): Language labels map to alias page templates.
|
||||
"""
|
||||
|
||||
template_file = os.path.join(
|
||||
root, "contributing-guides/translation-templates/alias-pages.md"
|
||||
)
|
||||
with open(template_file, encoding="utf-8") as f:
|
||||
template_file = root / "contributing-guides/translation-templates/alias-pages.md"
|
||||
with template_file.open(encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
# Parse alias-pages.md
|
||||
@@ -122,156 +121,115 @@ def get_templates(root):
|
||||
return templates
|
||||
|
||||
|
||||
def get_alias_page(file):
|
||||
"""
|
||||
Determine whether the given file is an alias page.
|
||||
|
||||
Parameters:
|
||||
file (string): Path to a page
|
||||
|
||||
Returns:
|
||||
str: "" If the file doesn't exit or is not an alias page,
|
||||
otherwise return what command the alias stands for.
|
||||
"""
|
||||
|
||||
if not os.path.isfile(file):
|
||||
return ""
|
||||
with open(file, "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
if match := re.search(r"^`tldr (.+)`", line):
|
||||
return match[1]
|
||||
return ""
|
||||
|
||||
|
||||
def set_alias_page(file, command, dry_run=False, language_to_update=""):
|
||||
def set_alias_page(
|
||||
path: Path, command: str, dry_run: bool = False, language_to_update: str = ""
|
||||
) -> str:
|
||||
"""
|
||||
Write an alias page to disk.
|
||||
|
||||
Parameters:
|
||||
file (string): Path to an alias page
|
||||
path (string): Path to an alias page
|
||||
command (string): The command that the alias stands for.
|
||||
dry_run (bool): Whether to perform a dry-run, i.e. only show the changes that would be made.
|
||||
language_to_update (string): Optionally, the language of the translation to be updated.
|
||||
|
||||
Returns:
|
||||
str: Execution status
|
||||
"" if the alias page standing for the same command already exists.
|
||||
"\x1b[36mpage added" if it's a new alias page.
|
||||
"\x1b[34mpage updated" if the command updates.
|
||||
"" if the alias page standing for the same command already exists or if the locale does not match language_to_update.
|
||||
"\x1b[36mpage added"
|
||||
"\x1b[34mpage updated"
|
||||
"\x1b[36mpage would be added"
|
||||
"\x1b[34mpage would updated"
|
||||
"""
|
||||
|
||||
# compute locale
|
||||
pages_dir = os.path.basename(os.path.dirname(os.path.dirname(file)))
|
||||
if "." in pages_dir:
|
||||
_, locale = pages_dir.split(".")
|
||||
else:
|
||||
locale = "en"
|
||||
locale = get_locale(path)
|
||||
if locale not in templates or (
|
||||
language_to_update != "" and locale != language_to_update
|
||||
):
|
||||
# return empty status to indicate that no changes were made
|
||||
return ""
|
||||
|
||||
# Test if the alias page already exists
|
||||
orig_command = get_alias_page(file)
|
||||
if orig_command == command:
|
||||
original_command = get_alias_page(path)
|
||||
if original_command == command:
|
||||
return ""
|
||||
|
||||
if orig_command == "":
|
||||
status_prefix = "\x1b[36m"
|
||||
action = "added"
|
||||
else:
|
||||
status_prefix = "\x1b[34m"
|
||||
action = "updated"
|
||||
status = get_status(
|
||||
"added" if original_command == "" else "updated", dry_run, "page"
|
||||
)
|
||||
|
||||
if dry_run:
|
||||
status = f"page would be {action}"
|
||||
else:
|
||||
status = f"page {action}"
|
||||
|
||||
status = f"{status_prefix}{status}\x1b[0m"
|
||||
|
||||
if not dry_run: # Only write to the file during a non-dry-run
|
||||
alias_name, _ = os.path.splitext(os.path.basename(file))
|
||||
if not dry_run: # Only write to the path during a non-dry-run
|
||||
alias_name = path.stem
|
||||
text = (
|
||||
templates[locale]
|
||||
.replace("example", alias_name, 1)
|
||||
.replace("example", command)
|
||||
)
|
||||
os.makedirs(os.path.dirname(file), exist_ok=True)
|
||||
with open(file, "w", encoding="utf-8") as f:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with path.open("w", encoding="utf-8") as f:
|
||||
f.write(text)
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def get_alias_page(path: Path) -> str:
|
||||
"""
|
||||
Determine whether the given path is an alias page.
|
||||
|
||||
Parameters:
|
||||
path (Path): Path to a page
|
||||
|
||||
Returns:
|
||||
str: "" If the path doesn't exit or is not an alias page,
|
||||
otherwise return what command the alias stands for.
|
||||
"""
|
||||
|
||||
if not path.exists():
|
||||
return ""
|
||||
with path.open(encoding="utf-8") as f:
|
||||
for line in f:
|
||||
# match alias (`tldr <alias>`)
|
||||
if match := re.search(r"^`tldr (.+)`", line):
|
||||
return match[1]
|
||||
return ""
|
||||
|
||||
|
||||
def sync(
|
||||
root, pages_dirs, alias_name, orig_command, dry_run=False, language_to_update=""
|
||||
):
|
||||
root: Path,
|
||||
pages_dirs: list[Path],
|
||||
alias_name: str,
|
||||
original_command: str,
|
||||
dry_run: bool = False,
|
||||
language_to_update: str = "",
|
||||
) -> list[Path]:
|
||||
"""
|
||||
Synchronize an alias page into all translations.
|
||||
|
||||
Parameters:
|
||||
root (str): TLDR_ROOT
|
||||
pages_dirs (list of str): Strings of page entry and platform, e.g. "page.fr/common".
|
||||
root (Path): TLDR_ROOT
|
||||
pages_dirs (list of Path's): Path's of page entry and platform, e.g. "page.fr/common".
|
||||
alias_name (str): An alias command with .md extension like "vi.md".
|
||||
orig_command (string): An Original command like "vim".
|
||||
original_command (str): An Original command like "vim".
|
||||
dry_run (bool): Whether to perform a dry-run, i.e. only show the changes that would be made.
|
||||
language_to_update (string): Optionally, the language of the translation to be updated.
|
||||
language_to_update (str): Optionally, the language of the translation to be updated.
|
||||
|
||||
Returns:
|
||||
list: A list of paths to be staged into git, using by --stage option.
|
||||
list (list of Path's): A list of Path's to be staged into git, using by --stage option.
|
||||
"""
|
||||
rel_paths = []
|
||||
paths = []
|
||||
for page_dir in pages_dirs:
|
||||
path = os.path.join(root, page_dir, alias_name)
|
||||
status = set_alias_page(path, orig_command, dry_run, language_to_update)
|
||||
path = root / page_dir / alias_name
|
||||
status = set_alias_page(path, original_command, dry_run, language_to_update)
|
||||
if status != "":
|
||||
rel_path = path.replace(f"{root}/", "")
|
||||
rel_paths.append(rel_path)
|
||||
print(f"\x1b[32m{rel_path} {status}\x1b[0m")
|
||||
return rel_paths
|
||||
rel_path = "/".join(path.parts[-3:])
|
||||
paths.append(rel_path)
|
||||
print(create_colored_line(Colors.GREEN, f"{rel_path} {status}"))
|
||||
return paths
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Sets the alias page for all translations of a page"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-p",
|
||||
"--page",
|
||||
type=str,
|
||||
required=False,
|
||||
default="",
|
||||
help='page name in the format "platform/alias_command.md"',
|
||||
)
|
||||
parser.add_argument(
|
||||
"-S",
|
||||
"--sync",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="synchronize each translation's alias page (if exists) with that of English page",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-l",
|
||||
"--language",
|
||||
type=str,
|
||||
required=False,
|
||||
default="",
|
||||
help='language in the format "ll" or "ll_CC" (e.g. "fr" or "pt_BR")',
|
||||
)
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
"--stage",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="stage modified pages (requires `git` to be on $PATH and TLDR_ROOT to be a Git repository)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-n",
|
||||
"--dry-run",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="show what changes would be made without actually modifying the pages",
|
||||
parser = create_argument_parser(
|
||||
"Sets the alias page for all translations of a page"
|
||||
)
|
||||
parser.add_argument("command", type=str, nargs="?", default="")
|
||||
args = parser.parse_args()
|
||||
@@ -281,51 +239,47 @@ def main():
|
||||
# A dictionary of all alias page translations
|
||||
global templates
|
||||
templates = get_templates(root)
|
||||
pages_dirs = [d for d in os.listdir(root) if d.startswith("pages")]
|
||||
rel_paths = []
|
||||
pages_dirs = get_pages_dir(root)
|
||||
|
||||
target_paths = []
|
||||
|
||||
# Use '--page' option
|
||||
if args.page != "":
|
||||
if not args.page.lower().endswith(".md"):
|
||||
args.page = f"{args.page}.md"
|
||||
|
||||
target_paths = [os.path.join(root, p, args.page) for p in pages_dirs]
|
||||
target_paths.sort()
|
||||
target_paths += get_target_paths(args.page, pages_dirs)
|
||||
|
||||
for path in target_paths:
|
||||
rel_path = path.replace(f"{root}/", "")
|
||||
rel_paths.append(rel_path)
|
||||
status = set_alias_page(path, args.command, args.language)
|
||||
rel_path = "/".join(path.parts[-3:])
|
||||
status = set_alias_page(path, args.command, args.dry_run, args.language)
|
||||
if status != "":
|
||||
print(f"\x1b[32m{rel_path} {status}\x1b[0m")
|
||||
print(create_colored_line(Colors.GREEN, f"{rel_path} {status}"))
|
||||
|
||||
# Use '--sync' option
|
||||
elif args.sync:
|
||||
pages_dirs.remove("pages")
|
||||
en_page = os.path.join(root, "pages")
|
||||
platforms = [i for i in os.listdir(en_page) if i not in IGNORE_FILES]
|
||||
pages_dirs.remove(root / "pages")
|
||||
en_path = root / "pages"
|
||||
platforms = [i.name for i in en_path.iterdir() if i.name not in IGNORE_FILES]
|
||||
for platform in platforms:
|
||||
platform_path = os.path.join(en_page, platform)
|
||||
platform_path = en_path / platform
|
||||
commands = [
|
||||
f"{platform}/{p}"
|
||||
for p in os.listdir(platform_path)
|
||||
if p not in IGNORE_FILES
|
||||
f"{platform}/{page.name}"
|
||||
for page in platform_path.iterdir()
|
||||
if page.name not in IGNORE_FILES
|
||||
]
|
||||
for command in commands:
|
||||
orig_command = get_alias_page(os.path.join(root, "pages", command))
|
||||
if orig_command != "":
|
||||
rel_paths += sync(
|
||||
original_command = get_alias_page(root / "pages" / command)
|
||||
if original_command != "":
|
||||
target_paths += sync(
|
||||
root,
|
||||
pages_dirs,
|
||||
command,
|
||||
orig_command,
|
||||
original_command,
|
||||
args.dry_run,
|
||||
args.language,
|
||||
)
|
||||
|
||||
# Use '--stage' option
|
||||
if args.stage and not args.dry_run:
|
||||
subprocess.call(["git", "add", *rel_paths], cwd=root)
|
||||
if args.stage and not args.dry_run and len(target_paths) > 0:
|
||||
stage(target_paths)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Reference in New Issue
Block a user