GitlabCIJob.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import yaml
  16. class Job(dict):
  17. """
  18. Gitlab CI job
  19. :param job: job data loaded from .gitlab-ci.yml
  20. :param job_name: job name
  21. """
  22. def __init__(self, job, job_name):
  23. super(Job, self).__init__(job)
  24. self['name'] = job_name
  25. self.tags = set(self['tags'])
  26. def match_group(self, group):
  27. """
  28. Match group by tags of job.
  29. All filters values of group should be included in tags.
  30. :param group: case group to match
  31. :return: True or False
  32. """
  33. match_result = False
  34. if 'case group' not in self and group.ci_job_match_keys == self.tags:
  35. # group not assigned and all tags match
  36. match_result = True
  37. return match_result
  38. def assign_group(self, group):
  39. """
  40. assign a case group to a test job.
  41. :param group: the case group to assign
  42. """
  43. self['case group'] = group
  44. def output_config(self, file_path):
  45. """
  46. output test config to the given path.
  47. file name will be job_name.yml
  48. :param file_path: output file path
  49. :return: None
  50. """
  51. file_name = os.path.join(file_path, self['name'] + '.yml')
  52. if 'case group' in self:
  53. with open(file_name, 'w') as f:
  54. yaml.safe_dump(self['case group'].output(), f, encoding='utf-8', default_flow_style=False)