-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathhelpers.py
64 lines (49 loc) · 1.9 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import absolute_import
from configparser import ConfigParser
import os
COLLABORATORS_CONFIG_FILE = os.path.join(os.path.dirname(__file__),
'collaborators.ini')
_test_path_roots = ['a/', 'b/']
def get_people_from_config(api, config_abs_path):
'''
Gets the people listed under a particular repo from a config file.
Note that the names (despite how they're in the file) will always
be parsed to 'lowercase'.
'''
config = ConfigParser()
config.read(config_abs_path)
repo = api.owner + '/' + api.repo
try:
return config.items(repo)
except ConfigParser.NoSectionError:
return [] # No people
def get_collaborators(api):
config_items = get_people_from_config(api, COLLABORATORS_CONFIG_FILE)
return [username for (username, _) in config_items]
def is_addition(diff_line):
"""
Checks if a line from a unified diff is an addition.
"""
return diff_line.startswith('+') and not diff_line.startswith('+++')
def normalize_file_path(filepath):
"""
Strip any leading/training whitespace.
Remove any test directories from the start of the path
"""
if filepath is None or filepath.strip() == '':
return None
filepath = filepath.strip()
for prefix in _test_path_roots:
if filepath.startswith(prefix):
return filepath[len(prefix):]
return filepath
def linear_search(sequence, element, callback=lambda thing: thing):
"""
The 'in' operator also does a linear search over a sequence, but it checks
for the exact match of the given object, whereas this makes use of '==',
which calls the '__eq__' magic method, which could've been overridden in
a custom class (which is the case for our test lint)
"""
for thing in sequence:
if element == thing: # element could have an overridden '__eq__'
callback(thing)