File: set_devpt_version_name.py

package info (click to toggle)
pa-dlna 1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 812 kB
  • sloc: python: 7,526; makefile: 27; sh: 14
file content (64 lines) | stat: -rw-r--r-- 1,953 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
"""Update the version using 'git describe'."""

import sys
import re
import subprocess
from packaging import version as pkg_version

INIT_FILE = 'pa_dlna/__init__.py'

def normalize(version, do_print=False):
    "Check and normalize 'version' as conform to PEP 440."

    try:
        v = pkg_version.Version(version)
        if do_print:
            v_dict = {
                'release': v.release,
                'pre': v.pre,
                'post': v.post,
                'dev': v.dev,
                'local': v.local,
            }
            for name, value in v_dict.items():
                print(f'{name}: {value}', file=sys.stderr)
        return v
    except pkg_version.InvalidVersion as e:
        print(e, file=sys.stderr)
        sys.exit(1)

def main():
    """Set the development version name in INIT_FILE.

    The 'git describe' command outputs:
        release'-'number_of_commits_since_last_tag'-g'short_commit_sha

    After all the characters after the last '-' in the output have been
    striped, the version is normalized (made conform to PEP 440) as:
        release'.post'number_of_commits_since_last_tag

    For example '0.14.post3'.
    """

    version = subprocess.check_output(['git', 'describe'])
    version = version.decode().strip()
    version = normalize(version.rsplit('-', maxsplit=1)[0], do_print=True)
    if version.post is None:
        print(f'*** Error:\n  Cannot set a development version name at release'
              f' {version}.\n  It must be followed by at least one commit.',
              file=sys.stderr)
        sys.exit(1)

    with open(INIT_FILE) as f:
        txt = f.read()

    regexp = re.compile(r"(__version__\s+=\s+)'([^']+)'")
    new_txt = regexp.sub(rf"\1'{version}'", txt)

    with open(INIT_FILE, 'w') as f:
        f.write(new_txt)
    print(f"{INIT_FILE} has been updated with: __version__ = '{version}'",
          file=sys.stderr)

if __name__ == '__main__':
    main()