diff --git a/obspy/core/tests/test_code_formatting.py b/obspy/core/tests/test_code_formatting.py index ff3c4532eef..881046576f2 100644 --- a/obspy/core/tests/test_code_formatting.py +++ b/obspy/core/tests/test_code_formatting.py @@ -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", @@ -34,6 +35,7 @@ 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"] @@ -41,6 +43,9 @@ def test_flake8(self): 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 diff --git a/obspy/core/util/misc.py b/obspy/core/util/misc.py index 791bfe280a3..57db72213bd 100644 --- a/obspy/core/util/misc.py +++ b/obspy/core/util/misc.py @@ -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 @@ -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)