forked from linuxmint/mintstick
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_additional_files.py
More file actions
executable file
·375 lines (322 loc) · 13.5 KB
/
generate_additional_files.py
File metadata and controls
executable file
·375 lines (322 loc) · 13.5 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/python3
DOMAIN = "driveutility"
SYSTEM_LOCALE_PATH = "/usr/share/locale"
LOCAL_LOCALE_PATH = "mo"
import os
import gettext
import subprocess
import glob
# --- Generate .mo files from .po files ---
def compile_translations():
"""Compile all .po files to .mo files in the mo/ directory."""
print("Compiling translations...")
# Get all .po files
po_files = glob.glob("po/*.po")
if not po_files:
print("No .po files found in po/ directory")
return False
# Create mo directory if it doesn't exist
os.makedirs(LOCAL_LOCALE_PATH, exist_ok=True)
success = True
for po_file in po_files:
# Extract language code from filename (e.g., "po/de.po" -> "de")
lang = os.path.basename(po_file)[:-3]
# Create language directory structure
mo_dir = os.path.join(LOCAL_LOCALE_PATH, lang, "LC_MESSAGES")
os.makedirs(mo_dir, exist_ok=True)
# Output .mo file path
mo_file = os.path.join(mo_dir, f"{DOMAIN}.mo")
try:
# Compile .po to .mo using msgfmt
result = subprocess.run(['msgfmt', '--output-file', mo_file, po_file],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
print(f" Compiled: {po_file} -> {mo_file}")
except subprocess.CalledProcessError as e:
print(f" Error compiling {po_file}: {e.stderr}")
success = False
except FileNotFoundError:
print("Error: msgfmt command not found. Please install gettext package.")
success = False
return success
# Compile translations first
if not compile_translations():
print("Warning: Some translations failed to compile")
# --- Determine the correct path for translations (local first) ---
if os.path.isdir(LOCAL_LOCALE_PATH):
LOCALE_PATH = LOCAL_LOCALE_PATH
print(f"Using local translations from: {LOCALE_PATH}")
elif os.path.isdir(SYSTEM_LOCALE_PATH):
LOCALE_PATH = SYSTEM_LOCALE_PATH
print(f"Using system translations from: {LOCALE_PATH}")
else:
LOCALE_PATH = None
print("Warning: No translation directory found. Only default strings will be used.")
# Set the default language to get the base strings
os.environ['LANGUAGE'] = "en_US.UTF-8"
if LOCALE_PATH:
gettext.install(DOMAIN, LOCALE_PATH)
else:
gettext.install(DOMAIN)
# --- Helper Functions ---
def strip_split_and_recombine(comma_separated):
"""Converts a comma-separated string like 'a, b, c' to 'a;b;c;' for the Keywords field."""
word_list = comma_separated.split(",")
out = ""
for item in word_list:
out += item.strip()
out += ";"
return out
def generate(domain, locale_path, filename, prefix, name, comment, suffix, genericName=None, keywords=None, append=False):
"""Generates a .desktop file with support for translations."""
directory_path = os.path.dirname(filename)
if directory_path:
os.makedirs(directory_path, exist_ok=True)
mode = "a" if append else "w"
with open(filename, mode, encoding="utf-8") as desktopFile:
desktopFile.writelines(prefix)
translatable_fields = {
"Name": name,
"Comment": comment,
"GenericName": genericName,
"Keywords": keywords
}
for key, value in translatable_fields.items():
if value is None:
continue
formatted_value = strip_split_and_recombine(value) if key == "Keywords" else value
desktopFile.write(f"{key}={formatted_value}\n")
if locale_path:
for directory in sorted(os.listdir(locale_path)):
mo_file = os.path.join(locale_path, directory, "LC_MESSAGES", f"{domain}.mo")
if os.path.exists(mo_file):
try:
language = gettext.translation(domain, locale_path, languages=[directory])
L_ = language.gettext
translated_value = L_(value)
if translated_value != value:
formatted_translated = strip_split_and_recombine(translated_value) if key == "Keywords" else translated_value
desktopFile.write(f"{key}[{directory}]={formatted_translated}\n")
except Exception:
pass
desktopFile.writelines(suffix)
def generate_polkit_policy(domain, locale_path, filename, prefix, description, message, suffix):
"""Generates a .policy file with support for translations."""
directory_path = os.path.dirname(filename)
if directory_path:
os.makedirs(directory_path, exist_ok=True)
with open(filename, "w", encoding="utf-8") as policyFile:
policyFile.writelines(prefix)
fields_to_translate = {
"description": description,
"message": message
}
for tag, text in fields_to_translate.items():
policyFile.write(f"<{tag}>{text}</{tag}>\n")
if locale_path:
for directory in sorted(os.listdir(locale_path)):
mo_file = os.path.join(locale_path, directory, "LC_MESSAGES", f"{domain}.mo")
if os.path.exists(mo_file):
try:
language = gettext.translation(domain, locale_path, languages=[directory])
L_ = language.gettext
translated_value = L_(text)
if translated_value != text:
translated_value = translated_value.replace("&", "&").replace("<", "<").replace(">", ">")
policyFile.write(f'<{tag} xml:lang="{directory}">{translated_value}</{tag}>\n')
except Exception:
pass
policyFile.writelines(suffix)
# --- 1. Create main .desktop file for application menu (System category) ---
# Main .desktop file for GNOME/GTK environments.
main_prefix = """[Desktop Entry]
Type=Application
Exec=driveutility
Icon=driveutility
Terminal=false
Categories=GTK;System;
NotShowIn=KDE;
"""
keywords = "usb,iso,image,write,flash,bootable,format,fat32,ntfs,ext4,erase,wipe,disk"
generate(DOMAIN, LOCALE_PATH, "share/applications/driveutility.desktop", main_prefix, _("Drive Utility"), _("Write disk images, format, or wipe drives"), "", genericName=_("Drive Management"), keywords=_(keywords))
# Optional .desktop file for KDE.
kde_prefix = """[Desktop Entry]
Type=Application
Exec=driveutility
Icon=driveutility
Terminal=false
Categories=System;
OnlyShowIn=KDE;
"""
generate(DOMAIN, LOCALE_PATH, "share/applications/driveutility-kde.desktop", kde_prefix, _("Drive Utility"), _("Write disk images, format, or wipe drives"), "", genericName=_("Drive Management"), keywords=_(keywords))
# --- 2. Create .desktop files for individual actions (Utility category) ---
# --- Write Action ---
write_prefix_gtk = """[Desktop Entry]
Type=Application
Exec=driveutility -m write
Icon=driveutility
Terminal=false
Categories=GTK;Utility;
NotShowIn=KDE;
"""
#generate(DOMAIN, LOCALE_PATH, "share/applications/driveutility-writer.desktop", write_prefix_gtk, _("Image writer"), _("Write a disk image to a device"), "")
write_prefix_kde = """[Desktop Entry]
Type=Application
Exec=driveutility -m write
Icon=driveutility
Terminal=false
Categories=Utility;
OnlyShowIn=KDE;
"""
#generate(DOMAIN, LOCALE_PATH, "share/applications/driveutility-writer-kde.desktop", write_prefix_kde, _("Image writer"), _("Write a disk image to a device"), "")
# --- Format Action ---
format_prefix_gtk = """[Desktop Entry]
Type=Application
Exec=driveutility -m format
Icon=driveutility
Terminal=false
Categories=GTK;Utility;
NotShowIn=KDE;
"""
#generate(DOMAIN, LOCALE_PATH, "share/applications/driveutility-formatter.desktop", format_prefix_gtk, _("Disk formatter"), _("Format a disk"), "")
format_prefix_kde = """[Desktop Entry]
Type=Application
Exec=driveutility -m format
Icon=driveutility
Terminal=false
Categories=Utility;
OnlyShowIn=KDE;
"""
#generate(DOMAIN, LOCALE_PATH, "share/applications/driveutility-formatter-kde.desktop", format_prefix_kde, _("Disk formatter"), _("Format a disk"), "")
# --- Wipe Action ---
wipe_prefix_gtk = """[Desktop Entry]
Type=Application
Exec=driveutility -m wipe
Icon=driveutility
Terminal=false
Categories=GTK;Utility;
NotShowIn=KDE;
"""
#generate(DOMAIN, LOCALE_PATH, "share/applications/driveutility-wiper.desktop", wipe_prefix_gtk, _("Disk wiper"), _("Wipe a disk"), "")
wipe_prefix_kde = """[Desktop Entry]
Type=Application
Exec=driveutility -m wipe
Icon=driveutility
Terminal=false
Categories=Utility;
OnlyShowIn=KDE;
"""
#generate(DOMAIN, LOCALE_PATH, "share/applications/driveutility-wiper-kde.desktop", wipe_prefix_kde, _("Disk wiper"), _("Wipe a disk"), "")
# --- 3. Create actions for file managers (Nemo) ---
# Action for Nemo (writing an image)
nemo_write_prefix = """[Nemo Action]
Active=true
Name[C]=Write image...
Comment[C]=Write this image file to a device
Exec=driveutility -m write -i %F
Icon-Name=driveutility
Selection=S
Extensions=iso;img;bin;raw;dd;
"""
generate(DOMAIN, LOCALE_PATH, "share/nemo/actions/driveutility-writer.nemo_action", nemo_write_prefix, _("Write image..."), _("Write this image file to a device"), "")
# Action for Nemo (formatting)
nemo_format_prefix = """[Nemo Action]
Active=true
Name[C]=Format...
Comment[C]=Format this device
Exec=driveutility -m format -d %F
Icon-Name=driveutility
Selection=S
Extensions=dir;
Mimetypes=inode/directory;
Dependencies=udisks2;
"""
generate(DOMAIN, LOCALE_PATH, "share/nemo/actions/driveutility-formatter.nemo_action", nemo_format_prefix, _("Format..."), _("Format this device"), "")
# Action for Nemo (wiping)
nemo_wipe_prefix = """[Nemo Action]
Active=true
Name[C]=Wipe...
Comment[C]=Wipe this device
Exec=driveutility -m wipe -d %F
Icon-Name=driveutility
Selection=S
Extensions=dir;
Mimetypes=inode/directory;
Dependencies=udisks2;
"""
generate(DOMAIN, LOCALE_PATH, "share/nemo/actions/driveutility-wiper.nemo_action", nemo_wipe_prefix, _("Wipe..."), _("Wipe this device"), "")
# --- 4. Create PolicyKit rules ---
polkit_suffix = """
<defaults>
<allow_any>no</allow_any>
<allow_inactive>no</allow_inactive>
<allow_active>auth_admin_keep</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
</action>
</policyconfig>
"""
# Rule for writing an image
polkit_write_prefix = f"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
<vendor>MiniOS</vendor>
<vendor_url>https://minios.dev</vendor_url>
<action id="dev.minios.driveutility-write">
<icon_name>driveutility</icon_name>
"""
polkit_write_suffix = f"""
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/driveutility-write</annotate>
{polkit_suffix}
"""
generate_polkit_policy(DOMAIN, LOCALE_PATH, "share/polkit/actions/dev.minios.driveutility-write.policy", polkit_write_prefix, _("Write a disk image"), _("Authentication is required to write an image to a device."), polkit_write_suffix)
# Rule for formatting
polkit_format_prefix = f"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
<vendor>MiniOS</vendor>
<vendor_url>https://minios.dev</vendor_url>
<action id="dev.minios.driveutility-format">
<icon_name>driveutility</icon_name>
"""
polkit_format_suffix = f"""
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/driveutility-format</annotate>
{polkit_suffix}
"""
generate_polkit_policy(DOMAIN, LOCALE_PATH, "share/polkit/actions/dev.minios.driveutility-format.policy", polkit_format_prefix, _("Format a disk"), _("Authentication is required to format a device."), polkit_format_suffix)
# Rule for wiping
polkit_wipe_prefix = f"""<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE policyconfig PUBLIC
\"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN\"
\"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd\">
<policyconfig>
<vendor>MiniOS</vendor>
<vendor_url>https://minios.dev</vendor_url>
<action id=\"dev.minios.driveutility-wipe\">
<icon_name>driveutility</icon_name>
"""
polkit_wipe_suffix = f"""
<annotate key=\"org.freedesktop.policykit.exec.path\">/usr/bin/driveutility-wipe</annotate>
{polkit_suffix}
"""
generate_polkit_policy(DOMAIN, LOCALE_PATH, "share/polkit/actions/dev.minios.driveutility-wipe.policy", polkit_wipe_prefix, _("Wipe a disk"), _("Authentication is required to wipe a device."), polkit_wipe_suffix)
# Rule for reading
polkit_read_prefix = f"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
<vendor>MiniOS</vendor>
<vendor_url>https://minios.dev</vendor_url>
<action id="dev.minios.driveutility-read">
<icon_name>driveutility</icon_name>
"""
polkit_read_suffix = f"""
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/driveutility-read</annotate>
{polkit_suffix}
"""
generate_polkit_policy(DOMAIN, LOCALE_PATH, "share/polkit/actions/dev.minios.driveutility-read.policy", polkit_read_prefix, _("Read a disk image"), _("Authentication is required to read a disk image from a device."), polkit_read_suffix)
print("Generated all files successfully.")