packer/scripts/sort-md-list.py
hashicorp-copywrite[bot] 19055df3ec
[COMPLIANCE] License changes (#12568)
* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at https://hashi.co/license-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUSL-1.1

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-10 15:53:29 -07:00

76 lines
2.2 KiB
Python
Executable file

#!/usr/bin/env python3
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1
"""
sort-md-list.py sorts markdown lists
Use this script to prepare sections of the changelog.
this script expects a bulleted markdown list in stdout
for example
./sort-md-list.py - <<EOF
* builder/amazon: Only delete temporary key if we created one. [GH-4850]
* core: Correctly reject config files which have junk after valid json.
[GH-4906]
* builder/azure: Replace calls to panic with error returns. [GH-4846]
* communicator/winrm: Use KeepAlive to keep long-running connections open. [GH-4952]
EOF
output:
* builder/amazon: Only delete temporary key if we created one. [GH-4850]
* builder/azure: Replace calls to panic with error returns. [GH-4846]
* communicator/winrm: Use KeepAlive to keep long-running connections open.
[GH-4952]
* core: Correctly reject config files which have junk after valid json.
[GH-4906]
As you can see, the output is sorted and spaced appropriately.
Limitations:
* nested lists are not supported
* must be passed a list to stdin, it does not process the changelog
* whitespace within the list is elided
"""
import fileinput
import sys
import textwrap
if __name__ == '__main__':
lines = []
working = []
for line in fileinput.input():
line = line.strip()
if line.startswith('*'):
if len(working):
lines.append( " ".join(working))
working = [line]
else:
working.append(line)
if len(working):
lines.append( " ".join(working))
# take care of blank line at start of selection
sys.stdin.seek(0)
if sys.stdin.readlines()[0].strip() == "":
print()
for line in sorted(lines, key=lambda s: s.lower()):
if line.strip() == "":
continue
# print "-"*79
wrapped = textwrap.wrap(line, 79)
print( wrapped[0] )
indented = " ".join([s.strip() for s in wrapped[1:]])
for iline in textwrap.wrap(indented, 79-4):
print(" " + iline)
# take care of blank line at end of selection
sys.stdin.seek(0)
if sys.stdin.readlines()[-1].strip() == "":
print()