-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsetup.py
executable file
·506 lines (428 loc) · 18.9 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/usr/bin/env python
#
# Copyright (c) 2005,2006,2007,2008,2009 Brett Adams <brett@belizebotanic.org>
# Copyright (c) 2015 Mario Frasca <mario@anche.no>
# Copyright (c) 2016,2017 Ross Demuth <rossdemuth123@gmail.com>
#
# This file is part of ghini.desktop.
#
# ghini.desktop is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ghini.desktop is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ghini.desktop. If not, see <http://www.gnu.org/licenses/>.
#
try:
import setuptools
except ImportError:
from ez_setup import use_setuptools
use_setuptools()
import setuptools
import gi
gi.require_version('Gtk', '3.0')
import os
import sys
import glob
spawn = setuptools.distutils.spawn
dep_util = setuptools.distutils.dep_util
dir_util = setuptools.distutils.dir_util
file_util = setuptools.distutils.file_util
from distutils.command.build import build as _build
#from setuptools.command.build_py import build_py as _build
from setuptools import Command
from setuptools.command.install import install as _install
from bauble import version
# TODO: external dependencies not in the PyPI: PyGTK>=2.14
# TODO: optional dependencies: MySQL-Python, psycopg2,
# Sphinx (for building docs, maybe include in buildr-requires)
# TODO: fix permissions on files when creating an sdist with:
# find . -regex '.*?\.\(glade\|xsl\|txt\|svg\|ui\)' -exec chmod 644 {} \;
# TODO: run the clean before creating an sdist, or at least the
# manifest should not include those files that would be cleaned
# relative path for locale files
locale_path = os.path.join('share', 'locale')
gtk_pkgs = ["pango", "atk", "gobject", "gtk", "cairo", "pangocairo", "gio"]
plugins = setuptools.find_packages(
where='bauble/plugins',
exclude=['test', 'bauble.*.test', 'ghini.*.test'])
plugins_pkgs = ['bauble.plugins.%s' % p for p in plugins]
all_packages = setuptools.find_packages(exclude=['test', 'bauble.*.test',
'ghini.*.test'])
package_data = {'': ['README.rst', 'CHANGES', 'LICENSE'],
'bauble': ['*.ui', '*.glade', 'images/*.png', 'pixmaps/*.png',
'images/*.svg', 'images/*.gif', 'images/*.ico',
'images/*.bmp']}
# ceate a list of the data patterns to look for in the packages
data_patterns = ['default/*.txt', '*.ui', '*.glade', '*.xsl', '*.xsd', '*.jpg',
'*.html', '*.csv', '*.svg', '*.ps', '*.png', '*.jj2', '*.mako']
for pkg in plugins_pkgs:
package_data[pkg] = data_patterns
all_package_dirs = {'': '.'}
for p in all_packages:
all_package_dirs[p] = p.replace('.', '/')
data_files = []
# setup py2exe and nsis installer
if sys.platform == 'win32' and sys.argv[1] in ('nsis', 'py2exe'):
import py2exe
from distutils.command.py2exe import py2exe as _py2exe_cmd
# setuptools.find packages doesn't dig deep enough so we search
# for a list of all packages in the sqlalchemy namespace
sqlalchemy_includes = ['sqlalchemy.dialects.sqlite',
'sqlalchemy.dialects.postgresql']
py2exe_includes = ['sqlite3', 'lxml', 'gdata', 'fibra', 'psycopg2',
'encodings', 'mako', 'mako.cache',
'MarkupSafe', 'jinja2',
'pygments.styles.default', 'pyparsing']
py2exe_includes += gtk_pkgs + plugins_pkgs + sqlalchemy_includes
py2exe_setup_args = {
'windows': [{'script': 'scripts/ghini',
'icon_resources': [(1, "bauble/images/icon.ico")]}]}
py2exe_options = {
"py2exe": {
#no compression makes for better NSIS compression
"compressed": False,
"optimize": 2,
"includes": py2exe_includes,
"dll_excludes": [
# GTK, included elsewhere
"iconv.dll", "intl.dll", "libatk-1.0-0.dll",
"libgdk_pixbuf-2.0-0.dll", "libgdk-win32-2.0-0.dll",
"libglib-2.0-0.dll", "libgmodule-2.0-0.dll",
"libgobject-2.0-0.dll", "libgthread-2.0-0.dll",
"libgtk-win32-2.0-0.dll", "libpango-1.0-0.dll",
"libpangowin32-1.0-0.dll", "libxml2-2.dll",
# windows dlls
"DNSAPI.DLL", "MSIMG32.DLL", "Secur32.dll", "SHFOLDER.dll",
"CRYPT32.dll", "MPR.dll"
]
}
}
# py2exe doesn't seem to respect packages_data so build data_files from
# package_data
for package, patterns in package_data.items():
pkg_dir = all_package_dirs[package]
for p in patterns:
matches = glob.glob(pkg_dir + '/' + p)
if matches != []:
index = p.rfind('/')
if index != -1:
install_dir = '%s/%s' % (pkg_dir, p[:index])
else:
install_dir = pkg_dir
data_files.append((install_dir,
[m.replace(os.sep, '/') for m in matches]))
class py2exe_cmd(_py2exe_cmd):
description = 'build standalone Windows executable'
def run(self):
# TODO: make sure we have everything installed that we need to
# bundle e.g. sqlite, psycopg2, others...
_py2exe_cmd.run(self)
# install locale files
locales = os.path.dirname(locale_path)
build_base = self.get_finalized_command('build').build_base
src = os.path.join(build_base, locales)
dir_util.copy_tree(src, os.path.join(self.dist_dir, locales))
# copy GTK to the dist directory, assuming PyGTK
# all-in-one installer
gtk_root = 'c:\\python27\\lib\\site-packages\\gtk-2.0\\runtime'
dist_gtk = os.path.join(self.dist_dir, 'gtk')
import shutil
if not os.path.exists(dist_gtk):
ignore = shutil.ignore_patterns('src', 'gtk-doc', 'icons',
'man', 'demo', 'aclocal',
'doc', 'include', 'emacs',
'gettext', 'glade3',
'gtksourceview-2.0', 'info',
'intltool')
shutil.copytree(gtk_root, dist_gtk, ignore=ignore)
# register the pixbuf loaders
# populate loaders.cache also
exe = '%s\\bin\\gdk-pixbuf-query-loaders.exe' % dist_gtk
dest1 = '%s\\etc\\gtk-2.0\\gdk-pixbuf.loaders' % dist_gtk
dest2 = '%s\\lib\\gdk-pixbuf-2.0\\2.10.0\\loaders.cache' % dist_gtk
cmd1 = 'call "%s" > "%s"' % (exe, dest1)
cmd2 = 'call "%s" > "%s"' % (exe, dest2)
print(cmd1)
print(cmd2)
os.system(cmd1)
os.system(cmd2)
# copy the the MS-Windows gtkrc to make it the default theme
rc = '%s\\share\\themes\\MS-Windows\\gtk-2.0\\gtkrc' % dist_gtk
dest = '%s\\etc\\gtk-2.0' % dist_gtk
file_util.copy_file(rc, dest)
# copy the gnome index.theme - stops stderr messages
gtheme = '%s\\share\\icons\\hicolor\\index.theme' % gtk_root
dest = '%s\\share\\icons\\hicolor' % self.dist_dir
dir_util.mkpath(dest)
file_util.copy_file(gtheme, dest)
# copy LICENSE to dist\share\LICENSE.ghini (for help>about)
file_util.copy_file(
"LICENSE",
os.path.join(self.dist_dir, 'share', 'ghini', 'LICENSE'))
class NsisCmd(Command):
"""Command to create a Windows NSIS installer"""
description = 'build windows NSIS installer'
user_options = [
('makensis=', 'm', 'path to makensis'),
('nsis-script=', 's', 'script to build installer from')
]
def initialize_options(self):
envars = ['programw6432', 'programfiles', 'localappdata']
mns = 'nsis\\makensis'
locs = [os.path.join(os.getenv(ev, 'c:\\'), mns) for ev in envars]
self.makensis = (
[loc for loc in locs if spawn.find_executable(loc)] +
[spawn.find_executable('makensis')])[0]
self.nsis_script = 'scripts\\build-multiuser.nsi'
def finalize_options(self):
if self.makensis:
is_exe = spawn.find_executable(self.makensis) is not None
exe_name = os.path.split(self.makensis)[-1]
is_makensis = exe_name in ('makensis', 'makensis.exe')
if not is_exe or not is_makensis:
raise Exception('makensis not found at: %s' % self.makensis)
else:
raise Exception('can not find makensis, NSIS needs to be '
'installed in the default location, on the '
'path or provided using --makensis=')
if not os.path.exists(self.nsis_script):
raise Exception('can not find nsis build script at: %s'
% self.nsis_script)
def run(self):
print(('using %s to build %s' % (self.makensis, self.nsis_script)))
os.system('"%s" %s' % (self.makensis, self.nsis_script))
else:
py2exe_options = {}
py2exe_setup_args = {}
py2exe_includes = []
class _empty_cmd(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
print("**Error: Can't run this command.")
print((sys.exit(1)))
class py2exe_cmd(_empty_cmd):
description = 'build Windows executable *ONLY AVAILABLE IN WINDOWS'
pass
class NsisCmd(_empty_cmd):
description = 'build Windows NSIS installer *ONLY AVAILABLE IN WINDOWS'
pass
# build command
class build(_build):
def run(self):
if sys.platform == 'win32':
# try to guess the path of the gettext utilities
os.environ['PATH'] = os.environ['PATH'] + \
';c:\\Program Files\\GnuWin32\\bin'
if not spawn.find_executable('msgfmt'):
msg = '** Error: Building Ghini requires the gettext utilities ' \
'be installed. If they are installed please ensure that ' \
'the msgfmt command is in your PATH'
print(msg)
sys.exit(1)
# create build/share directory
dir_util.mkpath(os.path.join(self.build_base, 'share', 'ghini'))
_build.run(self)
dest_tmpl = os.path.join(self.build_base, locale_path, '%s',
'LC_MESSAGES')
matches = glob.glob('po/*.po')
from bauble.i18n import TEXT_DOMAIN
for po in matches:
# create an .mo in build/share/locale/$LANG/LC_MESSAGES
loc, ext = os.path.splitext(os.path.basename(po))
localedir = dest_tmpl % loc
mo = '%s/%s.mo' % (localedir, TEXT_DOMAIN)
if not os.path.exists(localedir):
dir_util.mkpath(localedir)
if not os.path.exists(mo) or dep_util.newer(po, mo):
spawn.spawn(['msgfmt', po, '-o', mo])
# copy .desktop and icons
if sys.platform in ('linux', ):
app_dir = os.path.join(self.build_base, 'share', 'applications')
dir_util.mkpath(app_dir)
file_util.copy_file('data/ghini.desktop', app_dir)
icon_sizes = [16, 22, 24, 32, 48, 64]
icon_root = os.path.join(
self.build_base, 'share', 'icons', 'hicolor')
# copy scalable icon
scalable_dir = os.path.join(icon_root, 'scalable', 'apps')
dir_util.mkpath(scalable_dir)
file_util.copy_file('data/ghini.svg', scalable_dir)
pixmaps_dir = os.path.join(self.build_base, 'share', 'pixmaps')
dir_util.mkpath(pixmaps_dir)
file_util.copy_file('data/ghini.svg', pixmaps_dir)
# copy .png icons
dimension = lambda s: '%sx%s' % (s, s)
for size in icon_sizes:
img = 'data/ghini-%s.png' % size
dest = os.path.join(icon_root, '%s/apps/ghini.png'
% dimension(size))
dir_util.mkpath(os.path.split(dest)[0])
file_util.copy_file(img, dest)
# install command
class install(_install):
def has_i18n(self):
return True
def initialize_options(self):
_install.initialize_options(self)
self.skip_xdg = False
def finalize_options(self):
_install.finalize_options(self)
def run(self):
if sys.platform not in ('linux', 'win32', 'darwin'):
msg = "**Error: Can't install on this platform: %s" % sys.platform
print(msg)
sys.exit(1)
# create build/share directory
dir_util.mkpath(os.path.join(self.build_base, 'share', 'ghini'))
if not self.single_version_externally_managed:
print('before installing new egg, remove old ones!')
old_egg_dirs = [a for (a, b, c) in os.walk(self.install_data)
if (os.path.basename(a).startswith('bauble')
or os.path.basename(a).startswith('ghini.desktop'))
and os.path.basename(a).endswith('egg')]
for oed in old_egg_dirs:
dir_util.remove_tree(oed)
self.do_egg_install()
else:
_install.run(self)
# install ghini.desktop and icons
if sys.platform in ('linux', ):
# install everything in share
dir_util.copy_tree(os.path.join(self.build_base, 'share'),
os.path.join(self.install_data, 'share'))
elif sys.platform == 'win32':
# install only i18n files
locales = os.path.dirname(locale_path)
install_cmd = self.get_finalized_command('install')
build_base = install_cmd.build_base
src = os.path.join(build_base, locales)
dir_util.copy_tree(src, os.path.join(self.install_data, locales))
file_util.copy_file(
"LICENSE",
os.path.join(self.install_data, 'share', 'ghini', 'LICENSE'))
# docs command
DOC_BUILD_PATH = 'doc/.build/'
class docs(Command):
user_options = [('all', 'a', 'rebuild all the docs')]
def initialize_options(self):
self.all = False
def finalize_options(self):
pass
def run(self):
try:
import sphinx
sphinx
except ImportError:
print('Building the docs requires the '\
'Sphinx(http://sphinx.pocoo.org) package')
return
if not os.path.exists(DOC_BUILD_PATH):
dir_util.mkpath(DOC_BUILD_PATH)
cmd = ['sphinx-build', '-b', 'html', 'doc', DOC_BUILD_PATH]
if self.all:
# rebuild all the docs
cmd.insert(1, '-E')
spawn.spawn(cmd)
# clean command
class clean(Command):
user_options = [('all', 'a', 'clean everything'),
]
def initialize_options(self):
self.all = False
def finalize_options(self):
pass
def run(self):
patterns = ['MANIFEST', '*~', '*flymake*', '*.pyc', '*.h']
cwd = os.getcwd()
import fnmatch
for path, subdirs, files in os.walk(cwd):
for pattern in patterns:
matches = fnmatch.filter(files, pattern)
if matches:
def delete(p):
print(('removing %s' % p))
os.remove(p)
map(delete, [os.path.join(path, m) for m in matches])
if os.path.exists('dist'):
dir_util.remove_tree('dist')
if os.path.exists('build'):
dir_util.remove_tree('build')
if os.path.exists(DOC_BUILD_PATH):
dir_util.remove_tree(DOC_BUILD_PATH)
# older eggs, possibly going under previous name
egg_info_dirs = ['ghini.desktop.egg-info', 'bauble.egg-info']
for eid in egg_info_dirs:
if os.path.exists(eid):
dir_util.remove_tree(eid)
# deb_dist - used by stdeb
deb_dist = 'deb_dist'
if os.path.exists(deb_dist):
dir_util.remove_tree(deb_dist)
class run(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
cwd = os.getcwd()
os.system(os.path.join(cwd, 'ghini.sh'))
# require pysqlite if not using python2.5 or greater
needs_sqlite = []
try:
import sqlite3
sqlite3
except ImportError:
needs_sqlite = ["pysqlite>=2.3.2"]
scripts = ["scripts/ghini"]
if sys.platform == 'win32':
scripts = ["scripts/ghini", "scripts/ghini.bat", "scripts/ghini.vbs",
"scripts/ghini-update.bat"]
with open("README.rst", "r") as fh:
long_description = fh.read()
setuptools.setup(name="ghini.desktop",
cmdclass={'build': build, 'install': install,
'py2exe': py2exe_cmd, 'nsis': NsisCmd,
'docs': docs, 'clean': clean, 'run': run},
version=version,
scripts=scripts,
packages=all_packages,
package_dir=all_package_dirs,
package_data=package_data,
data_files=data_files,
install_requires=["SQLAlchemy==1.2.7",
"raven==6.7.0",
"Pillow==2.3.0",
"lxml",
"pyqrcode==1.2.1",
"mako==1.0.7",
"jinja2==2.10",
"gdata-python3==3.0.1",
"requests==2.25.1",
"fibra==0.0.20",
"pyparsing==2.2.0",
'python-dateutil==2.7.3'] + needs_sqlite,
extras_require={'docs': ['sphinx==1.7.9']},
test_suite="nose.collector",
author="Mario Frasca",
author_email="mario@anche.no",
description="Ghini: a biodiversity collection manager",
long_description=long_description,
license="GPLv2+",
keywords="database biodiversity botanic collection "
"botany herbarium arboretum",
url="http://ghini.github.io/",
options=py2exe_options,
**py2exe_setup_args
)