1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Generate commissioning user-data from template and code snippets.
6
This combines the `user_data.template` and the snippets of code in the
7
`snippets` directory into the main commissioning script.
9
Its contents are not customizable. To inject custom code, use the
10
:class:`CommissioningScript` model.
13
from __future__ import (
24
from os import listdir
30
def read_snippet(snippets_dir, name, encoding='utf-8'):
31
"""Read a snippet file.
35
path = os.path.join(snippets_dir, name)
36
with open(path, 'rb') as snippet_file:
37
return snippet_file.read().decode(encoding)
40
def is_snippet(filename):
41
"""Does `filename` represent a valid snippet name?"""
43
not filename.startswith('.'),
44
filename != '__init__.py',
45
not filename.endswith('.pyc'),
46
not filename.endswith('~'),
50
def list_snippets(snippets_dir):
51
"""List names of available snippets."""
52
return filter(is_snippet, listdir(snippets_dir))
55
def strip_name(snippet_name):
56
"""Canonicalize a snippet name."""
57
# Dot suffixes do not work well in tempita variable names.
58
return snippet_name.replace('.', '_')
61
def generate_user_data():
62
"""Produce the main commissioning script.
64
The script was templated so that code snippets become easier to
65
maintain, check for lint, and ideally, unit-test. However its
66
contents are static: there are no variables. It's perfectly
72
commissioning_dir = os.path.dirname(__file__)
73
template_file = os.path.join(commissioning_dir, 'user_data.template')
74
snippets_dir = os.path.join(commissioning_dir, 'snippets')
75
template = tempita.Template.from_filename(template_file, encoding=encoding)
78
strip_name(name): read_snippet(snippets_dir, name, encoding=encoding)
79
for name in list_snippets(snippets_dir)
81
return template.substitute(snippets).encode('utf-8')