tools/linelength.py

Wed, 22 Sep 2021 00:25:31 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 22 Sep 2021 00:25:31 +0300
changeset 135
d384df40c8e7
parent 114
4e03b0e2a29f
permissions
-rwxr-xr-x

Merge commit

114
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #!/usr/bin/env python3
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 import sys
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 def check(filename):
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 with open(filename) as file:
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 for linenumber, line in enumerate(file, 1):
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 if line[-1] == '\n':
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 line = line[:-1]
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 line = str.replace(line, '\t', ' ' * 4)
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 if len(line) > 120:
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 print(str.format(
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 '{filename}:{linenumber}: warning: line length exceeds 120 characters',
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 filename = filename,
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 linenumber = linenumber
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 ), file = sys.stderr)
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 def main():
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 for filename in sys.argv[1:]:
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 check(filename)
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 return 0
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 if __name__ == '__main__':
4e03b0e2a29f added build-time test for line length
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 sys.exit(main())

mercurial