github.com/hashicorp/packer@v1.14.3/scripts/sort-md-list.py (about)

     1  #!/usr/bin/env python3
     2  # Copyright (c) HashiCorp, Inc.
     3  # SPDX-License-Identifier: BUSL-1.1
     4  
     5  """
     6  sort-md-list.py sorts markdown lists
     7  
     8  Use this script to prepare sections of the changelog.
     9  
    10  this script expects a bulleted markdown list in stdout
    11  
    12  for example
    13  
    14  ./sort-md-list.py - <<EOF
    15  * builder/amazon: Only delete temporary key if we created one. [GH-4850]
    16  * core: Correctly reject config files which have junk after valid json.
    17          [GH-4906]
    18      * builder/azure: Replace calls to panic with error returns. [GH-4846]
    19  * communicator/winrm: Use KeepAlive to keep long-running connections open.  [GH-4952]
    20  EOF
    21  
    22  output:
    23  
    24  * builder/amazon: Only delete temporary key if we created one. [GH-4850]
    25  * builder/azure: Replace calls to panic with error returns. [GH-4846]
    26  * communicator/winrm: Use KeepAlive to keep long-running connections open.
    27      [GH-4952]
    28  * core: Correctly reject config files which have junk after valid json.
    29      [GH-4906]
    30  
    31  As you can see, the output is sorted and spaced appropriately.
    32  
    33  Limitations:
    34      * nested lists are not supported
    35      * must be passed a list to stdin, it does not process the changelog
    36      * whitespace within the list is elided
    37  """
    38  
    39  import fileinput
    40  import sys
    41  import textwrap
    42  
    43  
    44  if __name__ == '__main__':
    45      lines = []
    46      working = []
    47      for line in fileinput.input():
    48          line = line.strip()
    49          if line.startswith('*'):
    50              if len(working):
    51                  lines.append( " ".join(working))
    52              working = [line]
    53          else:
    54              working.append(line)
    55      if len(working):
    56          lines.append( " ".join(working))
    57  
    58      # take care of blank line at start of selection
    59      sys.stdin.seek(0)
    60      if sys.stdin.readlines()[0].strip() == "":
    61          print()
    62  
    63      for line in sorted(lines, key=lambda s: s.lower()):
    64          if line.strip() == "":
    65              continue
    66          # print "-"*79
    67          wrapped = textwrap.wrap(line, 79)
    68          print( wrapped[0] )
    69          indented = " ".join([s.strip() for s in wrapped[1:]])
    70          for iline in textwrap.wrap(indented, 79-4):
    71              print("     " + iline)
    72  
    73      # take care of blank line at end of selection
    74      sys.stdin.seek(0)
    75      if sys.stdin.readlines()[-1].strip() == "":
    76          print()