setup.py 7.18 KB
Newer Older
1
# encoding=UTF-8
2

3
# Copyright © 2008-2015 Jakub Wilk <jwilk@jwilk.net>
4
#
5
# This file is part of djvusmooth.
6
#
7 8 9 10 11 12 13 14
# djvusmooth is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# djvusmooth 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.
15 16

'''
17
*djvusmooth* is a graphical editor for `DjVu <http://djvu.org>`_ documents.
18 19
'''

20
from __future__ import with_statement
21 22 23
# Let's keep this __future__ import here, even though Python 2.5 is no longer
# supported, so that people running setup.py against the unsupported version
# get a nice error message instead of SyntaxError.
24

25 26
import glob
import os
27
import re
28 29

import distutils.core
30
import distutils.errors
31
from distutils.command.build import build as distutils_build
32
from distutils.command.clean import clean as distutils_clean
33
from distutils.command.sdist import sdist as distutils_sdist
34

35 36 37 38 39 40 41
try:
    import distutils644
except ImportError:
    pass
else:
    distutils644.install()

42 43 44
from lib import __version__

data_files = []
45

46 47 48 49 50
if os.name == 'posix':
    data_files += [
        (os.path.join('share', 'applications'), ['extra/djvusmooth.desktop'])
    ]

51 52 53 54 55 56 57
class build_mo(distutils_build):

    description = 'build binary message catalogs'

    def run(self):
        if os.name != 'posix':
            return
58
        for poname in glob.iglob(os.path.join('po', '*.po')):
59 60 61 62 63
            lang, _ = os.path.splitext(os.path.basename(poname))
            modir = os.path.join('locale', lang, 'LC_MESSAGES')
            if not os.path.isdir(modir):
                os.makedirs(modir)
            moname = os.path.join(modir, 'djvusmooth.mo')
64
            command = ['msgfmt', '-o', moname, '--verbose', '--check', '--check-accelerators', poname]
65 66
            self.make_file([poname], moname, distutils.spawn.spawn, [command])
            data_files.append((os.path.join('share', modir), [moname]))
67

68 69 70 71 72
class check_po(distutils_build):

    description = 'perform some checks on message catalogs'

    def run(self):
73
        for poname in glob.iglob(os.path.join('po', '*.po')):
74 75 76 77 78 79 80 81 82 83 84 85
            checkname = poname + '.sdist-check'
            with open(checkname, 'w'):
                pass
            try:
                for feature in 'untranslated', 'fuzzy':
                    with open(checkname, 'w'):
                        pass
                    command = ['msgattrib', '--' + feature, '-o', checkname, poname]
                    distutils.spawn.spawn(command)
                    with open(checkname) as file:
                        entries = file.read()
                    if entries:
86
                        raise IOError(None, '{po} has {n} entries'.format(po=poname, n=feature))
87 88 89
            finally:
                os.unlink(checkname)

90 91 92 93
class build_doc(distutils_build):

    description = 'build documentation'

94
    _url_regex = re.compile(
95
        r'^(\\%https?://.*)',
96 97 98 99 100 101 102 103 104 105 106 107 108
        re.MULTILINE
    )

    _date_regex = re.compile(
        '"(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/(?P<year>[0-9]{4})"'
    )

    def build_man(self, manname, commandline):
        self.spawn(commandline)
        with open(manname, 'r+') as file:
            contents = file.read()
            # Format URLs:
            contents = self._url_regex.sub(
109
                lambda m: r'\m[blue]\fI{0}\fR\m[]'.format(*m.groups()),
110 111 112 113
                contents,
            )
            # Use RFC 3339 date format:
            contents = self._date_regex.sub(
114
                lambda m: r'{year}\-{month}\-{day}'.format(**m.groupdict()),
115 116 117 118 119 120
                contents
            )
            file.seek(0)
            file.truncate()
            file.write(contents)

121 122 123
    def run(self):
        if os.name != 'posix':
            return
124
        for xmlname in glob.iglob(os.path.join('doc', '*.xml')):
125 126 127
            manname = os.path.splitext(xmlname)[0] + '.1'
            command = [
                'xsltproc', '--nonet',
128
                '--param', 'man.authors.section.enabled', '0',
129
                '--param', 'man.charmap.use.subset', '0',
130
                '--param', 'man.font.links', '"I"',
131 132 133 134
                '--output', 'doc/',
                'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl',
                xmlname,
            ]
135
            self.make_file([xmlname], manname, self.build_man, [manname, command])
136
            data_files.append(('share/man/man1', [manname]))
137

138 139 140 141
distutils_build.sub_commands[:0] = [
    ('build_doc', None),
    ('build_mo', None)
]
142

143 144 145 146 147 148
class clean(distutils_clean):

    def run(self):
        distutils_clean.run(self)
        if not self.all:
            return
149
        for manname in glob.iglob(os.path.join('doc', '*.1')):
150 151 152
            with open(manname, 'r') as file:
                stamp = file.readline()
            if stamp != sdist.manpage_stamp:
153
                self.execute(os.unlink, [manname], 'removing {0}'.format(manname))
154

155 156
class sdist(distutils_sdist):

157 158
    manpage_stamp = '''.\\" [created by setup.py sdist]\n'''

159 160
    def run(self):
        self.run_command('build_doc')
161 162
        self.run_command('check_po')
        self.run_command('build_mo')
163
        return distutils_sdist.run(self)
164

165 166 167 168 169 170 171 172
    def _rewrite_manpage(self, manname):
        with open(manname, 'r') as file:
            contents = file.read()
        os.unlink(manname)
        with open(manname, 'w') as file:
            file.write(self.manpage_stamp)
            file.write(contents)

173 174 175 176 177 178
    def _maybe_move_file(self, base_dir, src, dst):
        src = os.path.join(base_dir, src)
        dst = os.path.join(base_dir, dst)
        if os.path.exists(src):
            self.move_file(src, dst)

179 180
    def make_release_tree(self, base_dir, files):
        distutils_sdist.make_release_tree(self, base_dir, files)
181
        for manname in glob.iglob(os.path.join(base_dir, 'doc', '*.1')):
182
            self.execute(self._rewrite_manpage, [manname], 'rewriting {0}'.format(manname))
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        self._maybe_move_file(base_dir, 'COPYING', 'doc/COPYING')

classifiers = '''
Development Status :: 4 - Beta
Environment :: X11 Applications :: GTK
Intended Audience :: End Users/Desktop
License :: OSI Approved :: GNU General Public License (GPL)
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 2
Programming Language :: Python :: 2.6
Programming Language :: Python :: 2.7
Topic :: Text Processing
Topic :: Multimedia :: Graphics
'''.strip().splitlines()
198

199
distutils.core.setup(
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    name='djvusmooth',
    version=__version__,
    license='GNU GPL 2',
    description='graphical editor for DjVu',
    long_description=__doc__.strip(),
    classifiers=classifiers,
    url='http://jwilk.net/software/djvusmooth',
    author='Jakub Wilk',
    author_email='jwilk@jwilk.net',
    packages=(
        ['djvusmooth'] +
        ['djvusmooth.{mod}'.format(mod=mod) for mod in ['gui', 'models', 'text']]
    ),
    package_dir=dict(djvusmooth='lib'),
    scripts=['djvusmooth'],
    data_files=data_files,
    cmdclass=dict(
217
        build_doc=build_doc,
218
        build_mo=build_mo,
219 220 221
        check_po=check_po,
        clean=clean,
        sdist=sdist,
222
    ),
223 224
)

225
# vim:ts=4 sts=4 sw=4 et