repo: Format the script
Change-Id: I74364b6118046181935331bc77c644dae07e10f3
diff --git a/repo/repo_settings.py b/repo/repo_settings.py
index 2c8dafe..bbb2e9e 100755
--- a/repo/repo_settings.py
+++ b/repo/repo_settings.py
@@ -28,27 +28,30 @@
leaf_devices = "leaf/devices/devices.yaml"
gerrit_structure = "leaf/gerrit-config/structure.yaml"
+
def parse_args():
parser = argparse.ArgumentParser()
- subparsers = parser.add_subparsers(dest='subcommand')
+ subparsers = parser.add_subparsers(dest="subcommand")
subparsers.required = True
# update
- parser_update = subparsers.add_parser('update')
+ parser_update = subparsers.add_parser("update")
parser_update.add_argument("-b", "--branch")
parser_update.add_argument("-d", "--device")
- parser_update.add_argument("-f", "--project_file",
- default=".repo/manifests/snippets/leaf.xml")
- parser_update.add_argument("--no-default-branch", action='store_true')
+ parser_update.add_argument(
+ "-f", "--project_file", default=".repo/manifests/snippets/leaf.xml"
+ )
+ parser_update.add_argument("--no-default-branch", action="store_true")
# update_groups
- parser_update_groups = subparsers.add_parser('update_groups')
+ parser_update_groups = subparsers.add_parser("update_groups")
# fetch_structure_from_gerrit
- parser_fetch_structure = subparsers.add_parser('fetch_structure_from_gerrit')
+ parser_fetch_structure = subparsers.add_parser("fetch_structure_from_gerrit")
return parser.parse_args()
+
def check_gh_token():
gh_token = os.environ.get("GH_TOKEN")
if not gh_token:
@@ -56,6 +59,7 @@
sys.exit(1)
return gh_token
+
def check_gh_user(token):
user = requests.get(
"https://api.github.com/user",
@@ -66,6 +70,7 @@
).json()["login"]
return user
+
def create_github_repo(org, repo, token):
url = f"https://api.github.com/orgs/{org}/repos"
headers = {
@@ -81,6 +86,7 @@
}
requests.post(url, headers=headers, json=data)
+
def set_github_repo_settings(project, branch, token, no_default_branch):
url = f"https://api.github.com/repos/{project}"
headers = {
@@ -93,9 +99,10 @@
"has_wiki": False,
}
if not no_default_branch:
- data["default_branch"] = branch;
+ data["default_branch"] = branch
requests.patch(url, headers=headers, json=data)
+
def create_gerrit_project(project, branch, user):
projects = subprocess.run(
["ssh", "-n", "-p", PORT, f"{user}@{GERRIT}", "gerrit", "ls-projects"],
@@ -104,25 +111,58 @@
).stdout.split("\n")
if project not in projects:
subprocess.run(
- ["ssh", "-n", "-p", PORT, f"{user}@{GERRIT}",
- "gerrit", "create-project", project, "-b", branch],
+ [
+ "ssh",
+ "-n",
+ "-p",
+ PORT,
+ f"{user}@{GERRIT}",
+ "gerrit",
+ "create-project",
+ project,
+ "-b",
+ branch,
+ ],
check=True,
)
+
def set_gerrit_project_head(project, branch, user):
subprocess.run(
- ["ssh", "-n", "-p", PORT, f"{user}@{GERRIT}", "gerrit",
- "set-head", project, "--new-head", branch],
+ [
+ "ssh",
+ "-n",
+ "-p",
+ PORT,
+ f"{user}@{GERRIT}",
+ "gerrit",
+ "set-head",
+ project,
+ "--new-head",
+ branch,
+ ],
check=False,
)
+
def set_gerrit_project_parent(project, parent, user):
subprocess.run(
- ["ssh", "-n", "-p", PORT, f"{user}@{GERRIT}", "gerrit",
- "set-project-parent", project, "--parent", parent],
+ [
+ "ssh",
+ "-n",
+ "-p",
+ PORT,
+ f"{user}@{GERRIT}",
+ "gerrit",
+ "set-project-parent",
+ project,
+ "--parent",
+ parent,
+ ],
check=False,
)
+
def get_projects_from_devices(device, branch):
projects = []
@@ -136,6 +176,7 @@
return projects
+
def get_projects_from_manifests(project_file, branch):
projects = []
@@ -144,15 +185,21 @@
for project in root.findall("project"):
name = project.get("name")
- revision = branch or project.get("revision") or root.find("default").get("revision").split("/")[-1]
+ revision = (
+ branch
+ or project.get("revision")
+ or root.find("default").get("revision").split("/")[-1]
+ )
projects.append({"name": name, "revision": revision})
return projects
+
def get_projects_from_gerrit_structure():
with open(gerrit_structure, "r") as f:
return yaml.safe_load(f)
+
def get_projects_from_gerrit(auth=None):
url = f"https://{GERRIT}/a/projects/?t" if auth else f"https://{GERRIT}/projects/?t"
resp = requests.get(url, auth=auth)
@@ -172,20 +219,25 @@
nodes[project] = sorted(nodes[project])
return nodes
+
def main():
args = parse_args()
gh_token = check_gh_token()
gh_user = check_gh_user(gh_token)
- if args.subcommand == 'update':
- if (args.device):
+ if args.subcommand == "update":
+ if args.device:
projects = get_projects_from_devices(args.device, args.branch)
else:
projects = get_projects_from_manifests(args.project_file, args.branch)
for project in projects:
name = project["name"]
- if ("LeafOS-Project" in name) or ("LeafOS-Blobs" in name) or ("LeafOS-Devices" in name):
+ if (
+ ("LeafOS-Project" in name)
+ or ("LeafOS-Blobs" in name)
+ or ("LeafOS-Devices" in name)
+ ):
print(name)
org, repo = name.split("/")
branch = project["revision"]
@@ -194,17 +246,21 @@
create_gerrit_project(name, branch, gh_user)
if not args.no_default_branch:
set_gerrit_project_head(name, branch, gh_user)
- elif args.subcommand == 'update_groups':
+ elif args.subcommand == "update_groups":
projects = get_projects_from_gerrit_structure()
live_projects = get_projects_from_gerrit()
changes = {}
for parent, children in projects.items():
if parent in live_projects:
- if (not projects[parent] or set(live_projects[parent]) == set(projects[parent])):
+ if not projects[parent] or set(live_projects[parent]) == set(
+ projects[parent]
+ ):
continue
else:
- changes[parent] = list(set(projects[parent]) - set(live_projects[parent]))
+ changes[parent] = list(
+ set(projects[parent]) - set(live_projects[parent])
+ )
if not changes[parent]:
del changes[parent]
else:
@@ -213,9 +269,9 @@
if changes:
for parent, children in changes.items():
for child in children:
- print(f"Update parent of {child} to {parent}")
- set_gerrit_project_parent(child, parent, gh_user)
- elif args.subcommand == 'fetch_structure_from_gerrit':
+ print(f"Update parent of {child} to {parent}")
+ set_gerrit_project_parent(child, parent, gh_user)
+ elif args.subcommand == "fetch_structure_from_gerrit":
projects = get_projects_from_gerrit()
for node in sorted(projects.keys()):
@@ -225,5 +281,6 @@
for child in projects[node]:
print(f" - {child}")
+
if __name__ == "__main__":
main()