Skip to content

Commit

Permalink
[IMP] allow fix_manifest_website.py to fix only specified manifest files
Browse files Browse the repository at this point in the history
- fix_manifest_website.py gets a new variadic argument [manifest]
- if the argument is specified, only these manifests are fixed
  • Loading branch information
pasculorente committed Jan 30, 2022
1 parent 7214f95 commit 3dc0a39
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 30 deletions.
22 changes: 22 additions & 0 deletions tests/test_fix_manifest_website.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,25 @@ def test_fix_manifest_website(tmp_path):
assert (
tmp_path / "a2" / "__manifest__.py"
).read_text() == """{'name': 'a2', "website" :\n "https://new.url"}"""


def test_fix_specific_manifest_website(tmp_path):
(tmp_path / "a1").mkdir()
(tmp_path / "a1" / "__manifest__.py").write_text(
"""{'name': 'a1', 'website': '...'}"""
)
(tmp_path / "a2").mkdir()
(tmp_path / "a2" / "__manifest__.py").write_text(
"""{'name': 'a2', "website" :\n "https://bad.url"}"""
)
# Only manifest under a2 directory should be fixed
result = CliRunner().invoke(
main, ["--addons-dir", str(tmp_path), "https://new.url", str(tmp_path / "a2" / "__manifest__.py")]
)
assert result.exit_code == 0
assert (
tmp_path / "a1" / "__manifest__.py"
).read_text() == """{'name': 'a1', 'website': '...'}"""
assert (
tmp_path / "a2" / "__manifest__.py"
).read_text() == """{'name': 'a2', "website" :\n "https://new.url"}"""
78 changes: 48 additions & 30 deletions tools/fix_manifest_website.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Set the website key in addons manifests."""
import os
import re
from pathlib import Path

import click

Expand All @@ -12,36 +13,53 @@

@click.command()
@click.argument("url")
@click.argument("manifest", nargs=-1, type=click.Path())
@click.option("--addons-dir", default=".")
def main(url, addons_dir):
for addon_dir in os.listdir(addons_dir):
manifest_path = get_manifest_path(os.path.join(addons_dir, addon_dir))
if not manifest_path:
continue
try:
with open(manifest_path) as manifest_file:
manifest = parse_manifest(manifest_file.read())
except Exception:
raise click.ClickException(
"Error parsing manifest {}.".format(manifest_path)
)
if "website" not in manifest:
raise click.ClickException(
"website key not found in manifest in {}.".format(addon_dir)
)
def main(url, addons_dir, manifest):
# Specifying one or more manifest files has the highest priority
if manifest:
for manifest_file in manifest:
_fix_website(Path(manifest_file).parent, manifest_file, url)
else:
for addon_dir in os.listdir(addons_dir):
manifest_path = get_manifest_path(os.path.join(addons_dir, addon_dir))
if not manifest_path:
continue
_fix_website(addon_dir, manifest_path, url)


def _fix_website(addon_dir, manifest_path, url):
"""
Replaces the website in the manifest pointed by manifest_path by url.
:param manifest_path: path with the location of the manifest file, relative or absolute
:param addon_dir: used to indicate errors, points to parent path of manifest file
:param url: new value for the "website" key in the manifest content
"""
try:
with open(manifest_path) as manifest_file:
manifest_str = manifest_file.read()
new_manifest_str, n = WEBSITE_KEY_RE.subn(
r"\g<1>" + url + r"\g<3>", manifest_str
manifest = parse_manifest(manifest_file.read())
except Exception:
raise click.ClickException(
"Error parsing manifest {}.".format(manifest_path)
)
if "website" not in manifest:
raise click.ClickException(
"website key not found in manifest in {}.".format(addon_dir)
)
with open(manifest_path) as manifest_file:
manifest_str = manifest_file.read()
new_manifest_str, n = WEBSITE_KEY_RE.subn(
r"\g<1>" + url + r"\g<3>", manifest_str
)
if n == 0:
raise click.ClickException(
"no website key match in manifest in {}.".format(addon_dir)
)
if n > 1:
raise click.ClickException(
"more than one website key match in manifest in {}.".format(addon_dir)
)
if n == 0:
raise click.ClickException(
"no website key match in manifest in {}.".format(addon_dir)
)
if n > 1:
raise click.ClickException(
"more than one website key match in manifest in {}.".format(addon_dir)
)
if new_manifest_str != manifest_str:
with open(manifest_path, "w") as manifest_file:
manifest_file.write(new_manifest_str)
if new_manifest_str != manifest_str:
with open(manifest_path, "w") as manifest_file:
manifest_file.write(new_manifest_str)

0 comments on commit 3dc0a39

Please sign in to comment.