Debian Codegraph @

tasks/__init__.py

raw symbols SCIP
1 import pathlib
2 import shutil
3 import subprocess
4
5 import invoke
6 import parver
7
8 from towncrier._builder import (
9 find_fragments, render_fragments, split_fragments,
10 )
11 from towncrier._settings import load_config
12
13
14 ROOT = pathlib.Path(__file__).resolve().parent.parent
15
16 INIT_PY = ROOT.joinpath('src', 'shellingham', '__init__.py')
17
18
19 @invoke.task()
20 def clean(ctx):
21 """Clean previously built package artifacts.
22 """
23 ctx.run(f'python setup.py clean')
24 dist = ROOT.joinpath('dist')
25 print(f'[clean] Removing {dist}')
26 if dist.exists():
27 shutil.rmtree(str(dist))
28
29
30 def _read_version():
31 out = subprocess.check_output(['git', 'tag'], encoding='ascii')
32 version = max(parver.Version.parse(v).normalize() for v in (
33 line.strip() for line in out.split('\n')
34 ) if v)
35 return version
36
37
38 def _write_version(v):
39 lines = []
40 with INIT_PY.open() as f:
41 for line in f:
42 if line.startswith('__version__ = '):
43 line = f'__version__ = {repr(str(v))}\n'
44 lines.append(line)
45 with INIT_PY.open('w', newline='\n') as f:
46 f.write(''.join(lines))
47
48
49 def _render_log():
50 """Totally tap into Towncrier internals to get an in-memory result.
51 """
52 config = load_config(ROOT)
53 definitions = config['types']
54 fragments, fragment_filenames = find_fragments(
55 pathlib.Path(config['directory']).absolute(),
56 config['sections'],
57 None,
58 definitions,
59 )
60 rendered = render_fragments(
61 pathlib.Path(config['template']).read_text(encoding='utf-8'),
62 config['issue_format'],
63 split_fragments(fragments, definitions),
64 definitions,
65 config['underlines'][1:],
66 )
67 return rendered
68
69
70 REL_TYPES = ('major', 'minor', 'patch',)
71
72
73 def _bump_release(version, type_):
74 if type_ not in REL_TYPES:
75 raise ValueError(f'{type_} not in {REL_TYPES}')
76 index = REL_TYPES.index(type_)
77 next_version = version.base_version().bump_release(index)
78 print(f'[bump] {version} -> {next_version}')
79 return next_version
80
81
82 PREBUMP = 2 # Default to next patch number.
83
84
85 def _prebump(version):
86 next_version = version.bump_release(PREBUMP).bump_dev()
87 print(f'[bump] {version} -> {next_version}')
88 return next_version
89
90
91 @invoke.task(pre=[clean])
92 def release(ctx, type_, repo):
93 """Make a new release.
94 """
95 version = _read_version()
96 version = _bump_release(version, type_)
97 _write_version(version)
98
99 # Needs to happen before Towncrier deletes fragment files.
100 tag_content = _render_log()
101
102 ctx.run('towncrier')
103
104 ctx.run(f'git commit -am "Release {version}"')
105
106 tag_content = tag_content.replace('"', '\\"')
107 ctx.run(f'git tag -a {version} -m "Version {version}\n\n{tag_content}"')
108
109 ctx.run(f'setl publish --repository="{repo}"')
110
111 version = _prebump(version)
112 _write_version(version)
113
114 ctx.run(f'git commit -am "Prebump to {version}"')