diff --git a/scripts/tools/zap_regen_all.py b/scripts/tools/zap_regen_all.py index 99b0fafb4f6257..01845e8f4e3f73 100755 --- a/scripts/tools/zap_regen_all.py +++ b/scripts/tools/zap_regen_all.py @@ -25,6 +25,46 @@ os.path.join(os.path.dirname(__file__), '../..')) +class ZAPGenerateTarget: + def __init__(self, zap_config, template=None, output_dir=None): + self.script = './scripts/tools/zap/generate.py' + self.output_dir = output_dir + self.zap_config = zap_config + self.template = template + + def generate(self): + """Runs a ZAP generate command on the configured zap/template/outputs. + """ + cmd = [self.script, self.zap_config] + + if self.template: + cmd.append('-t') + cmd.append(self.template) + + if self.output_dir: + cmd.append('-o') + cmd.append(self.output_dir) + + logging.info("Generating target: %s" % " ".join(cmd)) + subprocess.check_call(cmd) + + def extractAnyGeneratedIDL(self): + """Searches for Clusters.matter in the output directory and if found, + will move it to stay along with the zap file config + """ + + idl_path = os.path.join(self.output_dir, "Clusters.matter") + if not os.path.exists(idl_path): + return + + target_path = self.zap_config.replace(".zap", ".matter") + if not target_path.endswith(".matter"): + # We expect "something.zap" and don't handle corner cases of + # multiple extensions. This is to work with existing codebase only + raise Error("Unexpected input zap file %s" % self.zap_config) + + os.rename(idl_path, target_path) + def checkPythonVersion(): if sys.version_info[0] < 3: print('Must use Python 3. Current version is ' + @@ -55,8 +95,11 @@ def getGlobalTemplatesTargets(): if not os.path.exists(output_dir): os.makedirs(output_dir) template = 'examples/placeholder/templates/templates.json' - targets.append([str(filepath), '-o', output_dir]) - targets.append([str(filepath), '-o', output_dir, '-t', template]) + + targets.append(ZAPGenerateTarget( + str(filepath), output_dir=output_dir)) + targets.append( + ZAPGenerateTarget(str(filepath), output_dir=output_dir, template=template)) continue logging.info("Found example %s (via %s)" % @@ -69,13 +112,11 @@ def getGlobalTemplatesTargets(): if not os.path.exists(output_dir): os.makedirs(output_dir) - targets.append([str(filepath), '-o', output_dir]) + targets.append(ZAPGenerateTarget(str(filepath), output_dir=output_dir)) - targets.extend([ - [ - './src/controller/data_model/controller-clusters.zap', - '-o', - os.path.join('zzz_generated/controller-clusters/zap-generated')]]) + targets.append(ZAPGenerateTarget( + './src/controller/data_model/controller-clusters.zap', + output_dir=os.path.join('zzz_generated/controller-clusters/zap-generated'))) return targets @@ -94,12 +135,12 @@ def getSpecificTemplatesTargets(): } for template, output_dir in templates.items(): - target = [ - 'src/controller/data_model/controller-clusters.zap', '-t', template] + target = ZAPGenerateTarget( + 'src/controller/data_model/controller-clusters.zap', template=template) if output_dir is not None: if not os.path.exists(output_dir): os.makedirs(output_dir) - target.extend(['-o', output_dir]) + target.output_dir = output_dir targets.append(target) @@ -123,9 +164,8 @@ def main(): targets = getTargets() for target in targets: - exec_list = ['./scripts/tools/zap/generate.py'] + target - logging.info("Generating target: %s" % " ".join(exec_list)) - subprocess.check_call(exec_list) + target.generate() + target.extractAnyGeneratedIDL() if __name__ == '__main__':