From c029ecccfdf87c72fa903f1be7110bdf6bdfbd8c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= <agateau@kde.org>
Date: Wed, 5 Feb 2014 11:52:05 +0100
Subject: [PATCH 1/8] Move code from depdiagram-prepare to a reusable module

---
 src/depdiagram-prepare             | 38 +++-----------------
 src/kapidox/depdiagram/__init__.py |  1 +
 src/kapidox/depdiagram/prepare.py  | 72 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 34 deletions(-)
 create mode 100644 src/kapidox/depdiagram/prepare.py

diff --git a/src/depdiagram-prepare b/src/depdiagram-prepare
index 3d133d9..a2f953e 100755
--- a/src/depdiagram-prepare
+++ b/src/depdiagram-prepare
@@ -25,58 +25,28 @@
 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 import argparse
 import os
-import shutil
-import subprocess
 import sys
 
-import yaml
+from kapidox import depdiagram
 
 DESCRIPTION = """
 Generate Graphviz dot files for all frameworks.
 """
 
-fnull = open("/dev/null", "w")
-
-def generate_dot(fw_dir, build_dir):
-    fw_name = os.path.basename(fw_dir)
-    ret = subprocess.call(["cmake", fw_dir, "--graphviz={}.dot".format(fw_name)],
-        stdout=fnull,
-        cwd=build_dir)
-    if ret != 0:
-        sys.stdout.write("Generating the dot file for {} failed.\n".format(fw_name))
-        sys.exit(ret)
-
 def main():
     parser = argparse.ArgumentParser(description=DESCRIPTION)
 
     parser.add_argument("fw_base_dir",
         help="Base dir containing all frameworks")
-    parser.add_argument("dot_base_dir",
+    parser.add_argument("dot_dir",
         help="Destination dir where dot files will be generated")
 
     args = parser.parse_args()
     fw_base_dir = os.path.abspath(args.fw_base_dir)
-    dot_base_dir = os.path.abspath(args.dot_base_dir)
-
-    lst = os.listdir(fw_base_dir)
-    for idx, fw_name in enumerate(lst):
-        fw_dir = os.path.join(fw_base_dir, fw_name)
-        yaml_path = os.path.join(fw_dir, fw_name + ".yaml")
-        if not os.path.exists(yaml_path):
-            continue
-
-        progress = 100 * (idx + 1) / len(lst)
-        print("{}% {}".format(progress, fw_name))
+    dot_dir = os.path.abspath(args.dot_dir)
 
-        with open(yaml_path) as f:
-            fw_info = yaml.load(f)
-            tier = fw_info["tier"]
+    depdiagram.prepare(fw_base_dir, dot_dir)
 
-        build_dir = os.path.join(dot_base_dir, "tier" + str(tier), fw_name)
-        if not os.path.exists(build_dir):
-            os.makedirs(build_dir)
-        generate_dot(fw_dir, build_dir)
-        shutil.copy(yaml_path, build_dir)
 
 if __name__ == "__main__":
     sys.exit(main())
diff --git a/src/kapidox/depdiagram/__init__.py b/src/kapidox/depdiagram/__init__.py
index e69de29..4e25f6d 100644
--- a/src/kapidox/depdiagram/__init__.py
+++ b/src/kapidox/depdiagram/__init__.py
@@ -0,0 +1 @@
+from prepare import *
diff --git a/src/kapidox/depdiagram/prepare.py b/src/kapidox/depdiagram/prepare.py
new file mode 100644
index 0000000..e4b9103
--- /dev/null
+++ b/src/kapidox/depdiagram/prepare.py
@@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2014  Aurélien Gâteau <agateau@kde.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# Python 2/3 compatibility (NB: we require at least 2.7)
+from __future__ import division, absolute_import, print_function, unicode_literals
+
+import os
+import shutil
+import subprocess
+import sys
+
+import yaml
+
+__all__ = ('prepare',)
+
+def _generate_dot(fw_dir, build_dir):
+    fw_name = os.path.basename(fw_dir)
+    ret = subprocess.call(["cmake", fw_dir, "--graphviz={}.dot".format(fw_name)],
+        stdout=open("/dev/null", "w"),
+        cwd=build_dir)
+    if ret != 0:
+        sys.stdout.write("Generating the dot file for {} failed.\n".format(fw_name))
+        sys.exit(ret)
+
+
+def prepare(fw_base_dir, dot_dir):
+    """Generate dot files for all frameworks.
+
+    Looks for frameworks in `fw_base_dir`. Output the dot files in sub dirs of
+    `dot_dir`.
+    """
+    lst = os.listdir(fw_base_dir)
+    for idx, fw_name in enumerate(lst):
+        fw_dir = os.path.join(fw_base_dir, fw_name)
+        yaml_path = os.path.join(fw_dir, fw_name + ".yaml")
+        if not os.path.exists(yaml_path):
+            continue
+
+        progress = int(100 * (idx + 1) / len(lst))
+        print("{}% {}".format(progress, fw_name))
+
+        with open(yaml_path) as f:
+            fw_info = yaml.load(f)
+            tier = fw_info["tier"]
+
+        build_dir = os.path.join(dot_dir, "tier" + str(tier), fw_name)
+        if not os.path.exists(build_dir):
+            os.makedirs(build_dir)
+        _generate_dot(fw_dir, build_dir)
+        shutil.copy(yaml_path, build_dir)
-- 
1.8.3.2

