Show More
@@ -1,169 +1,149 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2014-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | Utilities aimed to help achieve mostly basic tasks. |
|
23 | 23 | """ |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | from __future__ import division |
|
27 | 27 | |
|
28 | 28 | import re |
|
29 | 29 | import os |
|
30 | 30 | import time |
|
31 | 31 | import datetime |
|
32 | 32 | import logging |
|
33 | 33 | |
|
34 | 34 | from rhodecode.lib.vcs.conf import settings |
|
35 | 35 | from rhodecode.lib.vcs.exceptions import VCSError, VCSBackendNotSupportedError |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | log = logging.getLogger(__name__) |
|
39 | 39 | |
|
40 | 40 | |
|
41 | def get_scm(path, search_path_up=False, explicit_alias=None): | |
|
41 | def get_scm(path): | |
|
42 | 42 | """ |
|
43 | 43 | Returns one of alias from ``ALIASES`` (in order of precedence same as |
|
44 |
shortcuts given in ``ALIASES``) and |
|
|
44 | shortcuts given in ``ALIASES``) and working dir path for the given | |
|
45 | 45 | argument. If no scm-specific directory is found or more than one scm is |
|
46 | 46 | found at that directory, ``VCSError`` is raised. |
|
47 | ||
|
48 | :param search_path_up: if set to ``True``, this function would try to | |
|
49 | move up to parent directory every time no scm is recognized for the | |
|
50 | currently checked path. Default: ``False``. | |
|
51 | :param explicit_alias: can be one of available backend aliases, when given | |
|
52 | it will return given explicit alias in repositories under more than one | |
|
53 | version control, if explicit_alias is different than found it will raise | |
|
54 | VCSError | |
|
55 | 47 | """ |
|
56 | 48 | if not os.path.isdir(path): |
|
57 | 49 | raise VCSError("Given path %s is not a directory" % path) |
|
58 | 50 | |
|
59 | def get_scms(path): | |
|
60 | return [(scm, path) for scm in get_scms_for_path(path)] | |
|
61 | ||
|
62 | found_scms = get_scms(path) | |
|
63 | while not found_scms and search_path_up: | |
|
64 | newpath = os.path.abspath(os.path.join(path, os.pardir)) | |
|
65 | if newpath == path: | |
|
66 | break | |
|
67 | path = newpath | |
|
68 | found_scms = get_scms(path) | |
|
51 | found_scms = [(scm, path) for scm in get_scms_for_path(path)] | |
|
69 | 52 | |
|
70 | 53 | if len(found_scms) > 1: |
|
71 | for scm in found_scms: | |
|
72 | if scm[0] == explicit_alias: | |
|
73 | return scm | |
|
74 | 54 | found = ', '.join((x[0] for x in found_scms)) |
|
75 | 55 | raise VCSError( |
|
76 | 56 | 'More than one [%s] scm found at given path %s' % (found, path)) |
|
77 | 57 | |
|
78 | 58 | if len(found_scms) is 0: |
|
79 | 59 | raise VCSError('No scm found at given path %s' % path) |
|
80 | 60 | |
|
81 | 61 | return found_scms[0] |
|
82 | 62 | |
|
83 | 63 | |
|
84 | 64 | def get_scm_backend(backend_type): |
|
85 | 65 | from rhodecode.lib.vcs.backends import get_backend |
|
86 | 66 | return get_backend(backend_type) |
|
87 | 67 | |
|
88 | 68 | |
|
89 | 69 | def get_scms_for_path(path): |
|
90 | 70 | """ |
|
91 | 71 | Returns all scm's found at the given path. If no scm is recognized |
|
92 | 72 | - empty list is returned. |
|
93 | 73 | |
|
94 | 74 | :param path: path to directory which should be checked. May be callable. |
|
95 | 75 | |
|
96 | 76 | :raises VCSError: if given ``path`` is not a directory |
|
97 | 77 | """ |
|
98 | 78 | from rhodecode.lib.vcs.backends import get_backend |
|
99 | 79 | if hasattr(path, '__call__'): |
|
100 | 80 | path = path() |
|
101 | 81 | if not os.path.isdir(path): |
|
102 | 82 | raise VCSError("Given path %r is not a directory" % path) |
|
103 | 83 | |
|
104 | 84 | result = [] |
|
105 | 85 | for key in settings.available_aliases(): |
|
106 | 86 | try: |
|
107 | 87 | backend = get_backend(key) |
|
108 | 88 | except VCSBackendNotSupportedError: |
|
109 | 89 | log.warning('VCSBackendNotSupportedError: %s not supported', key) |
|
110 | 90 | continue |
|
111 | 91 | if backend.is_valid_repository(path): |
|
112 | 92 | result.append(key) |
|
113 | 93 | return result |
|
114 | 94 | |
|
115 | 95 | |
|
116 | 96 | def parse_datetime(text): |
|
117 | 97 | """ |
|
118 | 98 | Parses given text and returns ``datetime.datetime`` instance or raises |
|
119 | 99 | ``ValueError``. |
|
120 | 100 | |
|
121 | 101 | :param text: string of desired date/datetime or something more verbose, |
|
122 | 102 | like *yesterday*, *2weeks 3days*, etc. |
|
123 | 103 | """ |
|
124 | 104 | |
|
125 | 105 | text = text.strip().lower() |
|
126 | 106 | |
|
127 | 107 | INPUT_FORMATS = ( |
|
128 | 108 | '%Y-%m-%d %H:%M:%S', |
|
129 | 109 | '%Y-%m-%d %H:%M', |
|
130 | 110 | '%Y-%m-%d', |
|
131 | 111 | '%m/%d/%Y %H:%M:%S', |
|
132 | 112 | '%m/%d/%Y %H:%M', |
|
133 | 113 | '%m/%d/%Y', |
|
134 | 114 | '%m/%d/%y %H:%M:%S', |
|
135 | 115 | '%m/%d/%y %H:%M', |
|
136 | 116 | '%m/%d/%y', |
|
137 | 117 | ) |
|
138 | 118 | for format in INPUT_FORMATS: |
|
139 | 119 | try: |
|
140 | 120 | return datetime.datetime(*time.strptime(text, format)[:6]) |
|
141 | 121 | except ValueError: |
|
142 | 122 | pass |
|
143 | 123 | |
|
144 | 124 | # Try descriptive texts |
|
145 | 125 | if text == 'tomorrow': |
|
146 | 126 | future = datetime.datetime.now() + datetime.timedelta(days=1) |
|
147 | 127 | args = future.timetuple()[:3] + (23, 59, 59) |
|
148 | 128 | return datetime.datetime(*args) |
|
149 | 129 | elif text == 'today': |
|
150 | 130 | return datetime.datetime(*datetime.datetime.today().timetuple()[:3]) |
|
151 | 131 | elif text == 'now': |
|
152 | 132 | return datetime.datetime.now() |
|
153 | 133 | elif text == 'yesterday': |
|
154 | 134 | past = datetime.datetime.now() - datetime.timedelta(days=1) |
|
155 | 135 | return datetime.datetime(*past.timetuple()[:3]) |
|
156 | 136 | else: |
|
157 | 137 | days = 0 |
|
158 | 138 | matched = re.match( |
|
159 | 139 | r'^((?P<weeks>\d+) ?w(eeks?)?)? ?((?P<days>\d+) ?d(ays?)?)?$', text) |
|
160 | 140 | if matched: |
|
161 | 141 | groupdict = matched.groupdict() |
|
162 | 142 | if groupdict['days']: |
|
163 | 143 | days += int(matched.groupdict()['days']) |
|
164 | 144 | if groupdict['weeks']: |
|
165 | 145 | days += int(matched.groupdict()['weeks']) * 7 |
|
166 | 146 | past = datetime.datetime.now() - datetime.timedelta(days=days) |
|
167 | 147 | return datetime.datetime(*past.timetuple()[:3]) |
|
168 | 148 | |
|
169 | 149 | raise ValueError('Wrong date: "%s"' % text) |
General Comments 0
You need to be logged in to leave comments.
Login now