#!/usr/bin/env python3 # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; coding:utf-8 -*-*- # # Copyright 2025 Kenneth Loafman # # This file is part of duplicity. # # Duplicity is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2 of the License, or (at your # option) any later version. # # Duplicity is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with duplicity; if not, write to the Free Software Foundation, # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os import re import sys import time reldate: str = time.strftime("%B %d, %Y", time.gmtime(int(os.environ.get("SOURCE_DATE_EPOCH", time.time())))) def set_version(new_version): """ Mod the versioned files and add correct version and reldate """ # run from project root os.chdir(f"{os.path.dirname(__file__)}/..") # .TH DUPLICITY 1 "$reldate" "new_version $version" "User Manuals" \" -*- nroff -*- version_source( r"""\.TH\ DUPLICITY\ 1\ "(?P[^"]*)"\ "Version\ (?P[^"]*)"\ "User\ Manuals"\ \\"\ """ r"""\ \-\*\-\ nroff\ \-\*\-""", r"""\.TH\ DUPLICITY\ 1\ "(?P[^"]*)"\ "Version\ (?P[^"]*)"\ "User\ Manuals"\ \\"\ """ r"""\ \-\*\-\ nroff\ \-\*\-""", os.path.join("man", "duplicity.1"), new_version, ) # __version__ = "$version" version_source( r'__version__: str = "(?P[^"]*)"', r'__reldate__: str = "(?P[^"]*)"', os.path.join("duplicity", "__init__.py"), new_version, ) # version: $version version_source( r"version: (?P.*)\n", None, os.path.join("snap", "snapcraft.yaml"), new_version, ) # new_version: str = "$version" version_source( r'Version: str = "(?P[^\"]*)"', None, os.path.join(".", "setup.py"), new_version, ) # version = "$version" version_source( r'version = "(?P[^\"]*)"', None, os.path.join(".", "pyproject.toml"), new_version, ) def version_source(version_patt: str, reldate_patt: str, pathname: str, new_version: str): """ Copy source to dest, substituting current version with new_version current release date with today's date, i.e. December 28, 2008. """ with open(pathname, "rt") as fd: buffer = fd.read() # process version if version_patt: if m := re.search(version_patt, buffer): version_sub = re.escape(m.group("version")) newbuffer = re.sub(version_sub, new_version, buffer) if newbuffer == buffer: print(f"ERROR: version unchanged in {pathname}.", file=sys.stderr) else: buffer = newbuffer print(f"Substituted '{version_sub}' with '{new_version}' in {pathname}.") else: print(f"ERROR: {version_patt} not found in {pathname}.", file=sys.stderr) sys.exit(1) # process reldate if reldate_patt: if m := re.search(reldate_patt, buffer): reldate_sub = re.escape(m.group("reldate")) newbuffer = re.sub(reldate_sub, reldate, buffer) if newbuffer == buffer: print(f"ERROR: reldate unchanged in {pathname}.", file=sys.stderr) else: buffer = newbuffer print(f"Substituted '{reldate_sub}' with '{reldate}' in {pathname}.") else: print(f"ERROR: {reldate_patt} not found in {pathname}.", file=sys.stderr) sys.exit(1) # write new file with open(pathname, "w") as fd: fd.write(buffer) if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: setversion ") sys.exit(1) set_version(sys.argv[1])