summaryrefslogtreecommitdiff
path: root/setup.py
blob: a05f04068e2e30763584a39da23d2686b1438f43 (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
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
#!/usr/bin/env python
# -*- coding: utf-8; -*-
#
#  GStreamer Inspector - Multimedia system plugin introspection
#
#  Copyright (C) 2007 René Stadler <mail@renestadler.de>
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 3 of the License, or (at your option)
#  any later version.
#
#  This program 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.
#
#  You should have received a copy of the GNU General Public License along with
#  this program.  If not, see <http://www.gnu.org/licenses/>.

"""GStreamer Inspector distutils setup script."""

import sys
import os
import os.path

import distutils.cmd
from distutils.core import setup
from distutils.command.clean import clean
from distutils.command.build import build
from distutils.command.sdist import sdist
from distutils.command.install_scripts import install_scripts
from distutils.errors import *

def perform_substitution (filename, values):

    fp = file (filename, "rt")
    data = fp.read ()
    fp.close ()

    for name, value in values.items ():
        data = data.replace ("$%s$" % (name,), value)

    fp = file (filename, "wt")
    fp.write (data)
    fp.close ()

class tests (distutils.cmd.Command):

    description = "run unit tests"

    user_options = [("files=", "f", "test scripts",)]

    def initialize_options (self):

        self.files = []

    def finalize_options (self):

        from glob import glob

        if self.files:
            self.files = glob (os.path.join (*self.files.split ("/")))
        else:
            self.files = []

    def run (self):

        for filename in self.files:
            self.spawn ([sys.executable, filename])

class clean_custom (clean):

    def remove_file (self, path):

        if os.path.exists (path):
            print "removing '%s'" % (path,)
            if not self.dry_run:
                os.unlink (path)

    def remove_directory (self, path):

        from distutils import dir_util

        if os.path.exists (path):
            dir_util.remove_tree (path, dry_run = self.dry_run)

    def run (self):

        clean.run (self)

        if os.path.exists ("MANIFEST.in"):
            # MANIFEST is generated, get rid of it.
            self.remove_file ("MANIFEST")

        pot_file = os.path.join ("po", "gst-inspector.pot")
        self.remove_file (pot_file)

        self.remove_directory ("build")
        self.remove_directory ("dist")

        for path, dirs, files in os.walk ("."):
            for filename in files:
                if filename.endswith (".pyc") or filename.endswith (".pyo"):
                    file_path = os.path.join (path, filename)
                    self.remove_file (file_path)

class build_custom (build):

    def build_l10n (self):

        return not self.disable_l10n

    sub_commands = build.sub_commands + [("build_l10n", build_l10n,)]
    user_options = build.user_options + [("disable-l10n", None, "disable translations",)]
    boolean_options = build.boolean_options + ["disable-l10n"]

    def initialize_options (self):

        build.initialize_options (self)

        self.disable_l10n = False

class build_l10n (distutils.cmd.Command):

    # Based on code from python-distutils-extra by Sebastian Heinlein.

    description = "gettext framework integration"

    user_options = [("merge-desktop-files=", "m", ".desktop.in files to merge"),
                    ("merge-xml-files=", "x", ".xml.in files to merge"),
                    ("merge-schemas-files=", "s", ".schemas.in files to merge"),
                    ("merge-rfc822deb-files=", "d", "RFC822 files to merge"),
                    ("merge-key-files=", "k", ".key.in files to merge"),
                    ("domain=", "d", "gettext domain"),
                    ("bug-contact=", "c", "contact address for msgid bugs")]

    def initialize_options (self):

        self.merge_desktop_files = []
        self.merge_xml_files = []
        self.merge_key_files = []
        self.merge_schemas_files = []
        self.merge_rfc822deb_files = []
        self.domain = None
        self.bug_contact = None

    def finalize_options (self):

        for attr in ("desktop", "xml", "key", "schemas", "rfc822deb",):
            value = getattr (self, "merge_%s_files" % (attr,))
            if not value:
                value = []
            else:
                value = eval (value)
            setattr (self, "merge_%s_files" % (attr,), value)

        if self.domain is None:
            self.domain = self.distribution.metadata.name

    def run (self):

        from glob import glob

        data_files = self.distribution.data_files

        po_makefile = os.path.join ("po", "Makefile")
        if os.path.exists (po_makefile):
            raise DistutilsFileError ("file %s exists (intltool will pick up "
                                      "values from there)" % (po_makefile,))

        cwd = os.getcwd ()

        if self.bug_contact is not None:
            os.environ["XGETTEXT_ARGS"] = "--msgid-bugs-address=%s" % (self.bug_contact,)
        os.chdir (os.path.join (cwd, "po"))
        # Update .pot file.
        self.spawn (["intltool-update", "-p", "-g", self.domain])
        # Merge new strings into .po files.
        self.spawn (["intltool-update", "-r", "-g", self.domain])

        os.chdir (cwd)

        for po_file in glob (os.path.join ("po", "*.po")):
            lang = os.path.basename (po_file[:-3])
            if lang.startswith ("."):
                # Hidden file, like auto-save data from an editor.
                continue
            mo_dir = os.path.join ("build", "mo", lang, "LC_MESSAGES")
            mo_file = os.path.join (mo_dir, "%s.mo" % (self.domain,))
            self.mkpath (mo_dir)
            self.spawn (["msgfmt", po_file, "-o", mo_file])

            targetpath = os.path.join ("share", "locale", lang, "LC_MESSAGES")
            data_files.append ((targetpath, (mo_file,)))

        for parameter, option in ((self.merge_xml_files, "-x",),
                                  (self.merge_desktop_files, "-d",),
                                  (self.merge_schemas_files, "-s",),
                                  (self.merge_rfc822deb_files, "-r",),
                                  (self.merge_key_files, "-k",),):
            if not parameter:
                continue
            for target, files in parameter:
                build_target = os.path.join ("build", target)
                for file in files:
                    if file.endswith (".in"):
                        file_merged = os.path.basename (file[:-3])
                    else:
                        file_merged = os.path.basename (file)

                self.mkpath (build_target)
                file_merged = os.path.join (build_target, file_merged)
                self.spawn (["intltool-merge", option, "po", file, file_merged])
                data_files.append ((target, [file_merged],))

class distcheck (sdist):

    # Originally based on code from telepathy-python.

    description = "verify self-containedness of source distribution"

    def run (self):

        from distutils import dir_util
        from distutils.spawn import spawn

        # This creates e.g. dist/gst-inspector-0.1.tar.gz.
        sdist.run (self)

        base_dir = self.distribution.get_fullname ()
        distcheck_dir = os.path.join (self.dist_dir, "distcheck")
        self.mkpath (distcheck_dir)
        self.mkpath (os.path.join (distcheck_dir, "again"))

        cwd = os.getcwd ()
        os.chdir (distcheck_dir)

        if os.path.isdir (base_dir):
            dir_util.remove_tree (base_dir)

        # Unpack tarball into dist/distcheck, creating
        # e.g. dist/distcheck/gst-inspector-0.1.
        for archive in self.archive_files:
            if archive.endswith (".tar.gz"):
                archive_rel = os.path.join (os.pardir, os.pardir, archive)
                spawn (["tar", "-xzf", archive_rel, base_dir])
                break
        else:
            raise ValueError ("no supported archives were created")

        os.chdir (cwd)
        os.chdir (os.path.join (distcheck_dir, base_dir))
        spawn ([sys.executable, "setup.py", "sdist", "--formats", "gztar"])

        # Unpack tarball into dist/distcheck/again.
        os.chdir (cwd)
        os.chdir (os.path.join (distcheck_dir, "again"))
        archive_rel = os.path.join (os.pardir, base_dir, "dist", "%s.tar.gz" % (base_dir,))
        spawn (["tar", "-xzf", archive_rel, base_dir])

        os.chdir (cwd)
        os.chdir (os.path.join (distcheck_dir, base_dir))
        spawn ([sys.executable, "setup.py", "clean"])

        os.chdir (cwd)
        spawn (["diff", "-ru",
                os.path.join (distcheck_dir, base_dir),
                os.path.join (distcheck_dir, "again", base_dir)])

        if not self.keep_temp:
            dir_util.remove_tree (distcheck_dir)

class install_scripts_custom (install_scripts):

    user_options = install_scripts.user_options \
                   + [("substitute-files=", None,
                       "files to perform substitution on")]

    def initialize_options (self):

        install_scripts.initialize_options (self)

        self.substitute_files = "[]"

    def run (self):

        from os.path import normpath

        install = self.distribution.get_command_obj ("install")
        install.ensure_finalized ()

        values = {"DATADIR" : install.install_data or "",
                  "PREFIX" : install.home or install.prefix or "",
                  "SCRIPTSDIR" : self.install_dir or ""}

        if install.home:
            values["LIBDIR"] = os.path.normpath (install.install_lib)

        if install.root:
            root = normpath (install.root)
            len_root = len (root)
            for name, value in values.items ():
                if normpath (value).startswith (root):
                    values[name] = normpath (value)[len_root:]

        # Perform installation as normal...
        install_scripts.run (self)

        if self.dry_run:
            return

        # ...then substitute in-place:
        for filename in eval (self.substitute_files):
            perform_substitution (os.path.join (self.install_dir, filename), values)

cmdclass = {"build" : build_custom,
            "clean" : clean_custom,
            "install_scripts" : install_scripts_custom,

            "build_l10n" : build_l10n,
            "distcheck" : distcheck,
            "tests" : tests}

icons = []
for size in ("16x16", "22x22", "24x24", "32x32", "48x48", "scalable",):
    base_source = "data/icons/hicolor/%s/apps/gst-inspector" % (size,)
    sources = []
    for ext in ("png", "svg",):
        source = "%s.%s" % (base_source, ext,)
        # There's no svg for 24x24.
        if os.path.exists (source):
            sources.append (source)
    if sources:
        target = "share/icons/hicolor/%s/apps" % (size,)
        icons.append ((target, sources,))

setup (cmdclass = cmdclass,

       packages = ["GstInspector",
                   "GstInspector.GUI"],
       scripts = ["gst-inspector"],
       data_files = [("share/gst-inspector", ["data/about-dialog.ui",
                                              "data/inspector-window.ui",
                                              "data/menus.ui"],)] + icons,

       name = "gst-inspector",
       version = "0.5",
       description = "GStreamer Inspector -- Multimedia system plugin introspection",
       long_description = """\
GStreamer Inspector is an introspection data viewer for the GStreamer multimedia
framework. Developers of GStreamer applications can use it to find out about the
various elements available in the system.""",
       license = "GNU GPL",
       author = "Rene Stadler",
       author_email = "mail@renestadler.de",
       url = "http://renestadler.de/projects/gst-inspector")