1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# See LICENSE.txt included in this distribution for the specific
9# language governing permissions and limitations under the License.
10#
11# When distributing Covered Code, include this CDDL HEADER in each
12# file and include the License file at LICENSE.txt.
13# If applicable, add the following below this CDDL HEADER, with the
14# fields enclosed by brackets "[]" replaced with your own identifying
15# information: Portions Copyright [yyyy] [name of copyright owner]
16#
17# CDDL HEADER END
18#
19
20#
21# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
22#
23
24import abc
25from ..utils.command import Command
26
27
28class RepositoryException(Exception):
29    """
30    Exception returned when repository operation failed.
31    """
32    pass
33
34
35class Repository:
36    """
37    abstract class wrapper for Source Code Management repository
38    """
39
40    __metaclass__ = abc.ABCMeta
41
42    def __init__(self, logger, path, project, command, env, hooks,
43                 timeout):
44        self.logger = logger
45        self.path = path
46        self.project = project
47        self.timeout = timeout
48        if env:
49            self.env = env
50        else:
51            self.env = {}
52
53    def __str__(self):
54        return self.path
55
56    def getCommand(self, cmd, **kwargs):
57        kwargs['timeout'] = self.timeout
58        return Command(cmd, **kwargs)
59
60    def sync(self):
61        # Eventually, there might be per-repository hooks added here.
62        return self.reposync()
63
64    @abc.abstractmethod
65    def reposync(self):
66        """
67        Synchronize the repository by running sync command specific for
68        given repository type.
69
70        Return 1 on failure, 0 on success.
71        """
72        raise NotImplementedError()
73
74    def incoming(self):
75        """
76        Check if there are any incoming changes.
77
78        Return True if so, False otherwise.
79        """
80        return True
81