-
Notifications
You must be signed in to change notification settings - Fork 31
/
install.py
executable file
·186 lines (162 loc) · 7.02 KB
/
install.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
#!/usr/bin/env python3
import argparse
import os
import re
import shutil
import sys
import mmap
from collections import Counter
source_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'src')
install_dir = '/usr/bin/'
def copy(src, dst):
"""Copy file <src> to <dst>, creating all necessary dirs in between"""
try:
assert os.path.isfile(src)
assert not os.path.isdir(dst)
if not os.path.isdir(os.path.dirname(dst)):
os.makedirs(os.path.dirname(dst))
shutil.copy(src, dst)
return True
except Exception:
return False
def check_dependencies():
"""Check for required dependencies."""
required_found = True
print('Checking dependencies ...\n')
print('Required dependencies:')
print(' Python Imaging Library .......')
try:
from PIL import Image
print('OK')
except ImportError:
required_found = False
try:
import Image
assert Image.__version__ >= '1.1.5'
required_found = True
except AssertionError:
print(' !!! version ' + Image.__version__ + ' found, but 1.1.5 or higher is required')
required_found = False
except ImportError:
pass
finally:
if not required_found:
print(' ... Not found')
if not required_found:
print('\nCould not find all required dependencies!')
print('Please install them and try again.')
sys.exit(1)
print('')
MAJOR_VERSION_EXTRACTOR = re.compile(r'.* (\d*)\..*')
def gnome_shell_version():
major_version = None
gnome_session_match = MAJOR_VERSION_EXTRACTOR.search(os.popen('gnome-session --version').read()) # output is "gnome-session X.Y.Z"
major_version = gnome_session_match and gnome_session_match.group(1)
if not major_version:
gnome_shell_match = MAJOR_VERSION_EXTRACTOR.search(os.popen('gnome-shell --version').read()) # output is "GNOME Shell X.Y.Z"
major_version = gnome_shell_match and gnome_shell_match.group(1)
return ('gnome%s' % major_version) if major_version else None
VERSION_GUSSERS = {
'cinnamon': lambda: 'gnome3',
'enlightenment': lambda: 'enlightenment',
'gnome': gnome_shell_version,
'gnome-shell': gnome_shell_version,
'i3': lambda: 'i3',
'mate': lambda: 'gnome3',
'openbox': lambda: 'openbox',
'pantheon': lambda: 'pantheon',
'unity': lambda: 'unity',
'xfce4': lambda: 'xfce4',
}
KNOWN_DE = '|'.join(VERSION_GUSSERS.keys())
def check_desktop_env():
"""
Tries to guess the installed desktop environment. This is kind of heuristic and might (and most likely will)
fail
returns: Things like 'gnome2', 'gnome3', 'xfce4', 'openbox' or None
"""
value = os.popen("ps -A | grep --extended-regexp --ignore-case '(%s)$'" % KNOWN_DE).read()
if not value:
value = os.popen("ps -A | grep --extended-regexp --ignore-case '%s'" % KNOWN_DE).read()
counter = Counter(re.findall(r'(%s)' % KNOWN_DE, value))
if counter:
desktop_environment = counter.most_common(1)[0][0]
return VERSION_GUSSERS.get(desktop_environment, lambda: None)()
return None
def check_tumbler():
return os.path.isfile('/etc/xdg/tumbler/tumbler.rc')
def add_thumbnailer_to_tumbler():
with(open('/etc/xdg/tumbler/tumbler.rc', 'a+')) as tumbler_config:
config_content = mmap.mmap(tumbler_config.fileno(), 0, access=mmap.ACCESS_READ)
if config_content.find(b'# Ebook thumbnailer') == -1:
tumbler_config.write('\n')
tumbler_config.write('# Ebook thumbnailer\n')
tumbler_config.write('[EbookThumbnailer]\n')
tumbler_config.write('Disabled=false\n')
tumbler_config.write('Priority=1\n')
tumbler_config.write('Locations=\n')
tumbler_config.write('MaxFileSize=0\n')
print('Registered thumbnailer in tumbler.rc')
else:
print('Thumbnailer already present in tumbler.rc')
def install():
check_dependencies()
if not os.access(install_dir, os.W_OK):
print('You do not have write permissions to %s (maybe you need to sudo)' % install_dir)
sys.exit(1)
print('Installing epub-thumbnailer to %s ...' % install_dir)
if copy(os.path.join(source_dir, 'epub-thumbnailer.py'), os.path.join(install_dir, 'epub-thumbnailer')):
print('OK')
environment = check_desktop_env()
if environment == 'gnome2':
schema = os.path.join(source_dir, 'epub-thumbnailer.schemas')
os.popen('GCONF_CONFIG_SOURCE=$(gconftool-2 --get-default-source) '
'gconftool-2 --makefile-install-rule "%s" 2>/dev/null' %
schema)
print('\nRegistered epub archive thumbnailer in gconf (if available).')
print('The thumbnailer is only supported by some file managers, such as Nautilus, Caja and Thunar')
print('You might have to restart the file manager for the thumbnailer to be activated.\n')
elif environment is not None:
print('Installing thumbnailer hook in /usr/share/thumbnailers ...')
if copy(os.path.join(source_dir, 'epub.thumbnailer'), '/usr/share/thumbnailers/epub.thumbnailer'):
print('OK')
else:
print('Could not install')
exit(1)
else:
print('\nCould not determine your desktop environment version. You can still use the thumbnailer script manually.')
print('')
print('For example:')
print('')
print(' epub-thumbnailer Lawrence\ Lessig\ -\ Free\ Culture.epub cover.png 128')
print('')
print('Also, please consider reporting this in https://github.com/marianosimone/epub-thumbnailer/')
print('including information about your distribution, window manager, file manager and the result of `ps -A`')
exit(1)
else:
print('Could not install')
exit(1)
uses_tumbler = check_tumbler()
if uses_tumbler:
add_thumbnailer_to_tumbler()
print('You might have to restart your file manager for the thumbnailer to be activated.\n')
def uninstall():
print('Uninstalling epub-thumbnailer from', install_dir, '...')
environment = check_desktop_env()
os.remove(os.path.join(install_dir, 'epub-thumbnailer'))
if environment in ('gnome3', 'xfce4', 'unity'):
print('Uninstalling epub.thumbnailer from /usr/share/thumbnailers/ ...')
try:
os.remove('/usr/share/thumbnailers/epub.thumbnailer')
print('OK')
except:
print('Could not remove /usr/share/thumbnailers/epub.thumbnailer')
commands = {
'install': install,
'uninstall': uninstall
}
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Installs or uninstall epub-thumbnailer on your system')
parser.add_argument('action', metavar='action', choices=['install', 'uninstall'], help='the action to perform')
args = parser.parse_args()
commands[args.action]()