From 24a504f03a3d5edfe8957dcfaa4bde98ae0e60ec Mon Sep 17 00:00:00 2001
From: Kenny Woodson <kwoodson@redhat.com>
Date: Tue, 17 Jan 2017 15:31:42 -0500
Subject: Adding --verfiy to generate script.

---
 roles/lib_openshift/src/doc/generated    | 10 +++++
 roles/lib_openshift/src/generate.py      | 67 ++++++++++++++++++++-----------
 roles/lib_openshift/src/sources.yml      |  2 +
 roles/lib_utils/src/doc/generated        |  9 +++++
 roles/lib_utils/src/generate.py          | 68 +++++++++++++++++++++-----------
 roles/lib_utils/src/generate_sources.yml |  7 ----
 roles/lib_utils/src/sources.yml          |  8 ++++
 7 files changed, 118 insertions(+), 53 deletions(-)
 create mode 100644 roles/lib_openshift/src/doc/generated
 create mode 100644 roles/lib_utils/src/doc/generated
 delete mode 100644 roles/lib_utils/src/generate_sources.yml
 create mode 100644 roles/lib_utils/src/sources.yml

diff --git a/roles/lib_openshift/src/doc/generated b/roles/lib_openshift/src/doc/generated
new file mode 100644
index 000000000..b55d18cff
--- /dev/null
+++ b/roles/lib_openshift/src/doc/generated
@@ -0,0 +1,10 @@
+#!/usr/bin/env python
+# pylint: disable=missing-docstring
+# flake8: noqa: T001
+#     ___ ___ _  _ ___ ___    _ _____ ___ ___
+#    / __| __| \| | __| _ \  /_\_   _| __|   \
+#   | (_ | _|| .` | _||   / / _ \| | | _|| |) |
+#    \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____
+#   |   \ / _ \  | \| |/ _ \_   _| | __|   \_ _|_   _|
+#   | |) | (_) | | .` | (_) || |   | _|| |) | |  | |
+#   |___/ \___/  |_|\_|\___/ |_|   |___|___/___| |_|
diff --git a/roles/lib_openshift/src/generate.py b/roles/lib_openshift/src/generate.py
index 003136833..8451d99ab 100755
--- a/roles/lib_openshift/src/generate.py
+++ b/roles/lib_openshift/src/generate.py
@@ -3,43 +3,64 @@
   Generate the openshift-ansible/roles/lib_openshift_cli/library/ modules.
 '''
 
+import argparse
 import os
 import yaml
-
-# pylint: disable=anomalous-backslash-in-string
-GEN_STR = "#!/usr/bin/env python\n" + \
-          "# pylint: disable=missing-docstring\n" + \
-          "# flake8: noqa: T001\n" + \
-          "#     ___ ___ _  _ ___ ___    _ _____ ___ ___\n" + \
-          "#    / __| __| \| | __| _ \  /_\_   _| __|   \\\n" + \
-          "#   | (_ | _|| .` | _||   / / _ \| | | _|| |) |\n" + \
-          "#    \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____\n" + \
-          "#   |   \ / _ \  | \| |/ _ \_   _| | __|   \_ _|_   _|\n" + \
-          "#   | |) | (_) | | .` | (_) || |   | _|| |) | |  | |\n" + \
-          "#   |___/ \___/  |_|\_|\___/ |_|   |___|___/___| |_|\n"
+import six
 
 OPENSHIFT_ANSIBLE_PATH = os.path.dirname(os.path.realpath(__file__))
 OPENSHIFT_ANSIBLE_SOURCES_PATH = os.path.join(OPENSHIFT_ANSIBLE_PATH, 'sources.yml')  # noqa: E501
 
 
+class GenerateAnsibleException(Exception):
+    '''General Exception for generate function'''
+    pass
+
+
+def parse_args():
+    '''parse arguments to generate'''
+    parser = argparse.ArgumentParser(description="Generate ansible modules.")
+    parser.add_argument('--verify', action='store_true', default=False,
+                        help='Verify library code matches the generated code.')
+
+    return parser.parse_args()
+
+
+def generate(parts):
+    '''generate the source code for the ansible modules'''
+
+    data = six.StringIO()
+    for fpart in parts:
+        # first line is pylint disable so skip it
+        with open(os.path.join(OPENSHIFT_ANSIBLE_PATH, fpart)) as pfd:
+            for idx, line in enumerate(pfd):
+                if idx in [0, 1] and 'flake8: noqa' in line or 'pylint: skip-file' in line:  # noqa: E501
+                    continue
+
+                data.write(line)
+
+    return data
+
+
 def main():
     ''' combine the necessary files to create the ansible module '''
+    args = parse_args()
 
     library = os.path.join(OPENSHIFT_ANSIBLE_PATH, '..', 'library/')
     sources = yaml.load(open(OPENSHIFT_ANSIBLE_SOURCES_PATH).read())
+
     for fname, parts in sources.items():
-        with open(os.path.join(library, fname), 'w') as afd:
+        data = generate(parts)
+        fname = os.path.join(library, fname)
+        if args.verify:
+            if not open(fname).read() == data.getvalue():
+                raise GenerateAnsibleException('Generated content does not match for %s' % fname)
+
+            continue
+
+        with open(fname, 'w') as afd:
             afd.seek(0)
-            afd.write(GEN_STR)
-            for fpart in parts:
-                with open(os.path.join(OPENSHIFT_ANSIBLE_PATH, fpart)) as pfd:
-                    # first line is pylint disable so skip it
-                    for idx, line in enumerate(pfd):
-                        if idx in [0, 1] and 'flake8: noqa' in line \
-                           or 'pylint: skip-file' in line:
-                            continue
-
-                        afd.write(line)
+            afd.write(data.getvalue())
 
 
 if __name__ == '__main__':
diff --git a/roles/lib_openshift/src/sources.yml b/roles/lib_openshift/src/sources.yml
index 945d4d13f..08fbbc201 100644
--- a/roles/lib_openshift/src/sources.yml
+++ b/roles/lib_openshift/src/sources.yml
@@ -1,5 +1,6 @@
 ---
 oc_route.py:
+- doc/generated
 - doc/license
 - lib/import.py
 - doc/route
@@ -9,6 +10,7 @@ oc_route.py:
 - class/oc_route.py
 - ansible/oc_route.py
 oc_edit.py:
+- doc/generated
 - doc/license
 - lib/import.py
 - doc/edit
diff --git a/roles/lib_utils/src/doc/generated b/roles/lib_utils/src/doc/generated
new file mode 100644
index 000000000..054780313
--- /dev/null
+++ b/roles/lib_utils/src/doc/generated
@@ -0,0 +1,9 @@
+#!/usr/bin/env python
+# pylint: disable=missing-docstring
+#     ___ ___ _  _ ___ ___    _ _____ ___ ___
+#    / __| __| \| | __| _ \  /_\_   _| __|   \
+#   | (_ | _|| .` | _||   / / _ \| | | _|| |) |
+#    \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____
+#   |   \ / _ \  | \| |/ _ \_   _| | __|   \_ _|_   _|
+#   | |) | (_) | | .` | (_) || |   | _|| |) | |  | |
+#   |___/ \___/  |_|\_|\___/ |_|   |___|___/___| |_|
diff --git a/roles/lib_utils/src/generate.py b/roles/lib_utils/src/generate.py
index f4b46aa91..cece68fb4 100755
--- a/roles/lib_utils/src/generate.py
+++ b/roles/lib_utils/src/generate.py
@@ -3,42 +3,64 @@
   Generate the openshift-ansible/roles/lib_openshift_cli/library/ modules.
 '''
 
