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