~ubuntu-branches/ubuntu/vivid/mago/vivid

« back to all changes in this revision

Viewing changes to bin/magomatic

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
3
# Copyright (C) 2009-2011 Canonical Ltd
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
18
 
 
19
 
 
20
import gettext
 
21
import logging
 
22
import optparse
 
23
import os
 
24
import sys, subprocess
 
25
from re import search, sub
 
26
from shutil import copy, copytree
 
27
 
 
28
import gettext
 
29
from gettext import gettext as _
 
30
gettext.textdomain('magomatic')
 
31
 
 
32
# Add project root directory (enable symlink, and trunk execution).
 
33
PROJECT_ROOT_DIRECTORY = os.path.abspath(
 
34
    os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))))
 
35
 
 
36
if (os.path.exists(os.path.join(PROJECT_ROOT_DIRECTORY, 'mago'))
 
37
    and PROJECT_ROOT_DIRECTORY not in sys.path):
 
38
    sys.path.insert(0, PROJECT_ROOT_DIRECTORY)
 
39
    os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses
 
40
 
 
41
from mago import magomaticconfig
 
42
from mago import magomatic
 
43
 
 
44
LEVELS = (  logging.ERROR,
 
45
            logging.WARNING,
 
46
            logging.INFO,
 
47
            logging.DEBUG,
 
48
            )
 
49
 
 
50
def xprop():
 
51
    (command, appname) = (None, None)
 
52
    (wmname, wmtype) = (None, None)
 
53
    print "\
 
54
Please select the window about which you would like information \n\
 
55
by clicking the mouse in that window."
 
56
 
 
57
    try:
 
58
        sp = subprocess.Popen(['xprop','_NET_WM_PID', 'WM_NAME',
 
59
                               '_NET_WM_WINDOW_TYPE'], stdin=None,
 
60
                              stdout=subprocess.PIPE, stderr=None)
 
61
    except OSError, e:
 
62
        return [127, str(e)]
 
63
    out = sp.communicate()[0]
 
64
    for line in out.split('\n'):
 
65
        m = search('([^\(]+).* = (.+)', line)
 
66
        if m:
 
67
            wmprop = m.group(1)
 
68
            wmvalue = m.group(2)
 
69
 
 
70
            if wmprop.startswith('_NET_WM_PID'):
 
71
                try:
 
72
                    f = open('/proc/%d/comm' % int(wmvalue), 'rb')
 
73
                    command = f.read().strip()
 
74
                    f.close()
 
75
                except IOError, e:
 
76
                    # Parent process may not exists anymore when it forks
 
77
                    if e.errno == 2: # File does not exist
 
78
                        return (None, None)
 
79
                    else:
 
80
                        raise IOError,e
 
81
            elif wmprop.startswith('WM_NAME'):
 
82
                # Remove quotes and spaces to match naming conventions of ldtp
 
83
                wmname = wmvalue[1:-1].replace(' ','') 
 
84
            elif wmprop.startswith('_NET_WM_WINDOW_TYPE'):
 
85
                if 'NORMAL' in wmvalue:
 
86
                    wmtype = 'frm'
 
87
                elif 'DIALOG' in wmvalue:
 
88
                    wmtype = 'dlg'
 
89
 
 
90
    if wmname and wmtype:
 
91
        appname = wmtype + wmname
 
92
 
 
93
    return (command, appname)
 
94
 
 
95
def publish_to_mago(suitename, magodir):
 
96
    """ Copy a testsuite generated by magomatic to a mago tree"""
 
97
    logging.debug("Publishing '%s' to '%s' " % (suitename, magodir))
 
98
 
 
99
    # Check if the testsuite is there
 
100
    suitedir = magomaticconfig.get_data_file(suitename)
 
101
    appname = sub('\W', '_', suitename)
 
102
 
 
103
    for d in ('application', 'test_suite', appname):
 
104
        dp = os.path.join(suitedir,d)
 
105
        logging.debug("Checking if '%s' exists" % dp)
 
106
        if not os.path.exists(dp):
 
107
            print "E: Path '%s' does not exists. Exiting" % dp
 
108
            sys.exit(1)
 
109
    # Check if the destination directory exits
 
110
    for d in ('', 'mago/application','mago/test_suite'):
 
111
        mp = os.path.join(magodir,d)
 
112
        logging.debug("Checking if '%s' exists" % mp)
 
113
        if not os.path.exists(mp):
 
114
            print "E: Destination directory '%s' does not exists. Exiting" % mp
 
115
            sys.exit(2)
 
116
 
 
117
    # Everything is there, do the copy
 
118
    destdir = os.path.join(magodir, appname)
 
119
    logging.debug("Copying '%s' to '%s'" % (os.path.join(suitedir, appname), destdir))
 
120
    copytree(os.path.join(suitedir, appname), destdir)
 
121
 
 
122
    destdir = os.path.join(magodir, 'mago/application')
 
123
    logging.debug("Copying '%s' to '%s'" % (os.path.join(suitedir, "application", appname + ".py"), destdir))
 
124
    copy(os.path.join(suitedir, "application", appname + ".py"), destdir)
 
125
    destdir = os.path.join(magodir, 'mago/test_suite')
 
126
    logging.debug("Copying '%s' to '%s'" % (os.path.join(suitedir, "test_suite", appname + ".py"), destdir))
 
127
    copy(os.path.join(suitedir, "test_suite", appname + ".py"), destdir)
 
128
 
 
129
if __name__ == "__main__":
 
130
 
 
131
    version = magomaticconfig.__magomatic_data_directory__
 
132
    # Support for command line options.
 
133
    usage = _("magomatic [options] app_launcher main_window_title")
 
134
    parser = optparse.OptionParser(version="magomatic %s" % version, usage=usage)
 
135
    parser.add_option('-c', '--child', dest='child_window', action='store_true',
 
136
        default=False, help=_('Child window to navigate'))
 
137
    parser.add_option('-d', '--debug', dest='debug_mode', action='store_true',
 
138
        help=_('Print the maximum debugging info (implies -vv)'))
 
139
    parser.add_option('-o', '--overwrite', dest='overwrite', action='store_true',
 
140
        default=False,help=_('Overwrite existing files'))
 
141
    parser.add_option('-t', '--tree', dest='dumptree', action='store_true',
 
142
        default=False,help=_('Dump component tree'))
 
143
    parser.add_option('-v', '--verbose', dest='logging_level', action='count',
 
144
        help=_('set error_level output to warning, info, and then debug'))
 
145
    #parser.add_option('-p', '--publishto', dest='publish_to', metavar="DEST",
 
146
    #    help=_('Publish the application passed in argument to DEST (mago root directory)'))
 
147
    parser.set_defaults(logging_level=0, foo=None)
 
148
    (options, args) = parser.parse_args()
 
149
 
 
150
    # set the verbosity
 
151
    if options.debug_mode:
 
152
        options.logging_level = 3
 
153
    logging.basicConfig(level=LEVELS[options.logging_level], format='%(asctime)s %(levelname)s %(message)s')
 
154
    child_window = options.child_window
 
155
    overwrite = options.overwrite
 
156
 
 
157
    #if options.publish_to:
 
158
    #    if len(args) != 1:
 
159
    #        print "E: Name of the testsuite to publish is missing"
 
160
    #        sys.exit(0)
 
161
    #
 
162
    #    publish_to_mago(args[0], options.publish_to)
 
163
    #    sys.exit(0)
 
164
 
 
165
    if len(args) == 0:
 
166
        # Run xprop to guess the window name and binary
 
167
        (app_launcher, main_window) = xprop()
 
168
        if not app_launcher or not main_window:
 
169
            print "E: Unable to find information about that window."
 
170
            sys.exit(1)
 
171
    elif len(args) == 1:
 
172
        app_launcher = args[0]
 
173
        main_window = None
 
174
    else:
 
175
        app_launcher = args[0]
 
176
        main_window = args[1]
 
177
 
 
178
    magomatic = magomatic.Magomatic(app_launcher, main_window,
 
179
                                    child_window, overwrite)
 
180
    magomatic.create_app_folder()
 
181
    magomatic.discover_application(options.dumptree)
 
182
 
 
183
    logging.debug(_('end of prog'))