From 071a033264e74524807784f26d879584f1532d8c Mon Sep 17 00:00:00 2001 From: James Adams Date: Fri, 19 Jul 2024 16:06:55 +0100 Subject: [PATCH] Implement hooks --- mrepo.py | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/mrepo.py b/mrepo.py index 4d269ce..60bc182 100755 --- a/mrepo.py +++ b/mrepo.py @@ -237,6 +237,16 @@ def __init__(self): self.rsyncoptions = self.getoption('main', 'rsync-options', '-rtHL --partial') self.rsynctimeout = self.getoption('main', 'rsync-timeout', None) + self.hooks = {} + + self.hooks['pre-mirror'] = self.getoption('main', 'hook-pre-mirror', None) + self.hooks['post-mirror'] = self.getoption('main', 'hook-post-mirror', None) + self.hooks['pre-createmd'] = self.getoption('main', 'hook-pre-createmd', None) + self.hooks['post-createmd'] = self.getoption('main', 'hook-post-createmd', None) + self.hooks['pre-new-repo'] = self.getoption('main', 'hook-pre-new-repo', None) + self.hooks['lock'] = self.getoption('main', 'hook-lock', None) + self.hooks['unlock'] = self.getoption('main', 'hook-unlock', None) + self.alldists = [] self.dists = [] @@ -358,7 +368,6 @@ def __init__(self, dist, arch, config): self.sslkey = None self.sslca = None - def rewrite(self): "Rewrite (string) attributes to replace variables by other (string) attributes" varlist = VARIABLES @@ -468,10 +477,16 @@ def __init__(self, name, url, dist, config): def __repr__(self): return self.name + def _call_hook(self, hook_name): + if hook_name in CONFIG.hooks and CONFIG.hooks[hook_name]: + run("%s '%s' '%s'" % (CONFIG.hooks[hook_name], hook_name, self.srcdir)) + def mirror(self): "Check URL and pass on to mirror-functions." global EXITCODE + self._call_hook('pre-mirror') + ### Make a snapshot of the directory self.oldlist = self.rpmlist() self.newlist = self.oldlist @@ -501,6 +516,8 @@ def mirror(self): ### Make a snapshot of the directory self.newlist = self.rpmlist() + self._call_hook('post-mirror') + def rpmlist(self): "Capture a list of packages in the repository" filelist = set() @@ -558,8 +575,12 @@ def writesha1(self): def lock(self, action): if OPTIONS.dryrun: return True + + self._call_hook('lock') + lockfile = path_join(CONFIG.lockdir, self.dist.nick, action + '-' + self.name + '.lock') mkdir(os.path.dirname(lockfile)) + try: file_object = os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o0600) info(6, '%s: Setting lock %s' % (self.dist.nick, lockfile)) @@ -586,6 +607,9 @@ def lock(self, action): def unlock(self, action): if OPTIONS.dryrun: return + + self._call_hook('unlock') + lockfile = path_join(CONFIG.lockdir, self.dist.nick, action + '-' + self.name + '.lock') info(6, '%s: Removing lock %s' % (self.dist.nick, lockfile)) if path_exists(lockfile): @@ -608,18 +632,20 @@ def createmd(self): global EXITCODE metadata = ('createrepo', 'repomd') - if not self.changed and not OPTIONS.force: - return + self._call_hook('pre-createmd') - try: - ### Generate repository metadata - for metadata in self.dist.metadata: - if metadata in ('createrepo', 'repomd'): - self.repomd() - - except MrepoGenerateException as instance: - error(0, 'Generating repo failed for %s with message:\n %s' % (self.name, instance.value)) - EXITCODE = 2 + if self.changed or OPTIONS.force: + try: + ### Generate repository metadata + for metadata in self.dist.metadata: + if metadata in ('createrepo', 'repomd'): + self.repomd() + + except MrepoGenerateException as instance: + error(0, 'Generating repo failed for %s with message:\n %s' % (self.name, instance.value)) + EXITCODE = 2 + + self._call_hook('post-createmd') def repomd(self): "Create a repomd repository"