File: versionincrement.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 (81 lines) | stat: -rw-r--r-- 2,052 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
#!/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
"""
import logging
import os

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


OUTPUT_NAME = "versionincrement"

def increment(line: str, newversion: str):

    str_ver = line.split(":")

    # searching version.cache: version:0.3.5
    if "version" in line and len(str_ver) == 2:

        old_v = str_ver[1].split(".")

        if len(old_v) == 3:
            # increment
            build = str(int(old_v[-1])+1)

        line = str_ver[0] + ":" + newversion + "." + build

        # send status message only once
        print("[Version Incrementer] " + line.replace("version:", "version updated: "))

    return line


def update_version(version: str):
    from shutil import move
    from os import remove

    from utils.fun import version_cache

    filepath = version_cache()
    tmppath = filepath + "tmp"

    if os.path.exists(filepath):
        with open(filepath, "r") as src:
            with open(tmppath, "w") as dst:
                for l in src.readlines():

                    l = increment(l, version)
                    dst.write(l)
    
        # swap
        remove(filepath)
        move(tmppath, filepath)

    else:
        with open(filepath, "w") as dst:
            dst.write("version:" + version)


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(
       description='Increments the version in a version cache file.')

    parser.add_argument("version", type=str,
                        help="""current nomacs version - typically optained from cmake""")

    args = parser.parse_args()

    update_version(args.version)