aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMykyta Holubakha <hilobakho@gmail.com>2017-06-19 04:24:06 +0300
committerMykyta Holubakha <hilobakho@gmail.com>2017-06-19 04:24:06 +0300
commit7334e41998fb99dfd10dcf9fd977967ceb0f79f2 (patch)
tree07d821e70b1d72de917d0f6470bbbc184afc159e /pomu/package.py
parentmajor refactoring of pomu package source module (diff)
downloadpomu-7334e41998fb99dfd10dcf9fd977967ceb0f79f2.tar.gz
pomu-7334e41998fb99dfd10dcf9fd977967ceb0f79f2.tar.bz2
pomu-7334e41998fb99dfd10dcf9fd977967ceb0f79f2.zip
Numerous improvements and fixes
Documented most of the functions and classes. Added an option to fetch a package into a specified directory. Added a merge_into method to the Package class, which would merge it into a directory, and refactored repo::merge_package to use that. Extended the package class to store category, version and slot metadata. Added tests of the portage package source module.
Diffstat (limited to 'pomu/package.py')
-rw-r--r--pomu/package.py48
1 files changed, 39 insertions, 9 deletions
diff --git a/pomu/package.py b/pomu/package.py
index 470d76b..7b7ec55 100644
--- a/pomu/package.py
+++ b/pomu/package.py
@@ -4,12 +4,14 @@ A package can be installed into a repository.
A package is supposed to be created by a package source from a set of files.
"""
-from os import path, walk
+from os import path, walk, makedirs
+from shutil import copy2
from pomu.util.fs import strip_prefix
+from pomu.util.result import Result
class Package():
- def __init__(self, name, root, d_path=None, files=None):
+ def __init__(self, name, root, category=None, version=None, slot='0', d_path=None, files=None):
"""
Parameters:
name - name of the package
@@ -17,9 +19,13 @@ class Package():
d_path - a subdirectory of the root path, which would be sourced recursively.
could be a relative or an absolute path
files - a set of files to build a package from
+ category, version, slot - self-descriptive
"""
self.name = name
self.root = root
+ self.category = category
+ self.version = version
+ self.slot = slot
self.files = []
if d_path is None and files is None:
self.d_path = None
@@ -29,20 +35,44 @@ class Package():
self.read_path(path.join(self.root, self.d_path))
elif d_path is None:
for f in files:
- self.files.append(self.strip_root(f))
+ self.files.append(path.split(self.strip_root(f)))
else:
raise ValueError('You should specify either d_path, or files')
def strip_root(self, d_path):
- # the path should be either relative, or a child of root
- if d_path.startswith('/'):
- if path.commonprefix(d_path, self.root) != self.root:
- raise ValueError('Path should be a subdirectory of root')
- return strip_prefix(strip_prefix(d_path, self.root), '/')
- return d_path
+ """Strip the root component of a path"""
+ # the path should be either relative, or a child of root
+ if d_path.startswith('/'):
+ if path.commonprefix(d_path, self.root) != self.root:
+ raise ValueError('Path should be a subdirectory of root')
+ return strip_prefix(strip_prefix(d_path, self.root), '/')
+ return d_path
def read_path(self, d_path):
+ """Recursively add files from a subtree"""
for wd, dirs, files in walk(d_path):
wd = self.strip_root(wd)
self.files.extend([(wd, f) for f in files])
+ def merge_into(self, dst):
+ """Merges contents of the package into a specified directory"""
+ for wd, f in self.files:
+ dest = path.join(dst, wd)
+ try:
+ makedirs(dest, exist_ok=True)
+ copy2(path.join(self.root, wd, f), dest)
+ except PermissionError:
+ return Result.Err('You do not have enough permissions')
+ return Result.Ok()
+
+
+ def __str__(self):
+ s = ''
+ if self.category:
+ s = self.category + '/'
+ s += self.name
+ if self.version:
+ s += '-' + self.version
+ if self.slot != '0':
+ s += self.slot
+ return s