/usr/lib/2013.com.canonical.certification:checkbox/bin/run_templates is in plainbox-provider-checkbox 0.4-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #!/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:]))
|