From 155597aeff23d0a5ffed2b572c277b6d61291306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Val=C3=AD=C4=8Dek?= Date: Wed, 4 May 2022 15:02:55 +0200 Subject: [PATCH] Python: basic, package install, error handler for begining MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Václav Valíček --- .coveragerc | 1 + .gitignore | 1 + .idea/repo-cloner.iml | 4 ++ repo_cloner/__init__.py | 1 + repo_cloner/lib/__init__.py | 0 repo_cloner/lib/dir_not_found_error.py | 15 +++++ repo_cloner/lib/repo_dir_structure.py | 44 +++++++++++++++ repo_cloner/process_repository_dir.py | 21 +++++++ requirements.txt | 2 + setup.py | 12 ++++ test.py | 6 -- tests/lib/test_dir_not_found_error.py | 25 +++++++++ tests/lib/test_repo_dir_structure.py | 78 ++++++++++++++++++++++++++ 13 files changed, 204 insertions(+), 6 deletions(-) create mode 100644 repo_cloner/__init__.py create mode 100644 repo_cloner/lib/__init__.py create mode 100644 repo_cloner/lib/dir_not_found_error.py create mode 100644 repo_cloner/lib/repo_dir_structure.py create mode 100755 repo_cloner/process_repository_dir.py create mode 100644 setup.py delete mode 100755 test.py create mode 100644 tests/lib/test_dir_not_found_error.py create mode 100644 tests/lib/test_repo_dir_structure.py diff --git a/.coveragerc b/.coveragerc index 42a3f3b..810d27b 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,6 +1,7 @@ [run] omit = tests/* + setup.py diff --git a/.gitignore b/.gitignore index c948f4d..da6d3b3 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ __pycache__/ build/ dist/ htmlcov/ +*.egg-info diff --git a/.idea/repo-cloner.iml b/.idea/repo-cloner.iml index 74d515a..8e5446a 100644 --- a/.idea/repo-cloner.iml +++ b/.idea/repo-cloner.iml @@ -7,4 +7,8 @@ + + \ No newline at end of file diff --git a/repo_cloner/__init__.py b/repo_cloner/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/repo_cloner/__init__.py @@ -0,0 +1 @@ + diff --git a/repo_cloner/lib/__init__.py b/repo_cloner/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/repo_cloner/lib/dir_not_found_error.py b/repo_cloner/lib/dir_not_found_error.py new file mode 100644 index 0000000..5330933 --- /dev/null +++ b/repo_cloner/lib/dir_not_found_error.py @@ -0,0 +1,15 @@ +from pathlib import PosixPath + + +class DirNotFoundError(OSError): + + def __str__(self): + if len(self.args) >= 1: + path: str = "" + if isinstance(self.args[0], PosixPath): + path = str(PosixPath(self.args[0])) + else: + path = str(self.args[0]) + return f"Directory does not exist / is not a dir: {path}" + else: + return super.__str__(self) diff --git a/repo_cloner/lib/repo_dir_structure.py b/repo_cloner/lib/repo_dir_structure.py new file mode 100644 index 0000000..6d0c74d --- /dev/null +++ b/repo_cloner/lib/repo_dir_structure.py @@ -0,0 +1,44 @@ +import os.path +from repo_cloner.lib.dir_not_found_error import DirNotFoundError + + +class RepoDirStructure(): + _base_dir: str = "" + _conf_dir: str = "" + _cache_dir: str = "" + _repos_dir: str = "" + + def __init__(self, base_dir: str): + self._base_dir = base_dir + self._conf_dir = os.path.join(self._base_dir, "config") + self._cache_dir = os.path.join(self._base_dir, "cache") + self._repos_dir = os.path.join(self._base_dir, "repos") + + @property + def exists(self) -> bool: + if not os.path.isdir(self._base_dir): + raise DirNotFoundError(self._base_dir) + if not os.path.isdir(self._conf_dir): + raise DirNotFoundError(self._conf_dir) + if not os.path.isdir(self._cache_dir): + raise DirNotFoundError(self._cache_dir) + if not os.path.isdir(self._repos_dir): + raise DirNotFoundError(self._repos_dir) + + return True + + @property + def base_dir(self) -> str: + return self._base_dir + + @property + def cache_dir(self) -> str: + return self._cache_dir + + @property + def conf_dir(self) -> str: + return self._conf_dir + + @property + def repos_dir(self) -> str: + return self._repos_dir diff --git a/repo_cloner/process_repository_dir.py b/repo_cloner/process_repository_dir.py new file mode 100755 index 0000000..4e95f40 --- /dev/null +++ b/repo_cloner/process_repository_dir.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +import argparse + +from repo_cloner.lib.repo_dir_structure import RepoDirStructure + + +def main(): + # parse input arguments + parser = argparse.ArgumentParser(description = "repo-cloner entering script") + parser.add_argument('--base-dir', help = 'path to directory containing whole cloner structure', required = True, + default = None, type = str) + args = parser.parse_args() + + dirs = RepoDirStructure(args.base_dir) + print(dirs) + dirs.exists + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt index eebb98a..9593025 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ gitdb==4.0.9 GitPython==3.1.27 smmap==5.0.0 +-e . + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..b57156a --- /dev/null +++ b/setup.py @@ -0,0 +1,12 @@ +from setuptools import setup, find_packages + +print("Packages:", find_packages()) + +setup( + name = 'repo_cloner', + version = '0.1', + packages = find_packages(), + entry_points = { + 'console_scripts': ['process_repository_dir=repo_cloner.process_repository_dir:main'] + }, +) diff --git a/test.py b/test.py deleted file mode 100755 index d2b5087..0000000 --- a/test.py +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env python3 - -from git import Repo - -r = Repo("/home/vasek/dev/repo-cloner") -print(r.bare) diff --git a/tests/lib/test_dir_not_found_error.py b/tests/lib/test_dir_not_found_error.py new file mode 100644 index 0000000..4863111 --- /dev/null +++ b/tests/lib/test_dir_not_found_error.py @@ -0,0 +1,25 @@ +from repo_cloner.lib.dir_not_found_error import DirNotFoundError +from pathlib import PosixPath + + +def test_type(): + x = DirNotFoundError() + assert isinstance(x, DirNotFoundError) + + +def test_return(): + x = DirNotFoundError("/tmp") + s = x.__str__() + assert s == "Directory does not exist / is not a dir: /tmp" + + +def test_return_empty(): + x = DirNotFoundError() + s = x.__str__() + assert s == "DirNotFoundError()" + + +def test_posix_path(): + d = PosixPath("/nonexistent") + x = DirNotFoundError(d) + assert x.__str__() == "Directory does not exist / is not a dir: /nonexistent" diff --git a/tests/lib/test_repo_dir_structure.py b/tests/lib/test_repo_dir_structure.py new file mode 100644 index 0000000..d6f7038 --- /dev/null +++ b/tests/lib/test_repo_dir_structure.py @@ -0,0 +1,78 @@ +import xml.dom.expatbuilder + +from repo_cloner.lib.repo_dir_structure import RepoDirStructure +from repo_cloner.lib.dir_not_found_error import DirNotFoundError +from pathlib import PosixPath +import pytest + + +def test_init(): + X = RepoDirStructure("/tmp") + assert X._base_dir == "/tmp" + assert X._conf_dir == "/tmp/config" + assert X._repos_dir == "/tmp/repos" + assert X._cache_dir == "/tmp/cache" + + +def test_no_base_dir(tmp_path: PosixPath): + x = tmp_path.joinpath("nonexistent") + X = RepoDirStructure(x) + with pytest.raises(DirNotFoundError) as excinfo: + X.exists + assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly() + assert excinfo.exconly().endswith("/nonexistent") + + +def test_no_config(tmp_path: PosixPath): + X = RepoDirStructure(tmp_path) + with pytest.raises(DirNotFoundError) as excinfo: + X.exists + assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly() + assert excinfo.exconly().endswith("/config") + + +def test_no_cache(tmp_path: PosixPath): + tmp_path.joinpath("config").mkdir() + X = RepoDirStructure(tmp_path) + with pytest.raises(DirNotFoundError) as excinfo: + X.exists + assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly() + assert excinfo.exconly().endswith("/cache") + + +def test_no_repos(tmp_path: PosixPath): + tmp_path.joinpath("config").mkdir() + tmp_path.joinpath("cache").mkdir() + X = RepoDirStructure(tmp_path) + with pytest.raises(DirNotFoundError) as excinfo: + X.exists + assert "DirNotFoundError: Directory does not exist / is not a dir: " in excinfo.exconly() + assert excinfo.exconly().endswith("/repos") + + +def test_exists_okay(tmp_path: PosixPath): + tmp_path.joinpath("config").mkdir() + tmp_path.joinpath("cache").mkdir() + tmp_path.joinpath("repos").mkdir() + X = RepoDirStructure(tmp_path) + assert True == X.exists + + +def test_base_dir(): + X = RepoDirStructure("/a/b") + assert X.base_dir == "/a/b" + + +def test_cache_dir(): + X = RepoDirStructure("/a/b") + assert X.cache_dir == "/a/b/cache" + + +def test_config_dir(): + X = RepoDirStructure("/a/b") + assert X.conf_dir == "/a/b/config" + + +def test_repos_dir(): + X = RepoDirStructure("/a/c") + assert X.repos_dir == "/a/c/repos"