fix paste err on legal notice
[yaml_builds.git] / scripts / jcopy3.py
1 #!/usr/local/bin/python\r
2 ##############################################################################
3 # Copyright © 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 #\r
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
21 #\r
22 #  usage: jcopy.py <yaml> <in_dir_or_file> <out_dir>\r
23 #\r
24 #  Note: jcopy.sh is for Python2, jcopy3.sh is for Python3\r
25 #\r
26 \r
27 import os.path\r
28 import jinja2\r
29 import sys\r
30 import yaml\r
31 \r
32 def expand_files(target_dir, dir_name, files):\r
33         global total\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
39         for f in files:\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
47                         fd2.write(data)\r
48                         fd2.close()\r
49                         total += 1\r
50 \r
51 def expand_file(target_dir, file):\r
52         global total\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
61         fd2.write(data)\r
62         fd2.close()\r
63         total += 1\r
64 \r
65 if len(sys.argv) != 4:\r
66         print('usage: jcopy.py <yaml> <in_dir_or_file> <out_dir>')\r
67         sys.exit(1)\r
68 \r
69 with open(sys.argv[1]) as f:\r
70         yaml = yaml.safe_load(f)\r
71 \r
72 total = 0\r
73 if os.path.isfile(sys.argv[2]):\r
74         expand_file(sys.argv[3], os.path.abspath(sys.argv[2]))\r
75 else:\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
79 sys.exit(0)\r