Utility_Apps/Git/DOESN'T WORK-content_sparse_clone/content_sparse_clone.py
2025-09-10 09:31:12 -05:00

85 lines
2.9 KiB
Python

import os
import subprocess
import sys
import ifcopenshell
def main():
if len(sys.argv) < 6:
print("Usage: python content_sparse_clone.py <repo_url> <ifc_path> <sparse_path> <exclude_folder> <commit_hash>")
sys.exit(1)
repo_url = sys.argv[1]
ifc_path_in_repo = sys.argv[2].replace("\\", "/")
main_sparse_path = sys.argv[3].replace("\\", "/")
exclude_folder = sys.argv[4].replace("\\", "/")
commit_hash = sys.argv[5]
local_dir = os.path.join(os.getcwd(), os.path.basename(repo_url).replace(".git", ""))
print(f"[DEBUG] Local clone directory will be: {local_dir}")
# Clone shallowly
print(f"[DEBUG] Cloning repo shallowly into {local_dir}...")
subprocess.run(["git", "clone", "--no-checkout", repo_url, local_dir], check=True)
os.chdir(local_dir)
subprocess.run(["git", "checkout", commit_hash], check=True)
print(f"[DEBUG] Checked out commit {commit_hash}")
# Analyze IFC for external .blend references
ifc_file_full_path = os.path.join(local_dir, ifc_path_in_repo)
if not os.path.exists(ifc_file_full_path):
print(f"Error: IFC file {ifc_path_in_repo} not found after clone.")
sys.exit(1)
print(f"[DEBUG] IFC file found: {ifc_file_full_path}")
ifc = ifcopenshell.open(ifc_file_full_path)
referenced_blend_paths = set()
# Dummy logic: replace this with your actual IFC parsing for .blend references
# For example, look for IfcExternalReference or Blender-specific property
for obj in ifc.by_type("IfcProject"):
# Example: populate with relative paths you extract from IFC
pass
# Add known references manually or from your previous logic
# Example from your logs:
referenced_blend_paths.add("OD_Textures/Materials.blend")
# Initialize sparse checkout (cone mode works well)
print("[DEBUG] Initializing legacy sparse checkout...")
subprocess.run(["git", "sparse-checkout", "init", "--cone"], check=True)
sparse_rules = [main_sparse_path, ifc_path_in_repo]
sparse_rules.extend(referenced_blend_paths)
print(f"[DEBUG] Applying sparse-checkout rules: {sparse_rules}")
subprocess.run(["git", "sparse-checkout", "set"] + sparse_rules, check=True)
# Remove excluded folder if it was pulled accidentally
excluded_full_path = os.path.join(local_dir, exclude_folder)
if os.path.exists(excluded_full_path):
print(f"[DEBUG] Removing excluded folder from sparse checkout: {exclude_folder}")
subprocess.run(["git", "rm", "-r", "--cached", exclude_folder], check=False)
print("[DEBUG] Script completed.")
if __name__ == "__main__":
main()
'''
python content_sparse_clone.py `
"https://hub.openingdesign.com/OpeningDesign/Restaurant_Brookfield.git" `
"Open/Models/Bonsai/Restaurant_Brookfield_merged.ifc" `
"Open/Models/Bonsai" `
"Open/Models/Bonsai/OD_Textures" `
"8dee613638a242bb024fb49ca8628f2228a4c4af"
'''