Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformat Python code so it would pass linting checks in moz-central. #6519

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions automation/cargo-update-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@
# Usage: ./automation/cargo-update-pr.py

import argparse
from datetime import datetime
import subprocess
import sys
import webbrowser
from datetime import datetime

from shared import step_msg, fatal_err, run_cmd_checked, ensure_working_tree_clean
from shared import ensure_working_tree_clean, fatal_err, run_cmd_checked, step_msg

parser = argparse.ArgumentParser(description="Run cargo update and make a pull-request against main")
parser.add_argument("--remote",
default="origin",
help="The remote name that corresponds to the Application Services main repository.")
parser = argparse.ArgumentParser(
description="Run cargo update and make a pull-request against main"
)
parser.add_argument(
"--remote",
default="origin",
help="The remote name that corresponds to the Application Services main repository.",
)

args = parser.parse_args()
remote = args.remote
Expand All @@ -29,7 +34,11 @@

step_msg(f"Check if branch {branch_name} already exists")

res = subprocess.run(["git", "show-ref", "--verify", f"refs/heads/{branch_name}"], capture_output=True)
res = subprocess.run(
["git", "show-ref", "--verify", f"refs/heads/{branch_name}"],
capture_output=True,
check=False,
)

if res.returncode == 0:
fatal_err(f"The branch {branch_name} already exists!")
Expand All @@ -45,21 +54,27 @@

while True:
step_msg("Regenerating dependency summaries")
res = subprocess.run(["./tools/regenerate_dependency_summaries.sh"])
res = subprocess.run(["./tools/regenerate_dependency_summaries.sh"], check=False)
if res.returncode == 0:
break
print("It looks like the dependency summary generation script couldn't complete.")
input("Please fix the issue then press any key to try again.")

step_msg(f"Creating a commit with the changes")
run_cmd_checked(["git", "add", "-A"]) # We can use -A since we checked the working dir is clean.
step_msg("Creating a commit with the changes")
run_cmd_checked(
["git", "add", "-A"]
) # We can use -A since we checked the working dir is clean.
run_cmd_checked(["git", "commit", "-m", "Run cargo update [ci full]"])

step_msg("Print summary of changes")
run_cmd_checked(["git", "show", "--stat"])

response = input("Great! Would you like to push and open a pull-request? ([Y]/N)").lower()
if response != "y" and response != "" and response != "yes":
exit(0)
response = input(
"Great! Would you like to push and open a pull-request? ([Y]/N)"
).lower()
if response not in ("y", "", "yes"):
sys.exit(0)
run_cmd_checked(["git", "push", remote, branch_name])
webbrowser.open_new_tab(f"https://github.com/mozilla/application-services/pull/new/{branch_name}")
webbrowser.open_new_tab(
f"https://github.com/mozilla/application-services/pull/new/{branch_name}"
)
14 changes: 11 additions & 3 deletions automation/check_protobuf_files_current.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
# Dependencies: None
# Usage: ./automation/cargo-update-pr.py

from shared import step_msg, fatal_err, run_cmd_checked, find_app_services_root, ensure_working_tree_clean
from shared import (
# ensure_working_tree_clean,
fatal_err,
find_app_services_root,
run_cmd_checked,
step_msg,
)

step_msg("Checking that the generated protobuf Rust files are up-to-date")
# ensure_working_tree_clean()
Expand All @@ -16,7 +22,9 @@

if run_cmd_checked(["git", "status", "--porcelain"], capture_output=True).stdout:
run_cmd_checked(["git", "status"])
fatal_err("""
fatal_err(
"""
The protobuf rust files are outdated.
You can fix this yourself by running cargo run --bin protobuf-gen <APP_SERVICES_ROOT>/tools/protobuf_files.toml
""")
"""
)
34 changes: 24 additions & 10 deletions automation/prepare-release.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
# Dependencies: yaml
# Usage: ./automation/prepare-release.py

from datetime import datetime
import sys
import webbrowser
from datetime import datetime

