From 900bdc7d6c0dbfd350373062571e75d9bbf4356a Mon Sep 17 00:00:00 2001
From: Timo Koch <timo.koch@iws.uni-stuttgart.de>
Date: Wed, 20 Jun 2018 10:28:54 +0200
Subject: [PATCH] [util] Make cmake lists script executable and increase
 usability

now defaults to the dumux folder if no other folder is specified as command line argument.
---
 bin/util/create_cmakelists.py | 60 ++++++++++++++++++++---------------
 1 file changed, 35 insertions(+), 25 deletions(-)
 mode change 100644 => 100755 bin/util/create_cmakelists.py

diff --git a/bin/util/create_cmakelists.py b/bin/util/create_cmakelists.py
old mode 100644
new mode 100755
index e1d1e4873b..0b92a08cea
--- a/bin/util/create_cmakelists.py
+++ b/bin/util/create_cmakelists.py
@@ -1,38 +1,48 @@
-# Create files CMakeLists.txt for the current folder and all subfolders.
-# Should be run from the folder `dumux` that contains the header files, namely,
-# one level below the top dumux folder:
-# python ../bin/create_cmakelists.py.
+#!/usr/bin/env python
+
+"""
+Create files CMakeLists.txt for the given folder and all subfolders,
+including the add_subdirectory(...) and install(...) commands.
+Defaults to the folder `dumux` that contains the header files,
+if no folder was specified.
+"""
 
-# Import the os module, for the os.walk function
 import os
+import argparse
+
+parser = argparse.ArgumentParser()
+parser.add_argument('folder', type=str, nargs='?', help='the folder to create CMakeLists.txt\'s for', default=None)
+args = vars(parser.parse_args())
+
+# default to the dumux folder (relative path to the location of this script)
+if args['folder'] is None:
+    rootDir = os.path.dirname(os.path.abspath(__file__)) + "/../../dumux"
+else:
+    rootDir = args['folder']
 
-# Set the directory you want to start from
-rootDir = '.'
 for folderName, subFolders, files in os.walk(rootDir):
     subFolders = sorted(subFolders)
     files = sorted(files)
 
-    cmakelists = open(folderName + "/CMakeLists.txt", "w")
-
-    for subFolder in subFolders:
-        cmakelists.write("add_subdirectory(\"%s\")\n" % subFolder)
+    with open(folderName + "/CMakeLists.txt", "w") as cmakelists:
 
-    headersExist = False
-    for fileName in files:
-        if fileName != "CMakeLists.txt":
-            headersExist = True
-            break
-
-    if headersExist:
-        if subFolders:
-            cmakelists.write("\n")
-
-        cmakelists.write("install(FILES\n")
+        for subFolder in subFolders:
+            cmakelists.write("add_subdirectory({})\n".format(subFolder))
 
+        headersExist = False
         for fileName in files:
             if fileName != "CMakeLists.txt":
-                cmakelists.write("%s\n" % fileName)
+                headersExist = True
+                break
+
+        if headersExist:
+            if subFolders:
+                cmakelists.write("\n")
+
+            cmakelists.write("install(FILES\n")
 
-        cmakelists.write("DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dumux/%s)\n" % folderName[2:])
+            for fileName in files:
+                if fileName != "CMakeLists.txt":
+                    cmakelists.write("{}\n".format(fileName))
 
-    cmakelists.close()
+            cmakelists.write("DESTINATION ${{CMAKE_INSTALL_INCLUDEDIR}}/dumux/{})\n".format(folderName.replace(rootDir + '/', '')))
-- 
GitLab