class Agent(object): def __init__(self, element): self._element = element self.__thing_with_resources = ThingWithResources(element) @property def hostname(self): return self._element.attrib['hostname'] @property def resources(self): return self.__thing_with_resources.resources def ensure_resource(self, resource): self.__thing_with_resources.ensure_resource(resource)
def __init__(self, element): self.__element = element self.__thing_with_resources = ThingWithResources(element)
class Job(CommonEqualityMixin): def __init__(self, element): self.__element = element self.__thing_with_resources = ThingWithResources(element) def __repr__(self): return "Job('%s', %s)" % (self.name, self.tasks) @property def name(self): return self.__element.attrib['name'] @property def has_timeout(self): return 'timeout' in self.__element.attrib @property def timeout(self): if not self.has_timeout: raise RuntimeError("Job (%s) does not have timeout" % self) return self.__element.attrib['timeout'] @timeout.setter def timeout(self, timeout): self.__element.attrib['timeout'] = timeout def set_timeout(self, timeout): self.timeout = timeout return self @property def runs_on_all_agents(self): return self.__element.attrib.get('runOnAllAgents', 'false') == 'true' @runs_on_all_agents.setter def runs_on_all_agents(self, run_on_all_agents): self.__element.attrib['runOnAllAgents'] = 'true' if run_on_all_agents else 'false' def set_runs_on_all_agents(self, run_on_all_agents=True): self.runs_on_all_agents = run_on_all_agents return self @property def resources(self): return self.__thing_with_resources.resources def ensure_resource(self, resource): self.__thing_with_resources.ensure_resource(resource) return self @property def artifacts(self): artifact_elements = PossiblyMissingElement(self.__element).possibly_missing_child("artifacts").iterator return set([Artifact.get_artifact_for(e) for e in artifact_elements]) def ensure_artifacts(self, artifacts): if artifacts: artifacts_ensurance = Ensurance(self.__element).ensure_child("artifacts") artifacts_to_add = artifacts.difference(self.artifacts) for artifact in artifacts_to_add: artifact.append_to(artifacts_ensurance) return self @property def tabs(self): return [Tab(e.attrib['name'], e.attrib['path']) for e in PossiblyMissingElement(self.__element).possibly_missing_child('tabs').findall('tab')] def ensure_tab(self, tab): tab_ensurance = Ensurance(self.__element).ensure_child("tabs") if self.tabs.count(tab) == 0: tab.append_to(tab_ensurance) return self @property def tasks(self): return [Task(e) for e in PossiblyMissingElement(self.__element).possibly_missing_child("tasks").iterator] def add_task(self, task): return task.append_to(self.__element) def ensure_task(self, task): if self.tasks.count(task) == 0: return task.append_to(self.__element) else: return task def without_any_tasks(self): PossiblyMissingElement(self.__element).possibly_missing_child("tasks").remove_all_children() return self @property def environment_variables(self): return self.__thing_with_environment_variables.environment_variables @property def encrypted_environment_variables(self): return self.__thing_with_environment_variables.encrypted_environment_variables def ensure_environment_variables(self, environment_variables): self.__thing_with_environment_variables.ensure_environment_variables(environment_variables) return self def ensure_encrypted_environment_variables(self, environment_variables): self.__thing_with_environment_variables.ensure_encrypted_environment_variables(environment_variables) return self def without_any_environment_variables(self): self.__thing_with_environment_variables.remove_all() return self @property def __thing_with_environment_variables(self): return ThingWithEnvironmentVariables(self.__element) def reorder_elements_to_please_go(self): # see https://github.com/SpringerSBM/gomatic/issues/6 move_all_to_end(self.__element, "environment_variables") move_all_to_end(self.__element, "tasks") move_all_to_end(self.__element, "tabs") move_all_to_end(self.__element, "resources") move_all_to_end(self.__element, "artifacts") def as_python_commands_applied_to_stage(self): result = 'job = stage.ensure_job("%s")' % self.name if self.artifacts: if len(self.artifacts) > 1: artifacts_sorted = list(self.artifacts) artifacts_sorted.sort(key=lambda artifact: str(artifact)) result += '.ensure_artifacts(set(%s))' % artifacts_sorted else: artifact, = self.artifacts result += '.ensure_artifacts({%s})' % artifact result += self.__thing_with_environment_variables.as_python() for resource in self.resources: result += '.ensure_resource("%s")' % resource for tab in self.tabs: result += '.ensure_tab(%s)' % tab if self.has_timeout: result += '.set_timeout("%s")' % self.timeout if self.runs_on_all_agents: result += '.set_runs_on_all_agents()' for task in self.tasks: # we add instead of ensure because we know it is starting off empty and need to handle duplicate tasks result += "\njob.add_task(%s)" % task return result