+import argparse
 import os
+import six
 import yaml
 
-# pylint: disable=anomalous-backslash-in-string
-GEN_STR = "#!/usr/bin/env python\n" + \
-          "# pylint: disable=missing-docstring\n" + \
-          "#     ___ ___ _  _ ___ ___    _ _____ ___ ___\n" + \
-          "#    / __| __| \| | __| _ \  /_\_   _| __|   \\\n" + \
-          "#   | (_ | _|| .` | _||   / / _ \| | | _|| |) |\n" + \
-          "#    \___|___|_|\_|___|_|_\/_/_\_\_|_|___|___/_ _____\n" + \
-          "#   |   \ / _ \  | \| |/ _ \_   _| | __|   \_ _|_   _|\n" + \
-          "#   | |) | (_) | | .` | (_) || |   | _|| |) | |  | |\n" + \
-          "#   |___/ \___/  |_|\_|\___/ |_|   |___|___/___| |_|\n"
-
 OPENSHIFT_ANSIBLE_PATH = os.path.dirname(os.path.realpath(__file__))
-OPENSHIFT_ANSIBLE_SOURCES_PATH = os.path.join(OPENSHIFT_ANSIBLE_PATH, 'generate_sources.yml')  # noqa: E501
+OPENSHIFT_ANSIBLE_SOURCES_PATH = os.path.join(OPENSHIFT_ANSIBLE_PATH, 'sources.yml')  # noqa: E501
+
+
+class GenerateAnsibleException(Exception):
+    '''General Exception for generate function'''
+    pass
+
+
+def parse_args():
+    '''parse arguments to generate'''
+    parser = argparse.ArgumentParser(description="Generate ansible modules.")
+    parser.add_argument('--verify', action='store_true', default=False,
+                        help='Verify library code matches the generated code.')
+
+    return parser.parse_args()
+
+
+def generate(parts):
+    '''generate the source code for the ansible modules'''
+
+    data = six.StringIO()
+    for fpart in parts:
+        # first line is pylint disable so skip it
+        with open(os.path.join(OPENSHIFT_ANSIBLE_PATH, fpart)) as pfd:
+            for idx, line in enumerate(pfd):
+                if idx in [0, 1] and 'flake8: noqa' in line or 'pylint: skip-file' in line:  # noqa: E501
+                    continue
+
+                data.write(line)
+
+    return data
 
 
 def main():
     ''' combine the necessary files to create the ansible module '''
