GitlabCIJob.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. def match_group(self, group):
  26. """
  27. Match group by tags of job.
  28. All filters values of group should be included in tags.
  29. :param group: case group to match
  30. :return: True or False
  31. """
  32. match_result = False
  33. for _ in range(1):
  34. if "case group" in self:
  35. # this job is already assigned
  36. break
  37. for value in group.filters.values():
  38. if value not in self["tags"]:
  39. break
  40. else:
  41. continue
  42. break
  43. else:
  44. match_result = True
  45. return match_result
  46. def assign_group(self, group):
  47. """
  48. assign a case group to a test job.
  49. :param group: the case group to assign
  50. """
  51. self["case group"] = group
  52. def output_config(self, file_path):
  53. """
  54. output test config to the given path.
  55. file name will be job_name.yml
  56. :param file_path: output file path
  57. :return: None
  58. """
  59. file_name = os.path.join(file_path, self["name"] + ".yml")
  60. if "case group" in self:
  61. with open(file_name, "w") as f:
  62. yaml.dump(self["case group"].output(), f)