From 8297b2aeb2b3f8b5124fac67d67d05a517d33fdc Mon Sep 17 00:00:00 2001 From: bgme Date: Tue, 22 Aug 2023 02:39:54 +0800 Subject: [PATCH] add use-shutil-move option to config file --- archrepo2/repomon.py | 19 +++++++++++++++---- misc/archrepo2.ini.example | 12 ++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/archrepo2/repomon.py b/archrepo2/repomon.py index bbd1c6a..bb046ae 100755 --- a/archrepo2/repomon.py +++ b/archrepo2/repomon.py @@ -168,7 +168,7 @@ class EventHandler(pyinotify.ProcessEvent): _n_running = 0 def my_init( - self, filter_pkg, supported_archs, config, wm, + self, filter_pkg, supported_archs, config, wm, move_func, ): notification_type = config.get( @@ -184,6 +184,7 @@ class EventHandler(pyinotify.ProcessEvent): notification_type.replace('-', '_'), ) + self.move_func = move_func self.filter_pkg = filter_pkg self.moved_away = {} self.created = {} @@ -332,7 +333,7 @@ class EventHandler(pyinotify.ProcessEvent): newd = os.path.join(base, act.arch) newpath = os.path.join(newd, file) if not same_existent_file(path, newpath): - shutil.move(path, newpath) + self.move_func(path, newpath) act.path = newpath path = newpath @@ -513,6 +514,13 @@ def repomon(config): # assume none of the archs has regex meta characters regex = re.compile(r'(?:^|/)[^.].*-[^-]+-[\d.]+-(?:' + '|'.join(supported_archs) + r')\.pkg\.tar\.(?:xz|zst)(?:\.sig)?$') + _use_shutil_move = config.getboolean('use-shutil-move', False) + + if _use_shutil_move: + move_func = shutil.move + else: + move_func = os.rename + filter_func = partial(filter_pkg, regex) handler = EventHandler( filter_func, @@ -520,6 +528,7 @@ def repomon(config): supported_archs = supported_archs, config = config, wm = wm, + move_func = move_func ) ioloop = IOLoop.current() ret = [pyinotify.TornadoAsyncNotifier( @@ -536,6 +545,7 @@ def repomon(config): path = config.get('spool-directory'), dstpath = os.path.join(config.get('path'), 'any'), wm = wm, + move_func = move_func ) ret.append(pyinotify.TornadoAsyncNotifier( wm, default_proc_fun=handler, @@ -545,7 +555,8 @@ def repomon(config): return ret class SpoolHandler(pyinotify.ProcessEvent): - def my_init(self, filter_pkg, path, dstpath, wm): + def my_init(self, filter_pkg, path, dstpath, wm, move_func): + self.move_func = move_func self.filter_pkg = filter_pkg self.dstpath = dstpath self._ioloop = IOLoop.current() @@ -602,4 +613,4 @@ class SpoolHandler(pyinotify.ProcessEvent): def dispatch(self, path): filename = os.path.basename(path) - shutil.move(path, os.path.join(self.dstpath, filename)) + self.move_func(path, os.path.join(self.dstpath, filename)) diff --git a/misc/archrepo2.ini.example b/misc/archrepo2.ini.example index f4127d2..26a155f 100644 --- a/misc/archrepo2.ini.example +++ b/misc/archrepo2.ini.example @@ -57,4 +57,16 @@ notification-secret: JiUHuGY987G76djItfOskOj # If for any reason, you don't want actual database creation or update: #without-db: true +# Whether to use shutil.move() to move files, +# the default is to use os.rename() to move files. +# +# os. rename() +# If src and dst are on same filesystem, rename src to dst. +# If src and dst are on different filesystems, the operation will fail. +# +# shutil.move() +# If the destination is on the current filesystem, then os.rename() is used. +# Otherwise, src is copied to dst using shutil.copy2() and then removed. +use-shutil-move: false + # vim: se ft=dosini: