2021-07-22 09:16:28 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
import configparser
|
|
|
|
import sys
|
2021-07-24 12:54:32 +00:00
|
|
|
import re
|
2021-07-22 09:16:28 +00:00
|
|
|
|
2021-07-24 12:54:32 +00:00
|
|
|
submodule_pattern = re.compile(r"^submodule\ \"(?P<submodule>.+)\"$")
|
2021-07-22 09:16:28 +00:00
|
|
|
|
|
|
|
parser = configparser.ConfigParser()
|
|
|
|
parser.read(".gitmodules")
|
|
|
|
|
2021-07-24 12:54:32 +00:00
|
|
|
# criteria to group packages by
|
|
|
|
groups = ["ssh://", "https://"]
|
|
|
|
|
|
|
|
buckets = {}
|
|
|
|
|
|
|
|
valid = 0
|
|
|
|
assigned = 0
|
|
|
|
|
|
|
|
for group in groups:
|
|
|
|
buckets[group] = []
|
2021-07-22 09:16:28 +00:00
|
|
|
|
|
|
|
for section in parser:
|
2021-07-24 12:54:32 +00:00
|
|
|
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"
|
2021-07-22 09:16:28 +00:00
|
|
|
|
|
|
|
# rewrite gitmodules
|
|
|
|
with open(".gitmodules", "w") as f:
|
2021-07-24 12:54:32 +00:00
|
|
|
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")
|