+    args = parse_args()
 
     library = os.path.join(OPENSHIFT_ANSIBLE_PATH, '..', 'library/')
     sources = yaml.load(open(OPENSHIFT_ANSIBLE_SOURCES_PATH).read())
+
     for fname, parts in sources.items():
-        with open(os.path.join(library, fname), 'w') as afd:
+        data = generate(parts)
+        fname = os.path.join(library, fname)
+        if args.verify:
+            if not open(fname).read() == data.getvalue():
+                raise GenerateAnsibleException('Generated content does not match for %s' % fname)
+
+            continue
+
+        with open(fname, 'w') as afd:
             afd.seek(0)
-            afd.write(GEN_STR)
-            for fpart in parts:
-                with open(os.path.join(OPENSHIFT_ANSIBLE_PATH, fpart)) as pfd:
-                    # first line is pylint disable so skip it
-                    for idx, line in enumerate(pfd):
-                        if idx in [0, 1] and 'flake8: noqa' in line \
-                           or 'pylint: skip-file' in line:
-                            continue
-
-                        afd.write(line)
+            afd.write(data.getvalue())
 
 
 if __name__ == '__main__':
diff --git a/roles/lib_utils/src/generate_sources.yml b/roles/lib_utils/src/generate_sources.yml
deleted file mode 100644
index 83b21de1b..000000000
--- a/roles/lib_utils/src/generate_sources.yml
+++ /dev/null
@@ -1,7 +0,0 @@
----
-yedit.py:
-- doc/license
-- class/import.py
-- doc/yedit
-- class/yedit.py
-- ansible/yedit.py
diff --git a/roles/lib_utils/src/sources.yml b/roles/lib_utils/src/sources.yml
new file mode 100644
index 000000000..9cf3a0981
--- /dev/null
+++ b/roles/lib_utils/src/sources.yml
@@ -0,0 +1,8 @@
+---
+yedit.py:
+- doc/generated
+- doc/license
+- class/import.py
+- doc/yedit
+- class/yedit.py
+- ansible/yedit.py
-- 
cgit v1.2.3