def validate_if_file_is_present_in_repository(self, **keyargs): ''' Whenever we need to validate if artifact was uploaded properly ''' url = keyargs["build_details"]["file_path"] if (default_nexus_container_name in url): url = HelperServices.replace_hostname_with_actual( url, default_nexus_container_name, socket.gethostbyname(default_nexus_container_name)) ret = urllib2.urlopen(url) if ret.code == 200: print url + " is present in repository !!" return
def upload(self, **keyargs): ''' Whenever we need to upload a existing/new build ''' # Start by validating build_details self.validate_build_structure(**keyargs) #1.0 INITIALISE VARIABLES artifact_already_exists = False upload_skipped_ind = False # if full_file_path + '/' + fileName does not exists then we skip upload of artifact as per design force_upload_artifacts = str( self.repository_details.get("force_upload", "false")).lower() == "true" relative_path = keyargs["build_details"].get("additional_info").get( "relative_path") fileName = keyargs["build_details"].get("additional_info").get( "file_name") keyargs["build_details"]["file_path"] = join(self.repository_details.get("base_url"), join(relative_path, fileName)).\ replace("\\", '/') actual_build_details = copy.deepcopy(keyargs["build_details"]) file_to_upload = keyargs.get("file_to_upload", None) if file_to_upload: keyargs["file_to_upload"] = file_to_upload keyargs["directory_to_import_from"] = os.path.dirname( file_to_upload) else: keyargs["directory_to_import_from"] = keyargs.get( "directory_to_import_from") keyargs["file_to_upload"] = join( keyargs["directory_to_import_from"], relative_path, fileName) if keyargs.get("file_to_upload") is None and keyargs.get( "directory_to_import_from") is None: raise Exception( "Cannot upload build as both file_to_upload and directory_to_import_from were not provided." ) #1.1 CHECK IF BUILD ALREADY EXISTS try: url = actual_build_details["file_path"] if (default_nexus_container_name in url): url = HelperServices.replace_hostname_with_actual( actual_build_details["file_path"], default_nexus_container_name, socket.gethostbyname(default_nexus_container_name)) ret = urllib2.urlopen(url) if ret.code == 200: artifact_already_exists = True except Exception: # catch *all* exceptions pass #1.2 UPLOAD ARTIFACTS if keyargs["directory_to_import_from"] and (not artifact_already_exists or force_upload_artifacts): if os.path.exists(keyargs.get("file_to_upload")): keyargs["build_details"] = { "repo": actual_build_details.get("additional_info").get("repo_id"), "extension": actual_build_details.get("package_type"), "groupId": actual_build_details.get("additional_info").get("package"), "artifactId": actual_build_details.get("additional_info").get( "artifact"), "version": actual_build_details.get("additional_info").get("version"), "package": actual_build_details.get("package_type"), "classifier": str(actual_build_details.get("build_number")), "file_path": actual_build_details["file_path"], "relative_path": relative_path } #UPLOAD OR COPY FILE self.serviceHandler.upload(**keyargs) else: upload_skipped_ind = True # Skip upload as source server did not provide this file print "### We did not try to upload file: " + keyargs[ "file_to_upload"] + " as its not present in source directory ###" #1.3 validate_build_structure IF UPLOAD SUCCESSFUL if (not artifact_already_exists or force_upload_artifacts): try: url = actual_build_details["file_path"] if (default_nexus_container_name in url): url = HelperServices.replace_hostname_with_actual( actual_build_details["file_path"], default_nexus_container_name, socket.gethostbyname(default_nexus_container_name)) ret = urllib2.urlopen(url) if ret.code == 200: print actual_build_details[ "file_path"] + " was uploaded properly !!" except Exception as e: # catch *all* exceptions message = "But uploaded file was not found in repository: " + str( actual_build_details["file_path"]) + " Error: " + str(e) if upload_skipped_ind: message = "Upload mechanism was skipped." + message else: message = "Upload mechanism was completed." + message raise ValueError(message) return True