|
| 1 | +import logging |
| 2 | +from dataclasses import dataclass |
| 3 | +from pathlib import Path |
| 4 | +from typing import Dict, List, Union |
| 5 | + |
| 6 | +import git |
| 7 | + |
| 8 | +from pydocstringformatter.testutils.primer.const import PRIMER_DIRECTORY_PATH |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class _PackageToCheck: |
| 13 | + """Represents data about a package to be tested during primer tests.""" |
| 14 | + |
| 15 | + url: str |
| 16 | + """URL of the repository to clone.""" |
| 17 | + |
| 18 | + branch: str |
| 19 | + """Branch of the repository to clone.""" |
| 20 | + |
| 21 | + directories: List[str] |
| 22 | + """Directories within the repository to run the program over.""" |
| 23 | + |
| 24 | + @property |
| 25 | + def clone_directory(self) -> Path: |
| 26 | + """Directory to clone repository into.""" |
| 27 | + clone_name = "/".join(self.url.split("/")[-2:]).replace(".git", "") |
| 28 | + return PRIMER_DIRECTORY_PATH / clone_name |
| 29 | + |
| 30 | + @property |
| 31 | + def paths_to_lint(self) -> List[str]: |
| 32 | + """The paths we need to run against.""" |
| 33 | + return [str(self.clone_directory / path) for path in self.directories] |
| 34 | + |
| 35 | + def lazy_clone(self) -> None: |
| 36 | + """Clone the repo, if necessary.""" |
| 37 | + logging.info("Lazy cloning %s", self.url) |
| 38 | + |
| 39 | + # Clone if not yet cloned |
| 40 | + if not self.clone_directory.exists(): |
| 41 | + options: Dict[str, Union[str, int]] = { |
| 42 | + "url": self.url, |
| 43 | + "to_path": str(self.clone_directory), |
| 44 | + "branch": self.branch, |
| 45 | + "depth": 1, |
| 46 | + } |
| 47 | + logging.info("Directory does not exists, cloning: %s", options) |
| 48 | + git.Repo.clone_from(**options) |
| 49 | + return |
| 50 | + |
| 51 | + # Check if local clone is latest version |
| 52 | + remote_sha1_commit = ( |
| 53 | + git.cmd.Git().ls_remote(self.url, self.branch).split("\t")[0] |
| 54 | + ) |
| 55 | + local_sha1_commit = git.Repo(self.clone_directory).head.object.hexsha |
| 56 | + if remote_sha1_commit != local_sha1_commit: |
| 57 | + logging.info( |
| 58 | + "Remote sha is '%s' while local sha is '%s': pulling new commits", |
| 59 | + remote_sha1_commit, |
| 60 | + local_sha1_commit, |
| 61 | + ) |
| 62 | + repo = git.Repo(self.clone_directory) |
| 63 | + origin = repo.remotes.origin |
| 64 | + origin.pull() |
| 65 | + else: |
| 66 | + logging.info("Repository already up to date.") |
| 67 | + |
| 68 | + |
| 69 | +PACKAGES = { |
| 70 | + "pylint": _PackageToCheck("https://github.com/PyCQA/pylint", "main", ["pylint"]), |
| 71 | +} |
0 commit comments