-
Notifications
You must be signed in to change notification settings - Fork 37
/
generate-samples
executable file
·73 lines (54 loc) · 2.33 KB
/
generate-samples
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
#!/usr/bin/env python
from html.parser import HTMLParser
from os.path import basename, splitext
import subprocess
from urllib.request import urlopen
from urllib.parse import urlparse
OUTPUT_FOLDER = 'samples'
REGION = 'eu-west-1'
sample_templates_services = f'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/sample-templates-services-{REGION}.html'
sample_templates_appframeworks = f'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/sample-templates-appframeworks-{REGION}.html'
sample_templates_applications = f'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/sample-templates-applications-{REGION}.html'
sample_templates_resources = f'https://aws.amazon.com/cloudformation/resources/templates/{REGION}/'
EXAMPLES_URLS = [
sample_templates_services, sample_templates_appframeworks,
sample_templates_applications, sample_templates_resources]
#
# Some dot failure with the following templates:
# https://s3-eu-west-1.amazonaws.com/cloudformation-templates-eu-west-1/EC2InstanceWithEBSVolumeConditionalIOPs.template
# https://s3-eu-west-1.amazonaws.com/cloudformation-templates-eu-west-1/Parameter_Validate.template
# Known bug in dot:
# https://gitlab.com/graphviz/graphviz/issues/1213
def main():
parser = LinkParser()
for examples_url in EXAMPLES_URLS:
parser.feed(read_url(examples_url))
for template in filter(is_template, parser.links):
render(template)
def is_template(url):
return urlparse(url).path.endswith('.template')
def render(url):
template = urlopen(url).read()
p = subprocess.Popen(
'cfviz | dot -Tsvg -o %s' % output_for(url), shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, _ = p.communicate(input=template)
if output:
print(url)
print(output)
def output_for(url):
name, _ = splitext(basename(urlparse(url).path))
return f'{OUTPUT_FOLDER}/%s.svg' % name
class LinkParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.links = []
def handle_starttag(self, tag, attrs):
if tag == 'a':
for attr, value in attrs:
if attr == 'href':
self.links.append(value)
def read_url(url):
return str(urlopen(url).read())
if __name__ == '__main__':
main()