-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathformat_projects_list.py
executable file
·63 lines (49 loc) · 1.73 KB
/
format_projects_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# ===--- format_projects_list.py ------------------------------------------===
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ===----------------------------------------------------------------------===
"""Check for JSON syntax errors and format a given project index file."""
import argparse
import os
import json
import sys
import collections
import re
def strip_trailing_whitespace(text):
"""Return text stripped of trailing whitespace."""
return re.sub(r'\s+$', '', text, flags=re.M)
def parse_args():
"""Return parsed command line arguments."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"project_index",
help="a project index file to check (e.g. projects.json)",
type=os.path.abspath
)
return parser.parse_args()
def main():
# pylint: disable=I0011,C0111
args = parse_args()
with open(args.project_index) as project_index:
parsed_project_index = sorted(
json.JSONDecoder(
object_pairs_hook=collections.OrderedDict
).decode(project_index.read()),
key=lambda repo: repo['path']
)
json_string = strip_trailing_whitespace(
json.dumps(parsed_project_index, sort_keys=False, indent=2)
)
with open(args.project_index, 'w') as project_index:
project_index.write(json_string)
return 0
if __name__ == "__main__":
sys.exit(main())