File: versionupdate.py

package info (click to toggle)
nomacs 3.21.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,560 kB
  • sloc: cpp: 49,511; python: 718; sh: 241; xml: 175; makefile: 22
file content (114 lines) | stat: -rw-r--r-- 2,956 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
"""Increments the build version of a C++ projects.

This script increments the build version after every build of
a Visual Stuido project. It manipulates the DkVersion.h file in order
to do so. The version number defined there is displayed in help and
used for the VS_VERSION_INFO in the *.rc file
"""

__author__ = "Markus Diem"
__credits__ = ["Markus Diem"]
__license__ = "GPLv3"
__version__ = "0.3"
__maintainer__ = "Markus Diem"
__email__ = "markus@nomacs.org"
__status__ = "Production"


OUTPUT_NAME = "versionupdate"


def update(filepath, copy=False):
    from shutil import move
    from os import remove
    from utils.fun import version

    v = version()

    dstpath, ext = os.path.splitext(filepath)
    dstpath += "-versioned" + ext

    with open(filepath, "r") as src:
        with open(dstpath, "w") as dst:
            for l in src.readlines():
                l = update_version_string(v, l)
                l = update_version_rc(v, l)
                l = update_version_patch(v, l)

                dst.write(l)

    # replace current file?
    if not copy:
        remove(filepath)
        move(dstpath, filepath)


def update_version_string(version, line):
    # searching (DkVersion.h): #define NOMACS_VERSION_STR "3.14.42\0"
    # searching (msi installer): <?define ProductVersion = "3.14.42"?>
    # searching (inno installer): define MyAppVersion "3.14.42"
    if (
        "NOMACS_VERSION_STR" in line
        or "<?define ProductVersion" in line
        or "define MyAppVersion" in line
    ):
        str_ver = line.split('"')
        line = str_ver[0] + '"' + version + '"' + str_ver[-1]

    return line


def update_version_rc(version, line):
    # searching: #define NOMACS_VERSION_RC 3,14,42
    if "NOMACS_VERSION_RC" in line:
        str_ver = line.split(" ")
        str_ver[-1] = version.replace(".", ",")

        line = " ".join(str_ver) + "\n"

    return line


def update_version_patch(version, line):
    # get patch from 3.14.42
    vs = version.split(".")

    if len(vs) != 3:
        print("WARNING: could not split version: " + version)
        return

    # searching: #define NOMACS_VER_PATCH 0
    if "NOMACS_VER_PATCH" in line:
        str_ver = line.split(" ")
        str_ver[-1] = vs[-1]

        line = " ".join(str_ver) + "\n"

    return line


if __name__ == "__main__":
    import argparse
    import os

    parser = argparse.ArgumentParser(
        description="Increments the build version of a C++ project."
    )

    parser.add_argument(
        "inputfile",
        type=str,
        help="""full path to the file who's version should be updated""",
    )
    parser.add_argument(
        "--copy", action="store_true", help="""if set, a _ver file will be created"""
    )

    args = parser.parse_args()

    if not os.path.isfile(args.inputfile):
        print("input file does not exist: " + args.inputfile)
        exit()

    update(args.inputfile, args.copy)