Coverage for core/src/version_finder/main.py: 23%
75 statements
« prev ^ index » next coverage.py v7.7.0, created at 2025-03-18 10:30 +0000
« prev ^ index » next coverage.py v7.7.0, created at 2025-03-18 10:30 +0000
1import os
2import sys
3import subprocess
4from .common import parse_arguments, args_to_command
5from .version_finder import VersionFinder
8class ExternalInterfaceNotSupportedError(Exception):
9 """Raised when the external interface is not supported."""
11 def __init__(self, interface: str):
12 installation_instructions = (
13 f"Please install the CLI app using 'pip install version-finder-git-based-versions-{interface}'.")
14 super().__init__(f"External interface '{interface}' is not supported. {installation_instructions}")
17def is_package_installed(package_name: str) -> bool:
18 """Verify if the package is installed in the system using pip show."""
19 try:
20 print(f"Verifying installation of CLI: {package_name}")
21 result = subprocess.run(
22 [sys.executable, '-m', 'pip', 'show', package_name],
23 capture_output=True,
24 text=True
25 )
26 return result.returncode == 0
27 except Exception:
28 return False
31def call_cli_app(args):
32 """Call the CLI app."""
33 # Verify if the CLI app installed in the system
34 if not is_package_installed("version-finder-git-based-versions-cli"):
35 raise ExternalInterfaceNotSupportedError("cli")
37 # Call the CLI app with the provided arguments
38 os.system(f"version-finder-cli {args_to_command(args)}")
39 return 0
42def call_gui_app(args):
43 """Call the GUI app."""
44 # Verify if the GUI app installed in the system
45 if not is_package_installed("version-finder-git-based-versions-gui-app"):
46 raise ExternalInterfaceNotSupportedError("gui")
47 # Call the GUI app with the provided arguments
48 os.system(f"version-finder-gui {args_to_command(args)}")
49 return 0
52def main():
53 """Main entry point for the application."""
54 args = parse_arguments()
56 if args.version:
57 from .__version__ import __version__
58 print(f"version_finder v{__version__}")
59 return 0
61 if args.cli:
62 call_cli_app(args)
63 elif args.gui:
64 call_gui_app(args)
65 else:
66 if args.path is None:
67 args.path = os.getcwd()
68 if args.path and args.branch and args.commit:
69 try:
70 # Initialize with force parameter
71 vf = VersionFinder(path=args.path, force=args.force)
73 # Check for uncommitted changes
74 state = vf.get_saved_state()
75 if state.get("has_changes", False) and not args.force:
76 proceed = input("Repository has uncommitted changes. Proceed anyway? (y/N): ").lower() == 'y'
77 if not proceed:
78 print("Operation cancelled by user")
79 return 0
81 vf.update_repository(args.branch)
82 version = vf.find_first_version_containing_commit(args.commit, args.submodule)
84 if version:
85 print(f"The first version which includes commit {args.commit} is {version}")
86 else:
87 print(f"No version found for commit {args.commit}")
89 # Restore original state if requested
90 if args.restore_state:
91 print("Restoring original repository state")
93 # Get the state before restoration for logging
94 state = vf.get_saved_state()
95 has_changes = state.get("has_changes", False)
96 stash_created = state.get("stash_created", False)
98 if has_changes:
99 if stash_created:
100 print("Attempting to restore stashed changes")
101 else:
102 print("Warning: Repository had changes but they were not stashed")
104 # Perform the restoration
105 if vf.restore_repository_state():
106 print("Original repository state restored successfully")
108 # Verify the restoration
109 if has_changes and vf.has_uncommitted_changes():
110 print("Uncommitted changes were successfully restored")
111 elif has_changes and not vf.has_uncommitted_changes():
112 print("Error: Failed to restore uncommitted changes")
113 else:
114 print("Failed to restore original repository state")
116 except Exception as e:
117 print(f"Error: {str(e)}")
118 return 1
119 else:
120 print("Please provide a path, branch, and commit to search for.")
121 print("Or add --cli or --gui to run the CLI or GUI version respectively.")
124if __name__ == "__main__":
125 main()