summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Lint/MergeFiles.py
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2013-06-27 10:40:19 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2013-06-27 10:40:19 -0400
commit2d1f13115150af2dd9b74e1a928f40fc19cf0dd1 (patch)
treece8993bad3ae15ca4f61c5684450de89c6e11ca9 /src/lib/Bcfg2/Server/Lint/MergeFiles.py
parent67fda2597efe7cec04b037138cef86f1e328cc4c (diff)
downloadbcfg2-2d1f13115150af2dd9b74e1a928f40fc19cf0dd1.tar.gz
bcfg2-2d1f13115150af2dd9b74e1a928f40fc19cf0dd1.tar.bz2
bcfg2-2d1f13115150af2dd9b74e1a928f40fc19cf0dd1.zip
Options: migrated bcfg2-lint to new parser
Diffstat (limited to 'src/lib/Bcfg2/Server/Lint/MergeFiles.py')
-rw-r--r--src/lib/Bcfg2/Server/Lint/MergeFiles.py38
1 files changed, 22 insertions, 16 deletions
diff --git a/src/lib/Bcfg2/Server/Lint/MergeFiles.py b/src/lib/Bcfg2/Server/Lint/MergeFiles.py
index 2419c3d43..dff95fbf3 100644
--- a/src/lib/Bcfg2/Server/Lint/MergeFiles.py
+++ b/src/lib/Bcfg2/Server/Lint/MergeFiles.py
@@ -8,9 +8,24 @@ import Bcfg2.Server.Lint
from Bcfg2.Server.Plugins.Cfg import CfgGenerator
+def threshold(val):
+ """ Option type processor to accept either a percentage (e.g.,
+ "threshold=75") or a ratio (e.g., "threshold=.75") """
+ threshold = float(val)
+ if threshold > 1:
+ threshold /= 100
+ return threshold
+
+
class MergeFiles(Bcfg2.Server.Lint.ServerPlugin):
""" find Probes or Cfg files with multiple similar files that
might be merged into one """
+
+ options = Bcfg2.Server.Lint.ServerPlugin.options + [
+ Bcfg2.Options.Option(
+ cf=("MergeFiles", "threshold"), default="0.75", type=threshold,
+ help="The threshold at which to suggest merging files and probes")]
+
def Run(self):
if 'Cfg' in self.core.plugins:
self.check_cfg()
@@ -48,19 +63,10 @@ class MergeFiles(Bcfg2.Server.Lint.ServerPlugin):
""" Get a list of similar files from the entry dict. Return
value is a list of lists, each of which gives the filenames of
similar files """
- if "threshold" in self.config:
- # accept threshold either as a percent (e.g., "threshold=75") or
- # as a ratio (e.g., "threshold=.75")
- threshold = float(self.config['threshold'])
- if threshold > 1:
- threshold /= 100
- else:
- threshold = 0.75
rv = []
elist = list(entries.items())
while elist:
- result = self._find_similar(elist.pop(0), copy.copy(elist),
- threshold)
+ result = self._find_similar(elist.pop(0), copy.copy(elist))
if len(result) > 1:
elist = [(fname, fdata)
for fname, fdata in elist
@@ -68,7 +74,7 @@ class MergeFiles(Bcfg2.Server.Lint.ServerPlugin):
rv.append(result)
return rv
- def _find_similar(self, ftuple, others, threshold):
+ def _find_similar(self, ftuple, others):
""" Find files similar to the one described by ftupe in the
list of other files. ftuple is a tuple of (filename, data);
others is a list of such tuples. threshold is a float between
@@ -80,9 +86,9 @@ class MergeFiles(Bcfg2.Server.Lint.ServerPlugin):
cname, cdata = others.pop(0)
seqmatch = SequenceMatcher(None, fdata.data, cdata.data)
# perform progressively more expensive comparisons
- if (seqmatch.real_quick_ratio() > threshold and
- seqmatch.quick_ratio() > threshold and
- seqmatch.ratio() > threshold):
- rv.extend(self._find_similar((cname, cdata), copy.copy(others),
- threshold))
+ if (seqmatch.real_quick_ratio() > Bcfg2.Options.setup.threshold and
+ seqmatch.quick_ratio() > Bcfg2.Options.setup.threshold and
+ seqmatch.ratio() > Bcfg2.Options.setup.threshold):
+ rv.extend(
+ self._find_similar((cname, cdata), copy.copy(others)))
return rv