|
| 1 | +import logging |
| 2 | +import shutil |
| 3 | +from dataclasses import dataclass |
| 4 | +from pathlib import Path |
| 5 | +from typing import Dict, List, Union |
| 6 | + |
| 7 | +import git |
| 8 | + |
| 9 | +from pydocstringformatter.testutils.primer.const import PRIMER_DIRECTORY_PATH |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class _PackageToPrime: |
| 14 | + """Represents data about a package to be tested during primer tests.""" |
| 15 | + |
| 16 | + url: str |
| 17 | + """URL of the repository to clone.""" |
| 18 | + |
| 19 | + branch: str |
| 20 | + """Branch of the repository to clone.""" |
| 21 | + |
| 22 | + directories: List[str] |
| 23 | + """Directories within the repository to run the program over.""" |
| 24 | + |
| 25 | + @property |
| 26 | + def clone_directory(self) -> Path: |
| 27 | + """Directory to clone repository into.""" |
| 28 | + clone_name = "/".join(self.url.split("/")[-2:]).replace(".git", "") |
| 29 | + return PRIMER_DIRECTORY_PATH / clone_name |
| 30 | + |
| 31 | + @property |
| 32 | + def paths_to_lint(self) -> List[str]: |
| 33 | + """The paths we need to run against.""" |
| 34 | + return [str(self.clone_directory / path) for path in self.directories] |
| 35 | + |
| 36 | + def lazy_clone(self) -> None: |
| 37 | + """Clone the repo or pull any new commits. |
| 38 | +
|
| 39 | + # TODO(#80): Allow re-using an already cloned repistory instead of removing it |
| 40 | + Currently this is therefore not really 'lazy'. |
| 41 | + """ |
| 42 | + logging.info("Lazy cloning %s", self.url) |
| 43 | + |
| 44 | + if self.clone_directory.exists(): |
| 45 | + shutil.rmtree(self.clone_directory) |
| 46 | + |
| 47 | + options: Dict[str, Union[str, int]] = { |
| 48 | + "url": self.url, |
| 49 | + "to_path": str(self.clone_directory), |
| 50 | + "branch": self.branch, |
| 51 | + "depth": 1, |
| 52 | + } |
| 53 | + git.Repo.clone_from(**options) |
| 54 | + |
| 55 | + |
| 56 | +PACKAGES = { |
| 57 | + "pylint": _PackageToPrime( |
| 58 | + "https://github.com/PyCQA/pylint", |
| 59 | + "main", |
| 60 | + ["pylint"], |
| 61 | + ), |
| 62 | + "pydocstringformatter": _PackageToPrime( |
| 63 | + "https://github.com/DanielNoord/pydocstringformatter", |
| 64 | + "main", |
| 65 | + ["pydocstringformatter"], |
| 66 | + ), |
| 67 | +} |
0 commit comments