forked from mypaint/mypaint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
600 lines (488 loc) · 17.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# This file is part of MyPaint.
# Imports:
from __future__ import print_function
import subprocess
import glob
import os
import os.path
import sys
import textwrap
import tempfile
import shutil
from distutils.command.build import build
from distutils.command.clean import clean
from setuptools import setup
from setuptools import Extension
from setuptools import Command
from setuptools.command.build_ext import build_ext
from setuptools.command.install_scripts import install_scripts
# Helper classes and routines:
class BuildTranslations (Command):
"""Builds binary message catalogs for installation.
This is declared as a subcommand of "build", but it can be invoked
in its own right. The generated message catalogs are later installed
as data files.
"""
description = "build binary message catalogs (*.mo)"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
build = self.get_finalized_command("build")
for src in glob.glob("po/*.po"):
data_files = self._compile_message_catalog(src, build.build_temp)
if not self.dry_run:
self.distribution.data_files.extend(data_files)
def _compile_message_catalog(self, src, temp):
lang = os.path.basename(src)[:-3]
targ_dir = os.path.join(temp, "locale", lang, "LC_MESSAGES")
targ = os.path.join(targ_dir, "mypaint.mo")
install_dir = os.path.join("locale", lang, "LC_MESSAGES")
needs_update = True
if os.path.exists(targ):
if os.stat(targ).st_mtime >= os.stat(src).st_mtime:
needs_update = False
if needs_update:
cmd = ("msgfmt", src, "-o", targ)
if self.dry_run:
self.announce("would run %s" % (" ".join(cmd),), level=2)
return []
self.announce("running %s" % (" ".join(cmd),), level=2)
self.mkpath(targ_dir)
subprocess.check_call(cmd)
assert os.path.exists(targ)
return [(install_dir, [targ])]
class Build (build):
"""Custom build (build_ext 1st for swig, run build_translations)
distutils.command.build.build doesn't generate the extension.py for
an _extension.so, unless the build_ext is done first or you install
twice. The fix is to do the build_ext subcommand before build_py.
In our case, swig runs first. Still needed as of Python 2.7.13.
Fix adapted from https://stackoverflow.com/questions/17666018>.
This build also ensures that build_translations is run.
"""
sub_commands = (
[(a, b) for (a, b) in build.sub_commands if a == 'build_ext'] +
[(a, b) for (a, b) in build.sub_commands if a != 'build_ext'] +
[("build_translations", None)]
)
class BuildExt (build_ext):
"""Custom build_ext (extra --debug flags)."""
def build_extension(self, ext):
ccflags = ext.extra_compile_args
linkflags = ext.extra_link_args
if self.debug:
for flag in ["-DNDEBUG"]:
if flag in ccflags:
ccflags.remove(flag)
ccflags.extend([
"-O0",
"-g",
"-DHEAVY_DEBUG",
])
linkflags.extend([
"-O0",
])
else:
linkflags.append("-O3")
ccflags.append("-O3")
return build_ext.build_extension(self, ext)
class Clean (clean):
"""Custom clean: also remove swig-generated wrappers.
distutils's clean has always left these lying around in the source,
and they're a perpetual trip hazard when sharing the same source
tree with a Windows VM.
"""
def run(self):
build_temp_files = glob.glob("lib/mypaintlib_wrap.c*")
for file in build_temp_files:
self.announce("removing %r" % (file,), level=2)
os.unlink(file)
return clean.run(self)
class Demo (Command):
"""Builds, then do a test run from a temporary install tree"""
description = "[MyPaint] build, install, and run in a throwaway folder"
user_options = [
("temp-root=", None,
"parent dir (retained) for the demo install (deleted)"),
]
def initialize_options(self):
self.temp_root = None
def finalize_options(self):
pass
def run(self):
if self.dry_run:
self.announce(
"The demo command can't do anything in dry-run mode",
level=2,
)
return
build = self.get_finalized_command("build")
build.run()
build_scripts = self.get_finalized_command("build_scripts")
demo_cmd = [build_scripts.executable]
temp_dir = tempfile.mkdtemp(
prefix="demo-",
suffix="",
dir=self.temp_root,
)
try:
inst = self.distribution.get_command_obj("install", 1)
inst.root = temp_dir
inst.prefix = ""
inst.ensure_finalized()
inst.run()
script_path = None
potential_script_names = ["mypaint.py", "mypaint"]
for s in potential_script_names:
p = os.path.join(inst.install_scripts, s)
if os.path.exists(p):
script_path = p
break
if not script_path:
raise RuntimeError(
"Cannot locate installed script. "
"Tried all of %r in %r."
% (potential_script_names, inst.install_scripts)
)
config_dir = os.path.join(temp_dir, "_config")
demo_cmd.extend([script_path, "-c", config_dir])
self.announce("Demo: running %r..." % (demo_cmd,), level=2)
subprocess.check_call(demo_cmd)
except:
raise
finally:
self.announce("Demo: cleaning up %r..." % (temp_dir,), level=2)
shutil.rmtree(temp_dir, ignore_errors=True)
class InstallScripts (install_scripts):
"""Install scripts with ".py" suffix removal and version headers.
Bakes version information into each installed script.
The .py suffix is also removed on most platforms we support.
"""
def run(self):
if not self.skip_build:
self.run_command('build_scripts')
sys.path.insert(0, ".")
import lib.meta
relinfo_script = lib.meta._get_release_info_script(gitprefix="git")
header_tmpl = textwrap.dedent("""
#
# ***DO NOT EDIT THIS FILE***: edit {source} instead.
#
# Auto-generated version info follows.
{relinfo_script}
""")
self.mkpath(self.install_dir)
self.outfiles = []
src_patt = os.path.join(self.build_dir, "*")
for src in glob.glob(src_patt):
header = header_tmpl.format(
relinfo_script=relinfo_script,
source=os.path.basename(src),
)
outfiles = self._install_script(src, header)
self.outfiles.extend(outfiles)
def _install_script(self, src, header):
strip_ext = True
set_mode = False
if sys.platform == "win32":
if "MSYSTEM" not in os.environ: # and not MSYS2
strip_ext = False
targ_basename = os.path.basename(src)
if strip_ext and targ_basename.endswith(".py"):
targ_basename = targ_basename[:-3]
targ = os.path.join(self.install_dir, targ_basename)
self.announce("installing %s as %s" % (src, targ_basename), level=2)
if self.dry_run:
return []
with open(src, "rU") as in_fp:
with open(targ, "w") as out_fp:
line = in_fp.readline().rstrip()
if line.startswith("#!"):
print(line, file=out_fp)
print(header, file=out_fp)
if os.name == 'posix':
set_mode = True
else:
print(header, file=out_fp)
print(line, file=out_fp)
for line in in_fp.readlines():
line = line.rstrip()
print(line, file=out_fp)
if set_mode:
mode = ((os.stat(targ).st_mode) | 0o555) & 0o7777
self.announce("changing mode of %s to %o" % (targ, mode), level=2)
os.chmod(targ, mode)
return [targ]
class _ManagedInstBase (Command):
"""Base for commands that work on managed installs."""
MANAGED_FILES_LIST = "managed_files.txt"
# Common options:
user_options = [
("prefix=", None, "POSIX-style install prefix, e.g. /usr"),
]
def initialize_options(self):
self.prefix = "/usr/local"
def finalize_options(self):
self.prefix = os.path.abspath(self.prefix)
# Utility methods for subclasses:
def is_installed(self):
"""True if an installation is present at the prefix."""
inst = self.distribution.get_command_obj("install", 1)
inst.root = "/"
inst.prefix = self.prefix
inst.ensure_finalized()
inst.record = os.path.join(inst.install_lib, self.MANAGED_FILES_LIST)
return os.path.isfile(inst.record)
def uninstall(self):
"""Uninstalls an existing install."""
self.announce(
"Uninstalling from %r..." % (self.prefix,),
level=2,
)
inst = self.distribution.get_command_obj("install", 1)
inst.root = "/"
inst.prefix = self.prefix
inst.ensure_finalized()
inst.record = os.path.join(inst.install_lib, self.MANAGED_FILES_LIST)
self.announce(
"Using the installation record in %r" % (inst.record,),
level=2,
)
with open(inst.record, "r") as fp:
for path_rel in fp:
path_rel = path_rel.rstrip("\r\n")
path = os.path.join("/", path_rel)
self.rm(path)
self.rm(inst.record)
# Remove the trees that are entirely MyPaint's too
# See setup.cfg's [install] and get_data_files()
mypaint_data_trees = [
inst.install_lib,
os.path.join(inst.install_data, "mypaint"),
]
for path in mypaint_data_trees:
path = os.path.normpath(path)
assert os.path.basename(path) == "mypaint", \
"path %r does not end in mypaint" % (path,)
self.rmtree(path)
def rmtree(self, path):
"""Remove a tree recursively."""
self.announce("recursively removing %r" % (path,), level=2)
if not self.dry_run:
shutil.rmtree(path, ignore_errors=True)
def rm(self, path):
"""Remove a single file."""
self.announce("removing %r" % (path,), level=2)
if not self.dry_run:
if os.path.isfile(path):
os.unlink(path)
elif os.path.exists(path):
raise RuntimeError(
"ERROR: %r exists, but it is no longer a file"
% (path,)
)
else:
self.announce("remove: %r is already gone" % (path,), level=1)
class ManagedInstall (_ManagedInstBase):
"""Simplified "install" with a list of installed files.
This command and ManagedUninstall are temporary hacks which we have
to use because `pip {install,uninstall}` doen't work yet. Once we
have proper namespacing (prefixed `mypaint.{lib,gui}` modules), we
may be able to drop these commands and use standard ones.
"""
description = "[MyPaint] like install, but allow managed_uninstall"
def run(self):
if self.is_installed():
self.announce("Already installed, uninstalling first...", level=2)
self.uninstall()
self.announce(
"Installing (manageably) to %r" % (self.prefix,),
level=2,
)
inst = self.distribution.get_command_obj("install", 1)
inst.root = "/"
inst.prefix = self.prefix
inst.ensure_finalized()
inst.record = os.path.join(inst.install_lib, self.MANAGED_FILES_LIST)
self.announce(
"Record will be saved to %r" % (inst.record,),
level=2,
)
if not self.dry_run:
inst.run()
assert os.path.isfile(inst.record)
class ManagedUninstall (_ManagedInstBase):
"""Removes the files created by ManagedInstall."""
description = "[MyPaint] remove files that managed_install wrote"
user_options = [
("prefix=", None, "same as used in your earlier managed_install"),
]
def initialize_options(self):
self.prefix = "/usr/local"
def finalize_options(self):
self.prefix = os.path.abspath(self.prefix)
def run(self):
self.uninstall()
def uniq(items):
"""Order-preserving uniq()"""
seen = set()
result = []
for i in items:
if i in seen:
continue
seen.add(i)
result.append(i)
return result
def pkgconfig(packages, **kwopts):
"""Runs pkgconfig to update its args.
Also returns the modified args dict. Recipe adapted from
http://code.activestate.com/recipes/502261/
"""
flag_map = {
'-I': 'include_dirs',
'-L': 'library_dirs',
'-l': 'libraries',
}
extra_args_map = {
"--libs": "extra_link_args",
"--cflags": "extra_compile_args",
}
for (pc_arg, extras_key) in extra_args_map.items():
cmd = ["pkg-config", pc_arg] + list(packages)
for conf_arg in subprocess.check_output(cmd).split():
flag = conf_arg[:2]
flag_value = conf_arg[2:]
flag_key = flag_map.get(flag)
if flag_key:
kw = flag_key
val = flag_value
else:
kw = extras_key
val = conf_arg
kwopts.setdefault(kw, []).append(val)
for kw, val in list(kwopts.items()):
kwopts[kw] = uniq(val)
return kwopts
def get_ext_modules():
"""Return a list of binary Extensions for setup() to process."""
import numpy
extra_compile_args = [
'-Wall',
'-Wno-sign-compare',
'-Wno-write-strings',
'-D_POSIX_C_SOURCE=200809L',
"-DNO_TESTS", # FIXME: we're building against shared libmypaint now
'-g', # always include symbols, for profiling
]
extra_link_args = []
if sys.platform != "darwin":
extra_link_args.append("-fopenmp")
extra_compile_args.append("-fopenmp")
if sys.platform == "darwin":
pass
elif sys.platform == "win32":
pass
elif sys.platform == "msys":
pass
elif sys.platform == "linux2":
# Look up libraries dependencies relative to the library.
extra_link_args.append('-Wl,-z,origin')
extra_link_args.append('-Wl,-rpath,$ORIGIN')
mypaintlib_opts = pkgconfig(
packages=[
"pygobject-3.0",
"glib-2.0",
"libpng",
"lcms2",
"gtk+-3.0",
"libmypaint",
],
include_dirs=[
numpy.get_include(),
],
extra_link_args=extra_link_args,
extra_compile_args=extra_compile_args,
)
mypaintlib_swig_opts = ['-Wall', '-noproxydel', '-c++']
mypaintlib_swig_opts.extend([
"-I" + d
for d in mypaintlib_opts["include_dirs"]
])
# FIXME: building against the new shared lib, omit old test code
mypaintlib_swig_opts.extend(['-DNO_TESTS'])
mypaintlib = Extension(
'lib._mypaintlib',
[
'lib/mypaintlib.i',
'lib/fill.cpp',
'lib/gdkpixbuf2numpy.cpp',
'lib/pixops.cpp',
'lib/fastpng.cpp',
'lib/brushsettings.cpp',
],
swig_opts=mypaintlib_swig_opts,
language='c++',
**mypaintlib_opts
)
return [mypaintlib]
def get_data_files():
"""Return a list of data_files entries for setup() to process."""
# Target paths are relative to $base/share, assuming setup.py's
# default value for install-data.
data_files = [
# TARGDIR, SRCFILES
("appdata", ["desktop/mypaint.appdata.xml"]),
("applications", ["desktop/mypaint.desktop"]),
("thumbnailers", ["desktop/mypaint-ora.thumbnailer"]),
("mypaint/brushes", ["brushes/order.conf"]),
]
# Paths which can only derived from globbing the source tree.
data_file_patts = [
# SRCDIR, SRCPATT, TARGDIR
("desktop/icons", "hicolor/*/*/*", "icons"),
("backgrounds", "*.*", "mypaint/backgrounds"),
("backgrounds", "*/*.*", "mypaint/backgrounds"),
("brushes", "*/*.*", "mypaint/brushes"),
("palettes", "*.gpl", "mypaint/palettes"),
("pixmaps", "*.png", "mypaint/pixmaps"),
]
for (src_pfx, src_patt, targ_pfx) in data_file_patts:
for src_file in glob.glob(os.path.join(src_pfx, src_patt)):
file_rel = os.path.relpath(src_file, src_pfx)
targ_dir = os.path.join(targ_pfx, os.path.dirname(file_rel))
data_files.append((targ_dir, [src_file]))
return data_files
# Setup script "main()":
setup(
name='MyPaint',
version='1.3.0a0',
description='Simple painting program for use with graphics tablets.',
author='Andrew Chadwick',
author_email='a.t.chadwick@gmail.com',
license="GPLv2+",
url="http://mypaint.org",
packages=['lib', 'lib.layer', 'gui', 'gui.colors'],
package_data={
"gui": ['*.xml', '*.glade'],
},
data_files=get_data_files(),
cmdclass={
"build": Build,
"build_ext": BuildExt,
"build_translations": BuildTranslations,
"demo": Demo,
"managed_install": ManagedInstall,
"managed_uninstall": ManagedUninstall,
"install_scripts": InstallScripts,
"clean": Clean,
},
scripts=[
"mypaint.py",
"desktop/mypaint-ora-thumbnailer.py",
],
test_suite='tests',
ext_modules=get_ext_modules(),
)