aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMykyta Holubakha <hilobakho@gmail.com>2017-05-31 01:26:06 +0300
committerMykyta Holubakha <hilobakho@gmail.com>2017-05-31 01:26:06 +0300
commit7c9330d8f788e9671f7db95cafa0cdca045f0472 (patch)
treeb7cfe7dd23dfb212892a49ad8dbd48ecaf281468
parentAdded vim swap files to .gitignore (diff)
downloadpomu-7c9330d8f788e9671f7db95cafa0cdca045f0472.tar.gz
pomu-7c9330d8f788e9671f7db95cafa0cdca045f0472.tar.bz2
pomu-7c9330d8f788e9671f7db95cafa0cdca045f0472.zip
Refactoring
added a Result type separated presentation from logic moved utility code into separate modules
-rw-r--r--pomu/pomu.py111
-rw-r--r--pomu/repo/init.py78
-rw-r--r--pomu/repo/repo.py20
-rw-r--r--pomu/util/result.py60
4 files changed, 176 insertions, 93 deletions
diff --git a/pomu/pomu.py b/pomu/pomu.py
index 6d893d6..98c0947 100644
--- a/pomu/pomu.py
+++ b/pomu/pomu.py
@@ -1,9 +1,9 @@
-from shutil import rmtree
-
+"""pomu command line interface"""
import click
-from git import Repo
-import portage
-from os import path, makedirs
+
+from pomu.repo.init import init_plain_repo, init_portage_repo
+from pomu.repo.repo import portage_repo_path, portage_repos, pomu_status
+from pomu.util import ResultException
#TODO: global --repo option, (env var?)
@@ -37,93 +37,18 @@ def main(globalvars, no_portage, repo_path):
@pass_globals
def init(globalvars, list_repos, create, repo_dir, repo):
"""Initialise pomu for a repository"""
- rsets = portage.db[portage.root]['vartree'].settings.repositories
if list_repos:
print('Available repos:')
- for prepo in rsets.prepos_order:
- print('\t', prepo, rsets.prepos[prepo].location)
- return
- if globalvars.no_portage:
- init_plain_repo(create, globalvars.repo_path)
- else:
- init_portage_repo(create, repo, repo_dir)
-
-def init_plain_repo(create, repo_path):
- """Initialize a plain repository"""
- if not repo_path:
- print('Error: repository path required')
- return
- if create:
- if path.isdir(repo_path):
- print('Error: this repository already exists')
- return
- try:
- makedirs(repo_path)
- except PermissionError:
- print('Error: you do not have enough permissions to create the git repository')
- return
- Repo.init(repo_path)
- if not init_pomu(repo_path):
- rmtree(repo_path)
- else:
- if not path.isdir(repo_path):
- print('Error: directory not found')
- return
- init_pomu(repo_path)
-
-def init_portage_repo(create, repo, repo_dir):
- """Initialize a portage repository"""
- if not repo:
- print('Error: repository name required')
+ for prepo in portage_repos():
+ print('\t', prepo, portage_repo_path(prepo))
return
- rsets = portage.db[portage.root]['vartree'].settings.repositories
- if create:
- if repo in rsets.prepos_order:
- print('Error: a repository with such name already exists!')
- return
- repo_path = path.join(repo_dir, repo)
- try:
- makedirs(repo_path)
- except PermissionError:
- print('Error: you do not have enough permissions to create the git repository')
- return
- try:
- with open(path.join(portage.root, '/etc/portage/repos.conf', 'pomu.conf'), 'a') as f:
- f.write('[' + repo + ']' + '\n')
- f.write('location = ' + repo_path + '\n')
- except PermissionError:
- print('Error: you do not have enough permissions to setup a portage repo')
- rmtree(repo_path)
- return
- Repo.init(repo_path)
- if not init_pomu(repo_path, repo):
- rmtree(repo_path)
- else:
- if repo not in rsets.prepos_order:
- print('Error: repository not found')
- return
- init_pomu(rsets.prepos[repo], repo)
-
-def init_pomu(repo_path, name=' '):
- """Initialise pomu for a repository"""
- pomu_path = path.join(repo_path, 'metadata', 'pomu')
- if not path.isdir(path.join(repo_path, '.git')):
- print('Error: target repository should be a git repo')
- return False
- if path.isdir(pomu_path):
- print('Repository', name, 'already initialized')
- return True
- r = Repo(repo_path)
try:
- makedirs(pomu_path)
- open(path.join(pomu_path, '.sentinel'), 'w').close()
- except PermissionError:
- print('Error: you do not have enough permissions to modify the repo')
- return False
- r.index.add(pomu_path)
- r.index.commit('Initialized pomu')
- print('Initialized repository', name, 'successfully')
- return True
+ if globalvars.no_portage:
+ print(init_plain_repo(create, globalvars.repo_path).expect())
+ else:
+ print(init_portage_repo(create, repo, repo_dir).expect())
+ except ResultException as e:
+ print(str(e))
@main.command()
@pass_globals
@@ -133,13 +58,13 @@ def status(globalvars):
if not globalvars.repo_path:
print('Error: repo-path required')
return
- if path.isdir(path.join(globalvars.repo_path, 'metadata', 'pomu')):
+ if pomu_status(globalvars.repo_path):
print('pomu is initialized at', globalvars.repo_path)
print('pomu is not initialized')
else:
- rsets = portage.db[portage.root]['vartree'].settings.repositories
- for repo in rsets.prepos_order:
- if path.isdir(path.join(rsets.prepos[repo].location, 'metadata', 'pomu')):
- print('pomu is initialized for repository', repo, 'at', rsets.prepos[repo].location)
+ for repo in portage_repos():
+ if pomu_status(portage_repo_path(repo)):
+ print('pomu is initialized for repository', repo, 'at', portage_repo_path(repo))
return
print('pomu is not initialized')
+
diff --git a/pomu/repo/init.py b/pomu/repo/init.py
new file mode 100644
index 0000000..b065a9a
--- /dev/null
+++ b/pomu/repo/init.py
@@ -0,0 +1,78 @@
+"""Subroutines for repository initialization"""
+from shutil import rmtree
+
+from git import Repo
+from os import path, makedirs
+import portage
+
+from pomu.util import Result, ResultException
+
+def init_plain_repo(create, repo_path):
+ """Initialize a plain repository"""
+ if not repo_path:
+ return Result.Err('repository path required')
+ if create:
+ if path.isdir(repo_path):
+ return Result.Err('this repository already exists')
+ try:
+ makedirs(repo_path)
+ except PermissionError:
+ return Result.Err('you do not have enough permissions to create the git repository')
+ Repo.init(repo_path)
+ try:
+ init_pomu(repo_path).unwrap()
+ except ResultException as e:
+ rmtree(repo_path)
+ return Result.Err(str(e))
+ else:
+ if not path.isdir(repo_path):
+ return Result.Err('directory not found')
+ return init_pomu(repo_path)
+
+def init_portage_repo(create, repo, repo_dir):
+ """Initialize a portage repository"""
+ if not repo:
+ return Result.Err('repository name required')
+ rsets = portage.db[portage.root]['vartree'].settings.repositories
+ if create:
+ if repo in rsets.prepos_order:
+ return Result.Err('a repository with such name already exists!')
+ repo_path = path.join(repo_dir, repo)
+ try:
+ makedirs(repo_path)
+ except PermissionError:
+ return Result.Err('you do not have enough permissions to create the git repository')
+ try:
+ with open(path.join(portage.root, '/etc/portage/repos.conf', 'pomu.conf'), 'a') as f:
+ f.write('[' + repo + ']' + '\n')
+ f.write('location = ' + repo_path + '\n')
+ except PermissionError:
+ rmtree(repo_path)
+ return Result.Error('you do not have enough permissions to setup a portage repo')
+ Repo.init(repo_path)
+ try:
+ init_pomu(repo_path).unwrap()
+ except ResultException as e:
+ rmtree(repo_path)
+ return Result.Err(str(e))
+ else:
+ if repo not in rsets.prepos_order:
+ return Result.Err('repository not found')
+ return init_pomu(rsets.prepos[repo], repo)
+
+def init_pomu(repo_path, name=''):
+ """Initialise pomu for a repository"""
+ pomu_path = path.join(repo_path, 'metadata', 'pomu')
+ if not path.isdir(path.join(repo_path, '.git')):
+ return Result.Err('target repository should be a git repo')
+ if path.isdir(pomu_path):
+ return Result.Ok('Repository ' + name + ' already initialized')
+ r = Repo(repo_path)
+ try:
+ makedirs(pomu_path)
+ open(path.join(pomu_path, '.sentinel'), 'w').close()
+ except PermissionError:
+ return Result.Err('you do not have enough permissions to modify the repo')
+ r.index.add(pomu_path)
+ r.index.commit('Initialized pomu')
+ return Result.Ok('Initialized repository ' + name + ' successfully')
diff --git a/pomu/repo/repo.py b/pomu/repo/repo.py
new file mode 100644
index 0000000..bfa97fe
--- /dev/null
+++ b/pomu/repo/repo.py
@@ -0,0 +1,20 @@
+"""Subroutines with repositories"""
+from os import path
+
+import portage
+
+rsets = portage.db[portage.root]['vartree'].settings.repositories
+
+def portage_repos():
+ for repo in rsets.prepos_order:
+ yield repo
+
+def portage_repo_path(repo):
+ """Get the path of a given portage repository"""
+ if repo in rsets.prepos_order:
+ return rsets.prepos[repo].location
+ return None
+
+def pomu_status(repo_path):
+ """Check if pomu is enabled for a repository at a given path"""
+ return path.isdir(path.join(repo_path, 'metadata', 'pomu'))
diff --git a/pomu/util/result.py b/pomu/util/result.py
new file mode 100644
index 0000000..1d5092f
--- /dev/null
+++ b/pomu/util/result.py
@@ -0,0 +1,60 @@
+"""Result type module"""
+
+class ResultException(Exception):
+ pass
+
+class Result():
+ """Result = Err E | Ok V"""
+
+ def __init__(self, is_val):
+ self._is_val = is_val
+
+ @classmethod
+ def Ok(cls, val):
+ res = cls(True)
+ res._val = val
+ return res
+
+ @classmethod
+ def Err(cls, val):
+ res = cls(False)
+ res._val = val
+ return res
+
+ def is_ok(self):
+ return self._is_val
+
+ def is_err(self):
+ return not self._is_val
+
+ def ok(self):
+ return self._val if self._is_val else None
+
+ def err(self):
+ return self._val if not self._is_val else None
+
+ def map(self, f):
+ return Result.Ok(f(self.ok())) if self._is_val else Result.Err(self.err())
+
+ def map_err(self, f):
+ return Result.Err(f(self.err())) if not self._is_val else Result.Ok(self.ok())
+
+ def unwrap(self):
+ return self.expect(self.err())
+
+ def expect(self, msg='Error'):
+ if self._is_val:
+ return self._val
+ raise ResultException(msg + ': ' + self._val)
+
+ def unwrap_err(self):
+ return self.expect_err(self.ok())
+
+ def expect_err(self, msg='Error'):
+ if not self._is_val:
+ return self._val
+ raise ResultException(msg + ': ' + self._val)
+
+ def __iter__(self):
+ if self._is_val:
+ yield self._val