-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
executable file
·163 lines (138 loc) · 5.06 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
#!/usr/bin/env python3
import gettext
import os
import shlex
import shutil
import sys
import setuptools
from setuptools import Command
from setuptools.command.build_py import build_py as build
from setuptools.command.install import install
from MAGSBS.config import VERSION
BUILD_DIR = "build"
POT_FILE = "matuc.pot"
mo_base = lambda lang: os.path.join(BUILD_DIR, "mo", lang, "LC_MESSAGES")
def shell(cmd, hint=None):
ret = os.system(cmd)
if ret:
print("Command exited with error %d: %s\nAborting." % (ret, cmd))
if hint:
print(hint)
sys.exit(ret)
def mkmo(podir, pofile):
outpath = mo_base(os.path.splitext(pofile)[0])
if os.path.exists(outpath):
shutil.rmtree(outpath)
os.makedirs(outpath)
inpath = os.path.join(podir, pofile)
shell("msgfmt %s -o %s%s%s.mo" % (inpath, outpath, os.sep, "matuc"))
class I18nBuild(build):
"""Build gettext locale files and install them appropriately."""
user_options = build.user_options
def run(self, *args):
for pofile in os.listdir("po"):
# ~-files are known to cause issues, delete them
if pofile.endswith("~"):
os.remove(os.path.join("po", pofile))
elif pofile.endswith("po"): # only convert .po files
mkmo("po", pofile)
build.run(self, *args)
# pylint: disable=protected-access
# pylint: disable=inconsistent-return-statements
def locale_destdir():
"""Find best suitable directory for locales."""
loc_dirs = [gettext._default_localedir]
if sys.platform in ["linux", "darwin"]:
loc_dirs += ["/usr/share/locale", "/usr/local/share/locale"]
elif sys.platform == "win32":
# default installer place
loc_dirs.append(os.path.join(os.getenv("ProgramData"), "matuc", "locale"))
loc_dirs.append(
os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))),
"share",
"locale",
)
)
dir_with_no_perms = None
for directory in loc_dirs:
if os.path.exists(directory):
if os.access(directory, os.W_OK):
return directory
dir_with_no_perms = directory
else: # doesn't exist, but maybe a parent?
dirpath = directory[:]
while dirpath and not os.path.exists(dirpath):
dirpath = os.path.dirname(dirpath)
if dirpath and os.access(dirpath, os.W_OK):
return directory
if dir_with_no_perms:
print(
"Insufficient rights to install translations (.mo) to " + dir_with_no_perms
)
sys.exit(81)
class I18nGeneration(Command):
description = "Create/update po/pot translation files"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
files = (
shlex.quote(os.path.join(dname, file))
for dname, _d, files in os.walk("MAGSBS")
for file in files
if file.endswith(".py")
and not "build" in dname
and not "setup" in file
and not "test" in dname
)
create_pot = not os.path.exists("matuc.pot")
if not create_pot:
# query last modification time of py source files
matuc_mtime = os.path.getmtime("matuc.pot")
if any(os.path.getmtime(p) > matuc_mtime for p in files):
create_pot = True
if create_pot:
print("Extracting translatable strings...")
pygettext = "pygettext3" if shutil.which("pygettext3") else "pygettext"
shell(f"{pygettext} --keyword=_ --output=matuc.pot %s" % " ".join(files))
# merge new strings and old translations
for lang_po in os.listdir("po"):
shell("msgmerge -F -U %s matuc.pot" % os.path.join("po", lang_po))
class I18nInstall(install):
"""Install compiled .mo files."""
user_options = install.user_options
def run(self):
install.run(self)
for lang in os.listdir(os.path.join(BUILD_DIR, "mo")):
destdir = os.path.join(locale_destdir(), lang, "LC_MESSAGES")
if not os.path.exists(destdir):
os.makedirs(destdir)
src_mo = os.path.join(BUILD_DIR, "mo", lang, "LC_MESSAGES", "matuc.mo")
print(f"Installing {src_mo} to {destdir}")
shutil.copy(src_mo, destdir)
setuptools.setup(
author="Sebastian Humenda, Jens Voegler",
cmdclass={
"build_i18n": I18nGeneration,
"build_py": I18nBuild,
"install": I18nInstall,
},
description="MAGSBS - MarkDown AG SBS module",
entry_points={
"console_scripts": [
"matuc = MAGSBS.matuc:main",
"matuc_js = MAGSBS.matuc_js:main",
]
},
install_requires=["pandocfilters >= 1.4.2", "gladtex >= 3.1"],
include_package_data=True,
license="LGPL",
name="MAGSBS-matuc",
packages=setuptools.find_packages("."),
url="https://github.com/TUD-INF-IAI-MCI/AGSBS-infrastructure",
version=str(VERSION),
zip_safe=True,
)