18
18
from datetime import datetime , timezone
19
19
20
20
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
+
21
25
def utc_time_in_matter_epoch_s (time : datetime ):
22
26
""" Returns the time in matter epoch in s. """
23
27
# 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):
32
36
self .define_val = define_val
33
37
34
38
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 ))))
37
50
51
+
52
+ def main ():
38
53
parser = argparse .ArgumentParser ()
39
54
parser .add_argument ('--output' , help = "Output header name (inside gen dir)" )
40
55
parser .add_argument ('--gen-dir' ,
41
56
help = "Path to root of generated file directory tree." )
42
57
parser .add_argument ('--use-current-time' , default = False , action = 'store_true' ,
43
58
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' )
44
61
cmdline_options = parser .parse_args ()
45
62
63
+ if cmdline_options .update_fallback_time_in_file :
64
+ update_fallback_time_in_file ()
65
+ return
66
+
46
67
# The actual output file is inside the gen dir.
47
68
output = os .path .join (cmdline_options .gen_dir , cmdline_options .output )
48
69
49
70
define_name = 'CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S'
50
71
if cmdline_options .use_current_time :
51
72
build_time = utc_time_in_matter_epoch_s (datetime .now (tz = timezone .utc ))
52
73
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 ())
54
76
55
- return Options (output = output ,
77
+ opts = Options (output = output ,
56
78
define_name = define_name ,
57
79
define_val = str (build_time ))
80
+ write_header (opts )
58
81
59
82
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