-
Notifications
You must be signed in to change notification settings - Fork 996
Expand file tree
/
Copy pathhelpers.py
More file actions
49 lines (35 loc) · 1.23 KB
/
helpers.py
File metadata and controls
49 lines (35 loc) · 1.23 KB
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
"""Helpers for dealing with git, compilation databases, etc."""
import json
import os
import sys
import git
import yaml
def load_file(filename):
"""Load the entire contents of a file."""
with open(filename, encoding="utf-8") as file:
contents = file.read()
return contents
def get_line_from_offset(file_contents, offset):
"""Calculate line number from byte offset in source file."""
return file_contents[:offset].count("\n") + 1
def get_repo_root():
"""Get the git repo's root directory."""
cwd = os.getcwd()
repo = git.Repo(cwd, search_parent_directories=True)
root = repo.git.rev_parse("--show-toplevel")
return root
def load_fixes_file(filename):
"""Load the clang-tidy YAML fixes file."""
with open(filename, encoding="utf_8") as file:
return yaml.safe_load(file)
def load_compdb(filename):
"""Load the compilation database."""
with open(filename, encoding="utf_8") as file:
return json.load(file)
def index_compdb(file_contents):
"""Index the compilation database."""
result = set()
for item in file_contents:
filename = os.path.normpath(os.path.join(item["directory"], item["file"]))
result.add(filename)
return result