File: _activate_cola.py

package info (click to toggle)
git-cola 4.16.1-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,844 kB
  • sloc: python: 37,980; sh: 298; makefile: 223; xml: 108; tcl: 62
file content (128 lines) | stat: -rw-r--r-- 3,910 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Developer wrapper script helper functions
import configparser
import datetime
import os
import sys


def activate():
    """Activate the cola development environment"""
    initialize_python()
    initialize_version()


def get_prefix():
    """Return the path to the source tree"""
    realpath = os.path.abspath(os.path.realpath(__file__))
    return os.path.dirname(os.path.dirname(realpath))


def initialize_python():
    """Add the source directory to the python sys.path."""
    sys.path.insert(1, get_prefix())


def initialize_version():
    """Replace version.SCM_VERSION when running from the source tree"""
    scm_version = get_version()
    if scm_version:
        # version.SCM_VERSION = version
        update_pkginfo_version(scm_version)


def get_version():
    """Calculate a setuptools-scm compatible version number from the git worktree"""
    from cola import git

    worktree = git.Git(worktree=get_prefix())
    if not worktree.is_valid():
        return None
    status, out, _ = worktree.describe(dirty=True, long=True, match='v[0-9]*.[0-9]*')
    if status != 0 or not out:
        return None
    # We cap the number of splits to 3 (4-parts) but only 2 splits (3-parts) are also
    # accepted. Anything less is not a "git describe" output we support.
    parts = out.lstrip('v').split('-', 3)
    num_parts = len(parts)
    if num_parts < 3:
        return None
    # If we are clean and we are pointing at a tag then setuptools-scm will report
    # just the version number without any extra version details.
    if num_parts == 3 and parts[1] == '0':
        return parts[0]

    # Transform v4.8.2-24-gd7b743a2 into 4.8.3.dev28+gd7b743a2
    # Transform v4.8.2-24-gd7b743a2-dirty into 4.8.3.dev28+gd7b743a2.d20241005
    numbers = parts[0].split('.')
    # Increment the last number.
    if numbers:
        try:
            last_number = f'{int(numbers[-1]) + 1}'
        except ValueError:
            last_number = '1'
        numbers[-1] = last_number
        parts[0] = '.'.join(numbers)

    version = f'{parts[0]}.dev{parts[1]}+{parts[2]}'
    # Worktree is dirty. Append the current date.
    if num_parts == 4:
        now = datetime.datetime.now()
        date_string = now.strftime('.d%Y%m%d')
        version += date_string

    return version


def update_pkginfo_version(scm_version):
    """Update git_cola.egg_info/PKG-INFO with the specified version"""
    from cola import version

    pkginfo = os.path.join(get_prefix(), 'git_cola.egg-info', 'PKG-INFO')
    content, pkginfo_version = get_pkginfo_version(pkginfo)
    # If there's nothing to update then we can set the SCM_VERSION.
    if not content or not pkginfo_version:
        version.SCM_VERSION = scm_version
        return
    # If the versions match then there's nothing to do.
    if scm_version == pkginfo_version:
        return
    # Rewrite the PKG-INFO file to reflect the current version.
    new_lines = []
    replaced = False
    token = 'Version: '
    new_version = f'Version: {scm_version}'
    for line in content.splitlines():
        if not replaced and line.startswith(token):
            new_lines.append(new_version)
            replaced = True
        else:
            new_lines.append(line)
    new_lines.append('')

    try:
        with open(pkginfo, 'w', encoding='utf-8') as pkginfo_file:
            pkginfo_file.write('\n'.join(new_lines))
    except OSError:
        pass


def get_pkginfo_version(pkginfo):
    """Return the version from the PKG-INFO file"""
    version = None
    content = None
    try:
        with open(pkginfo, encoding='utf-8') as pkginfo_file:
            content = pkginfo_file.read()
    except OSError:
        return (content, version)

    token = 'Version: '
    for line in content.splitlines():
        if line.startswith(token):
            version = line[len(token) :]
            break

    return (content, version)


activate()