Revision d984217b

b/snf-image-helper/inject-files.py
1 1
#!/usr/bin/env python
2

  
3
# Copyright (C) 2011 GRNET S.A. 
2
#
3
# Copyright (C) 2011 GRNET S.A.
4 4
#
5 5
# This program is free software; you can redistribute it and/or modify
6 6
# it under the terms of the GNU General Public License as published by
......
30 30
import json
31 31
import datetime
32 32
import base64
33
import struct
34 33
from optparse import OptionParser
35 34

  
36 35

  
......
39 38
    current_time = now.strftime("%Y%m%d.%H%M%S")
40 39
    return current_time
41 40

  
41

  
42 42
def parse_arguments(input_args):
43 43
    usage = "Usage: %prog [options] <target>"
44 44
    parser = OptionParser(usage=usage)
45 45
    parser.add_option("-i", "--input",
46
                        action="store",type='string', dest="input_file",
46
                        action="store", type='string', dest="input_file",
47 47
                        help="get input from FILE instead of stdin",
48 48
                        metavar="FILE")
49 49
    parser.add_option("-d", "--decode",
......
54 54

  
55 55
    if len(args) != 1:
56 56
        parser.error('target is missing')
57
   
57

  
58 58
    target = args[0]
59 59
    if not os.path.isdir(target):
60 60
        parser.error('target is not a directory')
......
65 65
    else:
66 66
        if not os.path.isfile(input_file):
67 67
            parser.error('input file does not exist')
68
        input_file = open(input_file,'r')
69
        
68
        input_file = open(input_file, 'r')
69

  
70 70
    return (input_file, target, opts.decode)
71 71

  
72 72

  
......
74 74
    (input_file, target, decode) = parse_arguments(sys.argv[1:])
75 75

  
76 76
    files = json.load(input_file)
77
    
77

  
78 78
    if decode:
79 79
        manifest = open(target + '/manifest', 'w')
80
    
80

  
81 81
    count = 0
82 82
    for f in files:
83 83
        count += 1
84 84
        owner = f['owner'] if 'owner' in f else "root"
85 85
        group = f['group'] if 'group' in f else "root"
86
        mode = f['mode'] if 'mode' in f else 288 # 440 in oct = 288 in dec
86
        mode = f['mode'] if 'mode' in f else 0440
87 87

  
88 88
        filepath = f['path'] if not decode else str(count)
89 89
        filepath = target + "/" + filepath
......
99 99
        newfile = open(filepath, 'w')
100 100
        newfile.write(base64.b64decode(f['contents']))
101 101
        newfile.close()
102
        
102

  
103 103
        if decode:
104
            manifest.write(str(count))
105
            manifest.write(struct.pack('B', 0))
106
            manifest.write(owner)
107
            manifest.write(struct.pack('B', 0))
108
            manifest.write(group)
109
            manifest.write(struct.pack('B', 0))
110
            manifest.write("%o" % mode)
111
            manifest.write(struct.pack('B', 0))
112
            manifest.write(f['path'])
113
            manifest.write(struct.pack('B', 0))
114
 
104
            manifest.write("%s\x00%s\x00%s\x00%o\x00%s\x00" %
105
                           (count, owner, group, mode, f['path']))
106

  
115 107
    sys.stderr.write('Files were injected successfully\n')
116 108

  
117 109
    if decode:

Also available in: Unified diff