d041c26bbfb0edf146caf97861c7827f3ad103c0
[yaml_builds.git] / scripts / jcopy.py
1 #!/usr/bin/python
2 ##############################################################################
3 # Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.        #
4 #                                                                            #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may    #
6 # not use this file except in compliance with the License.                   #
7 #                                                                            #
8 # You may obtain a copy of the License at                                    #
9 #       http://www.apache.org/licenses/LICENSE-2.0                           #
10 #                                                                            #
11 # Unless required by applicable law or agreed to in writing, software        #
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT  #
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.           #
14 # See the License for the specific language governing permissions and        #
15 # limitations under the License.                                             #
16 ##############################################################################
17
18 #  jcopy.py - Copy a file or files to a target directory, making
19 #    substitutions as needed from the values contained in a YAML file.
20 #    compatibile with python and python3
21 #
22 #  usage: jcopy.py <yaml_input> <template_dir_or_file> <out_dir>
23
24 import os.path
25 import jinja2
26 import sys
27 import yaml
28
29 def usage(msg=None):
30   if not msg is None:
31     print(msg)
32   print( 'usage: jcopy.py <yaml> <template_dir_or_file> <out_dir>' )
33   sys.exit(1)
34
35 if len(sys.argv) != 4:
36   usage('incorrect number of arguments')
37
38 yaml_input=sys.argv[1]
39 j2in_name=sys.argv[2]
40 yaml_outdir=os.path.dirname(sys.argv[3]+'/')
41
42 if not os.path.isfile(yaml_input):
43   usage('input yaml file {} does not exist'.format(yaml_input))
44
45 with open(yaml_input) as f:
46   siteyaml = yaml.safe_load(f)
47
48 if os.path.isfile(j2in_name):
49   j2_env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(j2in_name)), trim_blocks=True, lstrip_blocks=True, undefined=jinja2.make_logging_undefined() )
50   templates=[ os.path.basename(j2in_name) ]
51 else:
52   j2_env = jinja2.Environment(loader=jinja2.FileSystemLoader(j2in_name), trim_blocks=True, lstrip_blocks=True, undefined=jinja2.make_logging_undefined() )
53   templates=j2_env.list_templates(extensions=('j2'))
54
55 # find length of longest template name to pad output
56 fill=len(max(templates,key=len))
57
58 for f in templates:
59   t=j2_env.get_template(name=f)
60   fname=yaml_outdir+'/'+f.replace('.j2','.yaml')
61   print( '### {0: <{fill}} ==> {1}'.format(f, fname, fill=fill) )
62   if not os.path.exists(os.path.dirname(fname)):
63     os.makedirs(os.path.dirname(fname))
64   t.stream(yaml=siteyaml).dump(fname)
65
66 print('%d files processed.' % len(templates))
67 sys.exit(0)
68
69 # sudo apt-get install python-jinja2 python-yaml
70 # sudo apt-get install python3-jinja2 python3-yaml
71