Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / tools / findit / common / utils.py
blob5011e76e2b128a14abdf7609ce36b137c803fea8
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import re
6 import sys
8 from http_client_local import HttpClientLocal
11 GIT_HASH_PATTERN = re.compile(r'^[0-9a-fA-F]{40}$')
14 def GetOSName(platform_name=sys.platform):
15 if platform_name == 'cygwin' or platform_name.startswith('win'):
16 return 'win'
17 elif platform_name.startswith('linux'):
18 return 'unix'
19 elif platform_name.startswith('darwin'):
20 return 'mac'
21 else:
22 return platform_name
25 def IsGitHash(revision):
26 return GIT_HASH_PATTERN.match(str(revision))
29 def GetHttpClient():
30 # TODO(stgao): return implementation for appengine when running on appengine.
31 return HttpClientLocal
34 def JoinLineNumbers(line_numbers, accepted_gap=1):
35 """Join line numbers into line blocks.
37 Args:
38 line_numbers: a list of line number.
39 accepted_gap: if two line numbers are within the give gap,
40 they would be combined together into a block.
41 Eg: for (1, 2, 3, 6, 7, 8, 12), if |accepted_gap| = 1, result
42 would be 1-3, 6-8, 12; if |accepted_gap| = 3, result would be
43 1-8, 12; if |accepted_gap| =4, result would be 1-12.
44 """
45 if not line_numbers:
46 return ''
48 line_numbers = map(int, line_numbers)
49 line_numbers.sort()
51 block = []
52 start_line_number = line_numbers[0]
53 last_line_number = start_line_number
54 for current_line_number in line_numbers[1:]:
55 if last_line_number + accepted_gap < current_line_number:
56 if start_line_number == last_line_number:
57 block.append('%d' % start_line_number)
58 else:
59 block.append('%d-%d' % (start_line_number, last_line_number))
60 start_line_number = current_line_number
61 last_line_number = current_line_number
62 else:
63 if start_line_number == last_line_number:
64 block.append('%d' % start_line_number)
65 else:
66 block.append('%d-%d' % (start_line_number, last_line_number))
68 return ', '.join(block)