Exemplo n.º 1
0
def debug_msg(status):
    caller_levels = dict(troubleshoot_cleaner=1,
                         troubleshoot_obj_call=2,
                         troubleshoot_test=3)
    frame = gfi(gof(cf(caller_levels['troubleshoot_test']))[1][0])
    print("{} [filename: {}, line: {}]".format(status, frame.filename,
                                               frame.lineno))
Exemplo n.º 2
0
def handle_flagged_args(args, config):
    """handle_flagged_args

This function will execute the appropriate steps for what arguments have been
given.  This is, essentially, a good "drop in" point for outside code to use
this module as long as args is a dict within the appropriate format or is an
empty dictionary.
    """
    error = None
    os_version = str()
    os_type = str()
    os_category = \
        BuildError(args, msg="Invalid build config", frame=gfi(cf()))
    # we're building for just one, and it should already be in
    # expected_operations...
    for item in expected_operations:
        if item.name == args['os_type']:
            for version in item.versions:
                os_version = version
                os_type = item.os_type
                if item.os_category == 'redhat':
                    os_category = build_redhat
                elif item.os_category == 'debian':
                    os_category = build_debian
                else:
                    error = False
                    os_category = os_category.print_msg
                break
    error = os_category(config, os_version, os_type=os_type) if error \
        else error
Exemplo n.º 3
0
def build_packages(config):
    """build_packages

Builds for the expected_operations' OperationSettings entries.

This function is intentionaly blind, dumb and stupid to produce what it can.
However, it should also exit with a non-zero when an error does occur to keep
from the caller thinking that things are 'okay' when they are not.
    """
    error = 0
    print("Building packages...")
    for operation in expected_operations:
        if operation.os_category == 'redhat':
            print("For Redhat...")
            function = build_redhat
        elif operation.os_category == 'debian':
            print("For Debian...")
            function = build_debian
        else:
            # can overwrite a previous error statement, but this does need to
            # be preserved...
            error = BuildError(operation, msg="Unexpected operation specified",
                               frame=gfi(cf()))
            function = Error.print_msg
        # preserve a previous iteration's error, if needed
        for os_version in operation.versions:
            error = function(config, os_version) if not error else error
            if error:
                break
        if error:
            print("A Failure occurred...", str(error))
            break
    if not error:
        print("Completed package builds...")
    return error
Exemplo n.º 4
0
def _load_json(fl):
    data = {}
    if os.path.isfile(fl) and os.access(fl, os.R_OK):
        with open(fl, 'r') as fh:
            data = json.loads(fh.read())
    else:
        print("Unable to open JSON file: " + fl)
        reason = 'Cannot Access!' if os.path.isfile(fl) else \
            "File does not exist!"
        print(reason)
        raise BuildError(fl, reason, msg="Unable to open JSON file",
                         frame=gfi(cf()), errnum=errno.ENODATA)
    return data
Exemplo n.º 5
0
def load_json():
    """load_json

Loads the config.JSON file system object's contents and translate it into a
Python object.  This python object is then returned.
    """
    try:
        env = configure.get_env()
        configure.export_to_json(env)
    except SystemExit:
        raise BuildError(msg="configure failed!  Could not build targets!",
                         frame=gfi(cf()), errno=errno.ENOSYS)
    json_fl = os.path.dirname(sys.argv[0]) + "/scripts/config.JSON"
    data = _load_json(json_fl)
    return data
Exemplo n.º 6
0
def _preliminary_construct(config):
    # this is an internal function; however, allows for quick and easy access
    # to execute these operations...
    error = 0
    construct_setups._construct_cfgs_from_json(config)
    try:
        pass
    except Exception as error:
        print("ASDASD")
        return error
    except SystemExit:
        error = BuildError(
            config['setup_cfg'],
            config['stdeb_cfg'],
            frame=gfi(cf()),
            errno=errno.ESPIPE,
            msg="Could not construct the setup files"
        )
    return error
Exemplo n.º 7
0
def debug_msg(status):
    caller_levels = dict(troubleshoot_cleaner=1, troubleshoot_obj_call=2,
                         troubleshoot_test=3)
    frame = gfi(gof(cf(caller_levels['troubleshoot_test']))[1][0])
    print("{} [filename: {}, line: {}]".format(status, frame.filename,
                                               frame.lineno))
Exemplo n.º 8
0
 def _set_frame(self, kargs):
     if 'frame' in kargs:
         self.frame = kargs['frame']
     else:
         # gof gets a stack of [inner->outer] tuples. tuple[0] is frame
         self.frame = gfi(gof(cf())[2][0])
