-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-sources.py
executable file
·273 lines (218 loc) · 8.97 KB
/
generate-sources.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env python3
import os
import sys
import argparse
import subprocess
import shutil
import logging
import tempfile
from ruamel.yaml import YAML
# ---------------------------
# Configuration
# ---------------------------
# Paths and files
MANIFEST_FILE = "com.artemis_rgb.Artemis.yml" # Path to your Flatpak manifest YAML file
# ---------------------------
# Functions
# ---------------------------
def error(message):
print(f"Error: {message}", file=sys.stderr)
sys.exit(1)
def check_command(cmd):
if shutil.which(cmd) is None:
error(f"'{cmd}' is not installed. Please install it and try again.")
# ---------------------------
# Parse Command Line Arguments
# ---------------------------
parser = argparse.ArgumentParser(description='Update Artemis and Artemis.Plugins sources.')
parser.add_argument('-v', action='store_true', help='Verbose output')
parser.add_argument('-a', action='store_true', help='Update Artemis NuGet sources')
parser.add_argument('-p', action='store_true', help='Update Artemis.Plugins NuGet sources')
args = parser.parse_args()
if args.v:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
if args.a and args.p:
UPDATE_TARGET = "all"
elif args.a:
UPDATE_TARGET = "a"
elif args.p:
UPDATE_TARGET = "p"
else:
UPDATE_TARGET = "all"
# ---------------------------
# Check for Required Tools
# ---------------------------
logging.info("Checking for required tools...")
REQUIRED_TOOLS = [
"git",
"python",
"flatpak",
]
for cmd in REQUIRED_TOOLS:
check_command(cmd)
logging.info("All required tools are installed.")
# ---------------------------
# Extract Variables from Manifest
# ---------------------------
logging.info("Extracting variables from manifest...")
if not os.path.isfile(MANIFEST_FILE):
error(f"Manifest file '{MANIFEST_FILE}' not found.")
yaml = YAML(typ='safe') # Use 'safe' load to prevent arbitrary code execution
with open(MANIFEST_FILE, 'r') as f:
manifest = yaml.load(f)
# Extract the .NET version
DOTNET_VERSION = None
sdk_extensions = manifest.get('sdk-extensions', [])
for ext in sdk_extensions:
if 'dotnet' in ext:
DOTNET_VERSION = ext.split('dotnet')[-1]
break
if not DOTNET_VERSION:
error("Failed to extract .NET version from manifest.")
logging.info(f".NET version: {DOTNET_VERSION}")
# Extract the Freedesktop runtime version
FREEDESKTOP_VERSION = manifest.get('runtime-version', '').replace("'", '')
if not FREEDESKTOP_VERSION:
error("Failed to extract Freedesktop runtime version from manifest.")
logging.info(f"Freedesktop runtime version: {FREEDESKTOP_VERSION}")
# Check and attempt to install the Flatpak SDK extension
logging.info("Checking for required Flatpak SDK extension...")
FLATPAK_EXT = f"org.freedesktop.Sdk.Extension.dotnet{DOTNET_VERSION}//{FREEDESKTOP_VERSION}"
cmd = ['flatpak', 'info', FLATPAK_EXT]
result = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
if result.returncode != 0:
logging.info(f"Installing required Flatpak SDK extension '{FLATPAK_EXT}'...")
cmd_install = ['flatpak', 'install', '-y', 'flathub', FLATPAK_EXT]
result_install = subprocess.run(cmd_install)
if result_install.returncode != 0:
error(f"Failed to install Flatpak SDK extension '{FLATPAK_EXT}'. Please install it manually and try again.")
else:
logging.info(f"Required Flatpak SDK extension '{FLATPAK_EXT}' is installed.")
# Extract commit hashes
modules = manifest.get('modules', [])
ARTEMIS_HASH = None
ARTEMIS_PLUGINS_HASH = None
for module in modules:
logging.debug(f"Processing module: {module}")
if isinstance(module, dict):
if module.get('name') == 'artemis':
sources = module.get('sources', [])
for source in sources:
if isinstance(source, dict):
url = source.get('url')
if url == 'https://github.com/Artemis-RGB/Artemis.git':
ARTEMIS_HASH = source.get('commit')
elif url == 'https://github.com/Artemis-RGB/Artemis.Plugins.git':
ARTEMIS_PLUGINS_HASH = source.get('commit')
else:
logging.debug(f"Skipping source (not a dict): {source}")
else:
logging.debug(f"Module name is not 'artemis': {module.get('name')}")
else:
logging.debug(f"Skipping module (not a dict): {module}")
continue
if not ARTEMIS_HASH:
error("Failed to extract Artemis commit hash from manifest.")
logging.info(f"Artemis commit hash: {ARTEMIS_HASH}")
if not ARTEMIS_PLUGINS_HASH:
error("Failed to extract Artemis.Plugins commit hash from manifest.")
logging.info(f"Artemis.Plugins commit hash: {ARTEMIS_PLUGINS_HASH}")
# ---------------------------
# Create Temporary Directory
# ---------------------------
logging.info("Creating temporary directory...")
TEMP_DIR = tempfile.mkdtemp(dir='.')
if not os.path.isdir(TEMP_DIR):
error("Failed to create temporary directory.")
logging.info(f"Temporary directory created at '{TEMP_DIR}'.")
# ---------------------------
# Update Sources Functions
# ---------------------------
def update_artemis_sources():
logging.info("Updating Artemis sources...")
if os.path.isfile('artemis-sources.json'):
shutil.move('artemis-sources.json', 'artemis-sources.bak')
logging.info("Backed up existing 'artemis-sources.json' to 'artemis-sources.bak'.")
artemis_repo_dir = os.path.join(TEMP_DIR, 'Artemis')
cmd = ['git', 'clone', '--recurse', 'https://github.com/Artemis-RGB/Artemis.git', artemis_repo_dir]
result = subprocess.run(cmd)
if result.returncode != 0:
error("Failed to clone Artemis repository.")
cmd = ['git', '-C', artemis_repo_dir, 'checkout', ARTEMIS_HASH]
result = subprocess.run(cmd)
if result.returncode != 0:
error(f"Failed to checkout commit '{ARTEMIS_HASH}'.")
projects = []
for root, dirs, files in os.walk(artemis_repo_dir):
for file in files:
if file == 'Artemis.UI.Linux.csproj':
projects.append(os.path.join(root, file))
if len(projects) == 0:
error("No 'Artemis.UI.Linux.csproj' file found.")
logging.info("Generating 'artemis-sources.json'...")
cmd = ['./builder-tools/dotnet/flatpak-dotnet-generator.py',
'-r', 'linux-x64', 'linux-arm64',
'-d', DOTNET_VERSION,
'-f', FREEDESKTOP_VERSION,
'artemis-sources.json'] + projects
result = subprocess.run(cmd)
if result.returncode != 0:
error("Failed to generate 'artemis-sources.json'.")
logging.info("'artemis-sources.json' generated successfully.")
def update_artemis_plugins_sources():
logging.info("Updating Artemis.Plugins sources...")
if os.path.isfile('artemis-plugins-sources.json'):
shutil.move('artemis-plugins-sources.json', 'artemis-plugins-sources.bak')
logging.info("Backed up existing 'artemis-plugins-sources.json' to 'artemis-plugins-sources.bak'.")
artemis_plugins_repo_dir = os.path.join(TEMP_DIR, 'Artemis.Plugins')
cmd = ['git', 'clone', '--recurse', 'https://github.com/Artemis-RGB/Artemis.Plugins.git', artemis_plugins_repo_dir]
result = subprocess.run(cmd)
if result.returncode != 0:
error("Failed to clone Artemis.Plugins repository.")
cmd = ['git', '-C', artemis_plugins_repo_dir, 'checkout', ARTEMIS_PLUGINS_HASH]
result = subprocess.run(cmd)
if result.returncode != 0:
error(f"Failed to checkout commit '{ARTEMIS_PLUGINS_HASH}'.")
projects = []
for root, dirs, files in os.walk(artemis_plugins_repo_dir):
for file in files:
if file.endswith('.csproj'):
projects.append(os.path.join(root, file))
if len(projects) == 0:
error("No '.csproj' files found in Artemis.Plugins.")
logging.info("Generating 'artemis-plugins-sources.json'...")
cmd = ['./builder-tools/dotnet/flatpak-dotnet-generator.py',
'-r', 'linux-x64', 'linux-arm64',
'-d', DOTNET_VERSION,
'-f', FREEDESKTOP_VERSION,
'artemis-plugins-sources.json'] + projects
result = subprocess.run(cmd)
if result.returncode != 0:
error("Failed to generate 'artemis-plugins-sources.json'.")
logging.info("'artemis-plugins-sources.json' generated successfully.")
# ---------------------------
# Update Sources Based on Selection
# ---------------------------
if UPDATE_TARGET == 'all':
update_artemis_sources()
update_artemis_plugins_sources()
elif UPDATE_TARGET == 'a':
update_artemis_sources()
elif UPDATE_TARGET == 'p':
update_artemis_plugins_sources()
else:
parser.print_help()
sys.exit(1)
# ---------------------------
# Clean Up
# ---------------------------
logging.info("Cleaning up temporary files...")
shutil.rmtree(TEMP_DIR)
logging.info(f"Temporary directory '{TEMP_DIR}' removed.")
logging.info("Temporary files cleaned up.")
# ---------------------------
# Completion Message
# ---------------------------
logging.info("Source update completed successfully.")