cmds: use version.check_git(...) in OpenParent
[git-cola.git] / cola / utils.py
blobd16bb7901defeed320dfb0d5f391f13a7a4f25ef
1 # Copyright (C) 2007-2018 David Aguilar and contributors
2 """This module provides miscellaneous utility functions."""
3 from __future__ import division, absolute_import, unicode_literals
4 import copy
5 import os
6 import random
7 import re
8 import shlex
9 import sys
10 import tempfile
11 import time
12 import traceback
14 from . import core
16 random.seed(hash(time.time()))
19 def asint(obj, default=0):
20 """Make any value into an int, even if the cast fails"""
21 try:
22 value = int(obj)
23 except TypeError:
24 value = default
25 return value
28 def clamp(value, lo, hi):
29 """Clamp a value to the specified range"""
30 return min(hi, max(lo, value))
33 def epoch_millis():
34 return int(time.time() * 1000)
37 def add_parents(paths):
38 """Iterate over each item in the set and add its parent directories."""
39 all_paths = set()
40 for path in paths:
41 while '//' in path:
42 path = path.replace('//', '/')
43 all_paths.add(path)
44 if '/' in path:
45 parent_dir = dirname(path)
46 while parent_dir:
47 all_paths.add(parent_dir)
48 parent_dir = dirname(parent_dir)
49 return all_paths
52 def format_exception(e):
53 exc_type, exc_value, exc_tb = sys.exc_info()
54 details = traceback.format_exception(exc_type, exc_value, exc_tb)
55 details = '\n'.join(map(core.decode, details))
56 if hasattr(e, 'msg'):
57 msg = e.msg
58 else:
59 msg = core.decode(repr(e))
60 return (msg, details)
63 def sublist(a, b):
64 """Subtracts list b from list a and returns the resulting list."""
65 # conceptually, c = a - b
66 c = []
67 for item in a:
68 if item not in b:
69 c.append(item)
70 return c
73 __grep_cache = {}
76 def grep(pattern, items, squash=True):
77 """Greps a list for items that match a pattern
79 :param squash: If only one item matches, return just that item
80 :returns: List of matching items
82 """
83 isdict = isinstance(items, dict)
84 if pattern in __grep_cache:
85 regex = __grep_cache[pattern]
86 else:
87 regex = __grep_cache[pattern] = re.compile(pattern)
88 matched = []
89 matchdict = {}
90 for item in items:
91 match = regex.match(item)
92 if not match:
93 continue
94 groups = match.groups()
95 if not groups:
96 subitems = match.group(0)
97 else:
98 if len(groups) == 1:
99 subitems = groups[0]
100 else:
101 subitems = list(groups)
102 if isdict:
103 matchdict[item] = items[item]
104 else:
105 matched.append(subitems)
107 if isdict:
108 result = matchdict
109 elif squash and len(matched) == 1:
110 result = matched[0]
111 else:
112 result = matched
114 return result
117 def basename(path):
119 An os.path.basename() implementation that always uses '/'
121 Avoid os.path.basename because git's output always
122 uses '/' regardless of platform.
125 return path.rsplit('/', 1)[-1]
128 def strip_one(path):
129 """Strip one level of directory"""
130 return path.strip('/').split('/', 1)[-1]
133 def dirname(path, current_dir=''):
135 An os.path.dirname() implementation that always uses '/'
137 Avoid os.path.dirname because git's output always
138 uses '/' regardless of platform.
141 while '//' in path:
142 path = path.replace('//', '/')
143 path_dirname = path.rsplit('/', 1)[0]
144 if path_dirname == path:
145 return current_dir
146 return path.rsplit('/', 1)[0]
149 def splitpath(path):
150 """Split paths using '/' regardless of platform
153 return path.split('/')
156 def join(*paths):
157 """Join paths using '/' regardless of platform
160 return '/'.join(paths)
163 def pathset(path):
164 """Return all of the path components for the specified path
166 >>> pathset('foo/bar/baz') == ['foo', 'foo/bar', 'foo/bar/baz']
167 True
170 result = []
171 parts = splitpath(path)
172 prefix = ''
173 for part in parts:
174 result.append(prefix + part)
175 prefix += part + '/'
177 return result
180 def select_directory(paths):
181 """Return the first directory in a list of paths"""
182 if not paths:
183 return core.getcwd()
185 for path in paths:
186 if core.isdir(path):
187 return path
189 return os.path.dirname(paths[0])
192 def strip_prefix(prefix, string):
193 """Return string, without the prefix. Blow up if string doesn't
194 start with prefix."""
195 assert string.startswith(prefix)
196 return string[len(prefix):]
199 def sanitize(s):
200 """Removes shell metacharacters from a string."""
201 for c in """ \t!@#$%^&*()\\;,<>"'[]{}~|""":
202 s = s.replace(c, '_')
203 return s
206 def tablength(word, tabwidth):
207 """Return length of a word taking tabs into account
209 >>> tablength("\\t\\t\\t\\tX", 8)
213 return len(word.replace('\t', '')) + word.count('\t') * tabwidth
216 def _shell_split(s):
217 """Split string apart into utf-8 encoded words using shell syntax"""
218 try:
219 return shlex.split(core.encode(s))
220 except ValueError:
221 return [core.encode(s)]
224 if sys.version_info[0] == 3:
225 # In Python 3, we don't need the encode/decode dance
226 shell_split = shlex.split
227 else:
228 def shell_split(s):
229 """Returns a unicode list instead of encoded strings"""
230 return [core.decode(arg) for arg in _shell_split(s)]
233 def tmp_filename(label, suffix=''):
234 label = 'git-cola-' + label.replace('/', '-').replace('\\', '-')
235 fd = tempfile.NamedTemporaryFile(prefix=label+'-', suffix=suffix)
236 fd.close()
237 return fd.name
240 def is_linux():
241 """Is this a linux machine?"""
242 return sys.platform.startswith('linux')
245 def is_debian():
246 """Is it debian?"""
247 return os.path.exists('/usr/bin/apt-get')
250 def is_darwin():
251 """Return True on OSX."""
252 return sys.platform == 'darwin'
255 def is_win32():
256 """Return True on win32"""
257 return sys.platform == 'win32' or sys.platform == 'cygwin'
260 def expandpath(path):
261 """Expand ~user/ and environment $variables"""
262 path = os.path.expandvars(path)
263 if path.startswith('~'):
264 path = os.path.expanduser(path)
265 return path
268 class Group(object):
269 """Operate on a collection of objects as a single unit"""
271 def __init__(self, *members):
272 self._members = members
274 def __getattr__(self, name):
275 """Return a function that relays calls to the group"""
276 def relay(*args, **kwargs):
277 for member in self._members:
278 method = getattr(member, name)
279 method(*args, **kwargs)
280 setattr(self, name, relay)
281 return relay
284 class Proxy(object):
285 """Wrap an object and override attributes"""
287 def __init__(self, obj, **overrides):
288 self._obj = obj
289 for k, v in overrides.items():
290 setattr(self, k, v)
292 def __getattr__(self, name):
293 return getattr(self._obj, name)
296 def slice_fn(input_items, map_fn):
297 """Slice input_items and call map_fn over every slice
299 This exists because of "errno: Argument list too long"
302 # This comment appeared near the top of include/linux/binfmts.h
303 # in the Linux source tree:
305 # /*
306 # * MAX_ARG_PAGES defines the number of pages allocated for arguments
307 # * and envelope for the new program. 32 should suffice, this gives
308 # * a maximum env+arg of 128kB w/4KB pages!
309 # */
310 # #define MAX_ARG_PAGES 32
312 # 'size' is a heuristic to keep things highly performant by minimizing
313 # the number of slices. If we wanted it to run as few commands as
314 # possible we could call "getconf ARG_MAX" and make a better guess,
315 # but it's probably not worth the complexity (and the extra call to
316 # getconf that we can't do on Windows anyways).
318 # In my testing, getconf ARG_MAX on Mac OS X Mountain Lion reported
319 # 262144 and Debian/Linux-x86_64 reported 2097152.
321 # The hard-coded max_arg_len value is safely below both of these
322 # real-world values.
324 # 4K pages x 32 MAX_ARG_PAGES
325 max_arg_len = (32 * 4096) // 4 # allow plenty of space for the environment
326 max_filename_len = 256
327 size = max_arg_len // max_filename_len
329 status = 0
330 outs = []
331 errs = []
333 items = copy.copy(input_items)
334 while items:
335 stat, out, err = map_fn(items[:size])
336 if stat < 0:
337 status = min(stat, status)
338 else:
339 status = max(stat, status)
340 outs.append(out)
341 errs.append(err)
342 items = items[size:]
344 return (status, '\n'.join(outs), '\n'.join(errs))
347 class seq(object):
349 def __init__(self, sequence):
350 self.seq = sequence
352 def index(self, item, default=-1):
353 try:
354 idx = self.seq.index(item)
355 except ValueError:
356 idx = default
357 return idx
359 def __getitem__(self, idx):
360 return self.seq[idx]