aboutsummaryrefslogtreecommitdiff
blob: ce3678a48524a2ca4b5654755d2094e9afe07a81 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Subroutines for repository initialization"""
from shutil import rmtree

from git import Repo
from os import path, makedirs
import portage

from pomu.util.result import Result, ResultException

def init_plain_repo(create, repo_path):
    """
    Initialize a plain repository
    Parameters:
        create - if true, create a new git repo,
                 else, reuse an existing one
        repo_path - a path for the repository to reside in
    """
    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:
            return Result.Ok(init_new(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
    Parameters:
        create - if true, create a new portage repo with git,
                 else, reuse an existing one
        repo - name of the repository
        repo_dir - location of the newly created repository, if applicable
    """
    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.Err('you do not have enough permissions to setup a portage repo')
        Repo.init(repo_path)
        try:
            return Result.Ok(init_new(repo_path, repo).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_new(repo_path, name=''):
    """
    Initialize a newly created repository (metadata/layout.conf and pomu)
    Parameters:
        repo_path - path to the repository
        name - name of the repository
    """
    cnf = path.join(repo_path, 'metadata', 'layout.conf')
    if not path.isfile(cnf):
        try:
            makedirs(path.join(repo_path, 'metadata'))
            with open(cnf, 'w') as f:
                f.write('masters = gentoo\n')
        except PermissionError:
            return Result.Err('you do not have enough permissions to modify the repo')
    return init_pomu(repo_path, name)

def init_pomu(repo_path, name=''):
    """
    Initialise pomu for a repository
    Parameters:
        repo_path - path to the repository
        name - name of the 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 if name else 'at {}'.format(repo_path) + ' 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([path.join('metadata', 'pomu')])
    r.index.commit('Initialized pomu')
    ret = Result.Ok('Initialized repository ' + name + ' successfully')
    return ret