Skip to content

Commit

Permalink
exclude files untracked by git from flake8 check
Browse files Browse the repository at this point in the history
@krischer, maybe can you check if we need something like in #492?
  • Loading branch information
megies committed Oct 2, 2013
1 parent ac078e5 commit d21d59f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions obspy/core/tests/test_code_formatting.py
Expand Up @@ -4,6 +4,7 @@
import inspect
import os
import unittest
from obspy.core.util.misc import get_untracked_files_from_git

EXCLUDE_FILES = [
"*/__init__.py",
Expand Down Expand Up @@ -34,13 +35,17 @@ def test_flake8(self):
obspy_dir = os.path.dirname(os.path.dirname(os.path.dirname(test_dir)))
error_count = 0
file_count = 0
untracked_files = get_untracked_files_from_git() or []
for dirpath, _, filenames in os.walk(obspy_dir):
filenames = [_i for _i in filenames if
os.path.splitext(_i)[-1] == os.path.extsep + "py"]
if not filenames:
continue
for py_file in filenames:
py_file = os.path.join(dirpath, py_file)
# ignore untracked files
if os.path.abspath(py_file) in untracked_files:
continue

# Check if the filename should not be excluded.
skip_file = False
Expand Down
26 changes: 26 additions & 0 deletions obspy/core/util/misc.py
Expand Up @@ -8,6 +8,9 @@
GNU Lesser General Public License, Version 3
(http://www.gnu.org/copyleft/lesser.html)
"""
import os
import inspect
from subprocess import Popen, PIPE
import warnings
import itertools
import numpy as np
Expand Down Expand Up @@ -217,6 +220,29 @@ def loadtxt(*args, **kwargs):
return np.atleast_1d(data)


def get_untracked_files_from_git():
"""
Tries to return a list of files (absolute paths) that are untracked by git
in the repository.
Returns `None` if the system call to git fails.
"""
dir_ = os.path.abspath(
os.path.dirname(inspect.getfile(inspect.currentframe())))
dir_ = os.path.dirname(os.path.dirname(os.path.dirname(dir_)))
try:
p = Popen(['git', 'status', '-u', '--porcelain'],
cwd=dir_, stdout=PIPE, stderr=PIPE)
p.stderr.close()
stdout = p.stdout.readlines()
files = [os.path.abspath(os.path.join(dir_, line.split()[1].strip()))
for line in stdout
if line.startswith("??")]
except:
return None
return files


if __name__ == '__main__':
import doctest
doctest.testmod(exclude_empty=True)

0 comments on commit d21d59f

Please sign in to comment.