def props_equal(self, objects, prop_name): """ compare 2 objects, stored in objects, objects[0] and objects[1] """ # compare objects, name defined in "props" ObjectCompare.object_equal(objects, self.fmw_config[prop_name]["props"]) # if the single object is list if isinstance(objects[0], list): if len(objects[0]) != len(objects[1]): raise APIException(prop_name) # sort ths list before comparing # sort object list base on a fieldy sort_on = "name" if "sort_on" in self.fmw_config[prop_name].keys(): sort_on = self.fmw_config[prop_name]["sort_on"] sorted_objects1 = None sorted_objects2 = None if sort_on == "name": sorted_objects1 = sorted(objects[0], key=ObjectCompare.get_name) sorted_objects2 = sorted(objects[1], key=ObjectCompare.get_name) if sort_on == "caption": sorted_objects1 = sorted(objects[0], key=ObjectCompare.get_caption) sorted_objects2 = sorted(objects[1], key=ObjectCompare.get_caption) # compare each for i in range(0, len(sorted_objects1)): obj1 = sorted_objects1[i] obj2 = sorted_objects2[i] prop_key = "%s_item" % prop_name self.props_equal([obj1, obj2], prop_key) # if the single object is dict if isinstance(objects[0], dict): if len(objects[0]) != len(objects[1]): raise APIException(prop_name) # "source" and "destination", as dir, have same props is_dir = "dir" in self.fmw_config[prop_name].keys() # compare each for sub_prop in self.fmw_config[prop_name]["sub_props"]: # some objects don't have the defined prop if sub_prop not in objects[0].keys( ) and sub_prop not in objects[1].keys(): continue sub_obj1 = objects[0][sub_prop] sub_obj2 = objects[1][sub_prop] # some dict objects are {} if len(sub_obj1) == 0 and len(sub_obj2) == 0: continue prop_key = "%s_dir" % prop_name if not is_dir: prop_key = "%s_%s" % (prop_name, sub_prop) self.props_equal([sub_obj1, sub_obj2], prop_key)
def object_equal(objects, props): if len(props) == 0: return if isinstance(objects[0], dict): obj1 = objects[0] obj2 = objects[1] for prop in props: if prop not in obj1.keys() and prop not in obj2.keys(): continue if obj1[prop] != obj2[prop]: raise APIException(prop)
def upload_fmw(self, repo_name, fmw_name, file): """Uploads an item to a repository. """ if not os.path.exists(file): raise APIException("File not found: %s" % file) bin_file = {'file': open(file, 'rb')} headers = { "Content-Disposition": "attachment; filename=\"%s\"" % fmw_name, "Accept": "application/json; charset=UTF-8" } response = self.create_api_caller("POST").call_api_upload( "create_fmw", bin_file, [repo_name], [200, 201], headers) return response
def download_prop(self, names, out_dir, overwrite, download_prop_act): prop_file = self.populate_prop_file_name(names, out_dir) if not overwrite and os.path.exists(prop_file): raise APIException("File already exists: %s" % prop_file) out_dir = os.path.dirname(prop_file) if not os.path.exists(out_dir): os.makedirs(out_dir) response = download_prop_act(*names) Path(os.path.dirname(prop_file)).mkdir(parents=True, exist_ok=True) with open(prop_file, 'wb') as f: f.write(response["response"].content) response["download_file"] = prop_file return response
def do_fmw_job(self, repo, fmw): repo_name = repo["name"] fmw_name = fmw["name"] full_name = "%s\\%s" % (repo_name, fmw_name) if self.job_config["fmw_filter"]["on"]: if fmw_name not in self.job_config["fmw_filter"]["items"]: return # self.log.write_line("Finding in %s ..." % full_name) parameters = self.api.list_fmw_parameters(repo_name, fmw_name) fmw_find = FMWFind(repo_name, fmw_name, parameters, self.prop_find, self.log) try: fmw_find.find() if self.prop_find.found(): line = "%s/%s" % (repo_name, fmw_name) self.fmw_found_list.append(line) self.log.write_line(line) except APIException as e: raise APIException("Error: %s. reason: %s" % (full_name, e.error["message"]))
def check_response(self, response, url, return_codes): if self.api_call_success(response, return_codes): return self.api_call_return(response) raise APIException(url, self.http_method, response)