aboutsummaryrefslogtreecommitdiff
blob: 65c6d181db19399cc4b27e5997d2a8cc91fd1013 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
A package is a set of files, situated in a root directory.
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

from pomu.util.string import strip_prefix

class Package():
    def __init__(self, name, root, d_path=None, files=None):
        """
        Parameters:
            name - name of the package
            root - root path of the repository (if applicable)
            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
        """
        self.name = name
        self.root = root
        self.files = []
        if d_path is None and files is None:
            self.d_path = None
            self.read_path(self.root)
        elif files is None:
            self.d_path = self.strip_root(d_path)
            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))
        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, root) != root:
                    raise ValueError('Path should be a subdirectory of root')
                return strip_prefix(strip_prefix(d_path, root), '/')
            return d_path

    def read_path(self, d_path):
        for wd, dirs, files in os.walk(d_path):
            wd = self.strip_root(wd)
            self.files.extend([(wd, f) for f in files])