aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Jaggars <jjaggars@redhat.com>2012-02-27 13:51:07 -0600
committerJesse Jaggars <jjaggars@redhat.com>2012-02-27 13:53:09 -0600
commit6aa392791870474e577adeb8a24bad2ccd1a8390 (patch)
treefd6da3f187937e045812e6d0ee0678ccc98bcc75
parentaf1567395d1cfc2ef84b68478309ca47515d622d (diff)
downloadsos-6aa392791870474e577adeb8a24bad2ccd1a8390.tar.gz
adding ability to specify multiple files for Plugin.fileGrep
-rw-r--r--Makefile3
-rw-r--r--sos/plugins/__init__.py11
-rw-r--r--sos/policies/__init__.py5
-rw-r--r--sos/utilities.py41
-rw-r--r--tests/policy_tests.py20
-rw-r--r--tests/utilities_tests.py37
6 files changed, 99 insertions, 18 deletions
diff --git a/Makefile b/Makefile
index 88a6532d..ee4859ca 100644
--- a/Makefile
+++ b/Makefile
@@ -102,3 +102,6 @@ test:
echo $$test; \
PYTHONPATH=`pwd` python $$test; \
done; \
+
+nose:
+ nosetests -v --with-cover --cover-package=sos --cover-html
diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py
index a1d38f45..dbf3ca1d 100644
--- a/sos/plugins/__init__.py
+++ b/sos/plugins/__init__.py
@@ -24,7 +24,7 @@
# pylint: disable-msg = W0611
# pylint: disable-msg = W0613
-from sos.utilities import sosGetCommandOutput, import_module
+from sos.utilities import sosGetCommandOutput, import_module, grep
from sos import _sos as _
import inspect
import os
@@ -392,11 +392,10 @@ class Plugin(object):
"""
self.collectProgs.append( (exe, suggest_filename, root_symlink, timeout) )
- def fileGrep(self, regexp, fname):
- try:
- return [l for l in open(fname).readlines() if re.match(regexp, l)]
- except: # IOError, AttributeError, etc.
- return []
+ def fileGrep(self, regexp, *fnames):
+ """Returns lines matched in fnames, where fnames can either be pathnames to files
+ to grep through or open file objects to grep through line by line"""
+ return grep(regexp, fnames)
def mangleCommand(self, exe):
# FIXME: this can be improved
diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py
index f5c0e036..88b00247 100644
--- a/sos/policies/__init__.py
+++ b/sos/policies/__init__.py
@@ -2,6 +2,7 @@ import os
import re
import platform
import time
+import fnmatch
from sos.utilities import ImporterHelper, import_module, get_hash_name
from sos.plugins import IndependentPlugin
@@ -40,7 +41,7 @@ class PackageManager(object):
"""
return fnmatch.filter(self.allPkgs().keys(), name)
- def allPkgsByNameRegex(self, regex_name, flags=None):
+ def allPkgsByNameRegex(self, regex_name, flags=0):
"""
Return a list of packages that match regex_name.
"""
@@ -60,7 +61,7 @@ class PackageManager(object):
"""
Return a list of all packages.
"""
- return []
+ return {}
def pkgNVRA(self, pkg):
fields = pkg.split("-")
diff --git a/sos/utilities.py b/sos/utilities.py
index 5aef0cb8..43ed1128 100644
--- a/sos/utilities.py
+++ b/sos/utilities.py
@@ -34,23 +34,31 @@ import logging
import zipfile
import tarfile
import hashlib
+from contextlib import closing
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import time
-def checksum(filename, chunk_size=128):
+def fileobj(path_or_file, mode='r'):
+ if isinstance(path_or_file, basestring):
+ return open(path_or_file, mode)
+ else:
+ return closing(path_or_file)
+
+def checksum(file_, chunk_size=128, algorithm=None):
"""Returns the checksum of the supplied filename. The file is read in
chunk_size blocks"""
- name = get_hash_name()
- digest = hashlib.new(name)
- fd = open(filename, 'rb')
- data = fd.read(chunk_size)
- while data:
- digest.update(data)
+ if not algorithm:
+ algorithm = get_hash_name()
+ digest = hashlib.new(algorithm)
+ with fileobj(file_, 'rb') as fd:
data = fd.read(chunk_size)
- return digest.hexdigest()
+ while data:
+ digest.update(data)
+ data = fd.read(chunk_size)
+ return digest.hexdigest()
def get_hash_name():
"""Returns the algorithm used when computing a hash"""
@@ -77,9 +85,12 @@ class DirTree(object):
self.buffer.append(s)
def printtree(self):
- print self.as_string()
+ print str(self)
def as_string(self):
+ return str(self)
+
+ def __str__(self):
return "\n".join(self.buffer)
def _build_tree(self):
@@ -262,6 +273,18 @@ def find(file_pattern, top_dir, max_depth=None, path_pattern=None):
yield os.path.join(path, name)
+def grep(pattern, *files_or_paths):
+ """Returns lines matched in fnames, where fnames can either be pathnames to files
+ to grep through or open file objects to grep through line by line"""
+ matches = []
+
+ for fop in files_or_paths:
+ with fileobj(fop) as fo:
+ matches.extend((line for line in fo if re.match(pattern, line)))
+
+ return matches
+
+
def sosGetCommandOutput(command, timeout=300):
"""Execute a command through the system shell. First checks to see if the
requested command is executable. Returns (returncode, stdout, 0)"""
diff --git a/tests/policy_tests.py b/tests/policy_tests.py
index 2654c82c..25795a1c 100644
--- a/tests/policy_tests.py
+++ b/tests/policy_tests.py
@@ -1,6 +1,6 @@
import unittest
-from sos.policies import Policy, import_policy
+from sos.policies import Policy, PackageManager, import_policy
from sos.plugins import Plugin, IndependentPlugin, RedHatPlugin, DebianPlugin
class FauxPolicy(Policy):
@@ -47,5 +47,23 @@ class PolicyTests(unittest.TestCase):
def test_cant_import(self):
self.assertTrue(import_policy('notreal') is None)
+
+class PackageManagerTests(unittest.TestCase):
+
+ def setUp(self):
+ self.pm = PackageManager()
+
+ def test_default_all_pkgs(self):
+ self.assertEquals(self.pm.allPkgs(), {})
+
+ def test_default_all_pkgs_by_name(self):
+ self.assertEquals(self.pm.allPkgsByName('doesntmatter'), [])
+
+ def test_default_all_pkgs_by_name_regex(self):
+ self.assertEquals(self.pm.allPkgsByNameRegex('.*doesntmatter$'), [])
+
+ def test_default_pkg_by_name(self):
+ self.assertEquals(self.pm.pkgByName('foo'), None)
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/utilities_tests.py b/tests/utilities_tests.py
new file mode 100644
index 00000000..d5115928
--- /dev/null
+++ b/tests/utilities_tests.py
@@ -0,0 +1,37 @@
+import os.path
+import unittest
+from StringIO import StringIO
+
+from sos.utilities import grep, DirTree, checksum
+import sos
+
+class GrepTest(unittest.TestCase):
+
+ def test_file_obj(self):
+ test_s = "\n".join(['this is only a test', 'there are only two lines'])
+ test_fo = StringIO(test_s)
+ matches = grep(".*test$", test_fo)
+ self.assertEquals(matches, ['this is only a test\n'])
+
+ def test_real_file(self):
+ matches = grep(".*unittest$", __file__.replace(".pyc", ".py"))
+ self.assertEquals(matches, ['import unittest\n'])
+
+ def test_open_file(self):
+ matches = grep(".*unittest$", open(__file__.replace(".pyc", ".py")))
+ self.assertEquals(matches, ['import unittest\n'])
+
+
+class DirTreeTest(unittest.TestCase):
+
+ def test_makes_tree(self):
+ # I'll admit, this a pretty lame test, but it will at least sniff out
+ # some syntax issues
+ t = DirTree(os.path.dirname(sos.__file__)).as_string()
+ self.assertTrue('Makefile' in t)
+
+class ChecksumTest(unittest.TestCase):
+
+ def test_simple_hash(self):
+ self.assertEquals(checksum(StringIO('this is a test'), algorithm="sha256"),
+ '2e99758548972a8e8822ad47fa1017ff72f06f3ff6a016851f45c398732bc50c')