xref: /openbmc/openbmc/poky/meta/lib/patchtest/patch.py (revision ac13d5f3)
1# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3#
4# patchtestpatch: PatchTestPatch class which abstracts a patch file
5#
6# Copyright (C) 2016 Intel Corporation
7#
8# SPDX-License-Identifier: GPL-2.0-only
9#
10
11import logging
12import utils
13
14logger = logging.getLogger('patchtest')
15
16class PatchTestPatch(object):
17    MERGE_STATUS_INVALID = 'INVALID'
18    MERGE_STATUS_NOT_MERGED = 'NOTMERGED'
19    MERGE_STATUS_MERGED_SUCCESSFULL = 'PASS'
20    MERGE_STATUS_MERGED_FAIL = 'FAIL'
21    MERGE_STATUS = (MERGE_STATUS_INVALID,
22                    MERGE_STATUS_NOT_MERGED,
23                    MERGE_STATUS_MERGED_SUCCESSFULL,
24                    MERGE_STATUS_MERGED_FAIL)
25
26    def __init__(self, path, forcereload=False):
27        self._path = path
28        self._forcereload = forcereload
29
30        self._contents = None
31        self._branch = None
32        self._merge_status = PatchTestPatch.MERGE_STATUS_NOT_MERGED
33
34    @property
35    def contents(self):
36        if self._forcereload or (not self._contents):
37            logger.debug('Reading %s contents' % self._path)
38            try:
39                with open(self._path, newline='') as _f:
40                    self._contents = _f.read()
41            except IOError:
42                logger.warn("Reading the mbox %s failed" % self.resource)
43        return self._contents
44
45    @property
46    def path(self):
47        return self._path
48
49    @property
50    def branch(self):
51        if not self._branch:
52            self._branch = utils.get_branch(self._path)
53        return self._branch
54
55    def setmergestatus(self, status):
56        self._merge_status = status
57
58    def getmergestatus(self):
59        return self._merge_status
60
61    merge_status = property(getmergestatus, setmergestatus)
62
63