File: _version.py

package info (click to toggle)
python-rcssmin 1%3A1.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,196 kB
  • sloc: python: 2,379; ansic: 1,216; sh: 110; makefile: 20
file content (136 lines) | stat: -rw-r--r-- 4,492 bytes parent folder | download | duplicates (4)
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
129
130
131
132
133
134
135
136
# -*- encoding: ascii -*-
"""
Release code
~~~~~~~~~~~~

"""

import errno as _errno
import os as _os


def update(ctx):
    """ Update version in relevant places """
    version = ctx.run('python setup.py --version', hide=True).stdout.strip()

    _userdoc(ctx, version)
    _download(ctx, version)
    _changes(ctx, version)


def _userdoc(ctx, version):
    """ Update version in userdoc """
    short = '.'.join(version.split('.')[:2])
    conf = _os.path.join(ctx.doc.sphinx.source, 'conf.py')

    with open(conf, 'rb') as fp:
        lines = fp.read().decode('latin-1').splitlines(True)

    seen = set()
    with open(conf, 'wb') as fp:
        for line in lines:
            if line.startswith('version'):
                line = u'version = %r\n' % (short,)
                seen.add('version')
            elif line.startswith('release'):
                line = u'release = %r\n' % (version,)
                seen.add('release')
            fp.write(line.encode('latin-1'))

    assert len(seen) == 2, "version/release not found in userdoc/conf.py"


def _download(ctx, version):  # noqa
    """ Update version in download files """
    # pylint: disable = too-many-branches, too-many-statements

    filename = _os.path.join(ctx.doc.sphinx.source, 'website_download.txt')
    isdev = 'dev' in version

    dllines, dlpath = [], ''
    if isdev:
        oldstable, hasstable = [], False
        try:
            with open(filename, 'rb') as fp:
                lines = fp.read().decode('latin-1').splitlines(True)
        except IOError as e:
            if e.errno != _errno.ENOENT:
                raise
        else:
            for line in lines:
                if line.startswith('.. begin stable'):
                    hasstable, dllines = True, oldstable
                oldstable.append(line)
        if not hasstable:
            dlpath = 'dev/'

    newdev = []
    with open(filename + '.in', 'rb') as fp:
        lines = fp.read().decode('latin-1').splitlines(True)
    if not dllines:
        dllines = lines
    else:
        for line in lines:
            if newdev:
                newdev.append(line)
                if line.startswith('.. end dev'):
                    break
            elif line.startswith('.. begin dev'):
                newdev.append(line)
        else:
            ctx.fail("Incomplete/missing dev marker in %s"
                     % (filename + '.in',))

    instable, indev = [], []
    with open(filename, 'wb') as fp:
        for line in dllines:
            if instable:
                instable.append(line)
                if line.startswith('.. end stable'):
                    if isdev:
                        res = ''.join(instable) if hasstable else ''
                    else:
                        res = (
                            ''.join(instable)
                            .replace('@@VERSION@@', version)
                            .replace('@@PATH@@', '')
                        )
                    fp.write(res.encode('latin-1'))
                    instable = []
            elif indev:
                indev.append(line)
                if line.startswith('.. end dev'):
                    if not isdev:
                        res = ''.join([indev[0], indev[-1]])
                    else:
                        res = (
                            ''.join(newdev or indev)
                            .replace('@@DEVVERSION@@', version)
                            .replace('@@PATH@@', 'dev/')
                        )
                    fp.write(res.encode('latin-1'))
                    indev = []
            elif line.startswith('.. begin stable'):
                instable.append(line)
            elif line.startswith('.. begin dev'):
                indev.append(line)
            elif isdev and hasstable:
                fp.write(line.encode('latin-1'))
            else:
                fp.write(line
                         .replace('@@VERSION@@', version)
                         .replace('@@PATH@@', dlpath)
                         .encode('latin-1'))


def _changes(ctx, version):
    """ Update version in CHANGES """
    fname = ctx.shell.native('docs/CHANGES')
    with open(fname, 'rb') as fp:
        lines = fp.read().decode('latin-1').splitlines(True)

    with open(fname, 'wb') as fp:
        for line in lines:
            if line.rstrip() == 'Changes with version':
                line = "%s %s\n" % (line.rstrip(), version)
            fp.write(line.encode('latin-1'))