Python: basic, package install, error handler for begining

Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
Václav Valíček 2022-05-04 15:02:55 +02:00
parent 5ce0f7da22
commit 155597aeff
Signed by: valicek
GPG Key ID: FF05BDCA0C73BB31
13 changed files with 204 additions and 6 deletions

View File

@ -1,6 +1,7 @@
[run] [run]
omit = omit =
tests/* tests/*
setup.py

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ __pycache__/
build/ build/
dist/ dist/
htmlcov/ htmlcov/
*.egg-info

View File

@ -7,4 +7,8 @@
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
</module> </module>

1
repo_cloner/__init__.py Normal file
View File

@ -0,0 +1 @@

View File

View File

@ -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)

View File

@ -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

View File

@ -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()

View File

@ -1,3 +1,5 @@
gitdb==4.0.9 gitdb==4.0.9
GitPython==3.1.27 GitPython==3.1.27
smmap==5.0.0 smmap==5.0.0
-e .

12
setup.py Normal file
View File

@ -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']
},
)

View File

@ -1,6 +0,0 @@
#!/usr/bin/env python3
from git import Repo
r = Repo("/home/vasek/dev/repo-cloner")
print(r.bare)

View File

@ -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"

View File

@ -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"