-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
setup.py
executable file
·380 lines (329 loc) · 15 KB
/
setup.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
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
376
377
378
379
380
#!/usr/bin/env python3
"""
There are four relevant customizations to the standard distutils installation
process: (1) allowing separate installations of aeidon and gaupol, (2) writing
the aeidon.paths module, (3) handling translations and (4) extensions.
(1) Allowing separate installations of aeidon and gaupol are handled through
global options --with-aeidon, --without-aeidon, --with-gaupol and
--without-gaupol. See 'python3 setup.py --help' and the file
'README.aeidon.md' for documentation and the Distribution class defined
in this file for the implementation and logic of how that is handled.
(2) Gaupol finds non-Python files based on the paths written in module
aeidon.paths. In aeidon/paths.py the paths default to the ones in the
source directory. During the 'install_lib' command the file gets rewritten
to build/aeidon/paths.py with the installation paths and that file will be
installed. The paths are based on variable 'install_data' with the 'root'
variable stripped if it is given. If doing distro-packaging, make sure this
file gets correctly written.
(3) During installation, the .po files are compiled into .mo files and the
appdata, desktop, pattern and extension metadata files are translated.
This requires gettext.
(4) Extensions are installed under the data directory. All python code included
in the extensions are compiled during the 'install_data' command, using the
same arguments for 'byte_compile' as used by the 'install_lib' command.
If the 'install_lib' command was given the '--no-compile' option, then
extensions are not compiled either.
"""
import glob
import logging
import os
import re
import shutil
import sys
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
try:
import distutils
except ImportError:
# Use distutils from setuptools, needed since Python 3.12.
# https://docs.python.org/3/whatsnew/3.12.html#distutils
import setuptools._distutils as distutils
from distutils.command.clean import clean
from distutils.command.install import install
from distutils.command.install_data import install_data
from distutils.command.install_lib import install_lib
from distutils.dist import Distribution as distribution
distribution.global_options.extend([
("mandir=", None, "relative installation directory for man pages (defaults to 'share/man')"),
("with-aeidon", None, "install the aeidon package"),
("without-aeidon", None, "don't install the aeidon package"),
("with-gaupol", None, "install the gaupol package"),
("without-gaupol", None, "don't install the gaupol package"),
("with-iso-codes", None, "install iso-codes JSON files"),
("without-iso-codes", None, "don't install iso-codes JSON files"),
])
distribution.negative_opt.update({
"without-aeidon": "with-aeidon",
"without-gaupol": "with-gaupol",
"without-iso-codes": "with-iso-codes",
})
def get_aeidon_version():
path = os.path.join("aeidon", "__init__.py")
with open(path, "r", encoding="utf_8") as f:
text = f.read()
return re.search(r"__version__ *= *['\"](.*?)['\"]", text).group(1)
def get_gaupol_version():
path = os.path.join("gaupol", "__init__.py")
with open(path, "r", encoding="utf_8") as f:
text = f.read()
return re.search(r"__version__ *= *['\"](.*?)['\"]", text).group(1)
def run_or_exit(cmd):
if os.system(cmd) == 0: return
log.error("command {!r} failed".format(cmd))
raise SystemExit(1)
def run_or_warn(cmd):
if os.system(cmd) == 0: return
log.warning("command {!r} failed".format(cmd))
class Clean(clean):
def run(self):
clean.run(self)
with open(os.path.join("manifests", "clean.manifest"), "r") as f:
for targets in [glob.glob(x.strip()) for x in f]:
for target in filter(os.path.isdir, targets):
log.info("removing {}".format(target))
if not self.dry_run:
shutil.rmtree(target)
for target in filter(os.path.isfile, targets):
log.info("removing {}".format(target))
if not self.dry_run:
os.remove(target)
class Distribution(distribution):
def __init__(self, attrs=None):
if sys.platform == "win32":
self.executables = []
self.mandir = "share/man"
self.with_aeidon = True
self.with_gaupol = True
self.with_iso_codes = True
distribution.__init__(self, attrs)
self.data_files = []
self.packages = []
self.scripts = []
def __find_data_files(self, name):
fok = lambda x: not x.endswith((".in", ".pyc"))
basename = "{}.manifest".format(name)
with open(os.path.join("manifests", basename), "r") as f:
for line in [x.strip() for x in f]:
if not line: continue
if line.startswith("["):
dest = line[1:-1]
continue
files = list(filter(fok, glob.glob(line)))
files = list(filter(os.path.isfile, files))
assert files
self.data_files.append((dest, files))
def __find_man_pages(self):
mandir = self.mandir
while mandir.endswith("/"):
mandir = mandir[:-1]
dest = "{}/man1".format(mandir)
self.data_files.append((dest, ["data/gaupol.1"]))
def __find_packages(self, name):
for root, dirs, files in os.walk(name):
init_path = os.path.join(root, "__init__.py")
if not os.path.isfile(init_path): continue
path = root.replace(os.sep, ".")
if path.endswith(".test"): continue
self.packages.append(path[path.find(name):])
def __find_scripts(self, name):
if name == "gaupol":
self.scripts.append("bin/gaupol")
def parse_command_line(self):
value = distribution.parse_command_line(self)
# Configuration files are parsed before the command line, so once the
# command line is parsed, all options have their final values and thus
# files corresponding to the packages to install can finally be set.
if self.with_aeidon:
self.__find_data_files("aeidon")
self.__find_packages("aeidon")
if self.with_aeidon and self.with_iso_codes:
self.__find_data_files("iso-codes")
if self.with_gaupol:
self.__find_data_files("gaupol")
self.__find_man_pages()
self.__find_packages("gaupol")
self.__find_scripts("gaupol")
# Redefine name, version and requires metadata attributes. These are
# used in the egg-info file written by distutils. While egg-info files
# appear completely useless, it needs to be ensured that if aeidon and
# gaupol are installed separately, they install differently named
# egg-info files in order to avoid overwriting each other.
if self.with_gaupol:
self.metadata.name = "gaupol"
self.metadata.version = get_gaupol_version()
else: # without gaupol
self.metadata.name = "aeidon"
self.metadata.version = get_aeidon_version()
return value
def parse_config_files(self, filenames=None):
value = distribution.parse_config_files(self, filenames)
strtobool = distutils.util.strtobool
for option in ["with_aeidon", "with_gaupol", "with_iso_codes"]:
value = getattr(self, option)
if isinstance(value, str):
setattr(self, option, strtobool(value))
return value
class Install(install):
def run(self):
install.run(self)
if not self.distribution.with_gaupol: return
get_command_obj = self.distribution.get_command_obj
root = get_command_obj("install").root
data_dir = get_command_obj("install_data").install_dir
# Assume we're actually installing if --root was not given.
if (root is not None) or (data_dir is None): return
directory = os.path.join(data_dir, "share", "applications")
log.info("updating desktop database in {}".format(directory))
run_or_warn('update-desktop-database "{}"'.format(directory))
class InstallData(install_data):
def __build_extensions(self):
get_command_obj = self.distribution.get_command_obj
if not get_command_obj("install_lib").compile: return
optimize = get_command_obj("install_lib").optimize
data_dir = get_command_obj("install_data").install_dir
data_dir = os.path.join(data_dir, "share", "gaupol")
files = glob.glob("{}/extensions/*/*.py".format(data_dir))
distutils.util.byte_compile(files, optimize, self.force, self.dry_run)
# Figure out paths of the compiled files and add them to
# self.outfiles so that 'setup.py --record' works correctly.
def get_cache_pattern(path):
dir, file = os.path.split(path)
file = os.path.splitext(file)[0] + ".*"
return os.path.join(dir, "__pycache__", file)
for pattern in map(get_cache_pattern, files):
for file in glob.glob(pattern):
if not file in self.outfiles:
self.outfiles.append(file)
def __generate_linguas(self):
linguas = sorted(glob.glob("po/*.po"))
linguas = [os.path.basename(x)[:-3] for x in linguas]
with open("po/LINGUAS", "w") as f:
f.write("\n".join(linguas) + "\n")
def __get_appdata_file(self):
path = os.path.join("data", "io.otsaloma.gaupol.appdata.xml")
command = "msgfmt --xml -d po --template {}.in -o {}"
run_or_warn(command.format(path, path))
if not os.path.isfile(path):
# The above can fail with an old version of gettext,
# fall back on copying the file without translations.
shutil.copy("{}.in".format(path), path)
return ("share/metainfo", [path])
def __get_desktop_file(self):
path = os.path.join("data", "io.otsaloma.gaupol.desktop")
command = "msgfmt --desktop -d po --template {}.in -o {}"
run_or_warn(command.format(path, path))
if not os.path.isfile(path):
# The above can fail with an old version of gettext,
# fall back on copying the file without translations.
shutil.copy("{}.in".format(path), path)
return ("share/applications", [path])
def __get_extension_file(self, extension_file):
assert extension_file.endswith(".in")
path = extension_file[:-3]
command = " ".join((
"msgfmt --desktop -d po --template {}.in -o {}",
"--keyword=",
"--keyword=Name",
"--keyword=Description",
))
run_or_warn(command.format(path, path))
if not os.path.isfile(path):
# The above can fail with an old version of gettext,
# fall back on copying the file without translations.
shutil.copy("{}.in".format(path), path)
extension = os.path.basename(os.path.dirname(extension_file))
return ("share/gaupol/extensions/{}".format(extension), [path])
def __get_extension_files(self):
files = sorted(glob.glob("data/extensions/*/*.extension.in"))
return [self.__get_extension_file(x) for x in files]
def __get_mo_file(self, po_file):
locale = os.path.basename(po_file[:-3])
mo_dir = os.path.join("locale", locale, "LC_MESSAGES")
mo_file = os.path.join(mo_dir, "gaupol.mo")
log.info("compiling {}".format(mo_file))
os.makedirs(mo_dir, exist_ok=True)
run_or_exit("msgfmt {} -o {}".format(po_file, mo_file))
dest_dir = os.path.join("share", mo_dir)
return (dest_dir, [mo_file])
def __get_mo_files(self):
if sys.platform == "win32": return []
files = sorted(glob.glob("po/*.po"))
return [self.__get_mo_file(x) for x in files]
def __get_pattern_file(self, pattern_file):
assert pattern_file.endswith(".in")
path = pattern_file[:-3]
command = " ".join((
"msgfmt --desktop -d po --template {}.in -o {}",
"--keyword=",
"--keyword=Name",
"--keyword=Description",
))
run_or_warn(command.format(path, path))
if not os.path.isfile(path):
# The above can fail with an old version of gettext,
# fall back on copying the file without translations.
shutil.copy("{}.in".format(path), path)
return ("share/gaupol/patterns", [path])
def __get_pattern_files(self):
files = sorted(glob.glob("data/patterns/*.in"))
return [self.__get_pattern_file(x) for x in files]
def run(self):
self.__generate_linguas()
if self.distribution.with_aeidon:
self.data_files.extend(self.__get_mo_files())
self.data_files.extend(self.__get_pattern_files())
if self.distribution.with_gaupol:
self.data_files.extend(self.__get_extension_files())
self.data_files.append(self.__get_appdata_file())
self.data_files.append(self.__get_desktop_file())
install_data.run(self)
if self.distribution.with_gaupol:
self.__build_extensions()
class InstallLib(install_lib):
def install(self):
if not self.distribution.with_aeidon:
return install_lib.install(self)
get_command_obj = self.distribution.get_command_obj
root = get_command_obj("install").root
prefix = get_command_obj("install").install_data
# Allow --root to be used like DESTDIR.
if root is not None:
prefix = os.path.abspath(prefix)
prefix = prefix.replace(os.path.abspath(root), "")
data_dir = os.path.join(prefix, "share", "gaupol")
locale_dir = os.path.join(prefix, "share", "locale")
# Write changes to the aeidon.paths module.
path = os.path.join(self.build_dir, "aeidon", "paths.py")
with open(path, "r", encoding="utf_8") as f:
text = f.read()
patt = r"^DATA_DIR = .*$"
repl = "DATA_DIR = {!r}".format(data_dir)
text = re.sub(patt, repl, text, flags=re.MULTILINE)
assert text.count(repl) == 1
patt = r"^LOCALE_DIR = .*$"
repl = "LOCALE_DIR = {!r}".format(locale_dir)
text = re.sub(patt, repl, text, flags=re.MULTILINE)
assert text.count(repl) == 1
with open(path, "w", encoding="utf_8") as f:
f.write(text)
return install_lib.install(self)
setup_kwargs = {
"name": "gaupol",
"version": get_gaupol_version(),
"author": "Osmo Salomaa",
"author_email": "[email protected]",
"url": "https://otsaloma.io/gaupol/",
"description": "Editor for text-based subtitle files",
"license": "GPL",
"distclass": Distribution,
"cmdclass": {
"clean": Clean,
"install": Install,
"install_data": InstallData,
"install_lib": InstallLib,
},
}
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__) or ".")
distutils.core.setup(**setup_kwargs)