summaryrefslogtreecommitdiff
path: root/icklib/statedir.py
blob: a663099cf65dd592d6b05eda0b3499d5649aa4f6 (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
# Copyright 2015  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import glob
import os

import icklib


class StateDirectory(object):

    def __init__(self):
        self.dirname = None

    def set_dirname(self, dirname):
        self.dirname = dirname

    def get_project_state_path(self, project_name):
        return os.path.join(self.dirname, project_name, 'project.yaml')

    def get_git_directory(self, project_name):
        return os.path.join(self.dirname, project_name, 'git')

    def get_apt_directory(self):  # pragma: no cover
        return os.path.join(self.dirname, 'debian')

    def get_build_infos(self, project_name):
        result = []
        pattern = os.path.join(
            self.dirname, project_name, 'builds', '*', 'build.yaml')
        for info_filename in glob.glob(pattern):
            build_info = icklib.BuildInformation()
            build_info.set_filename(info_filename)
            build_info.load()
            result.append(build_info)
        return sorted(result, key=lambda bi: bi.build_number)

    def create_new_build(self, project_name, new_build_number):
        build_dir = self._get_dirname_for_build_info(
            project_name, new_build_number)
        build_filename = os.path.join(build_dir, 'build.yaml')
        os.makedirs(build_dir)

        build_info = icklib.BuildInformation()
        build_info.build_number = new_build_number
        build_info.set_filename(build_filename)
        build_info.save()
        return build_info

    def _get_dirname_for_build_info(self, project_name, build_number):
        return os.path.join(
            self.dirname, project_name, 'builds', str(build_number))

    def update_build_info(self, project_name, build_info):
        build_info.save()


def create_statedir_from_ick(ick):  # pragma: no cover
    statedir = icklib.StateDirectory()
    statedir.set_dirname(ick['state'])
    return statedir