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 #!/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")
path = parser.get(section, "path")
url = parser.get(section, "url")
# extract path & url for group in groups:
path = parser.get(section, "path") grp_match = re.match(group, url)
url = parser.get(section, "url") if grp_match:
assigned += 1
buckets[group].append((submodule, path, url))
break
# i've got access to the package, must be one of mine if valid != assigned:
if url.startswith("ssh://"): print("We have a problem.")
packages.append((path, url)) sys.exit(1)
# no idea who this belongs to
elif url.startswith("https://"):
other.append((path, url))
# sort modules alphabetically # indentation for submodule path/url. defaults to tab.
packages.sort() indentation = "\t"
other.sort()
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")
# 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")