1#!/usr/bin/env python3
2# encoding: utf-8
3#!/usr/bin/env python
4
5from distutils.core import setup
6from distutils.command.install import install
7
8import os
9import sys
10import subprocess
11
12
13def read_version():
14    with open('../.version', 'r') as handle:
15        version_string = handle.read()
16
17    return version_string.strip()
18
19
20def get_prefix():
21    # distutils apparently has no sane way to get the install prefix early.
22    # (I realize that this is a stupid hack to do something obvious)
23    for idx, arg in enumerate(sys.argv):
24        if arg == '--user':
25            return os.path.expanduser('~/.local')
26
27        if arg.startswith('--prefix'):
28            if '=' in arg:
29                _, path = arg.split('=', 1)
30                return path
31            else:
32                return sys.argv[idx + 1]
33
34    return '/usr'
35
36
37PREFIX = get_prefix()
38
39
40class PrePlusPostInstall(install):
41    def run(self):
42        # Compile the resource bundle freshly
43        print('==> Compiling resource bundle')
44
45        if os.access('shredder/resources/shredder.gresource', os.R_OK):
46            print('==> Using existing. Lucky boy.')
47        else:
48            print('==> Calling glib-compile-resources')
49            try:
50                subprocess.call([
51                    'glib-compile-resources',
52                    'shredder/resources/shredder.gresource.xml',
53                    '--sourcedir',
54                    'shredder/resources'
55                ])
56            except subprocess.CalledProcessError as err:
57                print('==> Failed :(')
58
59        # Run the usual distutils install routine:
60        install.run(self)
61
62        # Make sure the schema file is updated.
63        # Otherwise the gui will trace trap.
64        print('==> Compiling GLib Schema files')
65
66        try:
67            subprocess.call([
68                'glib-compile-schemas',
69                os.path.join(PREFIX, 'share/glib-2.0/schemas')
70            ])
71        except subprocess.CalledProcessError as err:
72            print('==> Could not update schemas: ', err)
73            print('==> Please run the following manually:\n')
74            print('    sudo glib-compile-schemas {prefix}'.format(
75                prefix=os.path.join(PREFIX, 'share/glib-2.0/schemas')
76            ))
77        else:
78            print('==> OK!')
79
80
81setup(
82    name='Shredder',
83    version=read_version(),
84    description='A gui frontend to rmlint',
85    long_description='A graphical user interface to rmlint using GTK+',
86    author='Christopher Pahl',
87    author_email='sahib@online.de',
88    url='https://rmlint.rtfd.org',
89    license='GPLv3',
90    platforms='any',
91    cmdclass={'install': PrePlusPostInstall},
92    packages=['shredder', 'shredder.views'],
93    package_data={'': [
94        'resources/*.gresource'
95    ]},
96    data_files=[
97        (
98            os.path.join(PREFIX, 'share/icons/hicolor/scalable/apps'),
99            ['shredder/resources/shredder.svg']
100        ),
101        (
102            os.path.join(PREFIX, 'share/glib-2.0/schemas'),
103            ['shredder/resources/org.gnome.Shredder.gschema.xml']
104        ),
105        (
106            os.path.join(PREFIX, 'share/applications'),
107            ['shredder.desktop']
108        ),
109    ]
110)
111