0
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
|
16 |
|
|
17 |
import os
|
|
18 |
import sys
|
|
19 |
import fnmatch
|
|
20 |
import re
|
|
21 |
import logging
|
3
|
22 |
import ConfigParser
|
0
|
23 |
|
|
24 |
|
3
|
25 |
def get_cfg_files(paths, filename):
|
|
26 |
""" Find out the cone subscripts that are present in the"""
|
|
27 |
files = []
|
|
28 |
for path in paths:
|
|
29 |
cfgfile = os.path.join(path, filename)
|
|
30 |
if os.path.exists(cfgfile):
|
|
31 |
files.append(cfgfile)
|
|
32 |
return files
|
|
33 |
|
|
34 |
def get_config(cfgfiles):
|
|
35 |
config = ConfigParser.ConfigParser()
|
|
36 |
config.read(cfgfiles)
|
|
37 |
return config
|
|
38 |
|
|
39 |
def get_actions(cfgfiles):
|
|
40 |
""" Find out the cone subscripts that are present in the"""
|
|
41 |
subacts = ActionContainer()
|
|
42 |
config = get_config(cfgfiles)
|
|
43 |
actions_section = 'actions'
|
|
44 |
if not config.has_section(actions_section):
|
|
45 |
raise Exception('The cone.ini does not have any %s section.' % actions_section)
|
|
46 |
for (name, section) in config.items(actions_section):
|
|
47 |
subacts += get_subactions_from_configs(cfgfiles, section)
|
|
48 |
return subacts
|
|
49 |
|
|
50 |
def get_subactions_from_configs(cfgfiles, section):
|
|
51 |
""" Find out the cone subscripts that are present in the"""
|
|
52 |
subacts = ActionContainer()
|
|
53 |
config = get_config(cfgfiles)
|
|
54 |
paths = [os.path.dirname(cfgfile) for cfgfile in cfgfiles]
|
|
55 |
if not config.has_section(section):
|
|
56 |
raise Exception('The cone.ini does not have any %s section.' % section)
|
|
57 |
for (commandname, path) in config.items(section):
|
|
58 |
subacts.append(ConeAction(commandname, path, type=section, paths=paths))
|
|
59 |
|
|
60 |
return subacts
|
|
61 |
|
0
|
62 |
def get_subactions(path,pattern):
|
|
63 |
""" Find out the cone subscripts that are present in the ROOT_PATH """
|
|
64 |
subacts = ActionContainer()
|
|
65 |
for file in os.listdir(path):
|
|
66 |
if fnmatch.fnmatch(file, pattern):
|
|
67 |
sact = SubAction(file,pattern)
|
|
68 |
subacts[sact.name] = sact
|
|
69 |
return subacts
|
|
70 |
|
|
71 |
def get_log_level(level):
|
|
72 |
"""
|
|
73 |
Change the given user input log level to logging categorisation
|
|
74 |
"""
|
|
75 |
if level == 0 : return logging.NOTSET
|
|
76 |
elif level == 1 : return logging.CRITICAL
|
|
77 |
elif level == 2 : return logging.ERROR
|
|
78 |
elif level == 3 : return logging.WARNING
|
|
79 |
elif level == 4 : return logging.INFO
|
|
80 |
elif level == 5 : return logging.DEBUG
|
|
81 |
else : return logging.NOTSET
|
|
82 |
|
3
|
83 |
class ActionContainer(list):
|
|
84 |
pass
|
0
|
85 |
|
3
|
86 |
# def __init__(self):
|
|
87 |
# self._actions = {}
|
|
88 |
#
|
|
89 |
# def __len__(self):
|
|
90 |
# return len(self._actions)
|
|
91 |
#
|
0
|
92 |
def __getitem__(self, key):
|
3
|
93 |
for item in self:
|
|
94 |
if item.name == key:
|
|
95 |
return item
|
|
96 |
raise KeyError('Key %s not found in %s' % (key, self))
|
|
97 |
#
|
|
98 |
# def __setitem__( self, key, value):
|
|
99 |
# self._actions[key] = value
|
|
100 |
#
|
|
101 |
# def __delitem__( self, key):
|
|
102 |
# del self._actions[key]
|
|
103 |
#
|
|
104 |
# def __iter__( self):
|
|
105 |
# return self._actions.__iter__()
|
0
|
106 |
|
3
|
107 |
class ConeAction(object):
|
|
108 |
def __init__(self, name, path, **kwargs):
|
|
109 |
self.name = name
|
|
110 |
self.path = path
|
|
111 |
self.type = kwargs.get('type', '')
|
|
112 |
self.paths = kwargs.get('paths', [])
|
|
113 |
self._module = None
|
|
114 |
|
|
115 |
@property
|
|
116 |
def module_name(self):
|
|
117 |
return os.path.basename(self.path)
|
0
|
118 |
|
3
|
119 |
@property
|
|
120 |
def module_path(self):
|
|
121 |
return os.path.dirname(self.path)
|
0
|
122 |
|
3
|
123 |
@property
|
|
124 |
def module(self):
|
|
125 |
if not self._module:
|
|
126 |
paths = [os.path.join(pth, self.module_path) for pth in self.paths]
|
|
127 |
paths.append(self.module_path)
|
|
128 |
sys.path += paths
|
|
129 |
try:
|
|
130 |
self._module = __import__(self.module_name)
|
|
131 |
finally:
|
|
132 |
del sys.path[-len(paths):]
|
|
133 |
return self._module
|
|
134 |
|
|
135 |
def short_help(self):
|
|
136 |
if hasattr(self.module, 'short_help'):
|
|
137 |
return self.module.short_help
|
|
138 |
elif hasattr(self.module, 'main'):
|
|
139 |
helpstr = self.module.main.__doc__ or '<None>'
|
|
140 |
return helpstr.replace('\n', '').strip(' ')[:50]
|
|
141 |
else:
|
|
142 |
return 'Not a valid module!!'
|
|
143 |
|
|
144 |
def run(self):
|
|
145 |
self.module.main()
|
|
146 |
|
0
|
147 |
|
|
148 |
class SubAction(object):
|
|
149 |
def __init__(self, scriptname, pattern):
|
|
150 |
self._scriptname = scriptname
|
|
151 |
self._pattern = pattern
|
|
152 |
|
|
153 |
@property
|
|
154 |
def name(self):
|
|
155 |
""" translate the pattern """
|
|
156 |
pattern = fnmatch.translate(self._pattern).replace('.*', '(.*)')
|
|
157 |
m = re.match(pattern, self._scriptname)
|
|
158 |
return m.group(1)
|
|
159 |
|
|
160 |
def run(self):
|
|
161 |
module = __import__(self._scriptname.replace('.py',''))
|
|
162 |
module.main()
|