flipperzero-firmware/scripts/get_env.py

117 lines
3.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import ssl
import json
import os
import shlex
import re
import string
import random
import argparse
import datetime
import urllib.request
def id_gen(size=5, chars=string.ascii_uppercase + string.digits):
return "".join(random.choice(chars) for _ in range(size))
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--event_file", help="Current GitHub event file", required=True)
parser.add_argument(
2022-09-05 13:40:54 +00:00
"--type",
help="Event file type",
required=True,
choices=["pull", "tag", "other"],
)
args = parser.parse_args()
return args
def get_commit_json(event):
context = ssl._create_unverified_context()
[FL-3162] Moved ufbt to fbt codebase (#2520) * scripts: moved ufbt code * ufbt: fixed tool path * ufbt: fixed linter/formatter target descriptions * scripts: ufbt: cleanup * fbt: moved fap launch target to tools; ufbt fixes * fbt: fixed missing headers from SDK * ufbt: removed debug output * ufbt: moved project template to main codebase * ufbt: fixed vscode_dist * ufbt: path naming changes * fbt: error message for older ufbt versions * ufbt: docs fixes * ufbt: fixed build dir location * fbt: fixes for extapps objcopy * fbt: extapps: removed extra debug output; fixed formatting * ufbt: handle launch target for multiple known apps * ufbt: dropping wrapper; linter fixes * ufbt: fixed boostrap path * ufbt: renamed entrypoint * ufbt: updated vscode config * ufbt: moved sconsign db location * ufbt: fixed sconsign path * fbt: SDK builders rework * fbt: reworked sdk packaging * ufbt: additional checks and state processing * ufbt: fixed sdk state file location * dist: not packaging pycache * dump commit json content * Github: more workflow debug prints * Github: fix incorrect commit meta extraction in get_env.py * ufbt, fbt: changed SConsEnvironmentError->StopError * fbtenv: no longer needs SCRIPT_PATH pre-set * ufbt: fixed sdk state check * scripts: exception fixes for storage.py * scripts: fbtenv: added FBT_TOOLCHAIN_PATH for on Windows for compat * ufbt: app template: creating .gitkeep for images folder * ufbt: app template: fixed .gitkeep creation * docs: formatting fixes for AppManifests; added link to ufbt * fbt: added link to PyPI for old ufbt versions * sdk: fixed dir component paths Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2023-04-06 02:44:37 +00:00
commit_url = event["pull_request"]["base"]["repo"]["commits_url"].replace(
"{/sha}", f"/{event['pull_request']['head']['sha']}"
[FL-3162] Moved ufbt to fbt codebase (#2520) * scripts: moved ufbt code * ufbt: fixed tool path * ufbt: fixed linter/formatter target descriptions * scripts: ufbt: cleanup * fbt: moved fap launch target to tools; ufbt fixes * fbt: fixed missing headers from SDK * ufbt: removed debug output * ufbt: moved project template to main codebase * ufbt: fixed vscode_dist * ufbt: path naming changes * fbt: error message for older ufbt versions * ufbt: docs fixes * ufbt: fixed build dir location * fbt: fixes for extapps objcopy * fbt: extapps: removed extra debug output; fixed formatting * ufbt: handle launch target for multiple known apps * ufbt: dropping wrapper; linter fixes * ufbt: fixed boostrap path * ufbt: renamed entrypoint * ufbt: updated vscode config * ufbt: moved sconsign db location * ufbt: fixed sconsign path * fbt: SDK builders rework * fbt: reworked sdk packaging * ufbt: additional checks and state processing * ufbt: fixed sdk state file location * dist: not packaging pycache * dump commit json content * Github: more workflow debug prints * Github: fix incorrect commit meta extraction in get_env.py * ufbt, fbt: changed SConsEnvironmentError->StopError * fbtenv: no longer needs SCRIPT_PATH pre-set * ufbt: fixed sdk state check * scripts: exception fixes for storage.py * scripts: fbtenv: added FBT_TOOLCHAIN_PATH for on Windows for compat * ufbt: app template: creating .gitkeep for images folder * ufbt: app template: fixed .gitkeep creation * docs: formatting fixes for AppManifests; added link to ufbt * fbt: added link to PyPI for old ufbt versions * sdk: fixed dir component paths Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2023-04-06 02:44:37 +00:00
)
with urllib.request.urlopen(commit_url, context=context) as commit_file:
commit_json = json.loads(commit_file.read().decode("utf-8"))
return commit_json
def get_details(event, args):
data = {}
current_time = datetime.datetime.utcnow().date()
2022-09-05 13:40:54 +00:00
if args.type == "pull":
commit_json = get_commit_json(event)
[FL-3162] Moved ufbt to fbt codebase (#2520) * scripts: moved ufbt code * ufbt: fixed tool path * ufbt: fixed linter/formatter target descriptions * scripts: ufbt: cleanup * fbt: moved fap launch target to tools; ufbt fixes * fbt: fixed missing headers from SDK * ufbt: removed debug output * ufbt: moved project template to main codebase * ufbt: fixed vscode_dist * ufbt: path naming changes * fbt: error message for older ufbt versions * ufbt: docs fixes * ufbt: fixed build dir location * fbt: fixes for extapps objcopy * fbt: extapps: removed extra debug output; fixed formatting * ufbt: handle launch target for multiple known apps * ufbt: dropping wrapper; linter fixes * ufbt: fixed boostrap path * ufbt: renamed entrypoint * ufbt: updated vscode config * ufbt: moved sconsign db location * ufbt: fixed sconsign path * fbt: SDK builders rework * fbt: reworked sdk packaging * ufbt: additional checks and state processing * ufbt: fixed sdk state file location * dist: not packaging pycache * dump commit json content * Github: more workflow debug prints * Github: fix incorrect commit meta extraction in get_env.py * ufbt, fbt: changed SConsEnvironmentError->StopError * fbtenv: no longer needs SCRIPT_PATH pre-set * ufbt: fixed sdk state check * scripts: exception fixes for storage.py * scripts: fbtenv: added FBT_TOOLCHAIN_PATH for on Windows for compat * ufbt: app template: creating .gitkeep for images folder * ufbt: app template: fixed .gitkeep creation * docs: formatting fixes for AppManifests; added link to ufbt * fbt: added link to PyPI for old ufbt versions * sdk: fixed dir component paths Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2023-04-06 02:44:37 +00:00
data["commit_comment"] = shlex.quote(commit_json["commit"]["message"])
data["commit_hash"] = commit_json["sha"]
ref = event["pull_request"]["head"]["ref"]
data["pull_id"] = event["pull_request"]["number"]
data["pull_name"] = shlex.quote(event["pull_request"]["title"])
2022-09-05 13:40:54 +00:00
elif args.type == "tag":
data["commit_comment"] = shlex.quote(event["head_commit"]["message"])
data["commit_hash"] = event["head_commit"]["id"]
ref = event["ref"]
else:
data["commit_comment"] = shlex.quote(event["commits"][-1]["message"])
data["commit_hash"] = event["commits"][-1]["id"]
ref = event["ref"]
data["commit_sha"] = data["commit_hash"][:8]
data["branch_name"] = re.sub("refs/\w+/", "", ref)
data["suffix"] = (
data["branch_name"].replace("/", "_")
+ "-"
+ current_time.strftime("%d%m%Y")
+ "-"
+ data["commit_sha"]
)
if ref.startswith("refs/tags/"):
data["suffix"] = data["branch_name"].replace("/", "_")
return data
def add_env(name, value, file):
delimeter = id_gen()
print(f"{name}<<{delimeter}", file=file)
print(f"{value}", file=file)
print(f"{delimeter}", file=file)
def add_set_output_var(name, value, file):
print(f"{name}={value}", file=file)
def add_envs(data, gh_env_file, gh_out_file, args):
add_env("COMMIT_MSG", data["commit_comment"], gh_env_file)
add_env("COMMIT_HASH", data["commit_hash"], gh_env_file)
add_env("COMMIT_SHA", data["commit_sha"], gh_env_file)
add_env("SUFFIX", data["suffix"], gh_env_file)
add_env("BRANCH_NAME", data["branch_name"], gh_env_file)
add_env("DIST_SUFFIX", data["suffix"], gh_env_file)
add_env("WORKFLOW_BRANCH_OR_TAG", data["branch_name"], gh_env_file)
add_set_output_var("branch_name", data["branch_name"], gh_out_file)
2022-12-19 17:09:19 +00:00
add_set_output_var("commit_sha", data["commit_sha"], gh_out_file)
add_set_output_var("default_target", os.getenv("DEFAULT_TARGET"), gh_out_file)
2022-12-19 17:09:19 +00:00
add_set_output_var("suffix", data["suffix"], gh_out_file)
2022-09-05 13:40:54 +00:00
if args.type == "pull":
add_env("PULL_ID", data["pull_id"], gh_env_file)
add_env("PULL_NAME", data["pull_name"], gh_env_file)
def main():
args = parse_args()
event_file = open(args.event_file, "r")
event = json.load(event_file)
gh_env_file = open(os.environ["GITHUB_ENV"], "a")
gh_out_file = open(os.environ["GITHUB_OUTPUT"], "a")
data = get_details(event, args)
add_envs(data, gh_env_file, gh_out_file, args)
event_file.close()
gh_env_file.close()
gh_out_file.close()
if __name__ == "__main__":
main()