13 g_cppcheck_path_arg =
None 14 g_uncrustify_path_arg =
None 30 def __init__(self, contents, size, mode, sha1, status, path):
38 def fnmatch(self, pattern):
39 basename = os.path.basename(self.
path)
40 return fnmatch.fnmatch(basename, pattern)
44 print(
'* [Sheldon] ' + msg)
49 print(
'* [Sheldon] ' + msg)
53 print(
'*** [ERROR] ' + msg +
' ***')
57 print(
'* [Warning] ' + msg +
' *')
61 """return true if a string is binary data""" 62 return bool(s
and '\0' in s)
65 ExecutionResult = collections.namedtuple(
72 result = execute_command(
'git rev-parse --show-toplevel');
74 if result.status == 0:
75 return result.out.strip()
82 repo_root = get_repo_root()
84 f = open(os.path.join(repo_root,
"LICENSE/COPYING.LESSER"))
91 def _get_git_commit_datetime(path):
93 result = execute_command(
'git log -1 --format=%ad --date=format:%Y-%m-%dT%H:%M:%S ' + path)
95 if result.status != 0:
101 return datetime.datetime.strptime(result.out.strip(),
'%Y-%m-%dT%H:%M:%S')
102 except Exception
as e:
107 def get_file_datetime(path, check_commits_date):
109 creation_time = datetime.datetime.fromtimestamp(os.path.getctime(path))
110 modification_time = datetime.datetime.fromtimestamp(os.path.getmtime(path))
111 except Exception
as e:
115 modification_time =
None 117 if check_commits_date:
118 git_datetime = _get_git_commit_datetime(path)
121 if git_datetime
is not None:
125 if modification_time
is not None:
126 return modification_time
129 return datetime.datetime.today()
132 def execute_command(proc):
134 out = subprocess.check_output(proc.split(), stderr=subprocess.STDOUT)
135 except subprocess.CalledProcessError
as e:
136 return ExecutionResult(1, e.output)
138 return ExecutionResult(1, e.message)
140 return ExecutionResult(0, out)
143 def current_commit():
144 if execute_command(
'git rev-parse --verify HEAD').status != 0:
145 return '4b825dc642cb6eb9a060e54bf8d69288fbee4904' 150 def get_option(option, default, type=""):
152 out = subprocess.check_output((
'git config ' + type +
' ' + option).split()).strip()
154 except subprocess.CalledProcessError:
159 result = execute_command(
'git show ' + sha)
161 if result.status == 0:
168 def _diff_index(rev):
169 result = execute_command(
'git diff-index --cached -z --diff-filter=AM ' + rev)
171 if result.status == 0:
178 def _diff(rev, rev2):
179 result = execute_command(
'git diff --raw -z --diff-filter=AM ' + rev +
' ' + rev2)
181 if result.status == 0:
189 result = execute_command(
'git cat-file -s ' + sha)
190 if result.status == 0:
192 return int(result.out)
200 def files_in_rev(rev, rev2=''):
203 diff_row_regex = re.compile(
210 (?P<old_sha1>[^ ]+)... 212 (?P<new_sha1>[^ ]+)... 222 for match
in diff_row_regex.finditer(_diff(rev, rev2)):
223 mode, sha, status, path = match.group(
224 'new_mode',
'new_sha1',
'status',
'path' 227 if status
is None or status ==
'D':
231 file_status = status_of_file(get_repo_root() +
'/' + path)
233 if file_status
is None or file_status ==
'D':
236 content = _contents(sha)
238 if content
is None or len(content) <= 0:
243 if size
is None or size <= 0:
256 def files_staged_for_commit(rev):
259 diff_index_row_regex = re.compile(
277 for match
in diff_index_row_regex.finditer(_diff_index(rev)):
278 mode, sha, status, path = match.group(
279 'new_mode',
'new_sha1',
'status',
'path' 283 file_status = status_of_file(get_repo_root() +
'/' + path)
285 if status
is not None and status !=
'D' and file_status
is not None and file_status !=
'D':
296 def status_of_file(path):
300 gitstatus = execute_command(
'git status --porcelain ' + path)
302 if gitstatus.status != 0:
303 warn(
"File : " + path +
" is not in a git repository, sheldon will consider it like a new file")
305 out = gitstatus.out.split()
308 if out
and out[0] !=
'??':
314 def file_on_disk(path):
315 status = status_of_file(path)
317 if status
is not None and status !=
'D':
318 with open(path,
'r') as content_file: 319 content = content_file.read() 334 def directory_on_disk(path):
335 for root, dirs, files
in os.walk(path):
337 file_path = os.path.join(root, name)
338 yield file_on_disk(file_path).next()