diff --git a/python/build_flags.py b/python/build_flags.py index cf1f6e5..286a02b 100644 --- a/python/build_flags.py +++ b/python/build_flags.py @@ -1,24 +1,24 @@ Import("env") import os import sys -import subprocess import hashlib import fnmatch import time import re +import elrs_helpers - -build_flags = env['BUILD_FLAGS'] +build_flags = env.get('BUILD_FLAGS', []) UIDbytes = "" define = "" +target_name = env.get('PIOENV', '').upper() def print_error(error): time.sleep(1) - sys.stdout.write("\033[47;31m%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n") + sys.stdout.write("\n\n\033[47;31m%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n") sys.stdout.write("\033[47;31m!!! ExpressLRS Warning Below !!!\n") sys.stdout.write("\033[47;31m%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n") sys.stdout.write("\033[47;30m %s \n" % error) - sys.stdout.write("\033[47;31m%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n") + sys.stdout.write("\033[47;31m%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n\n") sys.stdout.flush() time.sleep(3) raise Exception('!!! %s !!!' % error) @@ -49,114 +49,81 @@ def process_flags(path): return parse_flags(path) +def escapeChars(x): + parts = re.search("(.*)=\w*\"(.*)\"$", x) + if parts and parts.group(2): + if parts.group(1) == "-DMY_STARTUP_MELODY_ARR": # ignoring escape chars for startup melody + return x + x = parts.group(1) + '="' + parts.group(2).translate(str.maketrans({ + "!": "\\\\\\\\041", + "\"": "\\\\\\\\042", + "#": "\\\\\\\\043", + "$": "\\\\\\\\044", + "&": "\\\\\\\\046", + "'": "\\\\\\\\047", + "(": "\\\\\\\\050", + ")": "\\\\\\\\051", + ",": "\\\\\\\\054", + ";": "\\\\\\\\073", + "<": "\\\\\\\\074", + ">": "\\\\\\\\076", + "\\": "\\\\\\\\134", + "`": "\\\\\\\\140", + "|": "\\\\\\\\174" + })) + '"' + return x + def condense_flags(): global build_flags for line in build_flags: # Some lines have multiple flags so this will split them and remove them all for flag in re.findall("!-D\s*[^\s]+", line): build_flags = [x.replace(flag[1:],"") for x in build_flags] # remove the flag which will just leave ! in their place + build_flags = [escapeChars(x) for x in build_flags] # perform escaping of flags with values build_flags = [x.replace("!", "") for x in build_flags] # remove the ! build_flags = [x for x in build_flags if (x.strip() != "")] # remove any blank items +def version_to_env(): + ver = elrs_helpers.get_git_version() + env.Append(GIT_SHA = ver['sha'], GIT_VERSION= ver['version']) + +def string_to_ascii(str): + return ",".join(["%s" % ord(char) for char in str]) + def get_git_sha(): - # Don't try to pull the git revision when doing tests, as - # `pio remote test` doesn't copy the entire repository, just the files - if env['PIOPLATFORM'] == "native": - return "012345" + return string_to_ascii(env.get('GIT_SHA')) - try: - import git - except ImportError: - sys.stdout.write("Installing GitPython") - subprocess.check_call([sys.executable, "-m", "pip", "install", "GitPython"]) - try: - import git - except ImportError: - env.Execute("$PYTHONEXE -m pip install GitPython") - try: - import git - except ImportError: - git = None - - sha = None - if git: - try: - git_repo = git.Repo( - os.path.abspath(os.path.join(os.getcwd(), os.pardir)), - search_parent_directories=False) - git_root = git_repo.git.rev_parse("--show-toplevel") - ExLRS_Repo = git.Repo(git_root) - sha = ExLRS_Repo.head.object.hexsha - - except git.InvalidGitRepositoryError: - pass - if not sha: - if os.path.exists("VERSION"): - with open("VERSION") as _f: - data = _f.readline() - _f.close() - sha = data.split()[1].strip() - else: - sha = "000000" - return ",".join(["%s" % ord(x) for x in sha[:6]]) - -def get_git_version(): - # Don't try to pull the git revision when doing tests, as - # `pio remote test` doesn't copy the entire repository, just the files - if env['PIOPLATFORM'] == "native": - return "001122334455" +def get_version(): + return string_to_ascii(env.get('GIT_VERSION')) - try: - import git - except ImportError: - sys.stdout.write("Installing GitPython") - subprocess.check_call([sys.executable, "-m", "pip", "install", "GitPython"]) - try: - import git - except ImportError: - env.Execute("$PYTHONEXE -m pip install GitPython") - try: - import git - except ImportError: - git = None - - ver = "ver. unknown" - if git: - try: - git_repo = git.Repo( - os.path.abspath(os.path.join(os.getcwd(), os.pardir)), - search_parent_directories=False) - try: - ver = git_repo.git.describe("--tags", "--exact-match") - except git.exc.GitCommandError: - try: - ver = git_repo.git.symbolic_ref("-q", "--short", "HEAD") - except git.exc.GitCommandError: - ver = "ver. unknown" - hash = git_repo.git.rev_parse("--short", "HEAD") - except git.InvalidGitRepositoryError: - pass - return ",".join(["%s" % ord(char) for char in ver]) process_flags("user_defines.txt") process_flags("super_defines.txt") # allow secret super_defines to override user_defines +version_to_env() build_flags.append("-DLATEST_COMMIT=" + get_git_sha()) -build_flags.append("-DLATEST_VERSION=" + get_git_version()) -build_flags.append("-DTARGET_NAME=" + re.sub("_VIA_.*", "", env['PIOENV'].upper())) +build_flags.append("-DLATEST_VERSION=" + get_version()) # version and domain +build_flags.append("-DTARGET_NAME=" + re.sub("_VIA_.*", "", target_name)) condense_flags() env['BUILD_FLAGS'] = build_flags -print("build flags: %s" % env['BUILD_FLAGS']) +sys.stdout.write("\nbuild flags: %s\n\n" % build_flags) -if fnmatch.filter(env['BUILD_FLAGS'], '*PLATFORM_ESP32*'): +if fnmatch.filter(build_flags, '*PLATFORM_ESP32*'): sys.stdout.write("\u001b[32mBuilding for ESP32 Platform\n") -elif fnmatch.filter(env['BUILD_FLAGS'], '*PLATFORM_STM32*'): +elif fnmatch.filter(build_flags, '*PLATFORM_STM32*'): sys.stdout.write("\u001b[32mBuilding for STM32 Platform\n") -elif fnmatch.filter(env['BUILD_FLAGS'], '*PLATFORM_ESP8266*'): +elif fnmatch.filter(build_flags, '*PLATFORM_ESP8266*'): sys.stdout.write("\u001b[32mBuilding for ESP8266/ESP8285 Platform\n") - if fnmatch.filter(env['BUILD_FLAGS'], '-DAUTO_WIFI_ON_INTERVAL*'): + if fnmatch.filter(build_flags, '-DAUTO_WIFI_ON_INTERVAL*'): sys.stdout.write("\u001b[32mAUTO_WIFI_ON_INTERVAL = ON\n") else: sys.stdout.write("\u001b[32mAUTO_WIFI_ON_INTERVAL = OFF\n") -time.sleep(1) +sys.stdout.flush() +time.sleep(.5) + +# Set upload_protovol = 'custom' for STM32 MCUs +# otherwise firmware.bin is not generated +stm = env.get('PIOPLATFORM', '') in ['ststm32'] +if stm: + env['UPLOAD_PROTOCOL'] = 'custom' diff --git a/python/elrs_helpers.py b/python/elrs_helpers.py index 9c2eafa..eca6e01 100644 --- a/python/elrs_helpers.py +++ b/python/elrs_helpers.py @@ -1,42 +1,13 @@ import os import re +import subprocess -def get_git(env): - """ - Returns a git.Repo class for a git repo in the current directory - installing GitPython if neeeded - """ - try: - import git - except ImportError: - import sys - import subprocess - sys.stdout.write("Installing GitPython") - subprocess.check_call([sys.executable, "-m", "pip", "install", "GitPython"]) - try: - import git - except ImportError: - env.Execute("$PYTHONEXE -m pip install GitPython") - try: - import git - except ImportError: - return None - try: - # Check one directory up for a git repo - orig_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) - git_repo = git.Repo(orig_dir, search_parent_directories=False) - # If that succeeded then find out where the top level is and open that - git_root = git_repo.git.rev_parse("--show-toplevel") - if orig_dir != git_root: - git_repo = git.Repo(git_root, search_parent_directories=False) - return git_repo - except git.InvalidGitRepositoryError: - pass +def git_cmd(*args): + return subprocess.check_output(["git"] + list(args)).decode("utf-8").rstrip('\r\n') - return None -def get_git_version(env): +def get_git_version(): """ Return a dict with keys version: The version tag if HEAD is a version, or branch otherwise @@ -46,21 +17,21 @@ def get_git_version(env): ver = "ver.unknown" sha = "000000" - git_repo = get_git(env) - if git_repo: - import git - sha = git_repo.head.object.hexsha + try: + sha = git_cmd("rev-parse", "HEAD") + ver = git_cmd("rev-parse", "--abbrev-ref", "HEAD") + # failure here is acceptable, unnamed commits might not have a branch + # associated try: - ver = re.sub(r".*/", "", git_repo.git.describe("--all", "--exact-match")) - except git.exc.GitCommandError: - try: - ver = git_repo.git.symbolic_ref("-q", "--short", "HEAD") - except git.exc.GitCommandError: - pass - elif os.path.exists("VERSION"): - with open("VERSION") as _f: - data = _f.readline() - _f.close() - sha = data.split()[1].strip() + ver = re.sub(r".*/", "", git_cmd("describe", + "--all", "--exact-match")) + except: + pass + except: + if os.path.exists("VERSION"): + with open("VERSION") as _f: + data = _f.readline() + _f.close() + sha = data.split()[1].strip() return dict(version=ver, sha=sha[:6])