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

View file

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