Represents a Zoekt server for indexing and searching repositories. Args: language (str): The language of the repositories to be indexed. Attributes: index_path (str): The path to the Zoekt index. zoekt_server (subprocess.Popen): The subprocess representing the
| 8 | import signal |
| 9 | |
| 10 | class ZoektServer: |
| 11 | """ |
| 12 | Represents a Zoekt server for indexing and searching repositories. |
| 13 | |
| 14 | Args: |
| 15 | language (str): The language of the repositories to be indexed. |
| 16 | |
| 17 | Attributes: |
| 18 | index_path (str): The path to the Zoekt index. |
| 19 | zoekt_server (subprocess.Popen): The subprocess representing the Zoekt web server. |
| 20 | repo_path (str): The path to the repository. |
| 21 | language (str): The language of the repositories. |
| 22 | |
| 23 | Methods: |
| 24 | setup_index: Sets up the index for the repository. |
| 25 | start_server: Starts the Zoekt web server. |
| 26 | search: Performs a search on the Zoekt server. |
| 27 | |
| 28 | """ |
| 29 | |
| 30 | def __init__(self, language, repo_path:Optional[str]=None, index_path:Optional[str]=None): |
| 31 | self.index_path = index_path |
| 32 | self.zoekt_server = None |
| 33 | self.repo_path = repo_path |
| 34 | self.language = language |
| 35 | |
| 36 | def setup_index(self, repo_path:str, root_index_path:str="/tmp/zoekt_tmp", index_path:Optional[str]=None): |
| 37 | """ |
| 38 | Sets up the index for the repository. |
| 39 | |
| 40 | Args: |
| 41 | repo_path (str): The path to the repository. |
| 42 | index_path (str, optional): The path to the index directory. Defaults to ".zoekt_tmp". |
| 43 | |
| 44 | """ |
| 45 | zoekt_index_repo_path = f"{root_index_path}/{repo_path.split('/')[-1]}" |
| 46 | self.repo_path = repo_path |
| 47 | self.index_path = zoekt_index_repo_path |
| 48 | if not Path(zoekt_index_repo_path).is_dir(): |
| 49 | Path(zoekt_index_repo_path).mkdir(parents=True) |
| 50 | subprocess.run( |
| 51 | f"$GOPATH/bin/zoekt-index -index {zoekt_index_repo_path} -parallelism=1 {repo_path}", |
| 52 | shell=True, |
| 53 | check=True, |
| 54 | stdout=subprocess.DEVNULL, |
| 55 | stderr=subprocess.DEVNULL, |
| 56 | ) |
| 57 | |
| 58 | @contextmanager |
| 59 | def start_server(self): |
| 60 | """ |
| 61 | Starts the Zoekt web server. |
| 62 | |
| 63 | Yields: |
| 64 | ZoektServer: The current instance of the ZoektServer class. |
| 65 | |
| 66 | """ |
| 67 | self.zoekt_server = subprocess.Popen( |
no outgoing calls
no test coverage detected