Created
January 9, 2025 01:01
-
-
Save kevinkhill/2589db68ba2d24736e04ffa15ddd3bcd to your computer and use it in GitHub Desktop.
Automatic Dependency Installer for OpenSCAD
This file contains hidden or 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
| import os | |
| import subprocess | |
| import re | |
| import argparse | |
| def extract_dependency_url(file_path): | |
| with open(file_path, "r") as file: | |
| for line in file: | |
| match = re.search(r"//\s*@dependency\s*(\S+)", line) | |
| if match: | |
| return match.group(1) | |
| return None | |
| def clone_repo_if_needed(repo_url): | |
| if not repo_url: | |
| print("No dependency URL found.") | |
| exit(1) | |
| repo_name = repo_url.split("/")[-1].replace(".git", "") | |
| if not os.path.exists(repo_name): | |
| try: | |
| print(f"Cloning repository from {repo_url}...") | |
| subprocess.run(["git", "clone", repo_url], check=True) | |
| print(f"Repository cloned into '{repo_name}'.") | |
| except subprocess.CalledProcessError as e: | |
| print(f"Error cloning repository: {e}") | |
| exit(1) | |
| else: | |
| print(f"Repository already cloned in '{repo_name}'.") | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="Manage OpenSCAD dependencies and open file." | |
| ) | |
| parser.add_argument("input_file", help="Path to the OpenSCAD file.") | |
| parser.add_argument( | |
| "--openscad", help="Path to the OpenSCAD executable.", required=True | |
| ) | |
| args = parser.parse_args() | |
| input_file = args.input_file | |
| if not os.path.isfile(input_file): | |
| print(f"Error: File '{input_file}' not found.") | |
| exit(1) | |
| repo_url = extract_dependency_url(input_file) | |
| clone_repo_if_needed(repo_url) | |
| try: | |
| print(f"Opening '{input_file}' with OpenSCAD...") | |
| subprocess.Popen([args.openscad, input_file]) | |
| except Exception as e: | |
| print(f"Error opening file with OpenSCAD: {e}") | |
| if __name__ == "__main__": | |
| main() | |
| p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment