summaryrefslogtreecommitdiff
path: root/tools/fetch-cloudformation-examples
blob: 4650dd2acaa7886d524ae69c723460665c72f739 (plain)
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
#!/usr/bin/env python

import httplib
import os
import sys
import shutil
import xml.etree.ElementTree as xml

basepath = os.path.abspath(os.path.join(sys.argv[0],
    os.path.pardir,
    os.path.pardir,
    'templates',
    'cloudformation-examples'))

bucket = 'cloudformation-templates-us-east-1'

def main():
    conn = httplib.HTTPConnection('s3.amazonaws.com')
    conn.request('GET', '/%s/' % bucket)
    resp = conn.getresponse()

    tree = xml.parse(resp)
    rootElement = tree.getroot()

    if os.path.exists(basepath):
        print 'Deleting %s' % basepath
        shutil.rmtree(basepath)

    os.makedirs(basepath)
    print 'Creating %s' % basepath

    for entry in rootElement.iter('{http://s3.amazonaws.com/doc/2006-03-01/}Key'):
        key = entry.text
        if key.endswith('.html'):
            continue
        filename = os.path.join(basepath, key)

        print 'Writing to %s' % filename
        conn.request('GET', '/%s/%s' % (bucket, key))
        resp = conn.getresponse()
        contents = resp.read()

        f = open(filename, 'w')
        f.write(contents)
        f.close()

if __name__ == '__main__':
    main()