1 #!/usr/local/bin/python
\r
2 ##############################################################################
3 # Copyright © 2018 AT&T Intellectual Property. All rights reserved. #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may #
6 # not use this file except in compliance with the License. #
8 # You may obtain a copy of the License at #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 ##############################################################################
19 # jcopy.py - Copy a file or files to a target directory, making
\r
20 # substitutions as needed from the values contained in a YAML file.
\r
22 # usage: jcopy.py <yaml> <in_dir_or_file> <out_dir>
\r
24 # Note: jcopy.sh is for Python2, jcopy3.sh is for Python3
\r
32 def expand_files(target_dir, dir_name, files):
\r
34 xlen = len(sys.argv[2])
\r
35 targdir = target_dir + dir_name[xlen:]
\r
36 if not os.path.exists(targdir):
\r
37 os.makedirs(targdir)
\r
38 env = jinja2.Environment()
\r
40 source_path = dir_name + '/' + f
\r
41 target_path = targdir + '/' + f
\r
42 if os.path.isfile(source_path):
\r
43 with open(source_path) as fd:
\r
44 template = env.from_string(fd.read())
\r
45 data = template.render(yaml=yaml)
\r
46 fd2 = open(target_path,'w')
\r
51 def expand_file(target_dir, file):
\r
53 if not os.path.exists(target_dir):
\r
54 os.makedirs(target_dir)
\r
55 env = jinja2.Environment()
\r
56 with open(file) as fd:
\r
57 template = env.from_string(fd.read())
\r
58 data = template.render(yaml=yaml)
\r
59 target_path = target_dir + '/' + os.path.basename(file)
\r
60 fd2 = open(target_path,'w')
\r
65 if len(sys.argv) != 4:
\r
66 print('usage: jcopy.py <yaml> <in_dir_or_file> <out_dir>')
\r
69 with open(sys.argv[1]) as f:
\r
70 yaml = yaml.safe_load(f)
\r
73 if os.path.isfile(sys.argv[2]):
\r
74 expand_file(sys.argv[3], os.path.abspath(sys.argv[2]))
\r
76 for root, dirs, files in os.walk(sys.argv[2]):
\r
77 expand_files(sys.argv[3], root, files)
\r
78 print('%d files processed.' % total)
\r