commit 5a9e5fe8a41236588f339b80499c2bc96997f24d Author: Damian Johnson atagar@torproject.org Date: Thu Jan 2 08:51:46 2014 -0800
Moving files_with_suffix() to system util
Our system module already has path manipulation helpers like expand_path() so probably a better home for files_with_suffix(). --- docs/change_log.rst | 4 ++++ stem/util/system.py | 24 ++++++++++++++++++++++++ test/unit/doctest.py | 3 ++- test/util.py | 41 ++++++++--------------------------------- 4 files changed, 38 insertions(+), 34 deletions(-)
diff --git a/docs/change_log.rst b/docs/change_log.rst index 7b71c47..352d4a8 100644 --- a/docs/change_log.rst +++ b/docs/change_log.rst @@ -47,6 +47,10 @@ The following are only available within stem's `git repository * Added `support for CELL_STATS events <api/response.html#stem.response.events.CellStatsEvent>`_ (:spec:`6f2919a`) * Added `support for TB_EMPTY events <api/response.html#stem.response.events.TokenBucketEmptyEvent>`_ (:spec:`6f2919a`)
+ * **Utilities** + + * Added :func:`stem.util.system.files_with_suffix` + .. _version_1.1:
Version 1.1 diff --git a/stem/util/system.py b/stem/util/system.py index db0943e..9b28e04 100644 --- a/stem/util/system.py +++ b/stem/util/system.py @@ -26,6 +26,7 @@ best-effort, providing **None** if the lookup fails. get_bsd_jail_id - provides the BSD jail id a given process is running within get_bsd_jail_path - provides the path of the given BSD jail expand_path - expands relative paths and ~ entries + files_with_suffix - provides files with the given suffix call - runs the given system command and provides back the results
get_process_name - provides our process' name @@ -807,6 +808,29 @@ def expand_path(path, cwd = None): return relative_path
+def files_with_suffix(base_path, suffix): + """ + Iterates over files in a given directory, providing filenames with a certain + suffix. + + .. versionadded:: 1.2.0 + + :param str base_path: directory to be iterated over + :param str suffix: filename suffix to look for + + :returns: iterator that yields the absolute path for files with the given suffix + """ + + if os.path.isfile(base_path): + if base_path.endswith(suffix): + yield base_path + else: + for root, _, files in os.walk(base_path): + for filename in files: + if filename.endswith(suffix): + yield os.path.join(root, filename) + + def call(command, default = UNDEFINED, ignore_exit_status = False): """ Issues a command in a subprocess, blocking until completion and returning the diff --git a/test/unit/doctest.py b/test/unit/doctest.py index ac9f430..1906480 100644 --- a/test/unit/doctest.py +++ b/test/unit/doctest.py @@ -11,6 +11,7 @@ import unittest import stem.descriptor.router_status_entry import stem.util.connection import stem.util.str_tools +import stem.util.system import stem.version
import test.util @@ -32,7 +33,7 @@ class TestDocumentation(unittest.TestCase): stem_dir = os.path.join(test.util.STEM_BASE, 'stem') is_failed = False
- for path in test.util._get_files_with_suffix(stem_dir): + for path in stem.util.system.files_with_suffix(stem_dir, '.py'): args = {'module_relative': False} test_run = None
diff --git a/test/util.py b/test/util.py index ef323e7..6f54a91 100644 --- a/test/util.py +++ b/test/util.py @@ -276,10 +276,10 @@ def get_stylistic_issues(paths): issues.setdefault(self.filename, []).append((offset + line_number, "%s %s" % (code, text)))
style_checker = pep8.StyleGuide(ignore = CONFIG["pep8.ignore"], reporter = StyleReport) - style_checker.check_files(_get_python_files(paths)) + style_checker.check_files(list(_python_files(paths)))
for path in paths: - for file_path in _get_files_with_suffix(path): + for file_path in stem.util.system.files_with_suffix(path, '.py'): if _is_test_data(file_path): continue
@@ -367,7 +367,7 @@ def get_pyflakes_issues(paths):
reporter = Reporter()
- for path in _get_python_files(paths): + for path in _python_files(paths): pyflakes.api.checkPath(path, reporter)
return issues @@ -437,7 +437,7 @@ def clean_orphaned_pyc(paths): orphaned_pyc = []
for path in paths: - for pyc_path in _get_files_with_suffix(path, ".pyc"): + for pyc_path in stem.util.system.files_with_suffix(path, '.pyc'): # If we're running python 3 then the *.pyc files are no longer bundled # with the *.py. Rather, they're in a __pycache__ directory. # @@ -471,7 +471,7 @@ def check_for_unused_tests(paths): unused_tests = []
for path in paths: - for py_path in _get_files_with_suffix(path, ".py"): + for py_path in stem.util.system.files_with_suffix(path, '.py'): if _is_test_data(py_path): continue
@@ -544,27 +544,6 @@ def _is_test_data(path): return os.path.normpath(CONFIG["integ.test_directory"]) in path
-def _get_files_with_suffix(base_path, suffix = ".py"): - """ - Iterates over files in a given directory, providing filenames with a certain - suffix. - - :param str base_path: directory to be iterated over - :param str suffix: filename suffix to look for - - :returns: iterator that yields the absolute path for files with the given suffix - """ - - if os.path.isfile(base_path): - if base_path.endswith(suffix): - yield base_path - else: - for root, _, files in os.walk(base_path): - for filename in files: - if filename.endswith(suffix): - yield os.path.join(root, filename) - - def run_tasks(category, *tasks): """ Runs a series of :class:`test.util.Task` instances. This simply prints 'done' @@ -587,15 +566,11 @@ def run_tasks(category, *tasks): println()
-def _get_python_files(paths): - results = [] - +def _python_files(paths): for path in paths: - for file_path in _get_files_with_suffix(path): + for file_path in stem.util.system.files_with_suffix(path, '.py'): if not _is_test_data(file_path): - results.append(file_path) - - return results + yield file_path
class Task(object):
tor-commits@lists.torproject.org