wgtest_client.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2009, Willow Garage, Inc.
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of the Willow Garage nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
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 = '-' # None values are invalid
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   # DEBUG
00231   #print body
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 


wg_invent_client
Author(s): Scott Hassan, Kevin Watts
autogenerated on Sat Dec 28 2013 17:55:42