|
1 | 1 | import os
|
2 |
| -import tempfile |
| 2 | +import shutil |
| 3 | +import subprocess |
3 | 4 | import concurrent.futures
|
4 | 5 |
|
5 | 6 | from git import Repo
|
|
19 | 20 | '.py': PythonAnalyzer()}
|
20 | 21 |
|
21 | 22 | class SourceAnalyzer():
|
22 |
| - def __init__(self, host: str = 'localhost', port: int = 6379, |
23 |
| - username: Optional[str] = None, password: Optional[str] = None) -> None: |
24 |
| - |
25 |
| - self.host = host |
26 |
| - self.port = port |
27 |
| - self.username = username |
28 |
| - self.password = password |
| 23 | + def __init__(self) -> None: |
| 24 | + self.host = os.getenv('FALKORDB_HOST') |
| 25 | + self.port = os.getenv('FALKORDB_PORT') |
| 26 | + self.username = os.getenv('FALKORDB_USERNAME') |
| 27 | + self.password = os.getenv('FALKORDB_PASSWORD') |
29 | 28 |
|
30 | 29 | def first_pass(self, ignore: List[str], executor: concurrent.futures.Executor) -> None:
|
31 | 30 | """
|
@@ -126,34 +125,59 @@ def analyze_sources(self, ignore: List[str]) -> None:
|
126 | 125 | # Second pass analysis of the source code
|
127 | 126 | self.second_pass(ignore, executor)
|
128 | 127 |
|
129 |
| - def analyze_github_repository(self, url: str) -> None: |
| 128 | + def analyze_github_repository( |
| 129 | + self, |
| 130 | + url: str, |
| 131 | + repo_path: Path, |
| 132 | + repo_name: str, |
| 133 | + ignore: Optional[List[str]] = [] |
| 134 | + ) -> None: |
130 | 135 | """
|
131 | 136 | Analyze a Git repository given its URL.
|
132 | 137 |
|
133 | 138 | Args:
|
134 |
| - url (str): The URL of the Git repository to analyze. |
| 139 | + url: The URL of the Git repository to analyze |
| 140 | + ignore_patterns: List of patterns to ignore during analysis |
| 141 | +
|
| 142 | + Raises: |
| 143 | + subprocess.SubprocessError: If git clone fails |
| 144 | + OSError: If there are filesystem operation errors |
135 | 145 | """
|
136 | 146 |
|
137 |
| - # Extract repository name from the URL |
138 |
| - components = url[:url.rfind('.')].split('/') |
139 |
| - n = len(components) |
140 |
| - repo_name = f'{components[n-2]}/{components[-1]}' |
141 |
| - logger.debug(f'repo_name: {repo_name}') |
142 |
| - #repo_name = url[url.rfind('/')+1:url.rfind('.')] |
| 147 | + # Extract repository name more reliably |
| 148 | + # Delete local repository if exists |
| 149 | + if repo_path.exists(): |
| 150 | + shutil.rmtree(repo_path) |
143 | 151 |
|
144 |
| - # Initialize the graph and analyzer |
145 |
| - self.graph = Graph(repo_name, self.host, self.port, self.username, |
146 |
| - self.password) |
| 152 | + # Create directory |
| 153 | + repo_path.mkdir(parents=True, exist_ok=True) |
| 154 | + |
| 155 | + # Clone repository |
| 156 | + # Prepare the git clone command |
| 157 | + command = ["git", "clone", url, repo_path] |
| 158 | + |
| 159 | + # Run the git clone command and wait for it to finish |
| 160 | + result = subprocess.run(command, check=True, capture_output=True, text=True) |
| 161 | + |
| 162 | + # Store original working directory |
| 163 | + original_dir = Path.cwd() |
| 164 | + |
| 165 | + # change working directory to local repository |
| 166 | + os.chdir(repo_path) |
| 167 | + |
| 168 | + try: |
| 169 | + # Initialize the graph and analyzer |
| 170 | + self.graph = Graph(repo_name, self.host, self.port, self.username, |
| 171 | + self.password) |
147 | 172 |
|
148 |
| - # Create a temporary directory for cloning the repository |
149 |
| - with tempfile.TemporaryDirectory() as temp_dir: |
150 |
| - logger.info(f"Cloning repository {url} to {temp_dir}") |
151 |
| - repo = Repo.clone_from(url, temp_dir) |
| 173 | + # Analyze repository |
| 174 | + self.analyze_sources(ignore) |
152 | 175 |
|
153 |
| - # Analyze source files |
154 |
| - self.analyze_sources(temp_dir) |
| 176 | + logging.info(f"Successfully processed repository: {repo_name}") |
155 | 177 |
|
156 |
| - logger.info("Done processing repository") |
| 178 | + finally: |
| 179 | + # Ensure we always return to the original directory |
| 180 | + os.chdir(original_dir) |
157 | 181 |
|
158 | 182 | def analyze_local_folder(self, path: str, ignore: Optional[List[str]] = []) -> Graph:
|
159 | 183 | """
|
|
0 commit comments