1 """Miscellaneous utility functions"""
15 random
.seed(hash(time
.time()))
18 def asint(obj
, default
=0):
19 """Make any value into an int, even if the cast fails"""
22 except (TypeError, ValueError):
27 def clamp(value
, low
, high
):
28 """Clamp a value to the specified range"""
29 return min(high
, max(low
, value
))
33 return int(time
.time() * 1000)
36 def add_parents(paths
):
37 """Iterate over each item in the set and add its parent directories."""
41 path
= path
.replace('//', '/')
44 parent_dir
= dirname(path
)
46 all_paths
.add(parent_dir
)
47 parent_dir
= dirname(parent_dir
)
51 def format_exception(exc
):
52 """Format an exception object for display"""
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(exc
, 'msg'):
59 msg
= core
.decode(repr(exc
))
63 def sublist(values
, remove
):
64 """Subtracts list b from list a and returns the resulting list."""
65 # conceptually, c = a - b
68 if item
not in remove
:
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
83 isdict
= isinstance(items
, dict)
84 if pattern
in __grep_cache
:
85 regex
= __grep_cache
[pattern
]
87 regex
= __grep_cache
[pattern
] = re
.compile(pattern
)
91 match
= regex
.match(item
)
94 groups
= match
.groups()
96 subitems
= match
.group(0)
101 subitems
= list(groups
)
103 matchdict
[item
] = items
[item
]
105 matched
.append(subitems
)
109 elif squash
and len(matched
) == 1:
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]
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.
142 path
= path
.replace('//', '/')
143 path_dirname
= path
.rsplit('/', 1)[0]
144 if path_dirname
== path
:
146 return path
.rsplit('/', 1)[0]
150 """Split paths using '/' regardless of platform"""
151 return path
.split('/')
155 """Split a path-like name. Returns tuple "(head, tail)" where "tail" is
156 everything after the final slash. The "head" may be empty.
158 This is the same as os.path.split() but only uses '/' as the delimiter.
167 return (dirname(name
), basename(name
))
171 """Join paths using '/' regardless of platform
173 >>> join('a', 'b', 'c')
177 return '/'.join(paths
)
180 def normalize_slash(value
):
181 """Strip and normalize slashes in a string
183 >>> normalize_slash('///Meow///Cat///')
187 value
= value
.strip('/')
188 new_value
= value
.replace('//', '/')
189 while new_value
!= value
:
191 new_value
= value
.replace('//', '/')
196 """Join a list of paths using '/' regardless of platform
198 >>> pathjoin(['a', 'b', 'c'])
206 """Return all of the path components for the specified path
208 >>> pathset('foo/bar/baz') == ['foo', 'foo/bar', 'foo/bar/baz']
213 parts
= splitpath(path
)
216 result
.append(prefix
+ part
)
222 def select_directory(paths
):
223 """Return the first directory in a list of paths"""
231 return os
.path
.dirname(paths
[0]) or core
.getcwd()
234 def strip_prefix(prefix
, string
):
235 """Return string, without the prefix. Blow up if string doesn't
236 start with prefix."""
237 assert string
.startswith(prefix
)
238 return string
[len(prefix
) :]
242 """Removes shell metacharacters from a string."""
243 for char
in """ \t!@#$%^&*()\\;,<>"'[]{}~|""":
244 value
= value
.replace(char
, '_')
248 def tablength(word
, tabwidth
):
249 """Return length of a word taking tabs into account
251 >>> tablength("\\t\\t\\t\\tX", 8)
255 return len(word
.replace('\t', '')) + word
.count('\t') * tabwidth
258 def _shell_split_py2(value
):
259 """Python2 requires bytes inputs to shlex.split(). Returns [unicode]"""
261 result
= shlex
.split(core
.encode(value
))
263 result
= core
.encode(value
).strip().split()
264 # Decode to unicode strings
265 return [core
.decode(arg
) for arg
in result
]
268 def _shell_split_py3(value
):
269 """Python3 requires unicode inputs to shlex.split(). Converts to unicode"""
271 result
= shlex
.split(value
)
273 result
= core
.decode(value
).strip().split()
278 def shell_split(value
):
280 # Encode before calling split()
281 values
= _shell_split_py2(value
)
283 # Python3 does not need the encode/decode dance
284 values
= _shell_split_py3(value
)
288 def tmp_filename(label
, suffix
=''):
289 label
= 'git-cola-' + label
.replace('/', '-').replace('\\', '-')
290 with tempfile
.NamedTemporaryFile(
291 prefix
=label
+ '-', suffix
=suffix
, delete
=False
297 """Is this a linux machine?"""
298 return sys
.platform
.startswith('linux')
303 return os
.path
.exists('/usr/bin/apt-get')
307 """Return True on OSX."""
308 return sys
.platform
== 'darwin'
312 """Return True on win32"""
313 return sys
.platform
in {'win32', 'cygwin'}
316 def launch_default_app(paths
):
317 """Execute the default application on the specified paths"""
320 if hasattr(os
, 'startfile'):
321 os
.startfile(path
) # pylint: disable=no-member
327 launcher
= 'xdg-open'
329 core
.fork([launcher
] + paths
)
332 def expandpath(path
):
333 """Expand ~user/ and environment $variables"""
334 path
= os
.path
.expandvars(path
)
335 if path
.startswith('~'):
336 path
= os
.path
.expanduser(path
)
341 """Operate on a collection of objects as a single unit"""
343 def __init__(self
, *members
):
344 self
._members
= members
346 def __getattr__(self
, name
):
347 """Return a function that relays calls to the group"""
349 def relay(*args
, **kwargs
):
350 for member
in self
._members
:
351 method
= getattr(member
, name
)
352 method(*args
, **kwargs
)
354 setattr(self
, name
, relay
)
359 """Wrap an object and override attributes"""
361 def __init__(self
, obj
, **overrides
):
363 for k
, v
in overrides
.items():
366 def __getattr__(self
, name
):
367 return getattr(self
._obj
, name
)
370 def slice_func(input_items
, map_func
):
371 """Slice input_items and call `map_func` over every slice
373 This exists because of "errno: Argument list too long"
376 # This comment appeared near the top of include/linux/binfmts.h
377 # in the Linux source tree:
380 # * MAX_ARG_PAGES defines the number of pages allocated for arguments
381 # * and envelope for the new program. 32 should suffice, this gives
382 # * a maximum env+arg of 128kB w/4KB pages!
384 # #define MAX_ARG_PAGES 32
386 # 'size' is a heuristic to keep things highly performant by minimizing
387 # the number of slices. If we wanted it to run as few commands as
388 # possible we could call "getconf ARG_MAX" and make a better guess,
389 # but it's probably not worth the complexity (and the extra call to
390 # getconf that we can't do on Windows anyways).
392 # In my testing, getconf ARG_MAX on Mac OS X Mountain Lion reported
393 # 262144 and Debian/Linux-x86_64 reported 2097152.
395 # The hard-coded max_arg_len value is safely below both of these
398 # 4K pages x 32 MAX_ARG_PAGES
399 max_arg_len
= (32 * 4096) // 4 # allow plenty of space for the environment
400 max_filename_len
= 256
401 size
= max_arg_len
// max_filename_len
407 items
= copy
.copy(input_items
)
409 stat
, out
, err
= map_func(items
[:size
])
411 status
= min(stat
, status
)
413 status
= max(stat
, status
)
418 return (status
, '\n'.join(outs
), '\n'.join(errs
))
422 def __init__(self
, sequence
):
423 self
.sequence
= sequence
425 def index(self
, item
, default
=-1):
427 idx
= self
.sequence
.index(item
)
432 def __getitem__(self
, idx
):
433 return self
.sequence
[idx
]
436 def catch_runtime_error(func
, *args
, **kwargs
):
437 """Run the function safely.
439 Catch RuntimeError to avoid tracebacks during application shutdown.
442 # Signals and callbacks can sometimes get triggered during application shutdown.
443 # This can happen when exiting while background tasks are still processing.
444 # Guard against this by making this operation a no-op.
447 result
= func(*args
, **kwargs
)
451 return (valid
, result
)