|
| 1 | +import logging |
| 2 | +from pathlib import Path |
| 3 | +from typing import Dict, List, Optional, Union |
| 4 | + |
| 5 | +import git |
| 6 | + |
| 7 | +PRIMER_DIRECTORY_PATH = Path(".pylint_primer_tests") |
| 8 | + |
| 9 | + |
| 10 | +class PackageToLint: |
| 11 | + """Represents data about a package to be tested during primer tests""" |
| 12 | + |
| 13 | + url: str |
| 14 | + """URL of the repository to clone""" |
| 15 | + |
| 16 | + branch: str |
| 17 | + """Branch of the repository to clone""" |
| 18 | + |
| 19 | + directories: List[str] |
| 20 | + """Directories within the repository to run pylint over""" |
| 21 | + |
| 22 | + commit: Optional[str] |
| 23 | + """Commit hash to pin the repository on""" |
| 24 | + |
| 25 | + pylint_additional_args: List[str] |
| 26 | + """Arguments to give to pylint""" |
| 27 | + |
| 28 | + pylintrc_relpath: Optional[str] |
| 29 | + """Path relative to project's main directory to the pylintrc if it exists""" |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + url: str, |
| 34 | + branch: str, |
| 35 | + directories: List[str], |
| 36 | + commit: Optional[str] = None, |
| 37 | + pylint_additional_args: Optional[List[str]] = None, |
| 38 | + pylintrc_relpath: Optional[str] = None, |
| 39 | + ) -> None: |
| 40 | + self.url = url |
| 41 | + self.branch = branch |
| 42 | + self.directories = directories |
| 43 | + self.commit = commit |
| 44 | + self.pylint_additional_args = pylint_additional_args or [] |
| 45 | + self.pylintrc_relpath = pylintrc_relpath |
| 46 | + |
| 47 | + @property |
| 48 | + def pylintrc(self) -> Optional[Path]: |
| 49 | + if self.pylintrc_relpath is None: |
| 50 | + return None |
| 51 | + return self.clone_directory / self.pylintrc_relpath |
| 52 | + |
| 53 | + @property |
| 54 | + def clone_directory(self) -> Path: |
| 55 | + """Directory to clone repository into""" |
| 56 | + clone_name = "/".join(self.url.split("/")[-2:]).replace(".git", "") |
| 57 | + return PRIMER_DIRECTORY_PATH / clone_name |
| 58 | + |
| 59 | + @property |
| 60 | + def paths_to_lint(self) -> List[str]: |
| 61 | + """The paths we need to lint""" |
| 62 | + return [str(self.clone_directory / path) for path in self.directories] |
| 63 | + |
| 64 | + @property |
| 65 | + def pylint_args(self) -> List[str]: |
| 66 | + options: List[str] = [] |
| 67 | + if self.pylintrc is not None: |
| 68 | + # There is an error if rcfile is given but does not exists |
| 69 | + options += [f"--rcfile={self.pylintrc}"] |
| 70 | + return self.paths_to_lint + options + self.pylint_additional_args |
| 71 | + |
| 72 | + def lazy_clone(self) -> None: # pragma: no cover |
| 73 | + """Concatenates the target directory and clones the file |
| 74 | +
|
| 75 | + Not expected to be tested as the primer won't work if it doesn't. |
| 76 | + It's tested in the continuous integration primers, only the coverage |
| 77 | + is not calculated on everything. If lazy clone breaks for local use |
| 78 | + we'll probably notice because we'll have a fatal when launching the |
| 79 | + primer locally. |
| 80 | + """ |
| 81 | + logging.info("Lazy cloning %s", self.url) |
| 82 | + if not self.clone_directory.exists(): |
| 83 | + options: Dict[str, Union[str, int]] = { |
| 84 | + "url": self.url, |
| 85 | + "to_path": str(self.clone_directory), |
| 86 | + "branch": self.branch, |
| 87 | + "depth": 1, |
| 88 | + } |
| 89 | + logging.info("Directory does not exists, cloning: %s", options) |
| 90 | + git.Repo.clone_from(**options) |
| 91 | + return |
| 92 | + |
| 93 | + remote_sha1_commit = ( |
| 94 | + git.cmd.Git().ls_remote(self.url, self.branch).split("\t")[0] |
| 95 | + ) |
| 96 | + local_sha1_commit = git.Repo(self.clone_directory).head.object.hexsha |
| 97 | + if remote_sha1_commit != local_sha1_commit: |
| 98 | + logging.info( |
| 99 | + "Remote sha is '%s' while local sha is '%s': pulling new commits", |
| 100 | + remote_sha1_commit, |
| 101 | + local_sha1_commit, |
| 102 | + ) |
| 103 | + repo = git.Repo(self.clone_directory) |
| 104 | + origin = repo.remotes.origin |
| 105 | + origin.pull() |
| 106 | + else: |
| 107 | + logging.info("Repository already up to date.") |
0 commit comments