3 """Inject files into a directory
5 This program injects files into a target directory.
6 The files are passed to the program through a JSON string either read from a
7 file or from standard input.
16 from optparse import OptionParser
20 now = datetime.datetime.now()
21 current_time = now.strftime("%Y%m%d.%H%M%S")
24 def parse_arguments(input_args):
25 usage = "Usage: %prog [options] <target>"
26 parser = OptionParser(usage=usage)
27 parser.add_option("-i", "--input",
28 action="store",type='string', dest="input_file",
29 help="get input from FILE instead of stdin",
32 opts, args = parser.parse_args(input_args)
35 parser.error('target is missing')
38 if not os.path.isdir(target):
39 parser.error('target is not a directory')
41 input_file = opts.input_file
42 if input_file is None:
43 input_file = sys.stdin
45 if not os.path.isfile(input_file):
46 parser.error('input file does not exist')
47 input_file = open(input_file,'r')
49 return (input_file, target)
53 (input_file, target) = parse_arguments(sys.argv[1:])
55 files = json.loads(input_file.read())
57 real_path = target + '/' + f['path']
58 if os.path.lexists(real_path):
59 backup_file = real_path + '.bak.' + timestamp()
60 os.rename(real_path, backup_file)
62 parentdir = os.path.dirname(real_path)
63 if not os.path.exists(parentdir):
64 os.makedirs(parentdir)
66 newfile = open(real_path, 'w')
67 newfile.write(base64.b64decode(f['contents']))
69 os.chmod(real_path, 0440)
70 sys.stderr.write('Successful personalization of Image\n')
76 if __name__ == "__main__":
79 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :