From a98c025718d5108915a01ba7d004be73b06c0a6f Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Sat, 2 Mar 2019 18:25:11 -0600 Subject: [PATCH] Add a clone_repository function. --- ci/jobgenerator.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/ci/jobgenerator.py b/ci/jobgenerator.py index 139edb0..d57f80c 100755 --- a/ci/jobgenerator.py +++ b/ci/jobgenerator.py @@ -15,5 +15,38 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import git +from os import getenv +from yaml import CLoader +from yaml import load as yaml_load +from shutil import rmtree +from tempfile import mkdtemp + +class Generator: + def clone_repository(self): + # Assuming this is ran inside Jenkins, this fetches the env vars set in + # the job config. If those don't exist, we need to throw an error, + # because otherwise we have no metadata to actually pull from + metadata_url = getenv("METADATA_URL") + metadata_repo_name = getenv("METADATA_REPO_NAME") + if not metadata_url or metadata_url == "" or not metadata_repo_name \ + or metadata_repo_name == "": + raise ValueError("METADATA_URL and METADATA_REPO_NAME must be set") + + # Create a temporary directory in the most secure manner possible and + # clone the metadata, throwing the directory away when we're done + try: + metadata_loc = mkdtemp() + git.Git(metadata_loc).clone(metadata_url) + + # Load ci.conf and parse it + with open(metadata_loc + "/" + metadata_repo_name + "/ci.conf") \ + as metadata_conf_file: + metadata_conf = yaml_load(metadata_conf_file, Loader=CLoader) + finally: + rmtree(metadata_loc) + + if __name__ == "__main__": - print("Hello world!") + generator = Generator() + generator.clone_repository()