Coverage for src/lcdoc/mkdocs/lp/plugs/python/pyplugs/git_changelog/__init__.py: 76.04%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2## Changelog
4Creates CHANGELOG.md (using git-changelog)
6File: `<project root>/CHANGELOG.md`. Reference in docs like:
8```bash
9~/repos/docutools❯ cat docs/about/changelog.md
10\{\!CHANGELOG.md!}
11```
13(without the backslashes)
16### Templates
18Links default jinja templates over to docs/lcd/changelog.
19In order to use your own, overwrite that directory.
22### Config
24```python
25 style = Choice(['angular', 'basic', 'atom'], default='angular')
26 versioning = Choice(['auto', 'semver', 'calver'], default='auto')
27```
29when set to auto we derive by inspecting your last git tag - if like X.foo.bar with X a
30number > 2000 we set to calver.
32You can set $versioning also via environ, which will have precedence then.
34"""
37from mkdocs.config import config_options lp|index.md
40from lcdoc.mkdocs import markdown lp|index.md
41from lcdoc.mkdocs.lp.plugs import python lp|index.md
42from lcdoc.mkdocs.tools import MDPlugin, app, link_assets lp|index.md
43from lcdoc.tools import dirname, os, project, read_file, write_file lp|index.md
45config, page, Session, lpkw = (python.config, python.page, python.Session, python.lpkw) lp|index.md
47formatted = True lp|index.md
50def latest_ver(changelog): lp|index.md
51 # depends on presence of 'Compare with' within the templates
52 try: lp|about/changelog.md
53 return changelog.split('Compare with', 1)[1].split('\n', 1)[0].split(']', 1)[0] lp|about/changelog.md
54 except Exception as exc:
55 msg = 'Not able to extract latest ver'
56 app.warn(msg, exc=exc)
57 return msg
60def git_changelog(commit_style, d_root, d_tmpl): lp|index.md
61 """A version of git-changelog monkey patched, so that versions have zero padded
62 months and days replaced to non zero padded versions
64 Otherwise the semver lib would complain about non-compliancy.
65 """
66 argv = ['-s', commit_style, '-t', 'path:%s' % d_tmpl, d_root] lp|about/changelog.md
67 from git_changelog import build lp|about/changelog.md
69 # monkey patched bump function:
70 bmp = lambda v, p, b: b(v.replace('.0', '.'), p) 70 ↛ exitline 70 didn't run the lambda on line 70lp|about/changelog.md
71 build.bump = lambda version, part='patch', b=build.bump: bmp(version, part, b) 71 ↛ exitline 71 didn't run the lambda on line 71lp|about/changelog.md
72 from git_changelog import cli lp|about/changelog.md
73 from io import StringIO lp|about/changelog.md
74 from contextlib import redirect_stdout lp|about/changelog.md
76 c = StringIO() lp|about/changelog.md
77 with redirect_stdout(c): lp|about/changelog.md
78 cli.main(argv) lp|about/changelog.md
79 cl = c.getvalue() lp|about/changelog.md
80 app.info('changelog created', latest=latest_ver(cl)) lp|about/changelog.md
81 return cl lp|about/changelog.md
84def gen_change_log(d_assets, versioning_scheme, commit_style): lp|index.md
85 """
86 Problem: The git-changelog cmd uses Jinja and wants .md
87 mkdocs macros also, different contexts though -> crash.
88 So we cannot have .md sources in the docs folder -> have to change on the fly when
89 using git-changelog. Here we do that. Also we dyn set the versioning message.
90 """
91 dr = project.root(config()) lp|about/changelog.md
92 d, dtmp = (dirname, dr + '/build/git_changelog_tmpl') lp|about/changelog.md
94 def set_version_scheme(fn, ver): lp|about/changelog.md
95 # todo: do it in jinaja in the template itself:
96 CV = 'This project adheres to [CalVer Versioning](http://calver.org) ' lp|about/changelog.md
97 CV += '![](https://img.shields.io/badge/calver-YYYY.M.D-22bfda.svg).' lp|about/changelog.md
98 SV = 'This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).' lp|about/changelog.md
99 VSM = CV if ver == 'calver' else SV lp|about/changelog.md
100 s = read_file(fn).replace('_VERSION_SCHEME_MSG_', VSM) lp|about/changelog.md
101 write_file(fn, s) lp|about/changelog.md
103 dcl = d_assets + '/keepachangelog' lp|about/changelog.md
104 os.makedirs(dtmp, exist_ok=True) lp|about/changelog.md
105 ver = versioning_scheme lp|about/changelog.md
106 if ver == 'auto': 106 ↛ 107line 106 didn't jump to line 107, because the condition on line 106 was never truelp|about/changelog.md
107 hint = 'export $versioning or set "versioning" in mkdocs plugin config '
108 hint += 'to calver or semver'
109 gt = os.popen('cd "%s" && git tag | tail -n 1' % dr).read().strip()
110 if not gt:
111 app.die('Cannot derive versioning scheme, no git tags yet', hint=hint)
112 ver = 'semver'
113 try:
114 if int(gt.split('.', 1)[0]) > 2000:
115 ver = 'calver'
116 except:
117 pass
118 app.info('Versioning derived from git tags', versioning=ver)
120 # '/home/gk/repos/docutools/docs/lcd/git_changelog/keepachangelog'
121 for k in os.listdir(dcl): lp|about/changelog.md
122 fn = dtmp + '/' + k.replace('.tmpl', '') lp|about/changelog.md
123 shutil.copyfile(dcl + '/' + k, fn) lp|about/changelog.md
124 if k == 'changelog.md.tmpl': lp|about/changelog.md
125 set_version_scheme(fn, ver) lp|about/changelog.md
126 return git_changelog(commit_style, dr, dtmp) lp|about/changelog.md
129def register(fmts): lp|index.md
130 fmts['git_changelog'] = make_changelog lp|index.md
133def g(key, dflt, *l): lp|index.md
134 for d in l: lp|about/changelog.md
135 v = d.get(key) lp|about/changelog.md
136 if v: lp|about/changelog.md
137 return v lp|about/changelog.md
138 return dflt lp|about/changelog.md
141def make_changelog(s, **show_kw): lp|index.md
142 l = (show_kw, lpkw(), os.environ) lp|about/changelog.md
143 style = g('commit_style', 'angular', *l) lp|about/changelog.md
144 ver = g('versioning', 'semver', *l) lp|about/changelog.md
145 d_assets = link_assets(python, __file__, config()) lp|about/changelog.md
146 os.system('ls docs/lcd/git_changelog') lp|about/changelog.md
148 r = gen_change_log(d_assets, ver, style) lp|about/changelog.md
149 return r lp|about/changelog.md