From 7b653245775b732d247101391bdd1b8057c02a63 Mon Sep 17 00:00:00 2001 From: Hanchuan Wu <whc.shmily@gmail.com> Date: Wed, 28 Jul 2021 14:53:25 +0200 Subject: [PATCH] [bin][util]->[bin] Reconstrcut folder (move scripts to bin) and adapt imports --- bin/create_cmakelists.py | 53 ++++++ bin/create_dockerimage.py | 152 ++++++++++++++++++ .../remove_clutter_after_last_endif.py | 0 bin/util/create_cmakelists.py | 51 ------ bin/util/createdockerimage.py | 150 ----------------- 5 files changed, 205 insertions(+), 201 deletions(-) create mode 100755 bin/create_cmakelists.py create mode 100644 bin/create_dockerimage.py rename bin/{util => }/remove_clutter_after_last_endif.py (100%) delete mode 100755 bin/util/create_cmakelists.py delete mode 100644 bin/util/createdockerimage.py diff --git a/bin/create_cmakelists.py b/bin/create_cmakelists.py new file mode 100755 index 0000000000..3a8d4d0fe3 --- /dev/null +++ b/bin/create_cmakelists.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +""" +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 os +import argparse + +if __name__ == "__main__": + + 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'] + + + ignore_folders = ["", "io/format/fmt", "io/xml"] + extensions = [".hh", ".inc"] + for fullFolderName, subFolders, files in os.walk(rootDir): + # alphabetically sort + subFolders = sorted(subFolders) + files = sorted(files) + # get folder name relative to dumux + folderName = fullFolderName.replace(rootDir + '/', '').replace(rootDir, '') + if folderName not in ignore_folders: + with open(fullFolderName + "/CMakeLists.txt", "w") as cmakelists: + # add subfolders + for subFolder in subFolders: + cmakelists.write("add_subdirectory({})\n".format(subFolder)) + + headersExist = False + for fileName in files: + ext = os.path.splitext(fileName)[1] + if ext in extensions: + headersExist = True + break + + if headersExist: + if subFolders: cmakelists.write("\n") + # collect all files to be installed in a CMake variable + headers_variable = "DUMUX_" + folderName.upper().replace("/", "_") + "_HEADERS" + cmakelists.write("file(GLOB {}{})\n".format(headers_variable, " *".join([''] + extensions))) + cmakelists.write("install(FILES ${{{}}}\n".format(headers_variable)) + cmakelists.write(" DESTINATION ${{CMAKE_INSTALL_INCLUDEDIR}}/dumux/{})\n".format(folderName)) diff --git a/bin/create_dockerimage.py b/bin/create_dockerimage.py new file mode 100644 index 0000000000..ed3d002631 --- /dev/null +++ b/bin/create_dockerimage.py @@ -0,0 +1,152 @@ +#!/usr/bin/env python3 +import os +import sys +import string +import shutil +import argparse +import subprocess +from util.moduleinfo import getModuleFile +from util.moduleinfo import extractModuleInfos + +# require python 3 +if sys.version_info[0] < 3: + sys.exit("\nERROR: Python3 required") + +if __name__ == "__main__": + + # input argument parser + parser = argparse.ArgumentParser( + description="Create a docker image for a given module and install script." + ) + + parser.add_argument('-m', '--modulepath', + required=True, + help='the path to the your module') + parser.add_argument('-i', '--installScript', + required=True, + help="Specify the installation script") + parser.add_argument('-t', '--templateFolder', + required=False, + help="Specify the folder with the template files") + + args = vars(parser.parse_args()) + + # get information on the module + modulePath = os.path.abspath(args['modulepath']) + modInfo = extractModuleInfos(getModuleFile(modulePath), + ['Module', 'Maintainer']) + moduleName = modInfo['Module'] + moduleMaintainer = modInfo['Maintainer'] + dockerTag = moduleName.lower() # docker only supports lower case + + # get folder with the template files + templateFolder = args['templateFolder'] + if not templateFolder: + templateFolder = os.path.join(modulePath, '../dumux/docker') + if not os.path.exists(templateFolder): + sys.exit("Template folder {} could not be found".format(templateFolder)) + + print("*"*54) + print("\n-- Creating a Docker image for module " + moduleName + " --\n") + print("*"*54) + + if os.path.exists("docker"): + print("\nA docker folder already exists. " + "Continue anyway? - will be overwritten - [y/N]\n") + delete = input() + if delete == "y" or delete == "Y": + shutil.rmtree("docker") + print("--> Deleted old docker folder.") + else: + sys.exit("Abort.") + + os.mkdir("docker") + print("--> Created the folder 'docker'.") + + # copy install script into docker folder and make it executable + installScriptPath = args['installScript'] + installScriptName = os.path.split(installScriptPath)[1] + installScript = os.path.join(os.path.join(os.getcwd(), 'docker'), + installScriptName) + shutil.copy(installScriptPath, installScript) + os.system("chmod +x {}".format(installScript)) + print("--> Using install script: {} to install dependencies for module {}." + .format(installScript, moduleName)) + + + # substitute content from template and write to target + def substituteAndWrite(template, target, mapping): + if not os.path.exists(template): + sys.exit("Template file '" + template + "' could not be found") + with open(target, 'w') as targetFile: + raw = string.Template(open(template).read()) + targetFile.write(raw.substitute(**mapping)) + + + # write setpermissions helper script + template = os.path.join(templateFolder, 'setpermissions.sh.template') + target = os.path.join(os.getcwd(), 'docker/setpermissions.sh') + substituteAndWrite(template, target, {}) + print("--> Created permission helper script for easier container setup.") + + # write welcome message file + template = os.path.join(templateFolder, 'WELCOME.template') + target = os.path.join(os.getcwd(), 'docker/WELCOME') + substituteAndWrite(template, target, + {'modName': moduleName, 'modFolder': moduleName}) + print("--> Created welcome message displayed on Docker container startup.") + + # write readme file + template = os.path.join(templateFolder, 'README.md.template') + target = os.path.join(os.getcwd(), 'docker/README.md') + substituteAndWrite(template, target, + {'modName': moduleName, 'dockerTag': dockerTag}) + print("--> Created README.md on how to use the docker image.") + + # write helper file for container spin-up (make it executable after creation) + template = os.path.join(templateFolder, 'docker.sh.template') + target = os.path.join(os.getcwd(), 'docker/docker_{}.sh'.format(dockerTag)) + substituteAndWrite(template, target, {'dockerTag': dockerTag}) + os.system("chmod +x " + target) + print("--> Created helper script to spin up the docker container.") + + # write the docker file + template = os.path.join(templateFolder, 'Dockerfile.template') + target = os.path.join(os.getcwd(), 'docker/Dockerfile') + substituteAndWrite(template, target, + { + 'modName': moduleName, + 'modMaintainer': moduleMaintainer, + 'dockerTag': dockerTag, + 'instScript': installScriptName + }) + print("--> Created Dockerfile. You can adapt it to your needs.") + print() + print("Do you want to directly build the Docker image? [y/N]") + + build = input() + if build == "y" or build == "Y": + print("Building Docker image... this may take several minutes.") + try: + os.chdir('docker') + subprocess.run(['docker', 'build', + '-f', 'Dockerfile', + '-t', dockerTag, '.'], check=True) + os.chdir('../') + except Exception: + os.chdir('../') + sys.exit("ERROR: docker image build failed") + + print() + print("Successfully built image: {}. " + "Have a look at docker/README.md.".format(dockerTag)) + print("Check the container by running " + "'docker run -it {} /bin/bash' in the same".format(dockerTag)) + print("directory as the Dockerfile, and try using the convenience script " + "docker_{}.sh".format(dockerTag)) + print("See docker/README.md for more information.") + else: + print("You can build your Docker image later by running " + "'docker build -f Dockerfile -t {}'".format(dockerTag)) + print("from within the folder 'docker' that was created by this script, " + "and in which you should find the 'Dockerfile'.") diff --git a/bin/util/remove_clutter_after_last_endif.py b/bin/remove_clutter_after_last_endif.py similarity index 100% rename from bin/util/remove_clutter_after_last_endif.py rename to bin/remove_clutter_after_last_endif.py diff --git a/bin/util/create_cmakelists.py b/bin/util/create_cmakelists.py deleted file mode 100755 index 6309295590..0000000000 --- a/bin/util/create_cmakelists.py +++ /dev/null @@ -1,51 +0,0 @@ -#!/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 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'] - - -ignore_folders = ["", "io/format/fmt", "io/xml"] -extensions = [".hh", ".inc"] -for fullFolderName, subFolders, files in os.walk(rootDir): - # alphabetically sort - subFolders = sorted(subFolders) - files = sorted(files) - # get folder name relative to dumux - folderName = fullFolderName.replace(rootDir + '/', '').replace(rootDir, '') - if folderName not in ignore_folders: - with open(fullFolderName + "/CMakeLists.txt", "w") as cmakelists: - # add subfolders - for subFolder in subFolders: - cmakelists.write("add_subdirectory({})\n".format(subFolder)) - - headersExist = False - for fileName in files: - ext = os.path.splitext(fileName)[1] - if ext in extensions: - headersExist = True - break - - if headersExist: - if subFolders: cmakelists.write("\n") - # collect all files to be installed in a CMake variable - headers_variable = "DUMUX_" + folderName.upper().replace("/", "_") + "_HEADERS" - cmakelists.write("file(GLOB {}{})\n".format(headers_variable, " *".join([''] + extensions))) - cmakelists.write("install(FILES ${{{}}}\n".format(headers_variable)) - cmakelists.write(" DESTINATION ${{CMAKE_INSTALL_INCLUDEDIR}}/dumux/{})\n".format(folderName)) diff --git a/bin/util/createdockerimage.py b/bin/util/createdockerimage.py deleted file mode 100644 index 96619ea5f3..0000000000 --- a/bin/util/createdockerimage.py +++ /dev/null @@ -1,150 +0,0 @@ -#!/usr/bin/env python3 -import os -import sys -import string -import shutil -import argparse -import subprocess -from getmoduleinfo import getModuleFile -from getmoduleinfo import extractModuleInfos - -# require python 3 -if sys.version_info[0] < 3: - sys.exit("\nERROR: Python3 required") - -# input argument parser -parser = argparse.ArgumentParser( - description="Create a docker image for a given module and install script." -) - -parser.add_argument('-m', '--modulepath', - required=True, - help='the path to the your module') -parser.add_argument('-i', '--installScript', - required=True, - help="Specify the installation script") -parser.add_argument('-t', '--templateFolder', - required=False, - help="Specify the folder with the template files") - -args = vars(parser.parse_args()) - -# get information on the module -modulePath = os.path.abspath(args['modulepath']) -modInfo = extractModuleInfos(getModuleFile(modulePath), - ['Module', 'Maintainer']) -moduleName = modInfo['Module'] -moduleMaintainer = modInfo['Maintainer'] -dockerTag = moduleName.lower() # docker only supports lower case - -# get folder with the template files -templateFolder = args['templateFolder'] -if not templateFolder: - templateFolder = os.path.join(modulePath, '../dumux/docker') -if not os.path.exists(templateFolder): - sys.exit("Template folder {} could not be found".format(templateFolder)) - -print("*"*54) -print("\n-- Creating a Docker image for module " + moduleName + " --\n") -print("*"*54) - -if os.path.exists("docker"): - print("\nA docker folder already exists. " - "Continue anyway? - will be overwritten - [y/N]\n") - delete = input() - if delete == "y" or delete == "Y": - shutil.rmtree("docker") - print("--> Deleted old docker folder.") - else: - sys.exit("Abort.") - -os.mkdir("docker") -print("--> Created the folder 'docker'.") - -# copy install script into docker folder and make it executable -installScriptPath = args['installScript'] -installScriptName = os.path.split(installScriptPath)[1] -installScript = os.path.join(os.path.join(os.getcwd(), 'docker'), - installScriptName) -shutil.copy(installScriptPath, installScript) -os.system("chmod +x {}".format(installScript)) -print("--> Using install script: {} to install dependencies for module {}." - .format(installScript, moduleName)) - - -# substitute content from template and write to target -def substituteAndWrite(template, target, mapping): - if not os.path.exists(template): - sys.exit("Template file '" + template + "' could not be found") - with open(target, 'w') as targetFile: - raw = string.Template(open(template).read()) - targetFile.write(raw.substitute(**mapping)) - - -# write setpermissions helper script -template = os.path.join(templateFolder, 'setpermissions.sh.template') -target = os.path.join(os.getcwd(), 'docker/setpermissions.sh') -substituteAndWrite(template, target, {}) -print("--> Created permission helper script for easier container setup.") - -# write welcome message file -template = os.path.join(templateFolder, 'WELCOME.template') -target = os.path.join(os.getcwd(), 'docker/WELCOME') -substituteAndWrite(template, target, - {'modName': moduleName, 'modFolder': moduleName}) -print("--> Created welcome message displayed on Docker container startup.") - -# write readme file -template = os.path.join(templateFolder, 'README.md.template') -target = os.path.join(os.getcwd(), 'docker/README.md') -substituteAndWrite(template, target, - {'modName': moduleName, 'dockerTag': dockerTag}) -print("--> Created README.md on how to use the docker image.") - -# write helper file for container spin-up (make it executable after creation) -template = os.path.join(templateFolder, 'docker.sh.template') -target = os.path.join(os.getcwd(), 'docker/docker_{}.sh'.format(dockerTag)) -substituteAndWrite(template, target, {'dockerTag': dockerTag}) -os.system("chmod +x " + target) -print("--> Created helper script to spin up the docker container.") - -# write the docker file -template = os.path.join(templateFolder, 'Dockerfile.template') -target = os.path.join(os.getcwd(), 'docker/Dockerfile') -substituteAndWrite(template, target, - { - 'modName': moduleName, - 'modMaintainer': moduleMaintainer, - 'dockerTag': dockerTag, - 'instScript': installScriptName - }) -print("--> Created Dockerfile. You can adapt it to your needs.") -print() -print("Do you want to directly build the Docker image? [y/N]") - -build = input() -if build == "y" or build == "Y": - print("Building Docker image... this may take several minutes.") - try: - os.chdir('docker') - subprocess.run(['docker', 'build', - '-f', 'Dockerfile', - '-t', dockerTag, '.'], check=True) - os.chdir('../') - except Exception: - os.chdir('../') - sys.exit("ERROR: docker image build failed") - - print() - print("Successfully built image: {}. " - "Have a look at docker/README.md.".format(dockerTag)) - print("Check the container by running " - "'docker run -it {} /bin/bash' in the same".format(dockerTag)) - print("directory as the Dockerfile, and try using the convenience script " - "docker_{}.sh".format(dockerTag)) - print("See docker/README.md for more information.") -else: - print("You can build your Docker image later by running " - "'docker build -f Dockerfile -t {}'".format(dockerTag)) - print("from within the folder 'docker' that was created by this script, " - "and in which you should find the 'Dockerfile'.") -- GitLab