NeeravS's picture
Update app.py
c050703 verified
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()