Welcome to mirror list, hosted at ThFree Co, Russian Federation.

setup.py - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5698bc6baec25d000b710fdbad390d263d20469f (plain)
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
#!/usr/bin/env python3

from __future__ import annotations

import os
import sys

if sys.version_info < (3, 9):
    sys.exit('Gajim needs Python 3.9+')

import subprocess
from pathlib import Path

from setuptools import setup
from setuptools.command.build_py import build_py as _build
from setuptools.command.install import install as _install


MAN_FILES = [
    'gajim.1',
    'gajim-remote.1'
]
META_FILES = [
    ('data/org.gajim.Gajim.desktop', 'share/applications', '--desktop'),
    ('data/org.gajim.Gajim.appdata.xml', 'share/metainfo', '--xml')]


TRANS_DIR = Path('po')
TRANS_TEMPLATE = TRANS_DIR / 'gajim.pot'
REPO_DIR = Path(__file__).resolve().parent
BUILD_DIR = REPO_DIR / 'build'

ALL_LINGUAS = sorted([lang.stem for lang in TRANS_DIR.glob('*.po')])


def newer(source: Path, target: Path) -> bool:
    if not source.exists():
        raise ValueError("file '%s' does not exist" % source.resolve())
    if not target.exists():
        return True

    from stat import ST_MTIME
    mtime1 = source.stat()[ST_MTIME]
    mtime2 = target.stat()[ST_MTIME]

    return mtime1 > mtime2


def build_translation() -> None:
    for lang in ALL_LINGUAS:
        po_file = TRANS_DIR / f'{lang}.po'
        mo_file = BUILD_DIR / 'mo' / lang / 'LC_MESSAGES' / 'gajim.mo'
        mo_dir = mo_file.parent
        if not (mo_dir.is_dir() or mo_dir.is_symlink()):
            mo_dir.mkdir(parents=True)

        if newer(po_file, mo_file):
            subprocess.run(['msgfmt',
                            str(po_file),
                            '-o',
                            str(mo_file)],
                           cwd=REPO_DIR,
                           check=True)

            print('Compiling %s >> %s', po_file, mo_file)


def install_trans(data_files) -> None:
    for lang in ALL_LINGUAS:
        mo_file = str(BUILD_DIR / 'mo' / lang / 'LC_MESSAGES' / 'gajim.mo')
        target = f'share/locale/{lang}/LC_MESSAGES'
        data_files.append((target, [mo_file]))


def build_man() -> None:
    '''
    Compress Gajim manual files
    '''
    newdir = BUILD_DIR / 'man'
    if not (newdir.is_dir() or newdir.is_symlink()):
        newdir.mkdir()

    for man in MAN_FILES:
        filename = Path('data') / man
        man_file_gz = newdir / (man + '.gz')
        if man_file_gz.exists():
            if newer(filename, man_file_gz):
                man_file_gz.unlink()
            else:
                continue

        import gzip
        # Binary io, so open is OK
        with open(filename, 'rb') as f_in,\
                gzip.open(man_file_gz, 'wb') as f_out:
            f_out.writelines(f_in)
            print('Compiling %s >> %s', filename, man_file_gz)


def install_man(data_files) -> None:
    man_dir = BUILD_DIR / 'man'
    target = 'share/man/man1'

    for man in MAN_FILES:
        man_file_gz = str(man_dir / (man + '.gz'))
        data_files.append((target, [man_file_gz]))


def build_intl() -> None:
    '''
    Merge translation files into desktop and mime files
    '''
    base = BUILD_DIR

    for filename, _, option in META_FILES:
        newfile = base / filename
        newdir = newfile.parent
        if not(newdir.is_dir() or newdir.is_symlink()):
            newdir.mkdir()
        merge(Path(filename + '.in'), newfile, option)


def install_intl(data_files) -> None:
    for filename, target, _ in META_FILES:
        data_files.append((target, [str(BUILD_DIR / filename)]))


def merge(in_file, out_file, option, po_dir: str = 'po') -> None:
    '''
    Run the msgfmt command.
    '''
    if in_file.exists():
        cmd = (('msgfmt %(opt)s -d %(po_dir)s --template %(in_file)s '
                '-o %(out_file)s') %
               {'opt': option,
                'po_dir': po_dir,
                'in_file': in_file,
                'out_file': out_file})
        if os.system(cmd) != 0:
            msg = ('ERROR: %s was not merged into the translation files!\n' %
                   out_file)
            raise SystemExit(msg)
        print('Compiling %s >> %s', in_file, out_file)


class build(_build):
    def run(self):
        build_translation()
        if sys.platform != 'win32':
            build_man()
            build_intl()
        _build.run(self)


class install(_install):
    def run(self):
        data_files = self.distribution.data_files
        install_trans(data_files)
        if sys.platform != 'win32':
            install_man(data_files)
            install_intl(data_files)
        _install.run(self)


# only install subdirectories of data
data_files_app_icon = [
    ("share/icons/hicolor/scalable/apps",
     ["gajim/data/icons/hicolor/scalable/apps/org.gajim.Gajim.svg"]),
    ("share/icons/hicolor/scalable/apps",
     ["gajim/data/icons/hicolor/scalable/apps/org.gajim.Gajim-symbolic.svg"])
]

data_files = data_files_app_icon

setup(
    cmdclass={
        'build_py': build,
        'install': install,
    },
    entry_points={
        'console_scripts': [
            'gajim-remote = gajim.gajim_remote:main',
        ],
        'gui_scripts': [
            'gajim = gajim.gajim:main',
        ]
    },
    data_files=data_files
)