from shared import (RefNames, get_moz_remote, step_msg, fatal_err, run_cmd_checked,
ensure_working_tree_clean, check_output)
from shared import (
RefNames,
check_output,
ensure_working_tree_clean,
fatal_err,
get_moz_remote,
run_cmd_checked,
step_msg,
)

# Constants
VERSION_FILE = "version.txt"
Expand All @@ -24,7 +32,7 @@
with open(VERSION_FILE, "r") as stream:
cur_version = stream.read().strip()

major_version_number = int(cur_version.split('.')[0])
major_version_number = int(cur_version.split(".")[0])
next_version_number = major_version_number + 1
release_version = f"{major_version_number}.0"
refs = RefNames(major_version_number, 0)
Expand Down Expand Up @@ -97,7 +105,7 @@
f"# v{next_version_number}.0 (In progress)",
"",
"[Full Changelog](In progress)",
""
"",
]
with open(CHANGELOG_FILE, "w") as stream:
stream.write("\n".join(changelog))
Expand All @@ -108,13 +116,19 @@
run_cmd_checked(["git", "commit", "-m", f"Start release v{next_version_number}"])

print()
response = input("Great! Would you like to push and open the two pull-requests? ([Y]/N)").lower()
if response != "y" and response != "" and response != "yes":
exit(0)
response = input(
"Great! Would you like to push and open the two pull-requests? ([Y]/N)"
).lower()
if response not in ("y", "", "yes"):
sys.exit(0)

run_cmd_checked(["git", "push", moz_remote, refs.release_pr])
run_cmd_checked(["git", "push", moz_remote, refs.start_release_pr])

webbrowser.open_new_tab(f"https://github.com/mozilla/application-services/compare/{refs.release}...{refs.release_pr}")
webbrowser.open_new_tab(f"https://github.com/mozilla/application-services/compare/{refs.main}...{refs.start_release_pr}")
webbrowser.open_new_tab(
f"https://github.com/mozilla/application-services/compare/{refs.release}...{refs.release_pr}"
)
webbrowser.open_new_tab(
f"https://github.com/mozilla/application-services/compare/{refs.main}...{refs.start_release_pr}"
)
run_cmd_checked(["git", "checkout", refs.main])
24 changes: 16 additions & 8 deletions automation/publish_to_maven_local_if_modified.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,38 @@
# Dependencies: None
# Usage: ./automation/publish_to_maven_local_if_modified.py

import argparse
import hashlib
import os
import sys
import time
import hashlib
import argparse
from shared import run_cmd_checked, find_app_services_root, fatal_err
import re

from shared import fatal_err, find_app_services_root, run_cmd_checked

LAST_CONTENTS_HASH_FILE = ".lastAutoPublishContentsHash"

GITIGNORED_FILES_THAT_AFFECT_THE_BUILD = ["local.properties"]

parser = argparse.ArgumentParser(description="Publish android packages to local maven repo, but only if changed since last publish")
parser = argparse.ArgumentParser(
description="Publish android packages to local maven repo, but only if changed since last publish"
)
parser.parse_args()

root_dir = find_app_services_root()
if str(root_dir) != os.path.abspath(os.curdir):
fatal_err(f"This only works if run from the repo root ({root_dir!r} != {os.path.abspath(os.curdir)!r})")
fatal_err(
f"This only works if run from the repo root ({root_dir!r} != {os.path.abspath(os.curdir)!r})"
)

# This doesn't work on "native" windows, so let's get that out of the way now.
if sys.platform.startswith("win"):
print("NOTE: The autoPublish workflows do not work on native windows.")
print("You must follow the instructions in /docs/howtos/setup-android-build-environment.md#using-windows")
print("then, manually ensure that the following command has completed successfully in WSL:")
print(
"You must follow the instructions in /docs/howtos/setup-android-build-environment.md#using-windows"
)
print(
"then, manually ensure that the following command has completed successfully in WSL:"
)
print(sys.argv)
print(f"(from the '{root_dir}' directory)")
print("Then restart the build")
Expand Down
44 changes: 31 additions & 13 deletions automation/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,62 @@

