File: setversion

package info (click to toggle)
duplicity 3.0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,492 kB
  • sloc: python: 25,523; sh: 976; ansic: 392; makefile: 83
file content (126 lines) | stat: -rwxr-xr-x 4,195 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; encoding: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<reldate>[^"]*)"\ "Version\ (?P<version>[^"]*)"\ "User\ Manuals"\ \\"\ """
        r"""\ \-\*\-\ nroff\ \-\*\-""",
        r"""\.TH\ DUPLICITY\ 1\ "(?P<reldate>[^"]*)"\ "Version\ (?P<version>[^"]*)"\ "User\ Manuals"\ \\"\ """
        r"""\ \-\*\-\ nroff\ \-\*\-""",
        os.path.join("man", "duplicity.1"),
        new_version,
    )

    # __version__ = "$version"
    version_source(
        r'__version__: str = "(?P<version>[^"]*)"',
        r'__reldate__: str = "(?P<reldate>[^"]*)"',
        os.path.join("duplicity", "__init__.py"),
        new_version,
    )

    # version: $version
    version_source(
        r"version: (?P<version>.*)\n",
        None,
        os.path.join("snap", "snapcraft.yaml"),
        new_version,
    )

    # new_version: str = "$version"
    version_source(
        r'Version: str = "(?P<version>[^\"]*)"',
        None,
        os.path.join(".", "setup.py"),
        new_version,
    )

    # version = "$version"
    version_source(
        r'version = "(?P<version>[^\"]*)"',
        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 <new_version>")
        sys.exit(1)
    set_version(sys.argv[1])