Exemplo n.º 9
0
 def processResponse(self, response):
     """
     Process the received result from request to
     the formated output fields defined by
     analisis_mode: scrapping, parse json
     analisis_params: {key:, stop_val:, path}
     """
     resp = {}
     if type(self.analisis_params) != dict:
         params = json.loads(self.analisis_params)
     else:
         params = self.analisis_params
     #
     # Analisis mode Comparision
     #
     if self.analisis_mode == 'comparision':
         self.logger.log(self.name, 'Comparission mode')
         self.logger.log(self.name, 'Cheking analisis_params\n')
         self.logger.log(self.name, json.dumps(params))
         print(self.name, response.keys())
         cond = None
         val1 = None
         val2 = None
         for par in params:
             if par is None:
                 continue
             if 'key' in par:
                 val1 = par['key']
             if 'comp' in par:
                 val2 = par['comp']
             if 'cond' in par:
                 cond = par['cond']
         self.logger.log(self.name, '{} {} {}'.format(val1, val2, cond))
         self.logger.log(self.name, 'val1 type {}'.format(type(val1)))
         val1 = response[val1]
         self.logger.log(self.name,
                         'Comparision Object type\n{}'.format(type(val1)))
         self.logger.log(self.name, '{}'.format(val1))
         index = None
         occurr = []
         res = False
         if type(val1) == dict:
             lis = json.dumps(list(val1.keys()))
             res = eval('"{}" {} {}'.format(val2, cond, lis))
         elif type(val1) == list:
             for el in val1:
                 if val2.lower() in json.dumps(el).lower():
                     res = True
                     self.logger.log(self.name, 'found ocurrence')
                     self.logger.log(self.name, json.dumps(el, indent=2))
                     occurr.append(el)
         if val1 and type(val1) != dict and type(val1) != list:
             if cond == 'in':
                 res = eval('"{}" {} "{}"'.format(val2, cond, val1))
                 occurr = val1
             else:
                 res = eval(val1 + ' ' + cond + ' ' + val2)
         self.logger.log(self.name, json.dumps([res, occurr]))
         return {'result': res, 'occurrences': occurr}
     #
     # Analisis mode HTML
     #
     if self.analisis_mode == 'HTML':
         # Procesing the data when analisis_node is HTML
         # first process the html to remove bad spacing
         # then use the analisis_params to detect
         # occurrences in the html
         print(self.name, 'html-content in response', 'html-content'
               in response.keys())
         if 'html-content' in response.keys():
             for conf in params:
                 # Extracting wildcards and replacing
                 # them in the conf occurrences
                 conf['occurrence'] = conf['occurrence'].replace('*', '===')
                 f = '==='
                 occ = conf['occurrence']
                 wildcards_index = [m.start() for m in re.finditer(f, occ)]
                 wildcards = []
                 print(conf['occurrence'], wildcards_index)
                 for i in range(0, len(wildcards_index), 2):
                     start = wildcards_index[i] + len('===')
                     buffer = ''
                     while start < len('occurrence'):
                         char = conf['occurrence'][start]
                         end = conf['occurrence'][start:start + len('===')]
                         if end != '===':
                             buffer += char
                         else:
                             break
                         start += 1
                     wildcards.append(buffer)
                 for wild in wildcards:
                     if '==={}==='.format(wild) in conf['occurrence']:
                         if wild in response.keys():
                             occ = conf['occurrence']
                             f = '==={}==='.format(wild)
                             w = response[wild]
                             occ = occ['occurrence'].replace(f, w)
                             conf['occurrence'] = occ
                 # Finish wildcards extraction
                 print('wildcards', wildcards)
                 print(conf['occurrence'])
                 print(self.name, type(conf))
                 if type(conf) == str:
                     print(self.name, json.loads(conf))
                     conf = json.loads(conf)
                 else:
                     print(self.name, conf)
                 occurrences = []
                 tmp = response['html-content']
                 tmp = tmp.replace('  ', '')
                 tmp = tmp.replace('><', '> <')
                 tmp = tmp.replace('\n', ' ')
                 tmp = tmp.replace('  ', '')
                 tmp = tmp.replace('><', '> <')
                 occ = conf['occurrence']
                 indexes = [m.start() for m in re.finditer(occ, tmp)]
                 print(self.name, 'Occurrences indexes', indexes)
                 for index in indexes:
                     start = index + len(conf['occurrence'])
                     buffer = ''
                     while True:
                         if start >= len(tmp):
                             break
                         char = tmp[start]
                         end = tmp[start:start + len(conf['stop'])]
                         # print(self.name, 'checking end', end)
                         if end != conf['stop']:
                             buffer += char
                             start += 1
                         else:
                             break
                     if buffer != '':
                         occurrences.append(buffer)
                 if len(occurrences) > 0:
                     return {'occurrences': occurrences}
                 return response
     #
     # Analisis mode Statistics
     #
     if self.analisis_mode == 'statistics':
         statistics = json.loads(self.analisis_params)
         samples, params = statistics[0]['samples'], \
             statistics[0]['parameters']
         print(self.name, 'Checking Statistics params')
         print('Samples\n', samples)
         print('Parameters\n', params)
         try:
             results = {}
             print(response.keys())
             average = 0
             for sample in samples.keys():
                 try:
                     samp = int(sample)
                     average += samp
                 except Exception as e:
                     average += int(response[sample])
                     pass
             results['sample'] = average / len(samples.keys())
             results['scores'] = {}
             for param in params.keys():
                 print(response[param])
                 # apply simple three rule
                 # pop -> population
                 # percent = (pop * 100%) / sample
                 sample = results['sample']
                 pop = int(response[param])
                 percent = (pop * 100) / sample
                 results['scores'][param] = {}
                 results['scores'][param]['value'] = \
                     '{:.2f}'.format(percent)
                 sign = '+'
                 if sample > pop:
                     sign = '-'
                 results['scores'][param]['diff'] = \
                     '{}{:.2f}'.format(sign, 100 - percent)
                 int_dif = sample - pop
                 results['scores'][param]['int_diff'] = \
                     '{}'.format(int(int_dif))
             print(results)
             return results
         except Exception as e:
             print('Failed: ', e)
             pass
     ######
     #
     # Contacts_list mode
     #
     #####
     if self.analisis_mode == 'contacts_list':
         return {'contacts_list': json.loads(self.data)}
     if params and len(params) > 0 and self.analisis_mode == 'JSON':
         ######
         #
         # JSON  value Extraction
         #
         #######
         # obj must be set as response object
         if self.work_type == 'request':
             response = response['request_data']
         print('Json proccess')
         # Extracting the data using the path setted in the
         # analisis_params
         for param in params:
             if param is None:
                 continue
             print(self.name, 'Extracting value from', param['path'])
             paths = param['path'].split('/')
             obj = response.copy()
             last = len(paths) - 1
             for i, path in enumerate(paths):
                 index = None
                 try:
                     index = int(path)
                     obj = obj[index]
                 except Exception as e:
                     print(self.name, ':', e)
                     if type(obj) == dict and path in obj.keys():
                         obj = obj[path]
                     elif path == 'random' and type(obj) == list:
                         print(self.name, 'choose random value')
                         le = len(obj) - 1
                         if le > 0:
                             pos = random.randint(0, le)
                             obj = obj[pos]
                             print(self.name, str(obj)[:50])
                 try:
                     print(path.encode('utf-8'))
                     print(obj.encode('utf-8'))
                 except Exception as e:
                     print(e)
             if last == i:
                 if obj is not None:
                     resp[param['key']] = obj
         # ------------------------------------------------------
         if self.analisis_mode == 'get_updates':
             # Checks a list or a dict
             # and determines whatever is a new record in the data
             obj = resp[param['key']]
             count = json.loads(self.data)['count']
             t = param['key'] + ' length is ' + str(len(obj))
             self.logger.log(self.name, t)
             if len(obj) > int(count):
                 # print('there is a new record')
                 # print(obj[len(obj) - 1])
                 # TODO
                 # set the counter to the new value
                 data = json.loads(self.data)
                 data['count'] = int(len(obj)) - 1
                 self.data = json.dumps(data)
                 self.save()
                 return obj[len(obj) - 1]
             else:
                 return {}
         if self.work_type == 'request':
             frameinfo = gfi(ctf())
             if len(resp) < 1:
                 return {}
         return resp
     else:
         if self.analisis_mode == 'replace':
             parsed = self.parse_string(self.string, response)
             return parsed
         return response
Exemplo n.º 10
0
 def build_pkg(config, os_type, os_version):
     result = _build_package(config, os_type, os_version)
     if not result.succeeded:
         print(result.cli)
         raise RedhatError(result, os_type, os_version, frame=gfi(cf()))