2022-06-26 15:00:03 +03:00
|
|
|
#!/usb/bin/env python3
|
|
|
|
|
|
|
|
import json
|
2023-05-03 08:48:49 +03:00
|
|
|
import os
|
|
|
|
import subprocess
|
2022-07-20 15:48:10 +03:00
|
|
|
from datetime import date, datetime
|
2022-06-26 15:00:03 +03:00
|
|
|
|
2023-05-03 08:48:49 +03:00
|
|
|
from flipper.app import App
|
|
|
|
|
2022-06-26 15:00:03 +03:00
|
|
|
|
|
|
|
class GitVersion:
|
2023-03-16 16:46:18 +03:00
|
|
|
REVISION_SUFFIX_LENGTH = 8
|
|
|
|
|
2022-06-26 15:00:03 +03:00
|
|
|
def __init__(self, source_dir):
|
|
|
|
self.source_dir = source_dir
|
|
|
|
|
|
|
|
def get_version_info(self):
|
2023-03-16 16:46:18 +03:00
|
|
|
commit = (
|
|
|
|
self._exec_git(f"rev-parse --short={self.REVISION_SUFFIX_LENGTH} HEAD")
|
|
|
|
or "unknown"
|
|
|
|
)
|
2022-06-26 15:00:03 +03:00
|
|
|
|
|
|
|
dirty = False
|
|
|
|
try:
|
|
|
|
self._exec_git("diff --quiet")
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
if e.returncode == 1:
|
|
|
|
dirty = True
|
|
|
|
|
|
|
|
# If WORKFLOW_BRANCH_OR_TAG is set in environment, is has precedence
|
|
|
|
# (set by CI)
|
|
|
|
branch = (
|
|
|
|
os.environ.get("WORKFLOW_BRANCH_OR_TAG", None)
|
|
|
|
or self._exec_git("rev-parse --abbrev-ref HEAD")
|
|
|
|
or "unknown"
|
|
|
|
)
|
|
|
|
|
2022-06-26 20:57:29 +03:00
|
|
|
version = (
|
|
|
|
os.environ.get("DIST_SUFFIX", None)
|
|
|
|
or "unknown"
|
|
|
|
)
|
2022-06-26 15:00:03 +03:00
|
|
|
|
2022-12-29 01:41:23 +03:00
|
|
|
force_no_dirty = (
|
|
|
|
os.environ.get("FORCE_NO_DIRTY", None)
|
|
|
|
or ""
|
|
|
|
)
|
|
|
|
if (force_no_dirty != ""):
|
|
|
|
dirty = False
|
|
|
|
|
2023-06-09 15:27:47 +03:00
|
|
|
if "SOURCE_DATE_EPOCH" in os.environ:
|
|
|
|
commit_date = datetime.utcfromtimestamp(
|
|
|
|
int(os.environ["SOURCE_DATE_EPOCH"])
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
commit_date = datetime.strptime(
|
2023-11-05 16:59:22 +03:00
|
|
|
self._exec_git("log -1 --format=%cd --date=default").strip(),
|
2023-06-09 15:27:47 +03:00
|
|
|
"%a %b %d %H:%M:%S %Y %z",
|
|
|
|
)
|
|
|
|
|
2023-05-14 05:38:00 +03:00
|
|
|
return {
|
|
|
|
"GIT_COMMIT": commit,
|
|
|
|
"GIT_BRANCH": branch,
|
|
|
|
"VERSION": version,
|
|
|
|
"BUILD_DIRTY": dirty and 1 or 0,
|
2023-06-14 18:00:24 +03:00
|
|
|
"GIT_ORIGIN": "https://github.com/DarkFlippers/unleashed-firmware.git",
|
2023-06-09 15:27:47 +03:00
|
|
|
"GIT_COMMIT_DATE": commit_date,
|
2023-05-14 05:38:00 +03:00
|
|
|
}
|
2023-06-14 18:00:24 +03:00
|
|
|
|
|
|
|
# "GIT_ORIGIN": ",".join(self._get_git_origins()),
|
2022-06-26 15:00:03 +03:00
|
|
|
|
2023-05-29 19:40:56 +03:00
|
|
|
def _get_git_origins(self):
|
|
|
|
try:
|
|
|
|
remotes = self._exec_git("remote -v")
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return set()
|
|
|
|
origins = set()
|
|
|
|
for line in remotes.split("\n"):
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
_, destination = line.split("\t")
|
|
|
|
url, _ = destination.split(" ")
|
|
|
|
origins.add(url)
|
|
|
|
return origins
|
|
|
|
|
2022-06-26 15:00:03 +03:00
|
|
|
def _exec_git(self, args):
|
|
|
|
cmd = ["git"]
|
|
|
|
cmd.extend(args.split(" "))
|
|
|
|
return (
|
|
|
|
subprocess.check_output(cmd, cwd=self.source_dir, stderr=subprocess.STDOUT)
|
|
|
|
.strip()
|
|
|
|
.decode()
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Main(App):
|
|
|
|
def init(self):
|
|
|
|
self.subparsers = self.parser.add_subparsers(help="sub-command help")
|
|
|
|
|
|
|
|
# generate
|
|
|
|
self.parser_generate = self.subparsers.add_parser(
|
|
|
|
"generate", help="Generate version header"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.parser_generate.add_argument("-o", dest="outdir", required=True)
|
|
|
|
self.parser_generate.add_argument(
|
|
|
|
"-t",
|
|
|
|
dest="target",
|
|
|
|
type=int,
|
|
|
|
help="hardware target",
|
|
|
|
required=True,
|
|
|
|
)
|
2023-05-29 19:40:56 +03:00
|
|
|
self.parser_generate.add_argument(
|
2023-11-15 19:27:35 +03:00
|
|
|
"--fw-origin",
|
2023-05-29 19:40:56 +03:00
|
|
|
dest="firmware_origin",
|
|
|
|
type=str,
|
|
|
|
help="firmware origin",
|
|
|
|
required=True,
|
|
|
|
)
|
2022-06-26 15:00:03 +03:00
|
|
|
self.parser_generate.add_argument("--dir", dest="sourcedir", required=True)
|
|
|
|
self.parser_generate.set_defaults(func=self.generate)
|
|
|
|
|
|
|
|
def generate(self):
|
|
|
|
current_info = GitVersion(self.args.sourcedir).get_version_info()
|
2022-07-20 15:48:10 +03:00
|
|
|
|
2023-06-09 15:27:47 +03:00
|
|
|
build_date = (
|
|
|
|
date.today()
|
|
|
|
if current_info["BUILD_DIRTY"]
|
|
|
|
else current_info["GIT_COMMIT_DATE"]
|
|
|
|
)
|
2022-07-20 15:48:10 +03:00
|
|
|
|
2022-06-26 15:00:03 +03:00
|
|
|
current_info.update(
|
|
|
|
{
|
2022-07-20 15:48:10 +03:00
|
|
|
"BUILD_DATE": build_date.strftime("%d-%m-%Y"),
|
2022-06-26 15:00:03 +03:00
|
|
|
"TARGET": self.args.target,
|
2023-05-29 19:40:56 +03:00
|
|
|
"FIRMWARE_ORIGIN": self.args.firmware_origin,
|
2022-06-26 15:00:03 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-06-09 15:27:47 +03:00
|
|
|
del current_info["GIT_COMMIT_DATE"]
|
|
|
|
|
2022-06-26 15:00:03 +03:00
|
|
|
version_values = []
|
|
|
|
for key in current_info:
|
|
|
|
val = current_info[key]
|
|
|
|
if isinstance(val, str):
|
|
|
|
val = f'"{val}"'
|
|
|
|
version_values.append(f"#define {key} {val}")
|
|
|
|
|
|
|
|
new_version_info_fmt = "\n".join(version_values) + "\n"
|
|
|
|
|
|
|
|
current_version_info = None
|
|
|
|
version_header_name = os.path.join(self.args.outdir, "version.inc.h")
|
|
|
|
version_json_name = os.path.join(self.args.outdir, "version.json")
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(version_header_name, "r") as file:
|
|
|
|
current_version_info = file.read()
|
|
|
|
except EnvironmentError as e:
|
|
|
|
if self.args.debug:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
if current_version_info != new_version_info_fmt:
|
|
|
|
if self.args.debug:
|
|
|
|
print("old: ", current_version_info)
|
|
|
|
print("new: ", new_version_info_fmt)
|
|
|
|
with open(version_header_name, "w", newline="\n") as file:
|
|
|
|
file.write(new_version_info_fmt)
|
|
|
|
# os.utime("../lib/toolbox/version.c", None)
|
|
|
|
print("Version information updated")
|
|
|
|
else:
|
|
|
|
if self.args.debug:
|
|
|
|
print("Version information hasn't changed")
|
|
|
|
|
|
|
|
version_json = {
|
|
|
|
"firmware_build_date": current_info["BUILD_DATE"],
|
|
|
|
"firmware_commit": current_info["GIT_COMMIT"],
|
|
|
|
"firmware_branch": current_info["GIT_BRANCH"],
|
|
|
|
"firmware_target": current_info["TARGET"],
|
2023-04-21 09:46:48 +03:00
|
|
|
"firmware_version": current_info["VERSION"],
|
2022-06-26 15:00:03 +03:00
|
|
|
}
|
|
|
|
with open(version_json_name, "w", newline="\n") as file:
|
|
|
|
json.dump(version_json, file, indent=4)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()()
|