1#! /usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# Copyright(C) 2010-2014 Christophe Benz, Laurent Bachelier
5#
6# This file is part of weboob.
7#
8# weboob is free software: you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# weboob is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU Lesser General Public License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public License
19# along with weboob. If not, see <http://www.gnu.org/licenses/>.
20
21from __future__ import print_function
22
23import glob
24import os
25import subprocess
26import sys
27from distutils.cmd import Command
28from distutils.log import WARN
29
30from setuptools import find_packages, setup
31
32
33PY3 = sys.version_info.major >= 3
34
35
36class BuildQt(Command):
37    description = 'build Qt applications'
38    user_options = []
39
40    def initialize_options(self):
41        pass
42
43    def finalize_options(self):
44        pass
45
46    def run(self):
47        self.announce('Building Qt applications...', WARN)
48        make = self.find_executable('make', ('gmake', 'make'))
49        if not PY3:
50            pyuic5 = self.find_executable(
51                'pyuic5',
52                ('python2-pyuic5', 'pyuic5-python2.7', 'pyuic5'))
53        else:
54            pyuic5 = self.find_executable(
55                'pyuic5',
56                ('python3-pyuic5', 'pyuic5-python3.7', 'pyuic5-python3.6', 'pyuic5-python3.5', 'pyuic5'))
57        if not pyuic5 or not make:
58            print('Install missing component(s) (see above) or disable Qt applications (with --no-qt).',
59                  file=sys.stderr)
60            sys.exit(1)
61
62        subprocess.check_call(
63            [make,
64             '-f', 'build.mk',
65             '-s', '-j2',
66             'all',
67             'PYUIC=%s%s' % (pyuic5, ' WIN32=1' if sys.platform == 'win32' else '')])
68
69    @staticmethod
70    def find_executable(name, names):
71        envname = '%s_EXECUTABLE' % name.upper()
72        if os.getenv(envname):
73            return os.getenv(envname)
74        paths = os.getenv('PATH', os.defpath).split(os.pathsep)
75        exts = os.getenv('PATHEXT', os.pathsep).split(os.pathsep)
76        for name in names:
77            for path in paths:
78                for ext in exts:
79                    fpath = os.path.join(path, name) + ext
80                    if os.path.exists(fpath) and os.access(fpath, os.X_OK):
81                        return fpath
82        print('Could not find executable: %s' % name, file=sys.stderr)
83
84
85def install_weboob():
86    data_files = [
87        ('man/man1', glob.glob('man/*')),
88    ]
89    data_files.extend([
90        ('share/applications', glob.glob('desktop/*')),
91        ('share/icons/hicolor/64x64/apps', glob.glob('icons/*')),
92    ])
93
94    requirements = [
95        'weboob',
96        'PyQt5',
97    ]
98
99    try:
100        if sys.argv[1] == 'requirements':
101            print('\n'.join(requirements))
102            sys.exit(0)
103    except IndexError:
104        pass
105
106    setup(
107        packages=find_packages(),
108        data_files=data_files,
109        cmdclass={
110            'build_qt': BuildQt,
111        },
112    )
113
114
115if os.getenv('WEBOOB_SETUP'):
116    args = os.getenv('WEBOOB_SETUP').split()
117else:
118    args = sys.argv[1:]
119
120args.insert(0, 'build_qt')
121
122sys.argv = [sys.argv[0]] + args
123
124install_weboob()
125