1# Software License Agreement (BSD License)
2#
3# Copyright (c) 2010, Willow Garage, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10#  * Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12#  * Redistributions in binary form must reproduce the above
13#    copyright notice, this list of conditions and the following
14#    disclaimer in the documentation and/or other materials provided
15#    with the distribution.
16#  * Neither the name of Willow Garage, Inc. nor the names of its
17#    contributors may be used to endorse or promote products derived
18#    from this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31# POSSIBILITY OF SUCH DAMAGE.
32
33from __future__ import absolute_import, print_function, unicode_literals
34import os
35import warnings
36
37
38_VCS_TYPES = {}
39
40
41def register_vcs(vcs_type, clazz):
42    """
43    :param vcs_type: id, ``str``
44    :param clazz: class extending VcsClientBase
45    """
46    _VCS_TYPES[vcs_type] = clazz
47
48
49def get_registered_vcs_types():
50    """
51    :returns: list of valid key to use as vcs_type
52    """
53    return list(_VCS_TYPES.keys())
54
55
56def get_vcs(vcs_type):
57    """
58    Returns the class interfacing with vcs of given type
59
60    :param vcs_type: id of the tpye, e.g. git, svn, hg, bzr
61    :returns: class extending VcsClientBase
62    :raises: ValueError for unknown vcs_type
63    """
64    vcs_class = _VCS_TYPES.get(vcs_type, None)
65    if not vcs_class:
66        raise ValueError('No Client type registered for vcs type "%s"' % vcs_type)
67    return vcs_class
68
69
70def get_vcs_client(vcs_type, path):
71    """
72    Returns a client with which to interact with the vcs at given path
73
74    :param vcs_type: id of the tpye, e.g. git, svn, hg, bzr
75    :returns: instance of VcsClientBase
76    :raises: ValueError for unknown vcs_type
77    """
78    clientclass = get_vcs(vcs_type)
79    return clientclass(path)
80
81
82class VcsClient(object):
83    """
84    *DEPRECATED* API for interacting with source-controlled paths
85    independent of actual version-control implementation.
86    """
87
88    def __init__(self, vcs_type, path):
89        self._path = path
90        warnings.warn("Class VcsClient is deprecated, use from vcstools" +
91                      " import get_vcs_client; get_vcs_client() instead")
92        self.vcs = get_vcs_client(vcs_type, path)
93
94    def path_exists(self):
95        return os.path.exists(self._path)
96
97    def get_path(self):
98        return self._path
99
100    # pass through VCSClientBase API
101    def get_version(self, spec=None):
102        return self.vcs.get_version(spec)
103
104    def get_current_version_label(self):
105        return self.vcs.get_current_version_label()
106
107    def get_remote_version(self, fetch=False):
108        return self.vcs.get_remote_version(fetch)
109
110    def get_default_remote_version_label(self):
111        return self.vcs.get_default_remote_version_label()
112
113    def checkout(self, url, version='', verbose=False, shallow=False):
114        return self.vcs.checkout(url,
115                                 version,
116                                 verbose=verbose,
117                                 shallow=shallow)
118
119    def url_matches(self, url, url_or_shortcut):
120        return self.vcs.url_matches(url=url, url_or_shortcut=url_or_shortcut)
121
122    def update(self, version='', verbose=False):
123        return self.vcs.update(version, verbose=verbose)
124
125    def detect_presence(self):
126        return self.vcs.detect_presence()
127
128    def get_vcs_type_name(self):
129        return self.vcs.get_vcs_type_name()
130
131    def get_url(self):
132        return self.vcs.get_url()
133
134    def get_diff(self, basepath=None):
135        return self.vcs.get_diff(basepath)
136
137    def get_status(self, basepath=None, untracked=False, **kwargs):
138        return self.vcs.get_status(basepath, untracked, **kwargs)
139
140    def get_log(self, relpath=None, limit=None):
141        return self.vcs.get_log(relpath, limit)
142
143    def export_repository(self, version, basepath):
144        return self.vcs.export_repository(version, basepath)
145
146    def get_branches(self, local_only=False):
147        return self.vcs.get_branches(local_only)
148
149
150# backwards compat
151VCSClient = VcsClient
152