arch-pkgs/.repo/rewrite-gitmodules

60 lines
1.4 KiB
Python
Executable File

#!/usr/bin/python
import configparser
import sys
import re
submodule_pattern = re.compile(r"^submodule\ \"(?P<submodule>.+)\"$")
parser = configparser.ConfigParser()
parser.read(".gitmodules")
# criteria to group packages by
groups = ["ssh://", "https://"]
buckets = {}
valid = 0
assigned = 0
for group in groups:
buckets[group] = []
for section in parser:
match = re.match(submodule_pattern, section)
if match:
valid += 1
submodule = match.group("submodule")
path = parser.get(section, "path")
url = parser.get(section, "url")
for group in groups:
grp_match = re.match(group, url)
if grp_match:
assigned += 1
buckets[group].append((submodule, path, url))
break
if valid != assigned:
print("We have a problem.")
sys.exit(1)
# indentation for submodule path/url. defaults to tab.
indentation = "\t"
# rewrite gitmodules
with open(".gitmodules", "w") as f:
for group in groups:
# skip empty buckets
if len(buckets[group]) == 0:
continue
# sort alphabetically
buckets[group].sort()
# write submodules to file
for module in buckets[group]:
submodule, path, url = module
f.write(f'[submodule "{submodule}"]\n')
f.write(f"{indentation}path = {path}\n")
f.write(f"{indentation}url = {url}\n")