Skip to content

Commit

Permalink
Implement hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jrha committed Jul 19, 2024
1 parent 8e0539e commit 071a033
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions mrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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))
Expand All @@ -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):
Expand All @@ -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"
Expand Down

0 comments on commit 071a033

Please sign in to comment.