3 # Copyright (C) 2012 GRNET S.A.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 """Decode a json encoded string with properties
22 This program decodes a json encoded properties string and outputs it in a
23 bash sourcable way. The properties are passed to the program through a JSON
24 string either read from a file or from standard input and are outputed to a
34 from StringIO import StringIO
38 options = sys.argv[1:]
40 prefix = ''.join(random.choice(string.ascii_uppercase) for x in range(8))
42 config = json.load(sys.stdin)
44 for key, value in config.items():
45 if str(key).upper() in options:
46 os.environ[prefix + str(key).upper()] = value
48 p = subprocess.Popen(['bash', '-c', 'set'], stdout=subprocess.PIPE)
49 output = StringIO(p.communicate()[0])
50 for line in iter(output):
51 if line.startswith(prefix):
52 print line[len(prefix):]
57 if __name__ == "__main__":
60 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :