3 """Decode a json encoded string with properties
5 This program decodes a json encoded properties string and outputs it in a
6 bash sourcable way. The properties are passed to the program through a JSON
7 string either read from a file or from standard input and are outputed to a
15 from StringIO import StringIO
16 from optparse import OptionParser
18 def parse_arguments(input_args):
19 usage = "Usage: %prog [options] <output_file>"
20 parser = OptionParser(usage=usage)
21 parser.add_option("-i", "--input",
22 action="store",type='string', dest="input_file",
23 help="get input from FILE instead of stdin",
26 opts, args = parser.parse_args(input_args)
29 parser.error('output file is missing')
32 if opts.input_file is not None:
33 if not os.path.isfile(opts.input_file):
34 parser.error('input file does not exist')
36 return (opts.input_file, output_file)
40 (input_file, output_file) = parse_arguments(sys.argv[1:])
42 infh = sys.stdin if input_file is None else open(input_file, 'r')
43 outfh = open(output_file, 'w')
45 properties = json.load(infh)
46 for key, value in properties.items():
47 os.environ['SNF_IMAGE_PROPERTY_' + key] = value
49 output = StringIO(subprocess.check_output(['bash', '-c', 'set']))
50 for line in iter(output):
51 if line.startswith('SNF_IMAGE_PROPERTY_'):
59 if __name__ == "__main__":
62 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :