summaryrefslogtreecommitdiff
path: root/setup.py
blob: e1ed75c6879cd4f6c8a7699cfa497f742773a6c7 (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env python

# pylint: disable=W0201
# Attribute defined outside __init__: custom commands require breaking this

import glob
import fnmatch
import os
import sys
import unittest

from distutils.core import Command, setup
from distutils.command.install import install
from distutils.command.install_egg_info import install_egg_info
from distutils.sysconfig import get_config_var
sysprefix = get_config_var("prefix")

from DistUtilsExtra.auto import sdist_auto
from DistUtilsExtra.command.build_i18n import build_i18n
from DistUtilsExtra.command.build_extra import build_extra
from DistUtilsExtra.command.build_icons import build_icons

from virtcli import cliconfig


def _generate_potfiles_in():
    def find(dirname, ext):
        ret = []
        for root, dirnames, filenames in os.walk(dirname):
            for filename in fnmatch.filter(filenames, ext):
                ret.append(os.path.join(root, filename))
        ret.sort(key=lambda s: s.lower())
        return ret

    scripts = ["virt-manager", "virt-manager-tui", "virt-install",
               "virt-clone", "virt-image", "virt-convert"]

    potfiles = "\n".join(scripts) + "\n\n"
    potfiles += "\n".join(find("virtManager", "*.py")) + "\n\n"
    potfiles += "\n".join(find("virtManagerTui", "*.py")) + "\n\n"
    potfiles += "\n".join(find("virtcli", "*.py")) + "\n\n"
    potfiles += "\n".join(find("virtconv", "*.py")) + "\n\n"
    potfiles += "\n".join(find("virtinst", "*.py")) + "\n\n"

    potfiles += "\n".join(["[type: gettext/glade]" + f for
                          f in find("ui", "*.ui")])

    return potfiles


class my_build_i18n(build_i18n):
    """
    Add our desktop files to the list, saves us having to track setup.cfg
    """
    def finalize_options(self):
        build_i18n.finalize_options(self)

        self.desktop_files = ('[("share/applications",' +
                              ' ("data/virt-manager.desktop.in", ))]')

    def run(self):
        potfiles = _generate_potfiles_in()
        potpath = "po/POTFILES.in"

        try:
            print "Writing %s" % potpath
            file(potpath, "w").write(potfiles)
            build_i18n.run(self)
        finally:
            print "Removing %s" % potpath
            os.unlink(potpath)


class my_build(build_extra):
    """
    Create simple shell wrappers for /usr/bin/ tools to point to /usr/share
    Compile .pod file
    """

    def _make_bin_wrappers(self):
        cmds = ["virt-manager", "virt-install", "virt-clone",
                "virt-image", "virt-convert"]
        if cliconfig.with_tui:
            cmds += ["virt-manager-tui"]

        if not os.path.exists("build"):
            os.mkdir("build")

        for app in cmds:
            sharepath = os.path.join(cliconfig.install_asset_dir, app)

            wrapper = "#!/bin/sh\n\n"
            wrapper += "exec \"%s\" \"$@\"" % (sharepath)

            newpath = os.path.abspath(os.path.join("build", app))
            print "Generating %s" % newpath
            file(newpath, "w").write(wrapper)


    def _make_man_pages(self):
        for path in glob.glob("man/*.pod"):
            base = os.path.basename(path)

            mantype = "1"
            newbase = base
            if base == "virt-image-xml.pod":
                mantype = "5"
                newbase = "virt-image.pod"

            newpath = os.path.join(os.path.dirname(path),
                                os.path.splitext(newbase)[0] + "." + mantype)

            print "Generating %s" % newpath
            ret = os.system('pod2man --release="" '
                            '--center "Virtual Machine Manager" '
                            '< %s > %s' % (path, newpath))
            if ret != 0:
                raise RuntimeError("Generating '%s' failed." % newpath)

        if os.system("grep -IRq 'Hey!' man/") == 0:
            raise RuntimeError("man pages have errors in them! "
                               "(grep for 'Hey!')")


    def run(self):
        self._make_bin_wrappers()
        self._make_man_pages()

        build_extra.run(self)


class my_build_icons(build_icons):
    """
    Fix up build_icon output to put or private icons in share/virt-manager
    """

    def run(self):
        data_files = self.distribution.data_files

        for size in glob.glob(os.path.join(self.icon_dir, "*")):
            for category in glob.glob(os.path.join(size, "*")):
                icons = []
                for icon in glob.glob(os.path.join(category,"*")):
                    if not os.path.islink(icon):
                        icons.append(icon)
                if not icons:
                    continue

                category = os.path.basename(category)
                dest = ("share/icons/hicolor/%s/%s" %
                        (os.path.basename(size), category))
                if category != "apps":
                    dest = dest.replace("share/", "share/virt-manager/")

                data_files.append((dest, icons))


class my_egg_info(install_egg_info):
    """
    Disable egg_info installation, seems pointless for a non-library
    """
    def run(self):
        pass


class my_install(install):
    """
    Error if we weren't 'configure'd with the correct install prefix
    """
    def finalize_options(self):
        if self.prefix is None:
            if cliconfig.prefix != sysprefix:
                print "Using prefix from 'configure': %s" % cliconfig.prefix
                self.prefix = cliconfig.prefix
        elif self.prefix != cliconfig.prefix:
            print ("Install prefix=%s doesn't match configure prefix=%s\n"
                   "Pass matching --prefix to 'setup.py configure'" %
                   (self.prefix, cliconfig.prefix))
            sys.exit(1)

        install.finalize_options(self)


###################
# Custom commands #
###################

class my_rpm(Command):
    user_options = []
    description = "Build a non-binary rpm."

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        """
        Run sdist, then 'rpmbuild' the tar.gz
        """
        self.run_command('sdist')
        os.system('rpmbuild -ta dist/virt-manager-%s.tar.gz' %
                  cliconfig.__version__)


class configure(Command):
    user_options = [
        ("prefix=", None, "installation prefix"),
        ("without-tui", None, "don't install virt-manager-tui"),
        ("qemu-user=", None,
         "user libvirt uses to launch qemu processes (default=root)"),
        ("libvirt-package-names=", None,
         "list of libvirt distro packages virt-manager will check for on "
         "first run. comma separated string (default=none)"),
        ("kvm-package-names=", None,
         "recommended kvm packages virt-manager will check for on first run "
         "(default=none)"),
        ("askpass-package-names=", None,
         "name of your distro's askpass package(s) (default=none)"),
        ("hide-unsupported-rhel-options", None,
         "Hide config bits that are not supported on RHEL (default=no)"),
        ("preferred-distros=", None,
         "Distros to list first in the New VM wizard (default=none)"),
        ("default-graphics=", None,
         "Default graphics type (spice or vnc) (default=vnc)"),

    ]
    description = "Configure the build, similar to ./configure"

    def finalize_options(self):
        pass

    def initialize_options(self):
        self.without_tui = 0
        self.qemu_user = "root"
        self.libvirt_package_names = ""
        self.kvm_package_names = ""
        self.askpass_package_names = ""
        self.hide_unsupported_rhel_options = 0
        self.preferred_distros = ""
        self.default_graphics = "vnc"
        self.prefix = sysprefix


    def run(self):
        template = ""
        template += "[config]\n"
        template += "prefix = %s\n" % self.prefix
        template += "with_tui = %s\n" % int(not self.without_tui)
        template += "default_qemu_user = %s\n" % self.qemu_user
        template += "libvirt_packages = %s\n" % self.libvirt_package_names
        template += "hv_packages = %s\n" % self.kvm_package_names
        template += "askpass_packages = %s\n" % self.askpass_package_names
        template += "preferred_distros = %s\n" % self.preferred_distros
        template += ("hide_unsupported_rhel_options = %s\n" %
                     self.hide_unsupported_rhel_options)
        template += "default_graphics = %s\n" % self.default_graphics

        file(cliconfig.cfgpath, "w").write(template)
        print "Generated %s" % cliconfig.cfgpath


tui_files = [
    ("share/virt-manager/", ["virt-manager-tui"]),

    ("share/virt-manager/virtManagerTui",
     glob.glob("virtManagerTui/*.py")),
    ("share/virt-manager/virtManagerTui/importblacklist",
     glob.glob("virtManagerTui/importblacklist/*.py")),
]
if not cliconfig.with_tui:
    tui_files = []


class TestBaseCommand(Command):
    user_options = [('debug', 'd', 'Show debug output')]
    boolean_options = ['debug']

    def initialize_options(self):
        self.debug = 0
        self._testfiles = []
        self._dir = os.getcwd()

    def finalize_options(self):
        if self.debug and "DEBUG_TESTS" not in os.environ:
            os.environ["DEBUG_TESTS"] = "1"

    def run(self):
        try:
            # Use system 'coverage' if available
            import coverage
            use_coverage = True
        except:
            use_coverage = False

        tests = unittest.TestLoader().loadTestsFromNames(self._testfiles)
        t = unittest.TextTestRunner(verbosity=1)

        if use_coverage:
            coverage.erase()
            coverage.start()

        if hasattr(unittest, "installHandler"):
            try:
                unittest.installHandler()
            except:
                print "installHandler hack failed"

        try:
            result = t.run(tests)
        except KeyboardInterrupt:
            sys.exit(1)

        if use_coverage:
            coverage.stop()

        sys.exit(int(bool(len(result.failures) > 0 or
                          len(result.errors) > 0)))


class TestCommand(TestBaseCommand):
    description = "Runs a quick unit test suite"
    user_options = TestBaseCommand.user_options + \
                   [("testfile=", None, "Specific test file to run (e.g "
                                        "validation, storage, ...)"),
                    ("skipcli", None, "Skip CLI tests")]

    def initialize_options(self):
        TestBaseCommand.initialize_options(self)
        self.testfile = None
        self.skipcli = None

    def finalize_options(self):
        TestBaseCommand.finalize_options(self)

    def run(self):
        '''
        Finds all the tests modules in tests/, and runs them.
        '''
        testfiles = []
        for t in glob.glob(os.path.join(self._dir, 'tests', '*.py')):
            if (t.endswith("__init__.py") or
                t.endswith("urltest.py")):
                continue

            base = os.path.basename(t)
            if self.testfile:
                check = os.path.basename(self.testfile)
                if base != check and base != (check + ".py"):
                    continue
            if self.skipcli and base.count("clitest"):
                continue

            testfiles.append('.'.join(['tests', os.path.splitext(base)[0]]))

        if not testfiles:
            raise RuntimeError("--testfile didn't catch anything")

        self._testfiles = testfiles
        TestBaseCommand.run(self)


class TestURLFetch(TestBaseCommand):
    description = "Test fetching kernels and isos from various distro trees"

    user_options = TestBaseCommand.user_options + \
                   [("match=", None, "Regular expression of dist names to "
                                     "match [default: '.*']"),
                    ("path=", None, "Paths to local iso or directory or check"
                                    " for installable distro. Comma separated")]

    def initialize_options(self):
        TestBaseCommand.initialize_options(self)
        self.match = None
        self.path = ""

    def finalize_options(self):
        TestBaseCommand.finalize_options(self)
        if self.match is None:
            self.match = ".*"

        origpath = str(self.path)
        if not origpath:
            self.path = []
        else:
            self.path = origpath.split(",")

    def run(self):
        import tests
        self._testfiles = ["tests.urltest"]
        tests.urltest.MATCH_FILTER = self.match
        if self.path:
            for p in self.path:
                tests.urltest.LOCAL_MEDIA.append(p)
        TestBaseCommand.run(self)


class CheckPylint(Command):
    user_options = []
    description = "Check code using pylint and pep8"

    def initialize_options(self):
        pass
    def finalize_options(self):
        pass

    def run(self):
        files = ["setup.py", "virt-install", "virt-clone", "virt-image",
                 "virt-convert", "virt-manager", "virt-manager-tui",
                 "virtcli", "virtinst", "virtconv", "virtManager",
                 "virtManagerTui", "tests"]

        output_format = sys.stdout.isatty() and "colorized" or "text"
        pylint_skip = [
            "Design",   # Things like 'too many arguments'
            "C0103",    # C0103: Name doesn't match some style regex
            "C0111",    # C0111: No docstring
            "C0301",    # C0301: Line too long
            "C0302",    # C0302: Too many lines in module
            "I0011",    # I0011: Warn about locally disabled pylint msgs
            "R0201",    # R0201: Method could be a function

            "W0142",    # W0142: *Used * or ** magic*
            "W0603",    # W0603: Using the global statement
            "W0702",    # W0702: No exception type specified for 'catch'
            "W0703",    # W0703: Catch 'Exception'


            # May be useful to enable someday
            "W1001",         # Use of 'property' on old style class
                             # pylint can't detect our Gtk subclasses are
                             # new style
            "W0511",         # W0511: FIXME and XXX: messages
            "Similarities",  # Finds duplicate code
        ]

        cmd = "pylint "
        cmd += "--ignore scriptimports "
        cmd += "--reports=n "
        cmd += "--output-format=%s " % output_format
        cmd += "--dummy-variables-rgx=\"ignore.*|.*_ignore\" "
        cmd += "--additional-builtins=_  "

        for s in pylint_skip:
            cmd += "--disable=%s " % s
        cmd += " ".join(files)

        os.system(cmd)


setup(
    name = "virt-manager",
    version = cliconfig.__version__,
    author = "Cole Robinson",
    author_email = "virt-tools-list@redhat.com",
    url = "http://virt-manager.org",
    license = "GPLv2+",

    # These wrappers are generated in our custom build command
    scripts = ([
        "build/virt-manager",
        "build/virt-clone",
        "build/virt-install",
        "build/virt-image",
        "build/virt-convert"] +
        (cliconfig.with_tui and ["build/virt-manager-tui"] or [])),

    data_files = [
        ("share/virt-manager/", [
            "virt-manager",
            "virt-install",
            "virt-clone",
            "virt-image",
            "virt-convert",
        ]),
        ("/etc/gconf/schemas", ["data/virt-manager.schemas"]),
        ("share/virt-manager/ui", glob.glob("ui/*.ui")),

        ("share/man/man1", [
            "man/virt-manager.1",
            "man/virt-install.1",
            "man/virt-clone.1",
            "man/virt-image.1",
            "man/virt-convert.1"
        ]),
        ("share/man/man5", ["man/virt-image.5"]),

        ("share/virt-manager/virtManager", glob.glob("virtManager/*.py")),

        ("share/virt-manager/virtcli",
         glob.glob("virtcli/*.py") + ["virtcli/cli.cfg"]),
        ("share/virt-manager/virtinst", glob.glob("virtinst/*.py")),
        ("share/virt-manager/virtconv", glob.glob("virtconv/*.py")),
        ("share/virt-manager/virtconv/parsers",
         glob.glob("virtconv/parsers/*.py")),
    ] + tui_files,

    cmdclass = {
        'build': my_build,
        'build_i18n': my_build_i18n,
        'build_icons': my_build_icons,

        'sdist': sdist_auto,
        'install': my_install,
        'install_egg_info': my_egg_info,

        'configure': configure,

        'pylint': CheckPylint,
        'rpm': my_rpm,
        'test': TestCommand,
        'test_urls' : TestURLFetch,
    }
)