# Common code used by the automation python scripts.

import os
import subprocess
import sys
from pathlib import Path


def step_msg(msg):
print(f"> \033[34m{msg}\033[0m")


def fatal_err(msg):
print(f"\033[31mError: {msg}\033[0m")
exit(1)
sys.exit(1)


def run_cmd_checked(*args, **kwargs):
"""Run a command, throwing an exception if it exits with non-zero status."""
kwargs["check"] = True
return subprocess.run(*args, **kwargs)
return subprocess.run(*args, **kwargs) # noqa: PLW1510


def check_output(*args, **kwargs):
"""Run a command, throwing an exception if it exits with non-zero status."""
return subprocess.check_output(*args, **kwargs, encoding='utf8')
return subprocess.check_output(*args, **kwargs, encoding="utf8")


def ensure_working_tree_clean():
"""Error out if there are un-committed or staged files in the working tree."""
if run_cmd_checked(["git", "status", "--porcelain"], capture_output=True).stdout:
fatal_err("The working tree has un-committed or staged files.")


def find_app_services_root():
"""Find the absolute path of the Application services repository root."""
cur_dir = Path(__file__).parent
while not Path(cur_dir, "LICENSE").exists():
cur_dir = cur_dir.parent
return cur_dir.absolute()


def get_moz_remote():
"""
Get the name of the remote for the official mozilla application-services repo
"""
for line in check_output(["git", "remote", "-v"]).splitlines():
split = line.split()
if (len(split) == 3
and split[1] == '[email protected]:mozilla/application-services.git'
and split[2] == '(push)'):
if (
len(split) == 3
and split[1] == "[email protected]:mozilla/application-services.git"
and split[2] == "(push)"
):
return split[0]
else:
fatal_err("Can't find remote origin for [email protected]:mozilla/application-services.git")
fatal_err(
"Can't find remote origin for [email protected]:mozilla/application-services.git"
)


def set_gradle_substitution_path(project_dir, name, value):
"""Set a substitution path property in a gradle `local.properties` file.
Expand All @@ -69,22 +81,25 @@ def set_gradle_substitution_path(project_dir, name, value):
for ln in f:
# Not exactly a thorough parser, but should be good enough...
if ln.startswith(name_eq):
cur_value = ln[len(name_eq):].strip()
cur_value = ln[len(name_eq) :].strip()
if Path(project_dir, cur_value).resolve() != abs_value:
fatal_error(f"Conflicting property {name}={cur_value} (not {abs_value})")
fatal_err(
f"Conflicting property {name}={cur_value} (not {abs_value})"
)
return
# The file does not contain the required property, append it.
# Note that the project probably expects a path relative to the project root.
ancestor = Path(os.path.commonpath([project_dir, abs_value]))
relpath = Path(".")
for _ in project_dir.parts[len(ancestor.parts):]:
for _ in project_dir.parts[len(ancestor.parts) :]:
relpath /= ".."
for nm in abs_value.parts[len(ancestor.parts):]:
for nm in abs_value.parts[len(ancestor.parts) :]:
relpath /= nm
step_msg(f"Setting relative path from {project_dir} to {abs_value} as {relpath}")
with properties_file.open("a") as f:
f.write(f"{name}={relpath}\n")


class RefNames:
"""
Contains the branch and tag names we use for automation.
Expand All @@ -95,6 +110,7 @@ class RefNames:
release_pr_branch -- Used for PRs against release_branch for a new version
start_release_pr_branch -- Used for PRs against main to start a new major release
"""

def __init__(self, major_version_number, minor_version_number):
major_version_number = int(major_version_number)
minor_version_number = int(minor_version_number)
Expand All @@ -106,4 +122,6 @@ def __init__(self, major_version_number, minor_version_number):
if minor_version_number == 0:
self.previous_version_tag = f"v{major_version_number-1}.0"
else:
self.previous_version_tag = f"v{major_version_number}.{minor_version_number-1}"
self.previous_version_tag = (
f"v{major_version_number}.{minor_version_number-1}"
)
Loading