Add check for existence of src and dst file

This commit is contained in:
leafee98 2025-03-26 00:30:07 +08:00
parent f267efa66b
commit f58ea58bd6

View file

@ -19,6 +19,9 @@ http_pool = urllib3.PoolManager()
class InvalidConfigException(Exception):
pass
class FileNotExistsException(Exception):
pass
class CopyEntry:
def __init__(self, d: Dict):
@ -46,15 +49,21 @@ class Config:
self.certs.append(CertEntry(cert_d))
def copyFile(src: str, dst: str):
src_mtime = os.path.getmtime(src)
dst_mtime = os.path.getmtime(dst)
if not os.path.exists(src):
raise FileNotExistsException('Src file not exists: {}'.format(src))
if src_mtime > dst_mtime:
if not os.path.exists(os.path.dirname(dst)):
raise FileNotExistsException('Direcotyr of dst not exists: {}'.format(dst))
is_dst_exists = os.path.exists(dst)
src_mtime = os.path.getmtime(src)
dst_mtime = os.path.getmtime(dst) if is_dst_exists else 0
if is_dst_exists and src_mtime < dst_mtime:
logger.info('Skipping file: {} --> {}'.format(src, dst))
else:
logger.info('Copying file: {} --> {}'.format(src, dst))
shutil.copyfile(src, dst)
else:
logger.info('Skipping file: {} --> {}'.format(src, dst))
def copyCert(entry: CertEntry):
copyFile(entry.priv.src, entry.priv.dst)