File size: 1,737 Bytes
c4e85f9
35eccef
c050703
 
 
 
 
 
 
 
c4e85f9
c050703
 
 
 
 
 
 
 
 
 
 
 
 
 
c4e85f9
c050703
35eccef
c050703
 
 
3c79350
c050703
 
 
 
 
 
 
 
f4cafbc
c050703
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import sys
from git import Repo
import importlib.util

def main():
    # GitHub Repository and Directory Setup
    repo_url = os.getenv("REPO_URL")  # The private repository URL from environment variables
    github_token = os.getenv("GITHUB_PAT")  # GitHub Personal Access Token from environment variables
    local_dir = "repo"

    # Clone the private repository if it doesn't exist locally
    if not os.path.exists(local_dir):
        try:
            # Prepare the repo URL with the token for authentication
            repo_url = repo_url.replace("https://", f"https://{github_token}@")
            print("Cloning the repository...")
            Repo.clone_from(repo_url, local_dir)
            print("Repository cloned successfully!")
            
            # Add the repo directory to the system path
            sys.path.insert(0, local_dir)
        except Exception as e:
            print(f"Error cloning repository: {e}")
            sys.exit(1)  # Exit the script if the repo can't be cloned

    # Dynamically import and execute the app.py from the cloned repository
    try:
        spec = importlib.util.spec_from_file_location("cloned_app", os.path.join(local_dir, "app.py"))
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)

        # If the module has a 'main' function, call it (for apps like FastAPI or Gradio)
        if hasattr(module, "main"):
            module.main()
        else:
            print("No main function found in the cloned app.")
    except Exception as e:
        print(f"Error running the app from the cloned repo: {e}")
        sys.exit(1)  # Exit the script if the cloned app can't be executed

if __name__ == "__main__":
    main()