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.
4
5import re
6import sys
7
8from http_client_local import HttpClientLocal
9
10
11GIT_HASH_PATTERN = re.compile(r'^[0-9a-fA-F]{40}$')
12
13
14def 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
23
24
25def IsGitHash(revision):
26  return GIT_HASH_PATTERN.match(str(revision))
27
28
29def GetHttpClient():
30  # TODO(stgao): return implementation for appengine when running on appengine.
31  return HttpClientLocal
32
33
34def JoinLineNumbers(line_numbers, accepted_gap=1):
35  """Join line numbers into line blocks.
36
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 ''
47
48  line_numbers = map(int, line_numbers)
49  line_numbers.sort()
50
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))
67
68  return ', '.join(block)
69