def write_effective_pom(pom_file_path, output_path): """Generates the effective pom for a given pom and writes it to a given directory Parameters ---------- pom_file_path : str Path to pom file to render the effective pom for. output_path : str Path to write the effective pom to. See --- * https://maven.apache.org/plugins/maven-help-plugin/effective-pom-mojo.html Returns ------- str Absolute path to the written effective pom generated from the given pom file path. Raises ------ StepRunnerException If issue generating effective pom. """ try: sh.mvn( # pylint: disable=no-member 'help:effective-pom', f'-f={pom_file_path}', f'-Doutput={output_path}') except sh.ErrorReturnCode as error: raise StepRunnerException( f"Error generating effective pom for '{pom_file_path}' to '{output_path}': {error}" ) from error return output_path
def maven(direct, *extra): print("cd {}".format(direct)) cd(direct) print("Maven install") mvn("install", *extra) print("Maven done") cd("..")
def git_code(self): log_print("开始git代码...") if self.operation == "rollback": log_print("不需要拉取代码.") return True if self.git_branch == None: self.git_branch = "master" sh.cd('%s/../work_space' % BASE_PATH) if os.path.exists(self.deploy_name): sh.rm('-fr', self.deploy_name) sh.git('clone', '-b', self.git_branch, self.git_uri, self.deploy_name, _out=process_output) sh.cd(self.git_home_path) self._git_info() log_print("开始mvn编译部分...") sh.mvn('clean', 'package', '-U', '-Dmaven.test.skip=true', _out=process_output) if not os.path.exists('target/%s' % self.package_file): log_print("mvn编译失败.") os._exit(4) log_print("mvn编译成功") sh.mkdir('docker-work')
def _run_step(self): """Runs the TSSC step implemented by this StepImplementer. Returns ------- dict Results of running this step. """ # ----- get generate-metadata items version = self._get_version() # ----- get package items artifacts = self._get_artifacts() maven_push_artifact_repo_id = self.get_config_value( 'maven-push-artifact-repo-id') maven_push_artifact_repo_url = self.get_config_value( 'maven-push-artifact-repo-url') settings_file = self._generate_maven_settings() report_artifacts = [] for artifact in artifacts: artifact_path = artifact['path'] group_id = artifact['group-id'] artifact_id = artifact['artifact-id'] package_type = artifact['package-type'] try: sh.mvn( # pylint: disable=no-member 'deploy:deploy-file', '-Dversion=' + version, '-Dfile=' + artifact_path, '-DgroupId=' + group_id, '-DartifactId=' + artifact_id, '-Dpackaging=' + package_type, '-Durl=' + maven_push_artifact_repo_url, '-DrepositoryId=' + maven_push_artifact_repo_id, '-s' + settings_file, _out=sys.stdout, _err=sys.stderr) except sh.ErrorReturnCode as error: raise RuntimeError(f'Error invoking mvn: {error}') from error report_artifacts.append({ 'artifact-id': artifact_id, 'group-id': group_id, 'version': version, 'path': artifact_path, }) results = { 'result': { 'success': True, 'message': 'push artifacts step completed - see report-artifacts', }, 'report-artifacts': report_artifacts } return results
def write_effective_pom( pom_file_path, output_path, profiles=None ): """Generates the effective pom for a given pom and writes it to a given directory Parameters ---------- pom_file_path : str Path to pom file to render the effective pom for. output_path : str Path to write the effective pom to. profiles : list Maven profiles to use when generating the effective pom. See --- * https://maven.apache.org/plugins/maven-help-plugin/effective-pom-mojo.html Returns ------- str Absolute path to the written effective pom generated from the given pom file path. Raises ------ StepRunnerException If issue generating effective pom. """ if not os.path.isabs(output_path): raise StepRunnerException( f"Given output path ({output_path}) is not absolute which will mean your output" f" file will actually end up being relative to the pom file ({pom_file_path}) rather" " than your expected root. Rather then handling this, just give this function an" " absolute path." " If you are a user seeing this, a programmer messed up somewhere, report an issue." ) profiles_arguments = "" if profiles: if isinstance(profiles, str): profiles = [profiles] profiles_arguments = ['-P', f"{','.join(profiles)}"] try: sh.mvn( # pylint: disable=no-member 'help:effective-pom', f'-f={pom_file_path}', f'-Doutput={output_path}', *profiles_arguments ) except sh.ErrorReturnCode as error: raise StepRunnerException( f"Error generating effective pom for '{pom_file_path}' to '{output_path}': {error}" ) from error return output_path
def update_license(github_repo, jira_client, git_user, git_password): """Update license""" url = github_repo.clone_url name = github_repo.name sh.git.clone(url, name) sh.cd(name) sh.git.checkout("develop") sh.mvn("license:format") if (sh.git.status("--porcelain") != ""): sh.git.add("src/java") sh.git.add("src/test/java") try: issue_key = "" tree = ET.parse('pom.xml') project = tree.getroot() ns = {"mvn": "http://maven.apache.org/POM/4.0.0"} properties = project.find("mvn:properties", ns) if properties is not None: jira_project_name = properties.find('mvn:jiraProjectName', ns) if jira_project_name is not None: project_key = jira_project_name.text version = jira_client.get_last_version(project_key) user = jira_client.user issue_key = jira_client.create_issue(project_key, "Update license", user, "Major", "Task", version) except FileNotFoundError, ET.ParseError: pass issue_key += " : " if issue_key else "" sh.git.commit("-m", "{}Update license".format(issue_key)) remote = str(sh.git.remote("get-url", "--push", "origin").replace("\n", "")) remote = remote.replace("https://", "https://{}:{}@".format(git_user, git_password)) sh.git.push(remote, "develop") jira_client.close_issue(issue_key)
def _run_step(self): # pylint: disable=too-many-locals """Runs the step implemented by this StepImplementer. Returns ------- StepResult Object containing the dictionary results of this step. """ step_result = StepResult.from_step_implementer(self) pom_file = self.get_value('pom-file') artifact_extensions = self.get_value('artifact-extensions') artifact_parent_dir = self.get_value('artifact-parent-dir') if not os.path.exists(pom_file): step_result.success = False step_result.message = f'Given pom file does not exist: {pom_file}' return step_result settings_file = self._generate_maven_settings() mvn_output_file_path = self.write_working_file('mvn_test_output.txt') try: with open(mvn_output_file_path, 'w') as mvn_output_file: out_callback = create_sh_redirect_to_multiple_streams_fn_callback( [sys.stdout, mvn_output_file]) err_callback = create_sh_redirect_to_multiple_streams_fn_callback( [sys.stderr, mvn_output_file]) sh.mvn( # pylint: disable=no-member 'clean', 'install', '-f', pom_file, '-s', settings_file, _out=out_callback, _err=err_callback) except sh.ErrorReturnCode as error: step_result.success = False step_result.message = "Package failures. See 'maven-output' report artifacts " \ f"for details: {error}" return step_result finally: step_result.add_artifact( description= "Standard out and standard error from 'mvn install'.", name='maven-output', value=mvn_output_file_path) # find the artifacts artifact_file_names = [] artifact_parent_dir_full_path = \ os.listdir(os.path.join( os.path.dirname(os.path.abspath(pom_file)), artifact_parent_dir)) for filename in artifact_parent_dir_full_path: if any(filename.endswith(ext) for ext in artifact_extensions): artifact_file_names.append(filename) # error if we find more then one artifact # see https://projects.engineering.redhat.com/browse/NAPSSPO-546 if len(artifact_file_names) > 1: step_result.success = False step_result.message = 'pom resulted in multiple artifacts with expected artifact ' \ f'extensions ({artifact_extensions}), this is unsupported' return step_result if len(artifact_file_names) < 1: step_result.success = False step_result.message = 'pom resulted in 0 with expected artifact extensions ' \ f'({artifact_extensions}), this is unsupported' return step_result artifact_id = get_xml_element(pom_file, 'artifactId').text group_id = get_xml_element(pom_file, 'groupId').text try: package_type = get_xml_element(pom_file, 'package').text except ValueError: package_type = 'jar' package_artifacts = { 'path': os.path.join(os.path.dirname(os.path.abspath(pom_file)), artifact_parent_dir, artifact_file_names[0]), 'artifact-id': artifact_id, 'group-id': group_id, 'package-type': package_type, 'pom-path': pom_file } # Currently, package returns ONE 'artifact', eg: one war file # However, in the future, an ARRAY could be returned, eg: several jar files step_result.add_artifact(name='package-artifacts', value=[package_artifacts]) return step_result
def _run_step(self): # pylint: disable=too-many-locals,too-many-statements """Runs the step implemented by this StepImplementer. Returns ------- StepResult Object containing the dictionary results of this step. """ step_result = StepResult.from_step_implementer(self) settings_file = self._generate_maven_settings() pom_file = self.get_value('pom-file') fail_on_no_tests = self.get_value('fail-on-no-tests') selenium_hub_url = self.get_value('selenium-hub-url') deployed_host_urls = ConfigValue.convert_leaves_to_values( self.get_value('deployed-host-urls') ) uat_maven_profile = self.get_value('uat-maven-profile') tls_verify = self.get_value('tls-verify') # NOTE: # at some point may need to do smarter logic if a deployable has more then one deployed # host URL to do UAT against all of them, but for now, use first one as target of UAT if isinstance(deployed_host_urls, list): target_base_url = deployed_host_urls[0] if len(deployed_host_urls) > 1: step_result.message = \ f"Given more then one deployed host URL ({deployed_host_urls})," \ f" targeting first one ({target_base_url}) for user acceptance test (UAT)." print(step_result.message) elif deployed_host_urls: target_base_url = deployed_host_urls else: target_base_url = self.get_value('target-host-url') # ensure surefire plugin enabled maven_surefire_plugin = self._get_effective_pom_element( element_path=MavenGeneric.SUREFIRE_PLUGIN_XML_ELEMENT_PATH ) if maven_surefire_plugin is None: step_result.success = False step_result.message = 'Unit test dependency "maven-surefire-plugin" ' \ f'missing from effective pom ({self._get_effective_pom()}).' return step_result # get surefire test results dir reports_dir = self._get_effective_pom_element( element_path=MavenGeneric.SUREFIRE_PLUGIN_REPORTS_DIR_XML_ELEMENT_PATH ) if reports_dir is not None: test_results_dir = reports_dir.text else: test_results_dir = os.path.join( os.path.dirname(os.path.abspath(pom_file)), MavenGeneric.DEFAULT_SUREFIRE_PLUGIN_REPORTS_DIR ) mvn_additional_options = [] if not tls_verify: mvn_additional_options += [ '-Dmaven.wagon.http.ssl.insecure=true', '-Dmaven.wagon.http.ssl.allowall=true', '-Dmaven.wagon.http.ssl.ignore.validity.dates=true', ] cucumber_html_report_path = os.path.join(self.work_dir_path, 'cucumber.html') cucumber_json_report_path = os.path.join(self.work_dir_path, 'cucumber.json') mvn_output_file_path = self.write_working_file('mvn_test_output.txt') try: with open(mvn_output_file_path, 'w') as mvn_output_file: out_callback = create_sh_redirect_to_multiple_streams_fn_callback([ sys.stdout, mvn_output_file ]) err_callback = create_sh_redirect_to_multiple_streams_fn_callback([ sys.stderr, mvn_output_file ]) sh.mvn( # pylint: disable=no-member 'clean', 'test', f'-P{uat_maven_profile}', f'-Dselenium.hub.url={selenium_hub_url}', f'-Dtarget.base.url={target_base_url}', f'-Dcucumber.plugin=' \ f'html:{cucumber_html_report_path},' \ f'json:{cucumber_json_report_path}', '-f', pom_file, '-s', settings_file, *mvn_additional_options, _out=out_callback, _err=err_callback ) if not os.path.isdir(test_results_dir) or len(os.listdir(test_results_dir)) == 0: if fail_on_no_tests: step_result.message = "No user acceptance tests defined" \ f" using maven profile ({uat_maven_profile})." step_result.success = False else: step_result.message = "No user acceptance tests defined" \ f" using maven profile ({uat_maven_profile})," \ " but 'fail-on-no-tests' is False." except sh.ErrorReturnCode: step_result.message = "User acceptance test failures. See 'maven-output'" \ ", 'surefire-reports', 'cucumber-report-html', and 'cucumber-report-json'" \ " report artifacts for details." step_result.success = False step_result.add_artifact( description=f"Standard out and standard error by 'mvn -P{uat_maven_profile} test'.", name='maven-output', value=mvn_output_file_path ) step_result.add_artifact( description=f"Surefire reports generated by 'mvn -P{uat_maven_profile} test'.", name='surefire-reports', value=test_results_dir ) step_result.add_artifact( description=f"Cucumber (HTML) report generated by 'mvn -P{uat_maven_profile} test'.", name='cucumber-report-html', value=cucumber_html_report_path ) step_result.add_artifact( description=f"Cucumber (JSON) report generated by 'mvn -P{uat_maven_profile} test'.", name='cucumber-report-json', value=cucumber_json_report_path ) return step_result
def _run_step(self): # pylint: disable=too-many-locals """Runs the step implemented by this StepImplementer. Returns ------- StepResult Object containing the dictionary results of this step. """ step_result = StepResult.from_step_implementer(self) # Get config items maven_push_artifact_repo_id = self.get_value( 'maven-push-artifact-repo-id') maven_push_artifact_repo_url = self.get_value( 'maven-push-artifact-repo-url') version = self.get_value('version') package_artifacts = self.get_value('package-artifacts') tls_verify = self.get_value('tls-verify') # disable tls verification mvn_additional_options = [] if not tls_verify: mvn_additional_options += [ '-Dmaven.wagon.http.ssl.insecure=true', '-Dmaven.wagon.http.ssl.allowall=true', '-Dmaven.wagon.http.ssl.ignore.validity.dates=true', ] # Create settings.xml settings_file = self._generate_maven_settings() # push the artifacts push_artifacts = [] mvn_output_file_path = self.write_working_file('mvn_test_output.txt') try: for package in package_artifacts: artifact_path = package['path'] group_id = package['group-id'] artifact_id = package['artifact-id'] package_type = package['package-type'] # push the artifact with open(mvn_output_file_path, 'a') as mvn_output_file: out_callback = create_sh_redirect_to_multiple_streams_fn_callback( [sys.stdout, mvn_output_file]) err_callback = create_sh_redirect_to_multiple_streams_fn_callback( [sys.stderr, mvn_output_file]) sh.mvn( # pylint: disable=no-member 'deploy:deploy-file', '-Dversion=' + version, '-Dfile=' + artifact_path, '-DgroupId=' + group_id, '-DartifactId=' + artifact_id, '-Dpackaging=' + package_type, '-Durl=' + maven_push_artifact_repo_url, '-DrepositoryId=' + maven_push_artifact_repo_id, '-s' + settings_file, *mvn_additional_options, _out=out_callback, _err=err_callback) # record the pushed artifact push_artifacts.append({ 'artifact-id': artifact_id, 'group-id': group_id, 'version': version, 'path': artifact_path, 'packaging': package_type, }) except sh.ErrorReturnCode as error: step_result.success = False step_result.message = "Push artifacts failures. See 'maven-output' report artifacts " \ f"for details: {error}" step_result.add_artifact( description="Standard out and standard error from 'mvn install'.", name='maven-output', value=mvn_output_file_path) step_result.add_artifact(name='push-artifacts', value=push_artifacts) return step_result
def _run_step(self): """Runs the TSSC step implemented by this StepImplementer. Returns ------- dict Results of running this step. """ pom_file = self.get_config_value('pom-file') fail_on_no_tests = self.get_config_value('fail-on-no-tests') if not os.path.exists(pom_file): raise ValueError('Given pom file does not exist: ' + pom_file) surefire_path = 'mvn:build/mvn:plugins/mvn:plugin/[mvn:artifactId="maven-surefire-plugin"]' maven_surefire_plugin = get_xml_element_by_path( pom_file, surefire_path, default_namespace='mvn') if maven_surefire_plugin is None: raise ValueError( 'Unit test dependency "maven-surefire-plugin" missing from POM.' ) reports_dir = get_xml_element_by_path( pom_file, f'{surefire_path}/mvn:configuration/mvn:reportsDirectory', default_namespace='mvn') if reports_dir is not None: test_results_dir = reports_dir.text else: test_results_dir = os.path.join( os.path.dirname(os.path.abspath(pom_file)), 'target/surefire-reports') settings_file = self._generate_maven_settings() try: sh.mvn( # pylint: disable=no-member 'clean', 'test', '-f', pom_file, '-s', settings_file, _out=sys.stdout, _err=sys.stderr) except sh.ErrorReturnCode as error: raise RuntimeError( "Error invoking mvn: {error}".format(error=error)) from error test_results_output_path = test_results_dir if not os.path.isdir(test_results_dir) or \ len(os.listdir(test_results_dir)) == 0: if fail_on_no_tests is not True: results = { 'result': { 'success': True, 'message': 'unit test step run successfully, but no tests were found' }, 'report-artifacts': [], 'options': { 'pom-path': pom_file, 'fail-on-no-tests': False } } else: # pragma: no cover # Added 'no cover' to bypass missing unit-test step coverage error # that is covered by the following unit test: # test_unit_test_run_attempt_fails_fail_on_no_tests_flag_true raise RuntimeError('Error: No unit tests defined') else: results = { 'result': { 'success': True, 'message': 'unit test step run successfully and junit reports were generated' }, 'options': { 'pom-path': pom_file }, 'report-artifacts': [{ 'name': 'maven unit test results generated using junit', 'path': f'file://{test_results_output_path}' }] } return results
# run mvn release:branch command print "\nBranching repositories" for b in builds: repository = b['repository'] print "\tBranching %s" % repository sh.cd(repository) branchNameArg = "-DbranchName={0}".format(branchName) masterVersion = "-DdevelopmentVersion={0}".format(nextMasterVersion) scmConnectionArg = "-Dscm.connection=scm:git:ssh://{0}@review.motechproject.org:29418/{1}".format(gerritUsername, repository) scmDeveloperConnectinArg = "-Dscm.developerConnection=scm:git:ssh://{0}@review.motechproject.org:29418/{1}".format(gerritUsername, repository) try: for line in sh.mvn("release:branch", branchNameArg, masterVersion, scmConnectionArg, scmDeveloperConnectinArg, _iter=True): if verbose: print(line), except Exception, e: print "\t\t*** Unabe to continue: exception branching %s ***" % b['repository'] print e return 1 sh.cd('..') print "\nUpdate MOTECH Version for module repositories" for b in builds: if b['name'] is "Platform-IntegrationTests": continue repository = b['repository']
def run_maven( #pylint: disable=too-many-arguments mvn_output_file_path, settings_file, pom_file, phases_and_goals, tls_verify=True, additional_arguments=None, profiles=None, no_transfer_progress=True ): """Runs maven using the given configuration. Parameters ---------- mvn_output_file_path : str Path to file containing the maven stdout and stderr output. phases_and_goals : [str] List of maven phases and/or goals to execute. additional_arguments : [str] List of additional arguments to use. pom_file : str (path) pom used when executing maven. tls_verify : boolean Disables TLS Verification if set to False profiles : [str] List of maven profiles to use. no_transfer_progress : boolean `True` to suppress the transfer progress of packages maven downloads. `False` to have the transfer progress printed.\ See https://maven.apache.org/ref/current/maven-embedder/cli.html settings_file : str (path) Maven settings file to use. Returns ------- str Standard Out and Standard Error from running Maven. Raises ------ StepRunnerException If maven returns a none 0 exit code. """ if not isinstance(phases_and_goals, list): phases_and_goals = [phases_and_goals] # create profile argument profiles_arguments = "" if profiles: profiles_arguments = ['-P', f"{','.join(profiles)}"] # create no transfer progress argument no_transfer_progress_argument = None if no_transfer_progress: no_transfer_progress_argument = '--no-transfer-progress' # create tls arguments tls_arguments = [] if not tls_verify: tls_arguments += [ '-Dmaven.wagon.http.ssl.insecure=true', '-Dmaven.wagon.http.ssl.allowall=true', '-Dmaven.wagon.http.ssl.ignore.validity.dates=true', ] if not additional_arguments: additional_arguments = [] # run maven try: with open(mvn_output_file_path, 'w', encoding='utf-8') as mvn_output_file: out_callback = create_sh_redirect_to_multiple_streams_fn_callback([ sys.stdout, mvn_output_file ]) err_callback = create_sh_redirect_to_multiple_streams_fn_callback([ sys.stderr, mvn_output_file ]) sh.mvn( # pylint: disable=no-member *phases_and_goals, '-f', pom_file, '-s', settings_file, *profiles_arguments, no_transfer_progress_argument, *tls_arguments, *additional_arguments, _out=out_callback, _err=err_callback ) except sh.ErrorReturnCode as error: raise StepRunnerException( f"Error running maven. {error}" ) from error
def _run_step(self): """Runs the step implemented by this StepImplementer. Returns ------- StepResult Object containing the dictionary results of this step. """ step_result = StepResult.from_step_implementer(self) pom_file = self.get_value('pom-file') fail_on_no_tests = self.get_value('fail-on-no-tests') # ensure surefire plugin enabled maven_surefire_plugin = self._get_effective_pom_element( element_path=MavenGeneric.SUREFIRE_PLUGIN_XML_ELEMENT_PATH ) if maven_surefire_plugin is None: step_result.success = False step_result.message = 'Unit test dependency "maven-surefire-plugin" ' \ f'missing from effective pom ({self._get_effective_pom()}).' return step_result # get surefire test results dir reports_dir = self._get_effective_pom_element( element_path=MavenGeneric.SUREFIRE_PLUGIN_REPORTS_DIR_XML_ELEMENT_PATH ) if reports_dir is not None: test_results_dir = reports_dir.text else: test_results_dir = os.path.join( os.path.dirname(os.path.abspath(pom_file)), MavenGeneric.DEFAULT_SUREFIRE_PLUGIN_REPORTS_DIR ) settings_file = self._generate_maven_settings() mvn_output_file_path = self.write_working_file('mvn_test_output.txt') try: with open(mvn_output_file_path, 'w') as mvn_output_file: out_callback = create_sh_redirect_to_multiple_streams_fn_callback([ sys.stdout, mvn_output_file ]) err_callback = create_sh_redirect_to_multiple_streams_fn_callback([ sys.stderr, mvn_output_file ]) sh.mvn( # pylint: disable=no-member 'clean', 'test', '-f', pom_file, '-s', settings_file, _out=out_callback, _err=err_callback ) if not os.path.isdir(test_results_dir) or len(os.listdir(test_results_dir)) == 0: if fail_on_no_tests: step_result.message = 'No unit tests defined.' step_result.success = False else: step_result.message = "No unit tests defined, but 'fail-on-no-tests' is False." except sh.ErrorReturnCode as error: step_result.message = "Unit test failures. See 'maven-output'" \ f" and 'surefire-reports' report artifacts for details: {error}" step_result.success = False finally: step_result.add_artifact( description="Standard out and standard error from 'mvn test'.", name='maven-output', value=mvn_output_file_path ) step_result.add_artifact( description="Surefire reports generated from 'mvn test'.", name='surefire-reports', value=test_results_dir ) return step_result
def _run_step(self): """ Runs the TSSC step implemented by this StepImplementer. Returns ------- dict Results of running this step. """ settings_file = self._generate_maven_settings() pom_file = self.get_config_value('pom-file') fail_on_no_tests = self.get_config_value('fail-on-no-tests') selenium_hub_url = self.get_config_value('selenium-hub-url') report_dir = self.get_config_value('report-dir') target_base_url = self._get_target_base_url() if not os.path.exists(pom_file): raise ValueError(f'Given pom file does not exist: {pom_file}') surefire_path = 'mvn:build/mvn:plugins/mvn:plugin/[mvn:artifactId="maven-surefire-plugin"]' maven_surefire_plugin = get_xml_element_by_path( pom_file, surefire_path, default_namespace='mvn') if maven_surefire_plugin is None: raise ValueError( 'Uat dependency "maven-surefire-plugin" missing from POM.') reports_dir = get_xml_element_by_path( pom_file, f'{surefire_path}/mvn:configuration/mvn:reportsDirectory', default_namespace='mvn') if reports_dir is not None: test_results_dir = reports_dir.text else: test_results_dir = os.path.join( os.path.dirname(os.path.abspath(pom_file)), 'target/surefire-reports') try: sh.mvn( # pylint: disable=no-member 'clean', '-Pintegration-test', f'-Dselenium.hub.url={selenium_hub_url}', f'-Dtarget.base.url={target_base_url}', f'-Dcucumber.plugin=html:target/{report_dir}/cucumber.html,' \ f'json:target/{report_dir}/cucumber.json', 'test', '-f', pom_file, '-s', settings_file, _out=sys.stdout, _err=sys.stderr ) except sh.ErrorReturnCode as error: raise RuntimeError(f'Error invoking mvn: {error}') from error if not os.path.isdir(test_results_dir) or len( os.listdir(test_results_dir)) == 0: if fail_on_no_tests is not True: results = { 'result': { 'success': True, 'message': 'Uat step run successfully, but no tests were found' }, 'report-artifacts': [], 'options': { 'pom-path': pom_file, 'fail-on-no-tests': False } } else: # pragma: no cover # Added 'no cover' to bypass missing uat step coverage error # that is covered by the following test: # test_uat_run_attempt_fails_fail_on_no_tests_flag_true raise RuntimeError('Error: No uat defined') else: results = { 'result': { 'success': True, 'message': 'Uat step run successfully and reports were generated' }, 'options': { 'pom-path': pom_file }, 'report-artifacts': [ { 'name': 'Uat results generated', 'path': f'file://{os.path.dirname(os.path.abspath(pom_file))}' \ f'/target/{report_dir}' } ] } return results
# pip3 install sh from sh import mvn, grep, rm, mkdir, sed, wget import csv rm("-rf", "libs") mkdir("libs") tree = sed(sed(mvn("dependency:tree", _tty_out=False), "s/\\[INFO\\]//"), "/\\[WARNING\\]/d") lines = sed( sed( sed( sed( sed(sed(sed(sed(tree, "s/+//"), "s/-//"), "s/\\\//"), "s/\|//g"), "s/ //g"), "s/:jar:/:/"), "s/:/,/g"), "s/\[INFO\]//") for gav in csv.reader(lines): if (len(gav) is not 4): continue # print(gav[0] + " " + gav[1]+ " " + gav[2]+ " " + gav[3]) mkdir("-p", "libs/" + gav[3]) try: wget( "http://central.maven.org/maven2/" + gav[0].replace(".", "/") + "/" + gav[1] + "/" + gav[2] + "/" + gav[1] + "-" + gav[2] + ".jar", "-O", "libs/" + gav[3] + "/" + gav[1] + "-" + gav[2] + ".jar") except: pass
def _run_step(self): """Runs the TSSC step implemented by this StepImplementer. Returns ------- dict Results of running this step. """ pom_file = self.get_config_value('pom-file') artifact_extensions = self.get_config_value('artifact-extensions') artifact_parent_dir = self.get_config_value('artifact-parent-dir') if not os.path.exists(pom_file): raise ValueError('Given pom file does not exist: ' + pom_file) settings_file = self._generate_maven_settings() try: sh.mvn( # pylint: disable=no-member, 'clean', 'install', '-f', pom_file, '-s', settings_file, _out=sys.stdout, _err=sys.stderr) except sh.ErrorReturnCode as error: raise RuntimeError(f'Error invoking mvn: {error}') from error # find the artifacts artifact_file_names = [] artifact_parent_dir_full_path = \ os.listdir(os.path.join( os.path.dirname(os.path.abspath(pom_file)), artifact_parent_dir)) for filename in artifact_parent_dir_full_path: if any(filename.endswith(ext) for ext in artifact_extensions): artifact_file_names.append(filename) # error if we find more then one artifact # see https://projects.engineering.redhat.com/browse/NAPSSPO-546 if len(artifact_file_names) > 1: raise ValueError( 'pom resulted in multiple artifacts with expected artifact extensions ' + '({artifact_extensions}), this is unsupported'.format( artifact_extensions=artifact_extensions)) if len(artifact_file_names) < 1: raise ValueError( 'pom resulted in 0 with expected artifact extensions ' + '({artifact_extensions}), this is unsupported'.format( artifact_extensions=artifact_extensions)) artifact_id = get_xml_element(pom_file, 'artifactId').text group_id = get_xml_element(pom_file, 'groupId').text try: package_type = get_xml_element(pom_file, 'package').text except ValueError: package_type = 'jar' results = { 'artifacts': [{ 'path': os.path.join(os.path.dirname(os.path.abspath(pom_file)), artifact_parent_dir, artifact_file_names[0]), 'artifact-id': artifact_id, 'group-id': group_id, 'package-type': package_type, 'pom-path': pom_file }] } return results