xref: /aosp_15_r20/external/libaom/tools/wrap-commit-msg.py (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*77c1e3ccSAndroid Build Coastguard Worker##
3*77c1e3ccSAndroid Build Coastguard Worker## Copyright (c) 2016, Alliance for Open Media. All rights reserved.
4*77c1e3ccSAndroid Build Coastguard Worker##
5*77c1e3ccSAndroid Build Coastguard Worker## This source code is subject to the terms of the BSD 2 Clause License and
6*77c1e3ccSAndroid Build Coastguard Worker## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7*77c1e3ccSAndroid Build Coastguard Worker## was not distributed with this source code in the LICENSE file, you can
8*77c1e3ccSAndroid Build Coastguard Worker## obtain it at www.aomedia.org/license/software. If the Alliance for Open
9*77c1e3ccSAndroid Build Coastguard Worker## Media Patent License 1.0 was not distributed with this source code in the
10*77c1e3ccSAndroid Build Coastguard Worker## PATENTS file, you can obtain it at www.aomedia.org/license/patent.
11*77c1e3ccSAndroid Build Coastguard Worker##
12*77c1e3ccSAndroid Build Coastguard Worker"""Wraps paragraphs of text, preserving manual formatting
13*77c1e3ccSAndroid Build Coastguard Worker
14*77c1e3ccSAndroid Build Coastguard WorkerThis is like fold(1), but has the special convention of not modifying lines
15*77c1e3ccSAndroid Build Coastguard Workerthat start with whitespace. This allows you to intersperse blocks with
16*77c1e3ccSAndroid Build Coastguard Workerspecial formatting, like code blocks, with written prose. The prose will
17*77c1e3ccSAndroid Build Coastguard Workerbe wordwrapped, and the manual formatting will be preserved.
18*77c1e3ccSAndroid Build Coastguard Worker
19*77c1e3ccSAndroid Build Coastguard Worker * This won't handle the case of a bulleted (or ordered) list specially, so
20*77c1e3ccSAndroid Build Coastguard Worker   manual wrapping must be done.
21*77c1e3ccSAndroid Build Coastguard Worker
22*77c1e3ccSAndroid Build Coastguard WorkerOccasionally it's useful to put something with explicit formatting that
23*77c1e3ccSAndroid Build Coastguard Workerdoesn't look at all like a block of text inline.
24*77c1e3ccSAndroid Build Coastguard Worker
25*77c1e3ccSAndroid Build Coastguard Worker  indicator = has_leading_whitespace(line);
26*77c1e3ccSAndroid Build Coastguard Worker  if (indicator)
27*77c1e3ccSAndroid Build Coastguard Worker    preserve_formatting(line);
28*77c1e3ccSAndroid Build Coastguard Worker
29*77c1e3ccSAndroid Build Coastguard WorkerThe intent is that this docstring would make it through the transform
30*77c1e3ccSAndroid Build Coastguard Workerand still be legible and presented as it is in the source. If additional
31*77c1e3ccSAndroid Build Coastguard Workercases are handled, update this doc to describe the effect.
32*77c1e3ccSAndroid Build Coastguard Worker"""
33*77c1e3ccSAndroid Build Coastguard Worker
34*77c1e3ccSAndroid Build Coastguard Worker__author__ = "[email protected]"
35*77c1e3ccSAndroid Build Coastguard Workerimport textwrap
36*77c1e3ccSAndroid Build Coastguard Workerimport sys
37*77c1e3ccSAndroid Build Coastguard Worker
38*77c1e3ccSAndroid Build Coastguard Workerdef wrap(text):
39*77c1e3ccSAndroid Build Coastguard Worker    if text:
40*77c1e3ccSAndroid Build Coastguard Worker        return textwrap.fill(text, break_long_words=False) + '\n'
41*77c1e3ccSAndroid Build Coastguard Worker    return ""
42*77c1e3ccSAndroid Build Coastguard Worker
43*77c1e3ccSAndroid Build Coastguard Worker
44*77c1e3ccSAndroid Build Coastguard Workerdef main(fileobj):
45*77c1e3ccSAndroid Build Coastguard Worker    text = ""
46*77c1e3ccSAndroid Build Coastguard Worker    output = ""
47*77c1e3ccSAndroid Build Coastguard Worker    while True:
48*77c1e3ccSAndroid Build Coastguard Worker        line = fileobj.readline()
49*77c1e3ccSAndroid Build Coastguard Worker        if not line:
50*77c1e3ccSAndroid Build Coastguard Worker            break
51*77c1e3ccSAndroid Build Coastguard Worker
52*77c1e3ccSAndroid Build Coastguard Worker        if line.lstrip() == line:
53*77c1e3ccSAndroid Build Coastguard Worker            text += line
54*77c1e3ccSAndroid Build Coastguard Worker        else:
55*77c1e3ccSAndroid Build Coastguard Worker            output += wrap(text)
56*77c1e3ccSAndroid Build Coastguard Worker            text=""
57*77c1e3ccSAndroid Build Coastguard Worker            output += line
58*77c1e3ccSAndroid Build Coastguard Worker    output += wrap(text)
59*77c1e3ccSAndroid Build Coastguard Worker
60*77c1e3ccSAndroid Build Coastguard Worker    # Replace the file or write to stdout.
61*77c1e3ccSAndroid Build Coastguard Worker    if fileobj == sys.stdin:
62*77c1e3ccSAndroid Build Coastguard Worker        fileobj = sys.stdout
63*77c1e3ccSAndroid Build Coastguard Worker    else:
64*77c1e3ccSAndroid Build Coastguard Worker        fileobj.seek(0)
65*77c1e3ccSAndroid Build Coastguard Worker        fileobj.truncate(0)
66*77c1e3ccSAndroid Build Coastguard Worker    fileobj.write(output)
67*77c1e3ccSAndroid Build Coastguard Worker
68*77c1e3ccSAndroid Build Coastguard Workerif __name__ == "__main__":
69*77c1e3ccSAndroid Build Coastguard Worker    if len(sys.argv) > 1:
70*77c1e3ccSAndroid Build Coastguard Worker        main(open(sys.argv[1], "r+"))
71*77c1e3ccSAndroid Build Coastguard Worker    else:
72*77c1e3ccSAndroid Build Coastguard Worker        main(sys.stdin)
73