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.
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'):
17 elif platform_name
.startswith('linux'):
19 elif platform_name
.startswith('darwin'):
25 def IsGitHash(revision
):
26 return GIT_HASH_PATTERN
.match(str(revision
))
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.
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.
48 line_numbers
= map(int, line_numbers
)
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
)
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
63 if start_line_number
== last_line_number
:
64 block
.append('%d' % start_line_number
)
66 block
.append('%d-%d' % (start_line_number
, last_line_number
))
68 return ', '.join(block
)