Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/scripts/check.py
blob: 20548344b28e93507ca28a91562229aae773eea0 (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
import os
import sys
from optparse import make_option
import random
import signal
import subprocess
import time

from jhbuild.commands import Command, register_command

scripts_dir = os.path.dirname(__file__)
data_dir = os.path.join(scripts_dir, 'data')
pylintrc_path = os.path.join(data_dir, 'pylintrc')

def check_display(display):
    result = subprocess.call(['xdpyinfo', '-display', ':%d' % display],
                             stdout=open(os.devnull, "w"),
                             stderr=open(os.devnull, "w"))
    return result == 0

class UICheck(object):
    def start(self):
        self._mainloop = gobject.MainLoop()
        self._exit_code = 0

        self._start_shell()

        gobject.idle_add(self._start_ui_check_cb)

        self._mainloop.run()

        return self._exit_code

    def _start_shell(self):
        print 'Launch the shell.'

        args = ['dbus-launch', '--exit-with-session', 'sugar-shell']
        shell_process = subprocess.Popen(args)

    def _ui_check_exit_cb(self, pid, condition):
        self._exit_code = os.WEXITSTATUS(condition)
        self._mainloop.quit()

    def _ui_check_read_cb(self, fd, condition):
        sys.stdout.write(os.read(fd, 1024))
        return True

    def _start_ui_check_cb(self):
        print 'Launch UI check.'

        pid, stdin, stdout, stderr = gobject.spawn_async(
            ['sugar-ui-check'], standard_output=True, standard_error=True,
            flags=gobject.SPAWN_SEARCH_PATH | gobject.SPAWN_DO_NOT_REAP_CHILD)

        gobject.io_add_watch(stdout, gobject.IO_IN, self._ui_check_read_cb)
        gobject.io_add_watch(stderr, gobject.IO_IN, self._ui_check_read_cb)

        gobject.child_watch_add(pid, self._ui_check_exit_cb)

class cmd_check(Command):

    name = 'check'
    usage_args = ''

    def __init__(self):
        Command.__init__(self, [
            make_option('-t', '--type', action='store',
                        dest='type', default=None,
                        help='specify the check type')
            ])

    def start_xvfb(self):
        result = None

        for port in range(100, 1000):
            if not check_display(port):
                try:
                    p = subprocess.Popen(['Xvfb', '-ac', ':%d' % port],
                                         stdout=open(os.devnull, "w"),
                                         stderr=subprocess.STDOUT)

                    tries = 10
                    while tries > 0:
                        if check_display(port):
                            os.environ['DISPLAY'] = ':%d' % port
                            return p.pid
                        else:
                            tries -= 1
                            time.sleep(0.1)
                except OSError:
                    break

        print 'Cannot execute xfvb, will use the default display.'
        return None

    def lint(self, module):
        subprocess.call(['pylint', module, '--rcfile=%s' % pylintrc_path])

    def lint_path(self, path):
        cwd = os.getcwd()
        os.chdir(path)

        python_files = []
        for (path, dirs, files) in os.walk('.'):
            for f in files:
                if f.endswith('.py'):
                    python_files.append(os.path.join(path, f))

        args = ['pylint', '--rcfile=%s' % pylintrc_path]
        subprocess.call(args + python_files)

        os.chdir(cwd)

    def check_ui(self, config):
        ui_check = UICheck()
        return ui_check.start()

    def check_pylint(self, config):
        sugar_path = os.path.join(config.prefix, 'share', 'sugar')

        print 'Pylint the sugar shell...'
        self.lint_path(os.path.join(sugar_path, 'shell'))
        self.lint_path(os.path.join(sugar_path, 'service'))
        print 'Done.\n'

        print 'Pylint the sugar module...'        
        self.lint('sugar')
        print 'Done.'

    def run(self, config, options, args):
        try:
            import gobject
        except ImportError:
            print 'You need pygobject to use this command.'
            return 1

        result = 0
        xvfb_pid = self.start_xvfb()

        try:
            if options.type == 'pylint':
                self.check_pylint(config)
            if options.type == 'ui':
                result = self.check_ui(config)
        finally:
            if xvfb_pid:
                os.kill(xvfb_pid, signal.SIGTERM)

        return result

register_command(cmd_check)