2018-04-02 11:43:32 -06:00
|
|
|
import collections
|
|
|
|
import copy
|
|
|
|
import itertools
|
|
|
|
import os
|
2018-04-02 19:36:18 -06:00
|
|
|
import sys
|
2018-04-02 11:43:32 -06:00
|
|
|
|
2020-04-03 21:29:10 -06:00
|
|
|
CHANGELOG_PATHS = (
|
|
|
|
'docs/changelog.txt',
|
|
|
|
'scripts/changelog.txt',
|
|
|
|
'library/xml/changelog.txt',
|
|
|
|
)
|
|
|
|
|
2018-04-02 11:43:32 -06:00
|
|
|
CHANGELOG_SECTIONS = [
|
|
|
|
'New Plugins',
|
|
|
|
'New Scripts',
|
|
|
|
'New Tweaks',
|
|
|
|
'New Features',
|
|
|
|
'New Internal Commands',
|
|
|
|
'Fixes',
|
|
|
|
'Misc Improvements',
|
|
|
|
'Removed',
|
2018-05-12 09:51:36 -06:00
|
|
|
'API',
|
2018-04-02 11:43:32 -06:00
|
|
|
'Internals',
|
|
|
|
'Lua',
|
|
|
|
'Ruby',
|
2018-07-07 19:41:43 -06:00
|
|
|
'Structures',
|
2018-04-02 11:43:32 -06:00
|
|
|
]
|
|
|
|
|
2018-04-05 09:21:45 -06:00
|
|
|
REPLACEMENTS = {
|
|
|
|
'`search`': '`search-plugin`',
|
|
|
|
}
|
2018-04-05 09:02:14 -06:00
|
|
|
|
2018-05-12 09:51:36 -06:00
|
|
|
def to_title_case(word):
|
|
|
|
if word == word.upper():
|
|
|
|
# Preserve acronyms
|
|
|
|
return word
|
|
|
|
return word[0].upper() + word[1:].lower()
|
|
|
|
|
2018-04-05 09:02:14 -06:00
|
|
|
def find_all_indices(string, substr):
|
|
|
|
start = 0
|
|
|
|
while True:
|
|
|
|
i = string.find(substr, start)
|
|
|
|
if i == -1:
|
|
|
|
return
|
|
|
|
yield i
|
|
|
|
start = i + 1
|
|
|
|
|
2018-04-05 09:21:45 -06:00
|
|
|
def replace_text(string, replacements):
|
|
|
|
for old_text, new_text in replacements.items():
|
|
|
|
new_string = ''
|
|
|
|
new_string_end = 0 # number of characters from string in new_string
|
|
|
|
for i in find_all_indices(string, old_text):
|
|
|
|
if i > 0 and string[i - 1] == '!':
|
|
|
|
# exempt if preceded by '!'
|
|
|
|
new_string += string[new_string_end:i - 1]
|
|
|
|
new_string += old_text
|
|
|
|
else:
|
|
|
|
# copy until this occurrence
|
|
|
|
new_string += string[new_string_end:i]
|
|
|
|
new_string += new_text
|
|
|
|
new_string_end = i + len(old_text)
|
|
|
|
new_string += string[new_string_end:]
|
|
|
|
string = new_string
|
|
|
|
return string
|
|
|
|
|
2018-04-02 11:43:32 -06:00
|
|
|
class ChangelogEntry(object):
|
|
|
|
def __init__(self, text, section, stable_version, dev_version):
|
|
|
|
text = text.lstrip('- ')
|
|
|
|
# normalize section to title case
|
2018-05-12 09:51:36 -06:00
|
|
|
self.section = ' '.join(map(to_title_case, section.strip().split()))
|
2018-04-02 11:43:32 -06:00
|
|
|
self.stable_version = stable_version
|
|
|
|
self.dev_version = dev_version
|
|
|
|
self.dev_only = text.startswith('@')
|
|
|
|
text = text.lstrip('@ ')
|
|
|
|
self.children = []
|
|
|
|
|
|
|
|
split_index = text.find(': ')
|
|
|
|
if split_index != -1:
|
|
|
|
self.feature, description = text[:split_index], text[split_index+1:]
|
|
|
|
if description.strip():
|
|
|
|
self.children.insert(0, description.strip())
|
|
|
|
else:
|
|
|
|
self.feature = text
|
2018-04-02 19:36:18 -06:00
|
|
|
self.feature = self.feature.replace(':\\', ':').rstrip(':')
|
2018-04-02 11:43:32 -06:00
|
|
|
|
|
|
|
self.sort_key = self.feature.upper()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return 'ChangelogEntry(%r, %r)' % (self.feature, self.children)
|
|
|
|
|
|
|
|
def parse_changelog():
|
|
|
|
entries = []
|
|
|
|
|
2020-04-03 21:29:10 -06:00
|
|
|
for fpath in CHANGELOG_PATHS:
|
|
|
|
if not os.path.isfile(fpath):
|
|
|
|
continue
|
|
|
|
with open(fpath) as f:
|
|
|
|
cur_stable = None
|
|
|
|
cur_dev = None
|
|
|
|
cur_section = None
|
|
|
|
last_entry = None
|
|
|
|
multiline = ''
|
|
|
|
for line_id, line in enumerate(f.readlines()):
|
|
|
|
line_id += 1
|
|
|
|
|
|
|
|
if multiline:
|
|
|
|
multiline += line
|
|
|
|
elif '[[[' in line:
|
|
|
|
multiline = line.replace('[[[', '')
|
|
|
|
|
|
|
|
if ']]]' in multiline:
|
|
|
|
line = multiline.replace(']]]', '')
|
|
|
|
multiline = ''
|
|
|
|
elif multiline:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not line.strip() or line.startswith('==='):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if line.startswith('##'):
|
|
|
|
cur_section = line.lstrip('#').strip()
|
|
|
|
elif line.startswith('#'):
|
|
|
|
cur_dev = line.lstrip('#').strip().lower()
|
|
|
|
if ('alpha' not in cur_dev and 'beta' not in cur_dev and
|
|
|
|
'rc' not in cur_dev):
|
|
|
|
cur_stable = cur_dev
|
|
|
|
elif line.startswith('-'):
|
|
|
|
if not cur_stable or not cur_dev or not cur_section:
|
|
|
|
raise ValueError(
|
|
|
|
'%s:%i: Entry without section' % (fpath, line_id))
|
|
|
|
last_entry = ChangelogEntry(line.strip(), cur_section,
|
|
|
|
cur_stable, cur_dev)
|
|
|
|
entries.append(last_entry)
|
|
|
|
elif line.lstrip().startswith('-'):
|
|
|
|
if not cur_stable or not cur_dev:
|
|
|
|
raise ValueError(
|
|
|
|
'%s:%i: Sub-entry without section' % (fpath, line_id))
|
|
|
|
if not last_entry:
|
|
|
|
raise ValueError(
|
|
|
|
'%s:%i: Sub-entry without parent' % (fpath, line_id))
|
|
|
|
last_entry.children.append(line.strip('- \n'))
|
|
|
|
else:
|
|
|
|
raise ValueError('Invalid line: ' + line)
|
|
|
|
|
|
|
|
if not entries:
|
|
|
|
raise RuntimeError('No changelog files with contents found')
|
2018-04-02 11:43:32 -06:00
|
|
|
|
|
|
|
return entries
|
|
|
|
|
|
|
|
def consolidate_changelog(all_entries):
|
|
|
|
for sections in all_entries.values():
|
|
|
|
for section, entries in sections.items():
|
|
|
|
# sort() is stable, so reverse entries so that older entries for the
|
|
|
|
# same feature are on top
|
|
|
|
entries.reverse()
|
|
|
|
entries.sort(key=lambda entry: entry.sort_key)
|
|
|
|
new_entries = []
|
|
|
|
for feature, group in itertools.groupby(entries,
|
|
|
|
lambda e: e.feature):
|
|
|
|
old_entries = list(group)
|
|
|
|
children = list(itertools.chain(*[entry.children
|
|
|
|
for entry in old_entries]))
|
|
|
|
new_entry = copy.deepcopy(old_entries[0])
|
|
|
|
new_entry.children = children
|
|
|
|
new_entries.append(new_entry)
|
|
|
|
entries[:] = new_entries
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-05 09:55:25 -06:00
|
|
|
def print_changelog(versions, all_entries, path, replace=True, prefix=''):
|
2018-04-02 11:43:32 -06:00
|
|
|
# all_entries: version -> section -> entry
|
|
|
|
with open(path, 'w') as f:
|
2018-04-05 09:55:25 -06:00
|
|
|
def write(line):
|
|
|
|
if replace:
|
|
|
|
line = replace_text(line, REPLACEMENTS)
|
|
|
|
f.write(prefix + line + '\n')
|
2018-04-02 11:43:32 -06:00
|
|
|
for version in versions:
|
|
|
|
sections = all_entries[version]
|
|
|
|
if not sections:
|
|
|
|
continue
|
|
|
|
version = 'DFHack ' + version
|
|
|
|
write(version)
|
|
|
|
write('=' * len(version))
|
|
|
|
write('')
|
|
|
|
for section in CHANGELOG_SECTIONS:
|
|
|
|
entries = sections[section]
|
|
|
|
if not entries:
|
|
|
|
continue
|
|
|
|
write(section)
|
|
|
|
write('-' * len(section))
|
|
|
|
for entry in entries:
|
|
|
|
if len(entry.children) == 1:
|
|
|
|
write('- ' + entry.feature + ': ' +
|
|
|
|
entry.children[0].strip('- '))
|
|
|
|
continue
|
|
|
|
elif entry.children:
|
|
|
|
write('- ' + entry.feature + ':')
|
|
|
|
write('')
|
|
|
|
for child in entry.children:
|
|
|
|
write(' - ' + child)
|
|
|
|
write('')
|
|
|
|
else:
|
|
|
|
write('- ' + entry.feature)
|
|
|
|
write('')
|
|
|
|
write('')
|
|
|
|
|
|
|
|
|
2018-04-05 09:55:25 -06:00
|
|
|
def generate_changelog(all=False):
|
2018-04-02 11:43:32 -06:00
|
|
|
entries = parse_changelog()
|
|
|
|
|
|
|
|
# scan for unrecognized sections
|
|
|
|
for entry in entries:
|
|
|
|
if entry.section not in CHANGELOG_SECTIONS:
|
|
|
|
raise RuntimeWarning('Unknown section: ' + entry.section)
|
|
|
|
|
|
|
|
# ordered versions
|
|
|
|
versions = ['future']
|
|
|
|
# map versions to stable versions
|
|
|
|
stable_version_map = {}
|
|
|
|
# version -> section -> entry
|
|
|
|
stable_entries = collections.defaultdict(lambda:
|
|
|
|
collections.defaultdict(list))
|
|
|
|
dev_entries = collections.defaultdict(lambda:
|
|
|
|
collections.defaultdict(list))
|
2018-04-05 09:55:25 -06:00
|
|
|
|
2018-04-02 11:43:32 -06:00
|
|
|
for entry in entries:
|
2018-04-05 09:55:25 -06:00
|
|
|
# build list of all versions
|
2018-04-02 11:43:32 -06:00
|
|
|
if entry.dev_version not in versions:
|
|
|
|
versions.append(entry.dev_version)
|
|
|
|
stable_version_map.setdefault(entry.dev_version, entry.stable_version)
|
2018-04-05 09:55:25 -06:00
|
|
|
|
2018-04-02 11:43:32 -06:00
|
|
|
if not entry.dev_only:
|
2018-04-05 09:55:25 -06:00
|
|
|
# add non-dev-only entries to both changelogs
|
2018-04-02 11:43:32 -06:00
|
|
|
stable_entries[entry.stable_version][entry.section].append(entry)
|
|
|
|
dev_entries[entry.dev_version][entry.section].append(entry)
|
|
|
|
|
|
|
|
consolidate_changelog(stable_entries)
|
|
|
|
|
|
|
|
print_changelog(versions, stable_entries, 'docs/_auto/news.rst')
|
|
|
|
print_changelog(versions, dev_entries, 'docs/_auto/news-dev.rst')
|
|
|
|
|
2018-04-05 09:55:25 -06:00
|
|
|
if all:
|
|
|
|
for version in versions:
|
2018-05-18 12:52:25 -06:00
|
|
|
if version not in stable_version_map:
|
|
|
|
print('warn: skipping ' + version)
|
|
|
|
continue
|
2018-04-05 09:55:25 -06:00
|
|
|
if stable_version_map[version] == version:
|
|
|
|
version_entries = {version: stable_entries[version]}
|
|
|
|
else:
|
|
|
|
version_entries = {version: dev_entries[version]}
|
|
|
|
print_changelog([version], version_entries,
|
|
|
|
'docs/_changelogs/%s-github.txt' % version,
|
|
|
|
replace=False)
|
|
|
|
print_changelog([version], version_entries,
|
|
|
|
'docs/_changelogs/%s-reddit.txt' % version,
|
|
|
|
replace=False,
|
|
|
|
prefix='> ')
|
|
|
|
|
2018-04-02 19:36:18 -06:00
|
|
|
return entries
|
|
|
|
|
2018-04-02 11:43:32 -06:00
|
|
|
if __name__ == '__main__':
|
2018-04-05 09:55:25 -06:00
|
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-a', '--all', action='store_true',
|
|
|
|
help='Print changelogs for all versions to docs/_changelogs')
|
|
|
|
parser.add_argument('-c', '--check', action='store_true',
|
|
|
|
help='Check that all entries are printed')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-04-02 11:43:32 -06:00
|
|
|
os.chdir(os.path.abspath(os.path.dirname(__file__)))
|
|
|
|
os.chdir('..')
|
2018-04-05 09:55:25 -06:00
|
|
|
entries = generate_changelog(all=args.all)
|
|
|
|
|
|
|
|
if args.check:
|
2018-04-02 19:36:18 -06:00
|
|
|
with open('docs/_auto/news.rst') as f:
|
|
|
|
content_stable = f.read()
|
|
|
|
with open('docs/_auto/news-dev.rst') as f:
|
|
|
|
content_dev = f.read()
|
|
|
|
for entry in entries:
|
|
|
|
for description in entry.children:
|
|
|
|
if not entry.dev_only and description not in content_stable:
|
|
|
|
print('stable missing: ' + description)
|
|
|
|
if description not in content_dev:
|
|
|
|
print('dev missing: ' + description)
|