startup.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. Application startup utilities.
  3. Handles initial setup of required directories and configuration files.
  4. This module ensures the application environment is properly configured
  5. before the main application starts.
  6. """
  7. import os
  8. import logging
  9. logger = logging.getLogger(__name__)
  10. def ensure_base_structure(base_dir: str) -> None:
  11. """
  12. Ensure required base directory structure exists.
  13. Creates necessary subdirectories for artifacts, configs, workdir,
  14. and secrets if they don't already exist.
  15. Args:
  16. base_dir: The base directory path (typically from CBS_BASEDIR)
  17. """
  18. if not base_dir:
  19. logger.warning("Base directory not specified, skipping initialization")
  20. return
  21. # Define required subdirectories
  22. subdirs = [
  23. 'artifacts',
  24. 'configs',
  25. 'workdir',
  26. 'secrets',
  27. ]
  28. for subdir in subdirs:
  29. path = os.path.join(base_dir, subdir)
  30. os.makedirs(path, exist_ok=True)
  31. logger.debug(f"Ensured directory exists: {path}")
  32. def ensure_remotes_json(base_dir: str, remote_name: str = "ardupilot") -> None:
  33. """
  34. Ensure remotes.json configuration file exists.
  35. If the remotes.json file doesn't exist, creates it by fetching release
  36. information from the specified remote.
  37. Args:
  38. base_dir: The base directory path (typically from CBS_BASEDIR)
  39. remote_name: The remote repository name to fetch releases from
  40. """
  41. if not base_dir:
  42. logger.warning(
  43. "Base directory not specified, "
  44. "skipping remotes.json initialization"
  45. )
  46. return
  47. remotes_json_path = os.path.join(base_dir, 'configs', 'remotes.json')
  48. if not os.path.isfile(remotes_json_path):
  49. logger.info(
  50. f"remotes.json not found at {remotes_json_path}, "
  51. f"creating it..."
  52. )
  53. try:
  54. from scripts import fetch_releases
  55. fetch_releases.run(
  56. base_dir=base_dir,
  57. remote_name=remote_name,
  58. )
  59. logger.info("Successfully created remotes.json")
  60. except Exception as e:
  61. logger.error(f"Failed to create remotes.json: {e}")
  62. raise
  63. else:
  64. logger.debug(f"remotes.json already exists at {remotes_json_path}")
  65. def initialize_application(base_dir: str) -> None:
  66. """
  67. Initialize the application environment.
  68. Performs all necessary setup operations including:
  69. - Creating required directory structure
  70. - Ensuring remotes.json configuration exists
  71. Args:
  72. base_dir: The base directory path (typically from CBS_BASEDIR)
  73. """
  74. if not base_dir:
  75. logger.warning("CBS_BASEDIR not set, skipping initialization")
  76. return
  77. logger.info(f"Initializing application with base directory: {base_dir}")
  78. # Ensure directory structure
  79. ensure_base_structure(base_dir)
  80. # Ensure remotes.json exists
  81. ensure_remotes_json(base_dir)
  82. logger.info("Application initialization complete")