-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup.py
264 lines (219 loc) · 7.1 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
#!/usr/bin/env python
from setuptools import setup, Command
import glob
import subprocess
import os
try:
FileExistsError()
except:
class FileExistsError(BaseException):
pass
install_requires = [
'pillow',
]
test_requires = [
'nose',
'mock'
]
data_files = [
('share/applications', ['dist/desktop/gscreenshot.desktop']),
('share/pixmaps', ['dist/pixmaps/gscreenshot.png']),
('share/menu', ['dist/menu/gscreenshot']),
('share/man/man1', ['generated/gscreenshot.1.gz']),
('share/zsh/site-functions', ['dist/completions/zsh/_gscreenshot']),
('share/bash-completion/completions', ['dist/completions/bash/gscreenshot'])
]
def print_warning(warning:str):
print("\n\n\n\n")
print("===> WARNING ====> " + warning)
print("\n\n\n\n")
def get_version_from_specfile() -> str:
'''
Gets the version from the RPM specfile in specs/
This is a hackaround because I always forget to update it
'''
version = None
with open('specs/gscreenshot.spec', 'r') as specfile:
for line in specfile:
if '%define version' in line:
version = line.split(' ')[2].strip()
break
if version is None:
raise Exception("Failed to get version")
return version
def build_data_files(version):
'''
This is somewhat of a workaround so that the data files that
contain a version number end up with the correct version number
in them since it's easy to forget to update them
This code assumes there will only ever be one file getting installed
at a particular path. For gscreenshot this is fine.
'''
try:
os.makedirs("generated")
except (OSError, FileExistsError) as e:
if not os.path.isdir("generated"):
raise
compile_locales()
compile_manpage()
files = []
for d in data_files:
generated_path = os.path.join('generated', d[1][0])
try:
if not os.path.exists(d[1][0]):
print("WARNING: missing intermediate file " + d[1][0])
continue
with open(d[1][0], 'r') as infile:
infile_data = infile.read()
updated_data = infile_data.replace('%%VERSION%%', version)
try:
os.makedirs(os.path.dirname(generated_path))
except (OSError, FileExistsError) as e:
if not os.path.isdir(os.path.dirname(generated_path)):
raise
with open(generated_path, 'w') as outfile:
outfile.write(updated_data)
files.append((d[0], [generated_path]))
except UnicodeDecodeError:
# Just assume this is a binary file that we don't need to
# replace anything in
files.append((d[0], d[1]))
return files
def compile_locales():
print('=> Compiling locales...')
locale_po = glob.glob('src/gscreenshot/resources/locale/*/LC_MESSAGES/*.po')
for po in locale_po:
dirname = os.path.dirname(po)
basename = os.path.basename(po)
outname = os.path.splitext(basename)[0] + ".mo"
original_dir = os.path.abspath(os.path.curdir)
print('===> Compiling ' + po)
os.chdir(dirname)
try:
subprocess.check_output(['msgfmt', basename, '-o', outname])
except:
print_warning("Failed to compile " + po)
os.chdir(original_dir)
def compile_manpage():
print('=> Compiling manpage...')
success = False
man_converters = [
[
'pandoc',
'--standalone',
'--to',
'man',
'README.md',
'-o',
'generated/gscreenshot.1'
],
[
'go-md2man',
'--in',
'README.md',
'--out',
'generated/gscreenshot.1'
]
]
for converter in man_converters:
try:
subprocess.check_output(converter)
success = True
break
except Exception as e:
continue
if not success:
print_warning("Non-fatal. Failed to compile manpage. Install pandoc or md2man (sometimes go-md2man).")
return
try:
subprocess.check_output([
'gzip',
'-f',
'generated/gscreenshot.1',
])
except:
print_warning("Failed to compress manpage")
return
pkg_version = get_version_from_specfile()
class LintCommand(Command):
description = 'lint the Python code'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
command = ['pylint', 'src', '--rcfile=pylintrc']
subprocess.check_call(command)
class TestCommand(Command):
description = 'run the tests'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
command = ['coverage', 'run', '--source', 'src/gscreenshot', '-m', 'pytest', 'test']
subprocess.check_call(command)
class CoverageCommand(Command):
description = 'run the tests and produce a coverage report'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
command = ['coverage', 'run', '--source', 'src/gscreenshot', '-m', 'pytest', 'test']
subprocess.check_call(command)
command = ['coverage', 'html']
subprocess.check_call(command)
command = ['xdg-open', 'htmlcov/index.html']
subprocess.check_call(command)
setup(name='gscreenshot',
cmdclass={
'lint': LintCommand,
'test': TestCommand,
'coverage': CoverageCommand,
},
version=pkg_version,
description='Lightweight GTK frontend to scrot',
author='Nate Levesque',
author_email='[email protected]',
url='https://github.com/thenaterhood/gscreenshot/archive/master.zip',
install_requires=install_requires,
tests_require=test_requires,
entry_points={
'gui_scripts': [
'gscreenshot = gscreenshot.frontend:delegate'
],
'console_scripts': [
'gscreenshot-cli = gscreenshot.frontend:delegate'
]
},
test_suite='nose.collector',
package_dir={'':'src'},
packages=[
'gscreenshot',
'gscreenshot.frontend',
'gscreenshot.frontend.gtk',
'gscreenshot.frontend.gtk.dialogs',
'gscreenshot.cursor_locator',
'gscreenshot.scaling',
'gscreenshot.screenshooter',
'gscreenshot.screenshot',
'gscreenshot.screenshot.effects',
'gscreenshot.selector',
'gscreenshot.resources',
'gscreenshot.resources.gui',
'gscreenshot.resources.gui.glade',
'gscreenshot.resources.locale.en.LC_MESSAGES',
'gscreenshot.resources.locale.es.LC_MESSAGES',
'gscreenshot.resources.pixmaps'
],
data_files=build_data_files(pkg_version),
package_data={
'': ['*.glade', 'LICENSE', '*.png', '*.mo',
'gscreenshot.1.gz', '_gscreenshot', 'gscreenshot']
},
include_package_data=True
)