Initial move of code from ATT to LF repo
[yaml_builds.git] / scripts / jcopy3.py
diff --git a/scripts/jcopy3.py b/scripts/jcopy3.py
new file mode 100755 (executable)
index 0000000..4ce9249
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/local/bin/python\r
+##############################################################################
+# Copyright © 2018 AT&T Intellectual Property. All rights reserved.          #
+#                                                                            #
+# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
+# not use this file except in compliance with the License.                   #
+#                                                                            #
+# You may obtain a copy of the License at                                    #
+#       http://www.apache.org/licenses/LICENSE-2.0                           #
+#                                                                            #
+# Unless required by applicable law or agreed to in writing, software        #
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT  #
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.           #
+# See the License for the specific language governing permissions and        #
+# limitations under the License.                                             #
+##############################################################################
+
+#\r
+#  jcopy.py - Copy a file or files to a target directory, making\r
+#    substitutions as needed from the values contained in a YAML file.\r
+#\r
+#  usage: jcopy.py <yaml> <in_dir_or_file> <out_dir>\r
+#\r
+#  Note: jcopy.sh is for Python2, jcopy3.sh is for Python3\r
+#\r
+\r
+import os.path\r
+import jinja2\r
+import sys\r
+import yaml\r
+\r
+def expand_files(target_dir, dir_name, files):\r
+       global total\r
+       xlen = len(sys.argv[2])\r
+       targdir = target_dir + dir_name[xlen:]\r
+       if not os.path.exists(targdir):\r
+               os.makedirs(targdir)\r
+       env = jinja2.Environment()\r
+       for f in files:\r
+               source_path = dir_name + '/' + f\r
+               target_path = targdir + '/' + f\r
+               if os.path.isfile(source_path):\r
+                       with open(source_path) as fd:\r
+                               template = env.from_string(fd.read())\r
+                       data = template.render(yaml=yaml)\r
+                       fd2 = open(target_path,'w')\r
+                       fd2.write(data)\r
+                       fd2.close()\r
+                       total += 1\r
+\r
+def expand_file(target_dir, file):\r
+       global total\r
+       if not os.path.exists(target_dir):\r
+               os.makedirs(target_dir)\r
+       env = jinja2.Environment()\r
+       with open(file) as fd:\r
+               template = env.from_string(fd.read())\r
+       data = template.render(yaml=yaml)\r
+       target_path = target_dir + '/' + os.path.basename(file)\r
+       fd2 = open(target_path,'w')\r
+       fd2.write(data)\r
+       fd2.close()\r
+       total += 1\r
+\r
+if len(sys.argv) != 4:\r
+       print('usage: jcopy.py <yaml> <in_dir_or_file> <out_dir>')\r
+       sys.exit(1)\r
+\r
+with open(sys.argv[1]) as f:\r
+       yaml = yaml.safe_load(f)\r
+\r
+total = 0\r
+if os.path.isfile(sys.argv[2]):\r
+       expand_file(sys.argv[3], os.path.abspath(sys.argv[2]))\r
+else:\r
+       for root, dirs, files in os.walk(sys.argv[2]):\r
+               expand_files(sys.argv[3], root, files)\r
+print('%d files processed.' % total)\r
+sys.exit(0)\r