diff --git a/eessi_bot_job_manager.py b/eessi_bot_job_manager.py index c01b4825..ed02d2c1 100644 --- a/eessi_bot_job_manager.py +++ b/eessi_bot_job_manager.py @@ -43,9 +43,8 @@ # Local application imports (anything from EESSI/eessi-bot-software-layer) from connections import github -from tools import config, run_cmd +from tools import config, job_metadata, run_cmd from tools.args import job_manager_parse -from tools.job_metadata import read_job_metadata_from_file, read_metadata_file from tools.pr_comments import get_submitted_job_comment, update_comment @@ -254,42 +253,6 @@ def determine_finished_jobs(self, known_jobs, current_jobs): return finished_jobs - def read_job_result(self, job_result_file_path): - """ - Read job result file and return the contents of the 'RESULT' section. - - Args: - job_result_file_path (string): path to job result file - - Returns: - (ConfigParser): instance of ConfigParser corresponding to the - 'RESULT' section or None - """ - # reuse function from module tools.job_metadata to read metadata file - result = read_metadata_file(job_result_file_path, self.logfile) - if result and "RESULT" in result: - return result["RESULT"] - else: - return None - - def read_job_test(self, job_test_file_path): - """ - Read job test file and return the contents of the 'TEST' section. - - Args: - job_test_file_path (string): path to job test file - - Returns: - (ConfigParser): instance of ConfigParser corresponding to the - 'TEST' section or None - """ - # reuse function from module tools.job_metadata to read metadata file - test = read_metadata_file(job_test_file_path, self.logfile) - if test and "TEST" in test: - return test["TEST"] - else: - return None - def process_new_job(self, new_job): """ Process a new job by verifying that it is a bot job and if so @@ -335,7 +298,9 @@ def process_new_job(self, new_job): # assuming that a bot job's working directory contains a metadata # file, its existence is used to check if the job belongs to the bot - metadata_pr = read_job_metadata_from_file(job_metadata_path, self.logfile) + metadata_pr = job_metadata.get_section_from_file(job_metadata_path, + job_metadata.JOB_PR_SECTION, + self.logfile) if metadata_pr is None: log(f"No metadata file found at {job_metadata_path} for job {job_id}, so skipping it", @@ -431,7 +396,9 @@ def process_running_jobs(self, running_job): job_metadata_path = os.path.join(job_dir, metadata_file) # check if metadata file exist - metadata_pr = read_job_metadata_from_file(job_metadata_path, self.logfile) + metadata_pr = job_metadata.get_section_from_file(job_metadata_path, + job_metadata.JOB_PR_SECTION, + self.logfile) if metadata_pr is None: raise Exception("Unable to find metadata file") @@ -525,11 +492,13 @@ def process_finished_job(self, finished_job): # check if _bot_jobJOBID.result exits job_result_file = f"_bot_job{job_id}.result" job_result_file_path = os.path.join(new_symlink, job_result_file) - job_results = self.read_job_result(job_result_file_path) + job_results = job_metadata.get_section_from_file(job_result_file_path, + job_metadata.JOB_RESULT_SECTION, + self.logfile) job_result_unknown_fmt = finished_job_comments_cfg[JOB_RESULT_UNKNOWN_FMT] # set fallback comment_description in case no result file was found - # (self.read_job_result returned None) + # (job_metadata.get_section_from_file returned None) comment_description = job_result_unknown_fmt.format(filename=job_result_file) if job_results: # get preformatted comment_description or use previously set default for unknown @@ -552,11 +521,13 @@ def process_finished_job(self, finished_job): # --> bot/test.sh and bot/check-test.sh scripts are run in job script used by bot for 'build' action job_test_file = f"_bot_job{job_id}.test" job_test_file_path = os.path.join(new_symlink, job_test_file) - job_tests = self.read_job_test(job_test_file_path) + job_tests = job_metadata.get_section_from_file(job_test_file_path, + job_metadata.JOB_TEST_SECTION, + self.logfile) job_test_unknown_fmt = finished_job_comments_cfg[JOB_TEST_UNKNOWN_FMT] # set fallback comment_description in case no test file was found - # (self.read_job_result returned None) + # (job_metadata.get_section_from_file returned None) comment_description = job_test_unknown_fmt.format(filename=job_test_file) if job_tests: # get preformatted comment_description or use previously set default for unknown @@ -576,7 +547,9 @@ def process_finished_job(self, finished_job): # obtain id of PR comment to be updated (from file '_bot_jobID.metadata') metadata_file = f"_bot_job{job_id}.metadata" job_metadata_path = os.path.join(new_symlink, metadata_file) - metadata_pr = read_job_metadata_from_file(job_metadata_path, self.logfile) + metadata_pr = job_metadata.get_section_from_file(job_metadata_path, + job_metadata.JOB_PR_SECTION, + self.logfile) if metadata_pr is None: raise Exception("Unable to find metadata file ... skip updating PR comment") diff --git a/tests/test_tools_job_metadata.py b/tests/test_tools_job_metadata.py index f5542c6f..0d788248 100644 --- a/tests/test_tools_job_metadata.py +++ b/tests/test_tools_job_metadata.py @@ -11,21 +11,21 @@ import os -from tools.job_metadata import read_job_metadata_from_file +from tools.job_metadata import get_section_from_file, JOB_PR_SECTION -def test_read_job_metadata_from_file(tmpdir): - logfile = os.path.join(tmpdir, 'test_read_job_metadata_from_file.log') +def test_get_section_from_file(tmpdir): + logfile = os.path.join(tmpdir, 'test_get_section_from_file.log') # if metadata file does not exist, we should get None as return value path = os.path.join(tmpdir, 'test.metadata') - assert read_job_metadata_from_file(path, logfile) is None + assert get_section_from_file(path, JOB_PR_SECTION, logfile) is None with open(path, 'w') as fp: fp.write('''[PR] repo=test pr_number=12345''') - metadata_pr = read_job_metadata_from_file(path, logfile) + metadata_pr = get_section_from_file(path, JOB_PR_SECTION, logfile) expected = { "repo": "test", "pr_number": "12345", diff --git a/tools/job_metadata.py b/tools/job_metadata.py index b1cb7b7b..e93218b0 100644 --- a/tools/job_metadata.py +++ b/tools/job_metadata.py @@ -27,6 +27,7 @@ JOB_RESULT_SECTION = "RESULT" JOB_RESULT_STATUS = "status" JOB_RESULT_SUCCESS = "SUCCESS" +JOB_TEST_SECTION = "TEST" def create_metadata_file(job, job_id, pr_comment):