scripts/set-alias-page.py, scripts/set-more-info-link.py: sync documentation and get_tldr_root (#12744)

Co-authored-by: Vítor Henrique <87824454+vitorhcl@users.noreply.github.com>
Co-authored-by: K.B.Dharun Krishna <kbdharunkrishna@gmail.com>
pull/28/head
Sebastiaan Speck 2024-05-13 16:21:30 +02:00 committed by GitHub
parent 3545dc9038
commit bc3423250b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 95 additions and 75 deletions

View File

@ -4,49 +4,56 @@
""" """
A Python script to generate or update alias pages. A Python script to generate or update alias pages.
Disclaimer: This script generates a lot of false positives so it Disclaimer: This script generates a lot of false positives so it isn't suggested to use the sync option. If used, only stage changes and commit verified changes for your language by using -l LANGUAGE.
isn't suggested to use the sync option. If used, only stage changes
and commit verified changes for your language by using -l LANGUAGE.
Note: If there is a symlink error when using the stage flag remove the `pages.en` Note: If the current directory or one of its parents is called "tldr", the script will assume it is the tldr root, i.e., the directory that contains a clone of https://github.com/tldr-pages/tldr
directory temporarily and try executing it again. If you aren't, the script will use TLDR_ROOT as the tldr root. Also, ensure 'git' is available.
Usage: Usage:
python3 scripts/set-alias-page.py [-p PAGE] [-l LANGUAGE] [-s] [-S] [-n] [COMMAND] python3 scripts/set-alias-page.py [-p PAGE] [-S] [-l LANGUAGE] [-s] [-n] [COMMAND]
Options: Options:
-p, --page PAGE -p, --page PAGE
Specify the alias page in the format "platform/alias_command.md". Specify the alias page in the format "platform/alias_command.md".
-S, --sync
Synchronize each translation's alias page (if exists) with that of the English page.
-l, --language LANGUAGE -l, --language LANGUAGE
Specify the language, a POSIX Locale Name in the form of "ll" or "ll_CC" (e.g. "fr" or "pt_BR"). Specify the language, a POSIX Locale Name in the form of "ll" or "ll_CC" (e.g. "fr" or "pt_BR").
-s, --stage -s, --stage
Stage modified pages (requires 'git' on $PATH and TLDR_ROOT to be a Git repository). Stage modified pages (requires 'git' on $PATH and TLDR_ROOT to be a Git repository).
-S, --sync
Synchronize each translation's alias page (if exists) with that of the English page.
-n, --dry-run -n, --dry-run
Show what changes would be made without actually modifying the page. Show what changes would be made without actually modifying the page.
Positional Argument:
COMMAND The command to be set as the alias command.
Examples: Examples:
1. Add 'vi' as an alias page of 'vim': 1. Add 'vi' as an alias page of 'vim':
python3 scripts/set-alias-page.py -p common/vi vim python3 scripts/set-alias-page.py -p common/vi vim
python3 scripts/set-alias-page.py --page common/vi vim
2. Read English alias pages and synchronize them into all translations: 2. Read English alias pages and synchronize them into all translations:
python3 scripts/set-alias-page.py -S python3 scripts/set-alias-page.py -S
python3 scripts/set-alias-page.py --sync
3. Read English alias pages and show what changes would be made: 3. Read English alias pages and synchronize them for Brazilian Portuguese pages only:
python3 scripts/set-alias-page.py -Sn
python3 scripts/set-alias-page.py --sync --dry-run
4. Read English alias pages and synchronize them for Brazilian Portuguese pages only:
python3 scripts/set-alias-page.py -S -l pt_BR python3 scripts/set-alias-page.py -S -l pt_BR
python3 scripts/set-alias-page.py --sync --language pt_BR 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
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 argparse
import os import os
import re import re
import subprocess import subprocess
from pathlib import Path
IGNORE_FILES = (".DS_Store", "tldr.md", "aria2.md") IGNORE_FILES = (".DS_Store", "tldr.md", "aria2.md")
@ -57,16 +64,16 @@ def get_tldr_root():
""" """
# If this script is running from tldr/scripts, the parent's parent is the root # If this script is running from tldr/scripts, the parent's parent is the root
f = os.path.normpath(__file__) f = Path(__file__).resolve()
if f.endswith("tldr/scripts/set-alias-page.py"): if (
return os.path.dirname(os.path.dirname(f)) tldr_root := next((path for path in f.parents if path.name == "tldr"), None)
) is not None:
if "TLDR_ROOT" in os.environ: return tldr_root
return os.environ["TLDR_ROOT"] elif "TLDR_ROOT" in os.environ:
else: return Path(os.environ["TLDR_ROOT"])
raise SystemExit( raise SystemExit(
"\x1b[31mPlease set TLDR_ROOT to the location of a clone of https://github.com/tldr-pages/tldr.\x1b[0m" "\x1b[31mPlease set TLDR_ROOT to the location of a clone of https://github.com/tldr-pages/tldr."
) )
def get_templates(root): def get_templates(root):
@ -177,7 +184,7 @@ def set_alias_page(file, command, dry_run=False, language_to_update=""):
action = "updated" action = "updated"
if dry_run: if dry_run:
status = f"page will be {action}" status = f"page would be {action}"
else: else:
status = f"page {action}" status = f"page {action}"
@ -237,6 +244,13 @@ def main():
default="", default="",
help='page name in the format "platform/alias_command.md"', 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( parser.add_argument(
"-l", "-l",
"--language", "--language",
@ -252,13 +266,6 @@ def main():
default=False, default=False,
help="stage modified pages (requires `git` to be on $PATH and TLDR_ROOT to be a Git repository)", help="stage modified pages (requires `git` to be on $PATH and TLDR_ROOT to be a Git repository)",
) )
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( parser.add_argument(
"-n", "-n",
"--dry-run", "--dry-run",

View File

@ -2,25 +2,23 @@
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
""" """
This script sets the "More information" link for all translations of a page. A Python script to add or update the "More information" link for all translations of a page.
It can be used to add or update the links in translations.
Note: Before running this script, ensure that TLDR_ROOT is set to the location Note: If the current directory or one of its parents is called "tldr", the script will assume it is the tldr root, i.e., the directory that contains a clone of https://github.com/tldr-pages/tldr
of a clone of https://github.com/tldr-pages/tldr, and 'git' is available. If you aren't, the script will use TLDR_ROOT as the tldr root. Also, ensure 'git' is available.
If there is a symlink error when using the stage flag remove the `pages.en`
directory temporarily and try executing it again.
Usage: python3 scripts/set-more-info-link.py [-p PAGE] [-s] [-S] [-n] [LINK] Usage:
python3 scripts/set-more-info-link.py [-p PAGE] [-S] [-s] [-n] [LINK]
Supported Arguments: Options:
-p, --page Specify the page name in the format "platform/command.md". -p, --page PAGE
This option allows setting the link for a specific page. Specify the alias page in the format "platform/alias_command.md". This option allows setting the link for a specific page.
-s, --stage Stage modified pages for commit. This option requires 'git' -S, --sync
to be on the $PATH and TLDR_ROOT to be a Git repository. Synchronize each translation's more information link (if exists) with that of the English page.
-S, --sync Synchronize each translation's more information link (if -s, --stage
exists) with that of the English page. This is useful to Stage modified pages (requires 'git' on $PATH and TLDR_ROOT to be a Git repository).
ensure consistency across translations. -n, --dry-run
-n, --dry-run Show what changes would be made without actually modifying the page. Show what changes would be made without actually modifying the page.
Positional Argument: Positional Argument:
LINK The link to be set as the "More information" link. LINK The link to be set as the "More information" link.
@ -28,11 +26,13 @@ Positional Argument:
Examples: Examples:
1. Set the link for a specific page: 1. Set the link for a specific page:
python3 scripts/set-more-info-link.py -p common/tar.md https://example.com python3 scripts/set-more-info-link.py -p common/tar.md https://example.com
python3 scripts/set-more-info-link.py --page common/tar.md https://example.com
2. Synchronize more information links across translations: 2. Read English pages and synchronize more information links across translations:
python3 scripts/set-more-info-link.py -S python3 scripts/set-more-info-link.py -S
python3 scripts/set-more-info-link.py --sync
3. Synchronize more information links across translations and stage modified pages for commit: 3. Read English pages, synchronize more information links across translations and stage modified pages for commit:
python3 scripts/set-more-info-link.py -Ss python3 scripts/set-more-info-link.py -Ss
python3 scripts/set-more-info-link.py --sync --stage python3 scripts/set-more-info-link.py --sync --stage
@ -90,13 +90,20 @@ labels = {
IGNORE_FILES = (".DS_Store",) IGNORE_FILES = (".DS_Store",)
def get_tldr_root() -> Path: def get_tldr_root():
f = Path(__file__).resolve() """
return next(path for path in f.parents if path.name == "tldr") Get the path of local tldr repository for environment variable TLDR_ROOT.
"""
if "TLDR_ROOT" in os.environ: # 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"]) return Path(os.environ["TLDR_ROOT"])
raise SystemError( raise SystemExit(
"\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."
) )
@ -151,7 +158,7 @@ def set_link(path: Path, link: str, dry_run=False) -> str:
action = "added" action = "added"
if dry_run: if dry_run:
status = f"link will be {action}" status = f"link would be {action}"
else: else:
status = f"link {action}" status = f"link {action}"
@ -213,13 +220,6 @@ def main():
default="", default="",
help='page name in the format "platform/command.md"', help='page name in the format "platform/command.md"',
) )
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( parser.add_argument(
"-S", "-S",
"--sync", "--sync",
@ -227,6 +227,13 @@ def main():
default=False, default=False,
help="synchronize each translation's more information link (if exists) with that of English page", help="synchronize each translation's more information link (if exists) with that of English page",
) )
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( parser.add_argument(
"-n", "-n",
"--dry-run", "--dry-run",

View File

@ -2,23 +2,23 @@
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
""" """
This script sets the title for all translations of a page. A Python script to add or update the page title for all translations of a page.
It can be used to update the titles in translations.
Note: If the current directory or one of its parents is called "tldr", the script will assume it is the tldr root, i.e., the directory that contains a clone of https://github.com/tldr-pages/tldr Note: If the current directory or one of its parents is called "tldr", the script will assume it is the tldr root, i.e., the directory that contains a clone of https://github.com/tldr-pages/tldr
If you aren't, the script will use TLDR_ROOT as the tldr root. Also, ensure 'git' is available. If you aren't, the script will use TLDR_ROOT as the tldr root. Also, ensure 'git' is available.
Usage: python3 scripts/set-page-title.py [-p PAGE] [-s] [-S] [-n] [TITLE] Usage:
python3 scripts/set-page-title.py [-p PAGE] [-S] [-s] [-n] [TITLE]
Supported Arguments: Options:
-p, --page Specify the page name in the format "platform/command.md". -p, --page PAGE
This option allows setting the title for a specific page. Specify the page in the format "platform/command.md". This option allows setting the title for a specific page.
-s, --stage Stage modified pages for commit. This option requires 'git' -S, --sync
to be on the $PATH and TLDR_ROOT to be a Git repository. Synchronize each translation's title (if exists) with that of the English page.
-S, --sync Synchronize each translation's title (if -s, --stage
exists) with that of the English page. This is useful to Stage modified pages (requires 'git' on $PATH and TLDR_ROOT to be a Git repository).
ensure consistency across translations. -n, --dry-run
-n, --dry-run Show what changes would be made without actually modifying the page. Show what changes would be made without actually modifying the page.
Positional Argument: Positional Argument:
TITLE The title to be set as the title. TITLE The title to be set as the title.
@ -26,9 +26,11 @@ Positional Argument:
Examples: Examples:
1. Set the title for a specific page: 1. Set the title for a specific page:
python3 scripts/set-page-title.py -p common/tar.md tar.1 python3 scripts/set-page-title.py -p common/tar.md tar.1
python3 scripts/set-page-title.py --page common/tar.md tar.1
2. Synchronize titles across translations: 2. Synchronize titles across translations:
python3 scripts/set-page-title.py -S python3 scripts/set-page-title.py -S
python3 scripts/set-page-title.py --sync
3. Synchronize titles across translations and stage modified pages for commit: 3. Synchronize titles across translations and stage modified pages for commit:
python3 scripts/set-page-title.py -Ss python3 scripts/set-page-title.py -Ss
@ -47,7 +49,11 @@ from pathlib import Path
IGNORE_FILES = (".DS_Store",) IGNORE_FILES = (".DS_Store",)
def get_tldr_root() -> Path: 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 # If this script is running from tldr/scripts, the parent's parent is the root
f = Path(__file__).resolve() f = Path(__file__).resolve()
if ( if (