""" This script runs in a loop and clone or update the clone of the ansible repo hosted in pagure.io """ from __future__ import print_function import datetime import logging import os import sched import subprocess import sys import time _log = logging.getLogger(__name__) s = sched.scheduler(time.time, time.sleep) # Time between each run in seconds delay = 3 * 60 # Folder where the mirror should be mirror_folder = "mirrors" # URLs to mirror urls = ["https://pagure.io/Fedora-Infra/ansible.git"] def run_command(command, cwd=None): """ Run the specified command in a specific working directory if one is specified. :arg command: the command to run :type command: list :kwarg cwd: the working directory in which to run this command :type cwd: str or None """ output = None try: output = subprocess.check_output(command, cwd=cwd, stderr=subprocess.PIPE) except subprocess.CalledProcessError as e: _log.error("Command `%s` return code: `%s`", " ".join(command), e.returncode) _log.error("stdout:\n-------\n%s", e.stdout) _log.error("stderr:\n-------\n%s", e.stderr) raise return output def mirror_from_pagure(): """ Sync packagers and schedules the next one. """ for url in urls: name = url.rsplit("/", 1)[-1] dest_folder = os.path.join(mirror_folder, name) if not os.path.exists(dest_folder): cmd = ["git", "clone", "--mirror", url] run_command(cmd, cwd=mirror_folder) cmd = ["git", "fetch"] run_command(cmd, cwd=dest_folder) cmd = ["git", "fsck"] run_command(cmd, cwd=dest_folder) print( "%s Run finished, next run in %s seconds" % (datetime.datetime.utcnow().isoformat(), delay) ) s.enter(delay, 1, mirror_from_pagure, argument=()) def main(): """ Run the program and log error and exit if needed. """ try: run() except Exception as err: print("Error: %s" % err) sys.exit(1) def run(): """ Schedule the first test and run the scheduler. """ if not os.path.exists(mirror_folder): raise OSError("No folder %s found on disk" % mirror_folder) s.enter(0, 1, mirror_from_pagure, argument=()) s.run() if __name__ == "__main__": try: main() except KeyboardInterrupt: from code import InteractiveConsole InteractiveConsole(locals={"s": s}).interact( "ENTERING THE DEBUG CONSOLE:\n s is the scheduler\n ^d to quit", "LEAVING THE DEBUG CONSOLE", )