forked from flathub/org.standardnotes.standardnotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
153 lines (128 loc) · 4.79 KB
/
update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
# Modified from https://raw.githubusercontent.com/flathub/net.veloren.veloren/master/update.py
import argparse
import json
import logging
import os
import stat
import subprocess
import sys
import shutil
import urllib.request
GENERATOR_SCRIPT_URL = f"https://github.com/flatpak/flatpak-builder-tools/raw/master/node/flatpak-node-generator.py"
FLATPAK_BUILDER_TOOLS_REPO_URL = "https://github.com/flatpak/flatpak-builder-tools"
def run(cmdline, cwd=None):
logging.info(f"Running {cmdline}")
if cwd is None:
cwd = os.getcwd()
try:
process = subprocess.run(
cmdline, cwd=cwd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
except subprocess.CalledProcessError as e:
logging.error(e.stderr.decode())
raise
return process.stdout.decode().strip()
def generate_sources(
app_source,
clone_dir=None,
generator_args=None,
):
cache_dir = os.environ.get("XDG_CACHE_HOME", os.path.expanduser("~/.cache"))
# download app repo
if clone_dir is None:
repo_dir = url_to_dir_name(app_source["url"])
clone_dir = os.path.join(cache_dir, "flatpak-updater", repo_dir)
if not os.path.isdir(os.path.join(clone_dir, ".git")):
run(
[
"git",
"clone",
"--depth",
"1",
"--branch",
app_source["ref"],
"--recursive",
app_source["url"],
clone_dir,
]
)
# download generator script repo
flatpak_builder_tools_clone_dir = os.path.join(cache_dir, "flatpak-updater", FLATPAK_BUILDER_TOOLS_REPO_URL)
if not os.path.isdir(os.path.join(clone_dir, ".git")):
run(
[
"git",
"clone",
"--depth",
"1",
"--recursive",
FLATPAK_BUILDER_TOOLS_REPO_URL,
flatpak_builder_tools_clone_dir,
]
)
# install generator script
run([
"pipx", "install", 'node'
])
generator_cmdline = ["flatpak-node-generator", "-o", "generated-sources.json"]
generator_cmdline.extend(generator_args)
run(generator_cmdline, cwd=clone_dir)
shutil.move(
os.path.join(clone_dir, "generated-sources.json"), "generated-sources.json"
)
generated_sources = None
with open("generated-sources.json") as generated_sources:
generated_sources = json.loads(generated_sources.read())
logging.info(f"Generation completed")
return generated_sources
def commit_changes(app_source, files, on_new_branch=True):
repo_dir = os.getcwd()
title = f'build: update to ref {app_source["ref"]}'
run(["git", "add", "-v", "--"] + files, cwd=repo_dir)
if on_new_branch:
target_branch = f'update-{app_source["ref"]}'
run(["git", "checkout", "-b", target_branch], cwd=repo_dir)
else:
target_branch = run(["git", "branch", "--show-current"], cwd=repo_dir)
run(["git", "commit", "-m", title], cwd=repo_dir)
new_commit = run(["git", "rev-parse", "HEAD"], cwd=repo_dir)
logging.info(f"Committed {new_commit[:7]} on {target_branch}")
return target_branch, new_commit
def url_to_dir_name(url: str)-> str:
return url.replace("://", "_").replace("/", "_")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--generator", required=False)
parser.add_argument("--generator-script-url", required=False)
parser.add_argument("-a", "--generator-arg", action="append", required=False)
parser.add_argument("-d", "--clone-dir", required=False)
parser.add_argument("-o", "--gen-output", default="generated-sources.json")
parser.add_argument("-n", "--new-branch", action="store_true")
parser.add_argument("--ref", default="master")
parser.add_argument("app_source_json")
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)
with open(args.app_source_json, "r") as f:
app_source = json.load(f)
if args.ref == app_source["ref"]:
logging.info(f'Ref {app_source["ref"]} is the latest')
sys.exit(0)
app_source.update({"ref": args.ref})
generated_sources = generate_sources(
app_source,
clone_dir=args.clone_dir,
generator_args=args.generator_arg,
)
with open(args.app_source_json, "w") as o:
json.dump(app_source, o, indent=4)
with open(args.gen_output, "w") as g:
json.dump(generated_sources, g, indent=4)
branch, new_commit = commit_changes(
app_source,
files=[args.app_source_json, args.gen_output],
on_new_branch=args.new_branch,
)
logging.info(f"Created commit {new_commit[:7]} on branch {branch}")
if __name__ == "__main__":
main()