-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #360 from XpressAI/fahreza/recursive-compile
✨ Add Recursive Compile
- Loading branch information
Showing
11 changed files
with
171 additions
and
45 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
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1 +1 @@ | ||
from .compiler import compile | ||
from .compiler import compile, recursive_compile |
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 |
---|---|---|
@@ -1,14 +1,62 @@ | ||
import json | ||
|
||
import os | ||
from xircuits.compiler.parser import XircuitsFileParser | ||
from xircuits.compiler.generator import CodeGenerator | ||
|
||
|
||
def compile(input_file, output_file, component_python_paths=None): | ||
def compile(input_file_path, output_file_path, component_python_paths=None): | ||
if component_python_paths is None: | ||
component_python_paths = {} | ||
|
||
parser = XircuitsFileParser() | ||
graph = parser.parse(input_file) | ||
with open(input_file_path, 'r', encoding='utf-8') as in_f: | ||
graph = parser.parse(in_f) | ||
generator = CodeGenerator(graph, component_python_paths) | ||
generator.generate(output_file) | ||
with open(output_file_path, 'w', encoding='utf-8') as out_f: | ||
generator.generate(out_f) | ||
|
||
def recursive_compile(input_file_path, component_python_paths=None, visited_files=None): | ||
if component_python_paths is None: | ||
component_python_paths = {} | ||
if visited_files is None: | ||
visited_files = set() | ||
|
||
# Normalize file path | ||
input_file_path = os.path.abspath(input_file_path) | ||
|
||
if input_file_path in visited_files: | ||
return | ||
|
||
visited_files.add(input_file_path) | ||
|
||
# Read the Xircuits workflow file | ||
try: | ||
with open(input_file_path, 'r', encoding='utf-8') as f: | ||
data = json.load(f) | ||
except Exception as e: | ||
print(f"Error reading {input_file_path}: {e}") | ||
return | ||
|
||
# Traverse layers to find nested workflows | ||
if 'layers' in data: | ||
for layer in data['layers']: | ||
if 'models' in layer and isinstance(layer['models'], dict): | ||
for model in layer['models'].values(): | ||
extras = model.get('extras', {}) | ||
if extras.get('type') == "xircuits_workflow": | ||
# Extract and transform the path from .py to .xircuits | ||
py_path = extras.get('path') | ||
if py_path: | ||
nested_xircuits_path = py_path.replace('.py', '.xircuits') | ||
# Resolve relative paths | ||
nested_xircuits_path = os.path.join(os.path.dirname(input_file_path), nested_xircuits_path) | ||
# Recursively compile the nested workflow | ||
recursive_compile(nested_xircuits_path, component_python_paths, visited_files) | ||
|
||
# Compile the current workflow | ||
py_output_path = input_file_path.replace('.xircuits', '.py') | ||
# print(f"Compiling {input_file_path} to {py_output_path}") | ||
|
||
try: | ||
compile(input_file_path, py_output_path, component_python_paths=component_python_paths) | ||
print(f"Compiled {input_file_path} to {py_output_path}") | ||
except Exception as e: | ||
print(f"Failed to compile {input_file_path}: {e}") |
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
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
Oops, something went wrong.