This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert version generating scripts to python, generate automatically …
…during build
- Loading branch information
1 parent
2243fca
commit 3e46ac2
Showing
4 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import subprocess | ||
import os | ||
import json | ||
|
||
def exec(cmd): | ||
return subprocess.check_output(cmd, shell=True).decode('utf-8').strip() | ||
|
||
def get_git_info(): | ||
return { | ||
'repo': exec('git remote get-url origin').replace('https://github.com/', '').replace('[email protected]:', '').replace('.git', ''), | ||
'tag': exec('git tag --points-at HEAD') or exec('git rev-parse --short HEAD'), | ||
'root': exec('git rev-parse --show-toplevel') | ||
} | ||
|
||
def read_package_json(): | ||
with open(os.path.join(os.path.dirname(__file__), 'package.json'), 'r') as f: | ||
return json.load(f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
from common import get_git_info, read_package_json | ||
from generate_versions_utils import generate_versions | ||
|
||
if __name__ == "__main__": | ||
use_real_data = '--withMd5Sums' in sys.argv | ||
use_zero_versions = '--withZeroVersions' in sys.argv | ||
package_json = read_package_json() | ||
git_info = get_git_info() | ||
generate_versions(package_json, git_info, use_real_data, use_zero_versions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import hashlib | ||
import os | ||
import json | ||
import shutil | ||
|
||
ZERO_MD5 = '00000000000000000000000000000000' | ||
|
||
version_property_prefixes = ['firmware', 'deviceProtocol', 'moduleProtocol', 'userConfig', 'hardwareConfig', 'smartMacros'] | ||
patch_versions = ['Major', 'Minor', 'Patch'] | ||
|
||
def generate_versions(package_json, git_info, use_real_shas, use_zero_versions): | ||
package_json = json.loads(json.dumps(package_json)) # Deep copy | ||
|
||
if use_zero_versions: | ||
git_info = { | ||
'repo': '', | ||
'tag': '' | ||
} | ||
|
||
version_variables = '\n'.join([ | ||
f"const version_t {prefix}Version = {{ {', '.join(['0' if use_zero_versions else package_json[f'{prefix}Version'].split('.')[i] for i in range(len(patch_versions))])} }};" | ||
for prefix in version_property_prefixes | ||
]) | ||
|
||
device_md5_sums = '\n'.join([ | ||
f' [{device["deviceId"]}] = "{ZERO_MD5 if not use_real_shas else calculate_md5_checksum_of_file(os.path.join(os.path.dirname(__file__), "..", device["source"]))}",' | ||
for device in package_json['devices'] | ||
]) | ||
|
||
module_md5_sums = '\n'.join([ | ||
f' [{module["moduleId"]}] = "{ZERO_MD5 if not use_real_shas else calculate_md5_checksum_of_file(os.path.join(os.path.dirname(__file__), "..", module["source"]))}",' | ||
for module in package_json['modules'] | ||
]) | ||
|
||
with open(os.path.join(os.path.dirname(__file__), '..', 'shared', 'versions.c'), 'w') as f: | ||
f.write(f"""// Please do not edit this file by hand! | ||
// It is to be regenerated by /scripts/generate_versions.py | ||
#include "versioning.h" | ||
{version_variables} | ||
const char gitRepo[] = "{git_info['repo']}"; | ||
const char gitTag[] = "{git_info['tag']}"; | ||
#ifdef DEVICE_COUNT | ||
const char *const DeviceMD5Checksums[DEVICE_COUNT + 1] = {{ | ||
{device_md5_sums} | ||
}}; | ||
#endif | ||
const char *const ModuleMD5Checksums[ModuleId_AllCount] = {{ | ||
{module_md5_sums} | ||
}}; | ||
""") | ||
|
||
return { | ||
'devices': package_json['devices'], | ||
'modules': package_json['modules'] | ||
} | ||
|
||
def calculate_md5_checksum_of_file(file_path): | ||
with open(file_path, 'rb') as f: | ||
file_data = f.read() | ||
return hashlib.md5(file_data).hexdigest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters