Skip to content

Commit 641e331

Browse files
committed
Get fallback from from file so it can be updated
This lets us update the fallback time via a script using a cron job if we want to.
1 parent f8d931d commit 641e331

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

build/chip/fallback_lkgt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
750275722

build/chip/write_build_time_header.py

+28-14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
from datetime import datetime, timezone
1919

2020

21+
SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
22+
FALLBACK_LKGT_FILENAME = os.path.abspath(os.path.join(SCRIPT_DIR, 'fallback_lkgt'))
23+
24+
2125
def utc_time_in_matter_epoch_s(time: datetime):
2226
""" Returns the time in matter epoch in s. """
2327
# Matter epoch is 0 hours, 0 minutes, 0 seconds on Jan 1, 2000 UTC
@@ -32,38 +36,48 @@ def __init__(self, output, define_name, define_val):
3236
self.define_val = define_val
3337

3438

35-
def GetOptions():
36-
fallback_lkgt = datetime(2023, 10, 3, 0, 0, 0, 0, timezone.utc)
39+
def write_header(options):
40+
with open(options.output, "w") as output_file:
41+
output_file.write("// Generated by write_build_time_header.py\n")
42+
output_file.write('#pragma once\n\n')
43+
44+
output_file.write(f'#define {options.define_name} {options.define_val}\n')
45+
46+
47+
def update_fallback_time_in_file():
48+
with open(FALLBACK_LKGT_FILENAME, "w") as output_file:
49+
output_file.write(str(utc_time_in_matter_epoch_s(datetime.now(tz=timezone.utc))))
3750

51+
52+
def main():
3853
parser = argparse.ArgumentParser()
3954
parser.add_argument('--output', help="Output header name (inside gen dir)")
4055
parser.add_argument('--gen-dir',
4156
help="Path to root of generated file directory tree.")
4257
parser.add_argument('--use-current-time', default=False, action='store_true',
4358
help="Set the LKGT to the current time. If this flag is not used, the LKGT is set to a hardcoded time.")
59+
parser.add_argument('--update-fallback-time-in-file', default=False, action='store_true',
60+
help='Write the current UTC time out to the fallback file')
4461
cmdline_options = parser.parse_args()
4562

63+
if cmdline_options.update_fallback_time_in_file:
64+
update_fallback_time_in_file()
65+
return
66+
4667
# The actual output file is inside the gen dir.
4768
output = os.path.join(cmdline_options.gen_dir, cmdline_options.output)
4869

4970
define_name = 'CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S'
5071
if cmdline_options.use_current_time:
5172
build_time = utc_time_in_matter_epoch_s(datetime.now(tz=timezone.utc))
5273
else:
53-
build_time = utc_time_in_matter_epoch_s(fallback_lkgt)
74+
with open(FALLBACK_LKGT_FILENAME, "r") as input_file:
75+
build_time = int(input_file.read())
5476

55-
return Options(output=output,
77+
opts = Options(output=output,
5678
define_name=define_name,
5779
define_val=str(build_time))
80+
write_header(opts)
5881

5982

60-
def WriteHeader(options):
61-
with open(options.output, "w") as output_file:
62-
output_file.write("// Generated by write_build_time_header.py\n")
63-
output_file.write('#pragma once\n\n')
64-
65-
output_file.write(f'#define {options.define_name} {options.define_val}\n')
66-
67-
68-
options = GetOptions()
69-
WriteHeader(options)
83+
main()

0 commit comments

Comments
 (0)