From f4e76b11ef0859a5a7e0ca9153878b1e0bc2ba88 Mon Sep 17 00:00:00 2001 From: Infrid Date: Sun, 3 Jun 2018 17:01:16 +0100 Subject: [PATCH] backups now mirror the project hierarchy --- gitkup.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/gitkup.py b/gitkup.py index 8e9874f..ed7b317 100644 --- a/gitkup.py +++ b/gitkup.py @@ -18,6 +18,8 @@ # import signal import argparse from datetime import datetime +import pathlib +import urllib.parse import configparser import logging @@ -179,15 +181,19 @@ def gitkup(BACKUP_DIR, URL, TOKEN): projects = r.json() for project in projects: url = project["ssh_url_to_repo"] - localPath = "{}/{}.git".format(BACKUP_DIR, project["path"]) - if not os.path.exists(localPath): - LOGGER.info("Create backup for %s", localPath) - Repo.clone_from(url, localPath) + url_parsed = urllib.parse.urlparse(project['http_url_to_repo']) + repo_path = pathlib.Path(url_parsed.path.lstrip('/')) + local_path = pathlib.Path(BACKUP_DIR) / repo_path + + if not local_path.is_dir(): + LOGGER.info("Create backup for %s", local_path) + local_path.mkdir(parents=True) + Repo.clone_from(url, str(local_path.resolve())) LOGGER.info("%s cloned", url) else: - LOGGER.info("Update backup for %s", localPath) + LOGGER.info("Update backup for %s", local_path) # TODO: refactor this shit - dir_command = ['cd', localPath] + dir_command = ['cd', str(local_path.resolve())] git_command = ['git', 'remote', 'update'] git_query = Popen(git_command) backup_state = Popen(dir_command, cwd=BACKUP_DIR,