build: finalise rewrite-gitmodules before upcoming rewrite

This commit is contained in:
George Rawlinson 2021-07-24 12:54:32 +00:00
parent dfc0f6c839
commit 2828cf0016
Signed by: grawlinson
GPG Key ID: E0959FEA8B550539
1 changed files with 43 additions and 42 deletions

View File

@ -1,58 +1,59 @@
#!/usr/bin/python
from typing import List
from typing import Tuple
import configparser
import sys
import re
#def add_modules(
# modules: List[Tuple[str, str]],
# parser: configparser.ConfigParser,
#) -> None:
# """Add set of submodules to gitconfig structure."""
# for module in modules:
# path, url = module
# section = f"submodule \"{path}\""
# parser.add_section(section)
# parser.set(section, "\tpath", path)
# parser.set(section, "\turl", url)
submodule_pattern = re.compile(r"^submodule\ \"(?P<submodule>.+)\"$")
parser = configparser.ConfigParser()
parser.read(".gitmodules")
packages = []
other = []
# criteria to group packages by
groups = ["ssh://", "https://"]
buckets = {}
valid = 0
assigned = 0
for group in groups:
buckets[group] = []
for section in parser:
# skip default section, it's useless
if section == "DEFAULT":
continue
# extract path & url
match = re.match(submodule_pattern, section)
if match:
valid += 1
submodule = match.group("submodule")
path = parser.get(section, "path")
url = parser.get(section, "url")
# i've got access to the package, must be one of mine
if url.startswith("ssh://"):
packages.append((path, url))
# no idea who this belongs to
elif url.startswith("https://"):
other.append((path, url))
for group in groups:
grp_match = re.match(group, url)
if grp_match:
assigned += 1
buckets[group].append((submodule, path, url))
break
# sort modules alphabetically
packages.sort()
other.sort()
if valid != assigned:
print("We have a problem.")
sys.exit(1)
def write_packages(packages, f):
for package in packages:
path, url = package
section = f"[submodule \"{path}\"]"
f.write(f"{section}\n")
f.write(f"\tpath = {path}\n")
f.write(f"\turl = {url}\n")
# indentation for submodule path/url. defaults to tab.
indentation = "\t"
# rewrite gitmodules
with open(".gitmodules", "w") as f:
write_packages(packages, f)
write_packages(other, 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")