|
1 |
| -import hashlib |
2 |
| -import os |
3 |
| -import subprocess |
4 |
| - |
5 |
| - |
6 |
| -def build_automaton(word_list): |
7 |
| - """ |
8 |
| - :type word_list: str |
9 |
| - :param word_list: optional word list file for ignoring certain words. |
10 |
| -
|
11 |
| - :rtype: (ahocorasick.Automaton, str) |
12 |
| - :returns: an automaton, and an iterated sha1 hash of the words in the word list. |
13 |
| - """ |
14 |
| - # Dynamic import due to optional-dependency |
15 |
| - try: |
16 |
| - import ahocorasick |
17 |
| - except ImportError: # pragma: no cover |
18 |
| - print('Please install the `pyahocorasick` package to use --word-list') |
19 |
| - raise |
20 |
| - |
21 |
| - # See https://pyahocorasick.readthedocs.io/en/latest/ |
22 |
| - # for more information. |
23 |
| - automaton = ahocorasick.Automaton() |
24 |
| - word_list_hash = hashlib.sha1() |
25 |
| - |
26 |
| - with open(word_list) as f: |
27 |
| - for line in f.readlines(): |
28 |
| - # .lower() to make everything case-insensitive |
29 |
| - line = line.lower().strip() |
30 |
| - if len(line) > 3: |
31 |
| - word_list_hash.update(line.encode('utf-8')) |
32 |
| - automaton.add_word(line, line) |
33 |
| - |
34 |
| - automaton.make_automaton() |
35 |
| - |
36 |
| - return ( |
37 |
| - automaton, |
38 |
| - word_list_hash.hexdigest(), |
39 |
| - ) |
40 |
| - |
41 |
| - |
42 |
| -def get_root_directory(): # pragma: no cover |
43 |
| - return os.path.realpath( |
44 |
| - os.path.join( |
45 |
| - os.path.dirname(__file__), |
46 |
| - '../../', |
47 |
| - ), |
48 |
| - ) |
49 |
| - |
50 |
| - |
51 |
| -def get_git_sha(path): |
52 |
| - """Returns the sha of the git checkout at the input path. |
53 |
| -
|
54 |
| - :type path: str |
55 |
| - :param path: directory of the git checkout |
56 |
| -
|
57 |
| - :rtype: str|None |
58 |
| - :returns: git sha of the input path |
59 |
| - """ |
60 |
| - try: |
61 |
| - with open(os.devnull, 'w') as fnull: |
62 |
| - return subprocess.check_output( |
63 |
| - ['git', 'rev-parse', '--verify', 'HEAD'], |
64 |
| - stderr=fnull, |
65 |
| - cwd=path, |
66 |
| - ).decode('utf-8').split()[0] |
67 |
| - except (subprocess.CalledProcessError, OSError, IndexError): # pragma: no cover |
68 |
| - return None |
69 |
| - |
70 |
| - |
71 |
| -def get_git_remotes(path): |
72 |
| - """Returns a list of unique git remotes of the checkout |
73 |
| - at the input path. |
74 |
| -
|
75 |
| - :type path: str |
76 |
| - :param path: directory of the git checkout |
77 |
| -
|
78 |
| - :rtype: List<str>|None |
79 |
| - :returns: A list of unique git urls |
80 |
| - """ |
81 |
| - try: |
82 |
| - with open(os.devnull, 'w') as fnull: |
83 |
| - git_remotes = subprocess.check_output( |
84 |
| - ['git', 'remote', '-v'], |
85 |
| - stderr=fnull, |
86 |
| - cwd=path, |
87 |
| - ).decode('utf-8').split('\n') |
88 |
| - return list({ |
89 |
| - git_remote.split()[1] |
90 |
| - for git_remote |
91 |
| - in git_remotes |
92 |
| - if len(git_remote) > 2 # split('\n') produces an empty list |
93 |
| - }) |
94 |
| - except (subprocess.CalledProcessError, OSError): # pragma: no cover |
95 |
| - return None |
0 commit comments