File: dg

package info (click to toggle)
argparse-manpage 4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 536 kB
  • sloc: python: 2,755; makefile: 47; sh: 18
file content (196 lines) | stat: -rwxr-xr-x 4,455 bytes parent folder | download | duplicates (3)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/python

from __future__ import print_function

import sys
import tempfile
import shutil

from argparse import ArgumentParser, RawDescriptionHelpFormatter

def error(msg):
    print(msg, file=sys.stderr)

def die(msg):
    error(msg)
    sys.exit(1)

description = \
"""
Generate script using predefined metadata about distribution and
templates.

As an example of 'dg' usage, to generate _Dockerfile_ for Fedora
21 64-bit system, you may use command(s):

 $ cd project/directory
 $ dg --spec      docker-data.yaml      \\
      --template  docker.tpl            \\
      --distro    fedora-21-x86_64.yaml
"""

parser = ArgumentParser(
    description=description,
    formatter_class=RawDescriptionHelpFormatter,
)

parser.man_short_description = "templating system/generator for distributions"

parser.add_argument(
    '--projectdir',
    metavar='PROJECTDIR',
    type=str,
    help='Directory with project (defaults to CWD)',
    default="."
)

parser.add_argument(
    '--distro',
    metavar='DIST',
    type=str,
    help='Use distribution metadata specified by DIST yaml file',
    default="fedora-21-x86_64.yaml",
)

parser.add_argument(
    '--multispec',
    metavar='MULTISPEC',
    type=str,
    help='Use MULTISPEC yaml file to fill the TEMPLATE file',
)

parser.add_argument(
    '--multispec-selector',
    metavar='MULTISPEC_SELECTOR',
    type=str,
    help='Selectors for the multispec file',
    action='append',
    default=[],
)

parser.add_argument(
    '--spec',
    metavar='SPEC',
    type=str,
    help='Use SPEC yaml file to fill the TEMPLATE file',
    action='append',
)

parser.add_argument(
    '--output',
    metavar='OUTPUT',
    type=str,
    help='Write result to OUTPUT file instead of stdout',
)

parser.add_argument(
    '--macros-from',
    metavar='PROJECTDIR',
    type=str,
    action='append',
    help='Load variables from PROJECTDIR',
)

parser.add_argument(
    '--container',
    metavar='CONTAINER_TYPE',
    type=str,
    help='Container type, e.g. \'docker\'',
    default=False,
)

parser.add_argument(
    '--macro',
    metavar='MACRO',
    type=str,
    action='append',
    help='Define distgen\'s macro',
)

parser.add_argument(
    '--max-passes',
    metavar='PASSES',
    type=int,
    default=1,
    help='Maximum number of rendering passes, defaults to 1 (== no re-rendering)',
)

tpl_or_combinations = parser.add_mutually_exclusive_group(required=True)

tpl_or_combinations.add_argument(
    '--template',
    metavar='TEMPLATE',
    type=str,
    help='Use TEMPLATE file, e.g. docker.tpl or a template string, '
    'e.g. "{{ config.docker.from }}"'
)

tpl_or_combinations.add_argument(
    '--multispec-combinations',
    action='store_true',
    help='Print available multispec combinations',
)

def print_multispec_combinations(args):
    ms = Multispec.from_path(args.projectdir, args.multispec)
    for c in ms.get_all_combinations():
        to_print = ['--distro {0}'.format(c.pop('distro'))]
        [to_print.append('--multispec-selector {0}={1}'.format(k, v)) for k, v in c.items()]
        print(' '.join(to_print))


def render_template(args):
    temp_filename = False
    output = sys.stdout
    try:
        if args.output:
            _, temp_filename = tempfile.mkstemp(prefix="distgen-")
            output = open(temp_filename, 'w')
    except:
        die("can't create temporary file for '{0}'".format(args.output))

    cmd_cfg = CommandsConfig()
    cmd_cfg.container = args.container

    explicit_macros = {}
    if args.macro:
        for i in args.macro:
            key, value = i.split(' ', 1)
            explicit_macros[key] = value

    if args.template == '-':
        args.template = "/proc/self/fd/0"

    generator = Generator()
    generator.load_project(args.projectdir)
    generator.render(
        args.spec,
        args.multispec,
        args.multispec_selector,
        args.template,
        args.distro,
        cmd_cfg,
        output,
        args.macros_from,
        explicit_macros,
        args.max_passes,
    )

    if temp_filename:
        try:
            output.close()
            shutil.move(temp_filename, args.output)
        except:
            die("can't move '{0}' into '{1}'".format(temp_filename, args.output))


def main():
    args = parser.parse_args()
    if args.multispec_combinations:
        print_multispec_combinations(args)
    else:
        render_template(args)


if __name__ == "__main__":
    main()