Skip to content
Snippets Groups Projects
update_envsetup.py 2.15 KiB
Newer Older
Stéphane Diemer's avatar
Stéphane Diemer committed
#!/usr/bin/env python3

from collections import OrderedDict
from pathlib import Path
Stéphane Diemer's avatar
Stéphane Diemer committed
import os
import subprocess
import sys


def load_conf() -> dict:
    conf = {}
    base_dir = str(Path(__file__).expanduser().resolve().parent)
    files = (
        (str(Path(base_dir, "global-conf.sh")), True),
        (str(Path(base_dir, "auto-generated-conf.sh")), False),
        (str(Path(base_dir, "conf.sh")), False),
    )
    only_default = True
    override = OrderedDict()
    for path, is_default in files:
        if not Path(path).exists():
            if is_default:
                return dict()
            continue
        # Load conf
        with open(path, "r") as fo:
            content = fo.read()
        # Parse conf
        for line in content.split("\n"):
            line = line.strip()
            if line and not line.startswith("#") and "=" in line:
                name, *val = line.split("=")
                name = name.strip(" \t'\"")
                val = ("=".join(val)).strip(" \t'\"")
                conf[name] = val
                if is_default:
                    override[name] = False
                else:
                    only_default = False
                    override[name] = True
    conf["_override"] = override
    return conf


def get_conf(name: str, default: str = None) -> str:
    conf = load_conf()

    return conf.get(name, default)
Stéphane Diemer's avatar
Stéphane Diemer committed

if __name__ == '__main__':
    branch = get_conf('ENVSETUP_BRANCH') or 'stable'
Stéphane Diemer's avatar
Stéphane Diemer committed
    os.chdir(os.path.dirname(os.path.abspath(os.path.expanduser(__file__))))
    sys.stdout.write('Updating envsetup: ')
    sys.stdout.flush()
    subprocess.call('find . -name *.pyc -type f -delete', shell=True)
    subprocess.call('find . -name __pycache__ -type d -delete', shell=True)
    subprocess.check_call(['git', 'fetch', '--recurse-submodules', '--all'])
    subprocess.check_call(['git', 'reset', '--hard', 'origin/{}'.format(branch)])
    if branch:
        subprocess.check_call(['git', 'checkout', branch])
    subprocess.check_call(['git', 'pull', '--recurse-submodules'])
    subprocess.check_call(['git', 'submodule', 'update', '--init', '--recursive'])
Stéphane Diemer's avatar
Stéphane Diemer committed
    subprocess.call('find . -type d -empty -delete', shell=True)