File size: 2,075 Bytes
114048b |
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import sys
import os
import subprocess
import logging
# Set up logging
logging.basicConfig(filename='bootstrapper.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
ENV_DIR = "app_env"
def create_virtual_env():
logging.info("Checking for virtual environment at {}".format(ENV_DIR))
if not os.path.exists(ENV_DIR):
logging.info("Virtual environment not found. Creating a new one.")
# Import virtualenv and create a new environment
import virtualenv
virtualenv.create_environment(ENV_DIR)
def install_dependencies():
logging.info("Installing dependencies.")
# Ensure the requirements.txt file is bundled with your application
requirements_path = "requirements.txt"
# pip executable within the virtual environment
pip_path = os.path.join(ENV_DIR, 'Scripts', 'pip')
try:
subprocess.check_call([pip_path, "install", "-r", requirements_path])
logging.info("Dependencies installed successfully.")
except Exception as e:
logging.error("Error installing dependencies: {}".format(e))
def main():
#try:
# create_virtual_env()
#except Exception as e:
# logging.error("An error occurred in the bootstrapper: {}".format(e), exc_info=True)
try:
import langchain
except ImportError:
logging.warning("Some dependencies are missing. Attempting to install.")
install_dependencies()
# Now you can run your main application logic.
# If it's in another file, you can use exec as shown before.
try:
with open('app.py', 'r') as file:
exec(file.read())
logging.info("Main application executed successfully.")
except Exception as e:
logging.error("Error executing main application: {}".format(e))
if __name__ == "__main__":
logging.info("Bootstrapper started.")
try:
main()
logging.info("Bootstrapper finished.")
except Exception as e:
logging.error("An error occurred in the bootstrapper: {}".format(e)) |