inject-files.py 2.04 KB
Newer Older
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
#!/usr/bin/env python

"""Inject files into a directory

This program injects files into a target directory.
The files are passed to the program through a JSON string either read from a
file or from standard input.

"""

import sys
import os
import json
import datetime
import base64
from optparse import OptionParser


def timestamp():
    now = datetime.datetime.now()
    current_time = now.strftime("%Y%m%d.%H%M%S")
    return current_time

def parse_arguments(input_args):
    usage = "Usage: %prog [options] <target>"
    parser = OptionParser(usage=usage)
    parser.add_option("-i", "--input",
                        action="store",type='string', dest="input_file",
                        help="get input from FILE instead of stdin",
                        metavar="FILE")

    opts, args = parser.parse_args(input_args)

    if len(args) != 1:
        parser.error('target is missing')
   
    target = args[0]
    if not os.path.isdir(target):
        parser.error('target is not a directory')

    input_file = opts.input_file
    if input_file is None:
        input_file = sys.stdin
    else:
        if not os.path.isfile(input_file):
            parser.error('input file does not exist')
        input_file = open(input_file,'r')
        
    return (input_file, target)


def main():
    (input_file, target) = parse_arguments(sys.argv[1:])

    files = json.loads(input_file.read())
    for f in files:
        real_path = target + '/' + f['path']
        if os.path.lexists(real_path):
            backup_file = real_path + '.bak.' + timestamp()
            os.rename(real_path, backup_file)

        parentdir = os.path.dirname(real_path)
        if not os.path.exists(parentdir):
            os.makedirs(parentdir)

        newfile = open(real_path, 'w')
        newfile.write(base64.b64decode(f['contents']))
        newfile.close()
        os.chmod(real_path, 0440)
    sys.stderr.write('Successful personalization of Image\n')

    input_file.close()
    return 0


if __name__ == "__main__":
    sys.exit(main())

# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :