2
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Copyright (C) 2009-2011 Canonical Ltd
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.
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.
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
24
import sys, subprocess
25
from re import search, sub
26
from shutil import copy, copytree
29
from gettext import gettext as _
30
gettext.textdomain('magomatic')
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]))))
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
41
from mago import magomaticconfig
42
from mago import magomatic
44
LEVELS = ( logging.ERROR,
51
(command, appname) = (None, None)
52
(wmname, wmtype) = (None, None)
54
Please select the window about which you would like information \n\
55
by clicking the mouse in that window."
58
sp = subprocess.Popen(['xprop','_NET_WM_PID', 'WM_NAME',
59
'_NET_WM_WINDOW_TYPE'], stdin=None,
60
stdout=subprocess.PIPE, stderr=None)
63
out = sp.communicate()[0]
64
for line in out.split('\n'):
65
m = search('([^\(]+).* = (.+)', line)
70
if wmprop.startswith('_NET_WM_PID'):
72
f = open('/proc/%d/comm' % int(wmvalue), 'rb')
73
command = f.read().strip()
76
# Parent process may not exists anymore when it forks
77
if e.errno == 2: # File does not exist
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:
87
elif 'DIALOG' in wmvalue:
91
appname = wmtype + wmname
93
return (command, appname)
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))
99
# Check if the testsuite is there
100
suitedir = magomaticconfig.get_data_file(suitename)
101
appname = sub('\W', '_', suitename)
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
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
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)
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)
129
if __name__ == "__main__":
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()
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
157
#if options.publish_to:
159
# print "E: Name of the testsuite to publish is missing"
162
# publish_to_mago(args[0], options.publish_to)
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."
172
app_launcher = args[0]
175
app_launcher = args[0]
176
main_window = args[1]
178
magomatic = magomatic.Magomatic(app_launcher, main_window,
179
child_window, overwrite)
180
magomatic.create_app_folder()
181
magomatic.discover_application(options.dumptree)
183
logging.debug(_('end of prog'))