scripts: add option to dry-run (#11883)
This commit is contained in:
@@ -10,7 +10,7 @@ of a clone of https://github.com/tldr-pages/tldr, and '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] [LINK]
|
||||
Usage: python3 scripts/set-more-info-link.py [-p PAGE] [-s] [-S] [-n] [LINK]
|
||||
|
||||
Supported Arguments:
|
||||
-p, --page Specify the page name in the format "platform/command.md".
|
||||
@@ -20,6 +20,7 @@ Supported Arguments:
|
||||
-S, --sync Synchronize each translation's more information link (if
|
||||
exists) with that of the English page. This is useful to
|
||||
ensure consistency across translations.
|
||||
-n, --dry-run Show what changes would be made without actually modifying the page.
|
||||
|
||||
Positional Argument:
|
||||
LINK The link to be set as the "More information" link.
|
||||
@@ -34,6 +35,10 @@ Examples:
|
||||
3. 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 --sync --stage
|
||||
|
||||
4. Show what changes would be made across translations:
|
||||
python3 scripts/set-more-info-link.py -Sn
|
||||
python3 scripts/set-more-info-link.py --sync --dry-run
|
||||
"""
|
||||
|
||||
import argparse
|
||||
@@ -99,7 +104,7 @@ def get_tldr_root():
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def set_link(file, link):
|
||||
def set_link(file, link, dry_run=False):
|
||||
with open(file, encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
@@ -135,17 +140,29 @@ def set_link(file, link):
|
||||
# return empty status to indicate that no changes were made
|
||||
return ""
|
||||
|
||||
status_prefix = "\x1b[36m" # Color code for pages
|
||||
|
||||
if re.search(r"^>.*<.+>", lines[desc_end]):
|
||||
# overwrite last line
|
||||
lines[desc_end] = new_line
|
||||
status = "\x1b[34mlink updated"
|
||||
status_prefix = "\x1b[34m"
|
||||
action = "updated"
|
||||
else:
|
||||
# add new line
|
||||
lines.insert(desc_end + 1, new_line)
|
||||
status = "\x1b[36mlink added"
|
||||
status_prefix = "\x1b[36m"
|
||||
action = "added"
|
||||
|
||||
with open(file, "w", encoding="utf-8") as f:
|
||||
f.writelines(lines)
|
||||
if dry_run:
|
||||
status = f"link will be {action}"
|
||||
else:
|
||||
status = f"link {action}"
|
||||
|
||||
status = f"{status_prefix}{status}\x1b[0m"
|
||||
|
||||
if not dry_run:
|
||||
with open(file, "w", encoding="utf-8") as f:
|
||||
f.writelines(lines)
|
||||
|
||||
return status
|
||||
|
||||
@@ -172,14 +189,14 @@ def get_link(file):
|
||||
return ""
|
||||
|
||||
|
||||
def sync(root, pages_dirs, command, link):
|
||||
def sync(root, pages_dirs, command, link, dry_run=False):
|
||||
rel_paths = []
|
||||
for page_dir in pages_dirs:
|
||||
path = os.path.join(root, page_dir, command)
|
||||
if os.path.exists(path):
|
||||
rel_path = path.replace(f"{root}/", "")
|
||||
rel_paths.append(rel_path)
|
||||
status = set_link(path, link)
|
||||
status = set_link(path, link, dry_run)
|
||||
if status != "":
|
||||
print(f"\x1b[32m{rel_path} {status}\x1b[0m")
|
||||
return rel_paths
|
||||
@@ -211,6 +228,13 @@ def main():
|
||||
default=False,
|
||||
help="synchronize each translation's more information link (if exists) with that of English page",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-n",
|
||||
"--dry-run",
|
||||
action="store_true",
|
||||
default=False,
|
||||
help="show what changes would be made without actually modifying the pages",
|
||||
)
|
||||
parser.add_argument("link", type=str, nargs="?", default="")
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -264,9 +288,9 @@ def main():
|
||||
for command in commands:
|
||||
link = get_link(os.path.join(root, "pages", command))
|
||||
if link != "":
|
||||
rel_paths += sync(root, pages_dirs, command, link)
|
||||
rel_paths += sync(root, pages_dirs, command, link, args.dry_run)
|
||||
|
||||
if args.stage:
|
||||
if args.stage and not args.dry_run:
|
||||
subprocess.call(["git", "add", *rel_paths], cwd=root)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user