00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 """
00036 usage: %(progname)s [args]
00037 """
00038
00039 import os, sys, string, time, getopt
00040
00041 import urllib, urllib2
00042 import invent_client
00043 import neo_cgi, neo_util
00044
00045 import yaml
00046 import types
00047
00048 import simple_hdfhelp as hdfhelp
00049 import attachment_help
00050
00051 def obj2hdf(prefix, obj, hdf=None):
00052 """
00053 Converts python object to HDF. Not loop-safe
00054 """
00055 return py2hdf(prefix, obj.__dict__)
00056
00057 def py2hdf(k, v, hdf=None):
00058 """
00059 Not loop-safe
00060 """
00061 if not hdf: hdf = neo_util.HDF()
00062
00063 k.replace(' ', '_')
00064
00065 if type(v) == str:
00066 if len(v) == 0:
00067 v = '-'
00068 elif v == 'None':
00069 v = '-'
00070
00071 hdf.setValue(k, v)
00072 elif type(v) == types.NoneType:
00073 pass
00074 elif type(v) == unicode:
00075 hdf.setValue(k, str(v))
00076 elif type(v) in (int, float, long):
00077 hdf.setValue(k, str(v))
00078 elif type(v) in (list, tuple):
00079 n = 0
00080 for item in v:
00081 n = n + 1
00082 py2hdf("%s.%d" % (k,n), item, hdf)
00083 elif type(v) == dict:
00084 n = 0
00085 for _k,_v in sorted(v.iteritems()):
00086 if not type(_k) == str:
00087 _k = str(_k)
00088 _k = _k.replace(" ", "_")
00089 py2hdf(k + "." + _k, _v, hdf)
00090 elif type(v) == types.InstanceType or type(v) == types.ClassType \
00091 or isinstance(v, object):
00092 py2hdf(k, v.__dict__, hdf)
00093
00094 return hdf
00095
00096 def _clean_val(val):
00097 if not type(val) == str:
00098 val = str(val)
00099
00100 return val.replace(' ', '_').replace('.', '').replace('-', '_')
00101
00102 class Value(object):
00103 """
00104 Stores measurement value
00105 """
00106 def __init__(self, value, min, max):
00107 self.value = value
00108 self.min = min
00109 self.max = max
00110
00111
00112 class SubtestData(object):
00113 """
00114 Stores subtest data to log into WG Test database
00115 """
00116 def __init__(self, name, result):
00117 """
00118 @param name str : Name of subtest
00119 @param result str : Result of subtest (ex: 'Pass')
00120 """
00121 self.name = name
00122 self.result = result
00123
00124 self.note = ''
00125 self.parameters = {}
00126 self.measurements = {}
00127
00128 def set_parameter(self, key, value):
00129 """
00130 Parameters are specific inputs to each subtest
00131 @param key str : Parameter name
00132 @param value str : Parameter value
00133 """
00134 k = _clean_val(key)
00135 self.parameters[k] = value
00136
00137 def set_measurement(self, key, value, min, max):
00138 """
00139 Measurements store specific quantities that are measured for each subtest
00140 @param key str : Name of measurment
00141 @param value str : Measurement value
00142 @param min str : Minimum value
00143 @param max str : Maximum value
00144 """
00145 k = _clean_val(key)
00146 self.measurements[k] = Value(value, min, max)
00147
00148 def set_note(self, note):
00149 """
00150 @param note str : Note to add to test log
00151 """
00152 self.note = note
00153
00154
00155 class TestData(object):
00156 """
00157 Stores test data to log into WG Test database
00158 """
00159 def __init__(self, name, desc, timestamp, reference, result):
00160 """
00161 @param name str : ID of test
00162 @param desc str : Description of test
00163 @param timestamp int : Timestamp test started
00164 @param reference str : Reference of test
00165 @param result str : Result of test. Ex: "PASS" or "FAIL"
00166 """
00167 self.name = name
00168 self.desc = desc
00169 self.reference = reference
00170 self.timestamp = timestamp
00171 self.result = result
00172
00173 self.attachment = None
00174
00175 self.note = ''
00176
00177 self.subtests = []
00178
00179 def set_attachment(self, content_type, fn):
00180 """
00181 @todo Attachment gets loaded into HDF. This should probably change
00182 @param content_type str : MIME type of attachment
00183 @param fn str : Filename
00184 """
00185 self.attachment = {"content_type":content_type, "filename":fn }
00186
00187 def set_note(self, note):
00188 """
00189 @param note str : Note to add to test log
00190 """
00191 self.note = note
00192
00193 def add_subtest(self, subtest):
00194 """
00195 Subtests are added to each test.
00196 @param subtest SubtestData : Subtest data
00197 """
00198 self.subtests.append(subtest)
00199
00200 def submit_log(client, testdata, attach_data):
00201 """
00202 Submits testing data to Invent system.
00203
00204 @param client Invent : Invent client to load data
00205 @param testdata TestData : Test data to load
00206 @param attach_data str : File data (read from file)
00207 @return bool : True if loaded successfully
00208 """
00209 if not client.login():
00210 return False
00211
00212 url = client.site + 'wgtest/api.py?Action.Add_TestData=1'
00213
00214 hdf = obj2hdf("test", testdata)
00215 test_data = hdf.writeString()
00216
00217 fields = []
00218 fields.append(('Action.Add_TestData', '1'))
00219
00220 files = []
00221 files.append(('testdata', 'testdata.hdf', test_data))
00222
00223 if testdata.attachment:
00224 files.append(('attach', testdata.attachment['filename'], attach_data))
00225
00226 log_input = attachment_help.build_request(client.site + "wgtest/api.py", fields, files)
00227
00228 body = client.opener.open(log_input).read()
00229
00230
00231
00232
00233 try:
00234 ohdf = neo_util.HDF()
00235 ohdf.readString(body)
00236
00237 logid = ohdf.getValue("logid", "")
00238 status = ohdf.getValue("status", "")
00239 except Exception, e:
00240 print 'Invalid HDF from server\n', body
00241 import traceback
00242 traceback.print_exc()
00243 return False
00244
00245 if not status == '1':
00246 msg = ohdf.getValue("msg", "")
00247 print >> sys.stderr, "Unable to submit test data. Message: %s" % msg
00248 print >> sys.stderr, "HDF input:", test_data
00249
00250 return status == '1'
00251