summaryrefslogtreecommitdiff
path: root/bin
diff options
authorSylvain Pineau <sylvain.pineau@canonical.com>2017-04-21 12:44:17 +0200
committerSylvain Pineau <sylvain.pineau@canonical.com>2017-04-21 12:44:17 +0200
commit10da577a09fd25159a399d38ed2de0cead695b12 (patch)
tree0eb2cf4b94715bd29c431a7242086bc597ebaccf /bin
parent02cb70091bf63a6075ee73517157c6280450d8bd (diff)
Remove the filter_templates and run_templates scripts
Diffstat (limited to 'bin')
-rwxr-xr-xbin/filter_templates134
-rwxr-xr-xbin/run_templates144
2 files changed, 0 insertions, 278 deletions
diff --git a/bin/filter_templates b/bin/filter_templates
deleted file mode 100755
index 1c3272d..0000000
--- a/bin/filter_templates
+++ /dev/null
@@ -1,134 +0,0 @@
-#!/usr/bin/env python3
-
-import re
-import sys
-import posixpath
-
-from optparse import OptionParser
-
-from checkbox_support.lib.path import path_expand_recursive
-from checkbox_support.lib.template import Template
-
-
-class FilterError(Exception):
-
- pass
-
-
-def compile_filters(filters):
- patterns = {}
- for filter in filters:
- if "=" not in filter:
- raise FilterError("Missing assignment in filter: %s"
- % filter)
-
- name, value = filter.split("=", 1)
- try:
- pattern = re.compile(r"^%s$" % value)
- except re.error:
- raise FilterError("Invalid regular expression in filter: %s"
- % value)
- patterns.setdefault(name, [])
- patterns[name].append(pattern)
-
- return patterns
-
-
-def match_patterns(patterns_table, element):
- matches = []
- for key, patterns in patterns_table.items():
- if key not in element:
- matches.append(False)
- else:
- value = element[key]
- for pattern in patterns:
- matches.append(True if pattern.match(value) else False)
-
- return matches
-
-
-def match_elements(elements, attributes=[], whitelist=[], blacklist=[]):
- whitelist_patterns = compile_filters(whitelist)
- blacklist_patterns = compile_filters(blacklist)
-
- # Apply attributes
- for element in elements:
- for attribute in attributes:
- name, value = attribute.split("=", 1)
- element[name] = value
-
- # Apply whitelist and blacklist
- matches = []
- for element in elements:
- if whitelist_patterns \
- and True not in match_patterns(whitelist_patterns, element):
- continue
- if blacklist_patterns \
- and True in match_patterns(blacklist_patterns, element):
- continue
-
- matches.append(element)
-
- return matches
-
-
-def parse_file(file, *args, **kwargs):
- template = Template()
- matches = match_elements(template.load_file(file), *args, **kwargs)
- template.dump_file(matches, sys.stdout)
-
-
-def parse_path(path, *args, **kwargs):
- for filename in path_expand_recursive(path):
- print("# %s" % filename)
-
- name = posixpath.basename(filename)
- if name.startswith(".") or name.endswith("~"):
- continue
-
- file = open(filename, "r")
- parse_file(file, *args, **kwargs)
-
-
-def parse_paths(paths, *args, **kwargs):
- for path in paths:
- parse_path(path, *args, **kwargs)
-
-
-def main(args):
- usage = "Usage: %prog [OPTIONS] [FILE...]"
- parser = OptionParser(usage=usage)
- parser.add_option("-a", "--attribute",
- action="append",
- type="string",
- default=[],
- help="Set additional attributes by name and value.")
- parser.add_option("-b", "--blacklist",
- action="append",
- type="string",
- default=[],
- help="Blacklist of elements by name and value.")
- parser.add_option("-w", "--whitelist",
- action="append",
- type="string",
- default=[],
- help="Whitelist of elements by name and value.")
- (options, args) = parser.parse_args(args)
-
- if args:
- parse_func = parse_paths
- else:
- parse_func = parse_file
- args = sys.stdin
-
- try:
- parse_func(args, options.attribute,
- options.whitelist, options.blacklist)
- except FilterError as error:
- parser.error(error.args[0])
-
- return 0
-
-
-if __name__ == "__main__":
- sys.exit(main(sys.argv[1:]))
diff --git a/bin/run_templates b/bin/run_templates
deleted file mode 100755
index 9e8bd2a..0000000
--- a/bin/run_templates
+++ /dev/null
@@ -1,144 +0,0 @@
-#!/usr/bin/env python3
-
-import os
-import re
-import sys
-import uuid
-
-from optparse import OptionParser
-from subprocess import Popen, PIPE
-
-from checkbox_support.lib.template import Template
-
-
-DEFAULT_INPUT = "-"
-DEFAULT_OUTPUT = "-"
-
-COMMAND_TEMPLATE = """cat <<%(separator)s
-%(input)s
-%(separator)s"""
-
-
-class Runner(object):
-
- def __init__(self, input, output):
- self.input = input
- self.output = output
-
- def get_args(self, record):
- return []
-
- def get_env(self, record):
- env = dict(os.environ)
- env["NF"] = str(len(record))
-
- return env
-
- def process(self, args, shell=False):
- process = Popen(
- args, shell=shell, stdout=PIPE, universal_newlines=True)
- records = self.process_output(process.stdout)
-
- for nr, record in enumerate(records):
- args = self.get_args(record)
- env = self.get_env(record)
- env["NR"] = str(nr)
-
- command_string = COMMAND_TEMPLATE % {
- "input": self.input,
- "separator": uuid.uuid4()}
- command = ["sh", "-c", command_string] + args
-
- process = Popen(command,
- env=env,
- stdout=self.output,
- universal_newlines=True)
- process.communicate()
-
- def process_output(self, output):
- raise NotImplementedError
-
-
-class LineRunner(Runner):
-
- field_separator = r"\s+"
- record_separator = r"(?:\r?\n)"
-
- def get_args(self, record):
- args = [record]
- args.extend(re.split(self.field_separator, record))
-
- return args
-
- def process_output(self, file):
- # Strip trailing separator
- data = re.sub(r"%s$" % self.record_separator, "", file.read())
-
- return re.split(self.record_separator, data)
-
-
-class TemplateRunner(Runner):
-
- def get_env(self, record):
- env = super(TemplateRunner, self).get_env(record)
- env.update(record)
-
- return env
-
- def process_output(self, output):
- template = Template()
- return template.load_file(output)
-
-
-def main(args):
- usage = "Usage: %prog [OPTIONS] [COMMAND]"
- parser = OptionParser(usage=usage)
- parser.add_option("-i", "--input",
- metavar="FILE",
- default=DEFAULT_INPUT,
- help="Input from the given file name, - for stdin")
- parser.add_option("-o", "--output",
- metavar="FILE",
- default=DEFAULT_OUTPUT,
- help="Output to the given file name, - for stdout")
- parser.add_option("-s", "--shell",
- action="store_true",
- help="Run the command as a shell script")
- parser.add_option("-t", "--template",
- action="store_true",
- help="Interpret the command output as a template")
- (options, args) = parser.parse_args(args)
-
- # Default args to echo command
- if not args:
- args = ["echo"]
-
- # Read input
- if options.input == "-":
- input = sys.stdin.read()
- else:
- input_file = open(options.input, "r")
- try:
- input = input_file.read()
- finally:
- input_file.close()
-
- # Open output
- if options.output == "-":
- output_file = sys.stdout
- else:
- output_file = open(options.output, "w")
-
- # Determine runner class
- if options.template:
- runner_class = TemplateRunner
- else:
- runner_class = LineRunner
-
- runner = runner_class(input, output_file)
- runner.process(args, options.shell)
-
- return 0
-
-if __name__ == "__main__":
- sys.exit(main(sys.argv[1:]))