OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2008-2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2008-2010 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Generate index.html files for a Google Storage for Developers directory. | 6 """Generate index.html files for a Google Storage for Developers directory. |
7 | 7 |
8 Google Storage for Developers provides only a raw set of objects. | 8 Google Storage for Developers provides only a raw set of objects. |
9 For some buckets we would like to be able to support browsing of the directory | 9 For some buckets we would like to be able to support browsing of the directory |
10 tree. This utility will generate the needed index and upload/update it. | 10 tree. This utility will generate the needed index and upload/update it. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 sz = str(int(sz / 100000000) / 10.0) + 'G' | 44 sz = str(int(sz / 100000000) / 10.0) + 'G' |
45 return sz | 45 return sz |
46 | 46 |
47 | 47 |
48 def GetPathInfo(path, options): | 48 def GetPathInfo(path, options): |
49 """Collect size, date, md5 for a give gsd path.""" | 49 """Collect size, date, md5 for a give gsd path.""" |
50 # Check current state. | 50 # Check current state. |
51 cmd = [options.gsutil, 'ls', '-l', path] | 51 cmd = [options.gsutil, 'ls', '-l', path] |
52 p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | 52 p = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
53 p_stdout, _ = p.communicate() | 53 p_stdout, _ = p.communicate() |
| 54 assert p.returncode == 0 |
54 # Extract intersting fields. | 55 # Extract intersting fields. |
55 fields = {} | 56 fields = {} |
56 fields['size'] = FixupSize(re.search('\tObject size:\t([0-9]+)\n', | 57 fields['size'] = FixupSize(re.search('\tObject size:\t([0-9]+)\n', |
57 p_stdout).group(1)) | 58 p_stdout).group(1)) |
58 fields['md5'] = re.search('\tMD5:\t([^\n]+)\n', p_stdout).group(1) | 59 fields['md5'] = re.search('\tMD5:\t([^\n]+)\n', p_stdout).group(1) |
59 fields['date'] = re.search('\tLast mod:\t([^\n]+)\n', p_stdout).group(1) | 60 fields['date'] = re.search('\tLast mod:\t([^\n]+)\n', p_stdout).group(1) |
60 return fields | 61 return fields |
61 | 62 |
62 | 63 |
63 def GenerateIndex(path, children, directories, options): | 64 def GenerateIndex(path, children, directories, options): |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 index += '<td>%s</td>' % fields['date'] | 104 index += '<td>%s</td>' % fields['date'] |
104 index += '<td><b>%s</b></td>' % fields['size'] | 105 index += '<td><b>%s</b></td>' % fields['size'] |
105 index += '<td>%s</td>' % fields['md5'] | 106 index += '<td>%s</td>' % fields['md5'] |
106 index += '</tr>' | 107 index += '</tr>' |
107 index += '<tr><th colspan="4"><hr></th></tr>' | 108 index += '<tr><th colspan="4"><hr></th></tr>' |
108 index += '</table>' | 109 index += '</table>' |
109 index += '</body>' | 110 index += '</body>' |
110 index += '</html>' | 111 index += '</html>' |
111 # Check current state. | 112 # Check current state. |
112 cmd = [options.gsutil, 'cat', posixpath.join(path, GENERATED_INDEX)] | 113 cmd = [options.gsutil, 'cat', posixpath.join(path, GENERATED_INDEX)] |
113 p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | 114 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
114 p_stdout, _ = p.communicate() | 115 p_stdout, _ = p.communicate() |
115 # Done if it's alrady right. | 116 # Done if it's alrady right (and the cat worked). |
116 if p_stdout == index and not options.force: | 117 if p.returncode == 0 and p_stdout == index and not options.force: |
117 print '%s -- skipping, up to date' % path | 118 print '%s -- skipping, up to date' % path |
118 return | 119 return |
119 # Write to a file. | 120 # Write to a file. |
120 f = tempfile.NamedTemporaryFile(suffix='.html') | 121 f = tempfile.NamedTemporaryFile(suffix='.html') |
121 filename = f.name | 122 filename = f.name |
122 f.write(index) | 123 f.write(index) |
123 f.flush() | 124 f.flush() |
124 # Upload index. | 125 # Upload index. |
125 cmd = [options.gsutil, 'cp'] | 126 cmd = [options.gsutil, 'cp'] |
126 if options.acl: | |
127 cmd += ['-a', options.acl] | |
128 cmd += [filename, posixpath.join(path, GENERATED_INDEX)] | 127 cmd += [filename, posixpath.join(path, GENERATED_INDEX)] |
129 p = subprocess.Popen(cmd) | 128 p = subprocess.Popen(cmd) |
130 p.communicate() | 129 p.communicate() |
| 130 assert p.returncode == 0 |
| 131 # Optionally update acl. |
| 132 if options.acl: |
| 133 cmd = [options.gsutil, 'setacl', options.acl] |
| 134 cmd += [posixpath.join(path, GENERATED_INDEX)] |
| 135 p = subprocess.Popen(cmd) |
| 136 p.communicate() |
| 137 assert p.returncode == 0 |
131 print '%s -- updated index' % path | 138 print '%s -- updated index' % path |
132 | 139 |
133 | 140 |
134 def GenerateIndexes(path, options): | 141 def GenerateIndexes(path, options): |
135 """Generate all relevant indexes for a given gsd path.""" | 142 """Generate all relevant indexes for a given gsd path.""" |
136 # Get a list of objects under this prefix. | 143 # Get a list of objects under this prefix. |
137 cmd = [options.gsutil, 'ls', posixpath.join(path, '*')] | 144 cmd = [options.gsutil, 'ls', posixpath.join(path, '*')] |
138 p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | 145 p = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
139 p_stdout, _ = p.communicate() | 146 p_stdout, _ = p.communicate() |
| 147 assert p.returncode == 0 |
140 objects = str(p_stdout).splitlines() | 148 objects = str(p_stdout).splitlines() |
141 objects = [o for o in objects if posixpath.basename(o) != GENERATED_INDEX] | 149 objects = [o for o in objects if posixpath.basename(o) != GENERATED_INDEX] |
142 # Find common prefixes. | 150 # Find common prefixes. |
143 directories = set() | 151 directories = set() |
144 for o in objects: | 152 for o in objects: |
145 part = posixpath.dirname(o) | 153 part = posixpath.dirname(o) |
146 while part.startswith(path): | 154 while part.startswith(path): |
147 directories.add(part) | 155 directories.add(part) |
148 part = posixpath.dirname(part) | 156 part = posixpath.dirname(part) |
149 objects += list(directories) | 157 objects += list(directories) |
(...skipping 20 matching lines...) Expand all Loading... |
170 dest='gsutil', help='path to gsutil') | 178 dest='gsutil', help='path to gsutil') |
171 options, args = parser.parse_args(argv) | 179 options, args = parser.parse_args(argv) |
172 if len(args) != 2 or not args[1].startswith('gs://'): | 180 if len(args) != 2 or not args[1].startswith('gs://'): |
173 parser.print_help() | 181 parser.print_help() |
174 return 1 | 182 return 1 |
175 return GenerateIndexes(args[1], options) | 183 return GenerateIndexes(args[1], options) |
176 | 184 |
177 | 185 |
178 if __name__ == '__main__': | 186 if __name__ == '__main__': |
179 sys.exit(main(sys.argv)) | 187 sys.exit(main(sys.argv)) |
OLD | NEW |