This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
/
deps.py
326 lines (285 loc) · 8.09 KB
/
deps.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
#!/usr/bin/env python3
import locale
import os
import sys
try:
# suppress warnings from GI
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Poppler', '0.18')
gi.require_version('PangoCairo', '1.0')
gi.require_version('Notify', '0.7')
except: # noqa: E722
pass
g_gtk_available = False
g_glib_available = False
try:
from gi.repository import GLib
g_glib_available = True
except Exception as exc:
pass
try:
if g_glib_available:
from gi.repository import Gtk
g_gtk_available = True
except Exception as exc:
pass
"""
Some modules/libraries required by Paperwork cannot be installed with pip or
easy_install. So we will just help the user detecting what is missing and what
must be installed
"""
LANGUAGES = {
None: {
'aspell': 'en',
'tesseract': 'eng',
},
'fr': {
'aspell': 'fr',
'tesseract': 'fra',
},
'de': {
'aspell': 'de',
'tesseract': 'deu',
},
'en': {
'aspell': 'en',
'tesseract': 'eng',
},
}
DEFAULT_LANG = {
'aspell': '<your language>',
'tesseract': '<your language>',
}
MODULES = [
(
'Gtk (GObject introspection bindings)', 'gi.repository.Gtk',
{
'debian': 'gir1.2-gtk-3.0',
'fedora': 'gtk3',
'gentoo': 'x11-libs/gtk+',
'linuxmint': 'gir1.2-gtk-3.0',
'ubuntu': 'gir1.2-gtk-3.0',
'suse': 'python-gtk',
},
),
(
'Notify (GObject introspection bindings)', 'gi.repository.Notify',
{
'debian': 'gir1.2-notify-0.7',
'ubuntu': 'gir1.2-notify-0.7',
}
),
]
DATA_FILES = [
(
"Gnome symbolic icons"
" (/usr/share/icons/gnome/(...)/go-previous-symbolic.svg",
[
"/usr/share/icons/gnome/scalable/actions/go-previous-symbolic.svg",
"/usr/local/share/icons/gnome/scalable/"
"actions/go-previous-symbolic.svg",
],
{
'debian': 'gnome-icon-theme-symbolic',
'ubuntu': 'gnome-icon-theme-symbolic',
'fedora': 'gnome-icon-theme-symbolic',
}
),
(
"Gnome symbolic icons"
" (/usr/share/icons/gnome/(...)/edit-copy.png",
[
"/usr/share/icons/gnome/24x24/actions/edit-copy.png",
"/usr/local/share/icons/gnome/24x24/actions/edit-copy.png",
],
{
'debian': 'gnome-icon-theme',
'ubuntu': 'gnome-icon-theme',
'fedora': 'gnome-icon-theme',
}
),
]
def get_language():
lang = locale.getdefaultlocale()[0]
if lang:
lang = lang[:2]
if lang in LANGUAGES:
return LANGUAGES[lang]
print(
"[WARNING] Unable to figure out the exact language package to install"
)
return DEFAULT_LANG
def find_missing_modules():
"""
look for dependency that setuptools cannot check or that are too painful to
install with setuptools
"""
missing_modules = []
for module in MODULES:
try:
__import__(module[1])
except ImportError:
missing_modules.append(module)
return missing_modules
def find_missing_ocr(lang):
"""
OCR tools are a little bit more tricky
"""
missing = []
try:
from pyocr import pyocr
ocr_tools = pyocr.get_available_tools()
except ImportError:
print(
"[WARNING] Couldn't import Pyocr. Will assume OCR tool is not"
" installed yet"
)
ocr_tools = []
if len(ocr_tools) <= 0:
langs = []
missing.append(
(
'Tesseract', '(none)',
{
'debian': 'tesseract-ocr',
'fedora': 'tesseract',
'gentoo': 'app-text/tesseract',
'linuxmint': 'tesseract-ocr',
'ubuntu': 'tesseract-ocr',
},
)
)
else:
try:
langs = ocr_tools[0].get_available_languages()
except Exception as exc:
print("[WARNING] Exception while looking for available languages:"
" {}".format(str(exc)))
langs = []
if (len(langs) <= 0 or lang['tesseract'] not in langs):
missing.append(
(
'Tesseract language data', '(none)',
{
'debian': ('tesseract-ocr-%s' % lang['tesseract']),
'fedora': ('tesseract-langpack-%s' % lang['tesseract']),
'linuxmint': ('tesseract-ocr-%s' % lang['tesseract']),
'ubuntu': ('tesseract-ocr-%s' % lang['tesseract']),
},
)
)
return missing
def find_missing_dict(lang):
if os.name == "nt":
return []
import enchant
missing = []
try:
enchant.request_dict(lang['aspell'])
except: # noqa: E722
missing.append(
(
'Dictionary', '(none)',
{
'debian': ('aspell-%s' % lang['aspell']),
'fedora': ('aspell-%s' % lang['aspell']),
'gentoo': ('aspell-%s' % lang['aspell']),
'linuxmint': ('aspell-%s' % lang['aspell']),
'ubuntu': ('aspell-%s' % lang['aspell']),
}
)
)
return missing
def _check_cairo():
class CheckCairo(object):
def __init__(self):
self.test_successful = False
def on_draw(self, widget, cairo_ctx):
self.test_successful = True
Gtk.main_quit()
return False
def quit(self):
try:
Gtk.main_quit()
except Exception as exc:
print("FAILED TO STOP GTK !")
print("ASSUMING python-gi-cairo is not installed")
print("Exception was: {}".format(exc))
sys.exit(1)
check = CheckCairo()
try:
if g_glib_available and g_gtk_available:
window = Gtk.Window()
da = Gtk.DrawingArea()
da.set_size_request(200, 200)
da.connect("draw", check.on_draw)
window.add(da)
da.queue_draw()
window.show_all()
GLib.timeout_add(2000, check.quit)
Gtk.main()
window.set_visible(False)
while Gtk.events_pending():
Gtk.main_iteration()
except Exception:
pass
return check.test_successful
def check_cairo():
missing = []
if not g_gtk_available:
success = False
else:
success = _check_cairo()
if not success:
missing.append(
(
'python-gi-cairo', '(none)',
{
'debian': 'python3-gi-cairo',
'linuxmint': 'python3-gi-cairo',
'ubuntu': 'python3-gi-cairo',
},
)
)
return missing
def check_sane():
import pyinsane2
missing = []
try:
pyinsane2.init()
pyinsane2.exit()
except: # noqa: E722
missing.append(
(
'libsane', '(none)',
{
'debian': 'libsane',
'fedora': 'sane-backends',
'linuxmint': 'libsane',
'ubuntu': 'libsane',
},
)
)
return missing
def find_missing_data_files():
missings = []
for (user_name, file_paths, packages) in DATA_FILES:
for file_path in file_paths:
if os.path.exists(file_path):
break
else:
missings.append((user_name, "(none)", packages))
return missings
def find_missing_dependencies():
lang = get_language()
# missing_modules is an array of
# (common_name, python_name, { "distrib": "package" })
missing = []
missing += find_missing_modules()
missing += find_missing_ocr(lang)
missing += find_missing_dict(lang)
missing += find_missing_data_files()
missing += check_cairo()
missing += check_sane()
return missing