From 64bbc3ce48ba8c0fea77a86781b21d7e60c70923 Mon Sep 17 00:00:00 2001 From: Nils Wentzell Date: Thu, 31 Oct 2019 17:26:31 -0400 Subject: [PATCH] Use python instead of bash for the replace and rename operation --- README.md | 4 ++-- share/replace_and_rename.py | 45 +++++++++++++++++++++++++++++++++++++ share/replace_and_rename.sh | 30 ------------------------- 3 files changed, 47 insertions(+), 32 deletions(-) create mode 100755 share/replace_and_rename.py delete mode 100755 share/replace_and_rename.sh diff --git a/README.md b/README.md index 62eff3ca..67c9f481 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ To adapt this skeleton for a new TRIQS application, the following steps are nece git clone https://github.com/triqs/app4triqs --branch unstable appname cd appname ./share/squash_history.sh -./share/replace_and_rename.sh appname +./share/replace_and_rename.py appname git add -A && git commit -m "Adjust app4triqs skeleton for appname" ``` @@ -43,7 +43,7 @@ If you should encounter any conflicts resolve them and `git commit`. Finally we repeat the replace and rename command from the initial setup. ```bash -./share/replace_and_rename.sh appname +./share/replace_and_rename.py appname git commit --amend ``` diff --git a/share/replace_and_rename.py b/share/replace_and_rename.py new file mode 100755 index 00000000..1d9eecb7 --- /dev/null +++ b/share/replace_and_rename.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +import sys +import os +import glob + +if len(sys.argv) != 2: + print "Please pass the application name" + sys.exit() + +app_name = str(sys.argv[1]).lower() +capital_name = app_name.upper() + +# Move app4triqs directories if necessary +if os.path.isdir("c++/app4triqs"): os.rename("c++/app4triqs", "c++/" + app_name) +if os.path.isdir("python/app4triqs"): os.rename("python/app4triqs", "python/" + app_name) + +# Ignore these files +ignore_lst = [".git/", "replace_and_rename.py", "squash_history.sh"] + +# Find the root directory of app4triqs +app4triqs_root = os.path.abspath(os.path.dirname(__file__) + "/..") + +# Recurse over all subdirectories and files +for root, dirs, files in os.walk(app4triqs_root): + + for fname in files: + fpath = os.path.join(root, fname) + + # Ignore certain files / directories + if any(it in fpath for it in ignore_lst): continue + + if os.path.isfile(fpath): + # Rename files containing app4triqs in their filename + if "app4triqs" in fname: + new_fpath = os.path.join(root, fname.replace("app4triqs", app_name)) + os.rename(fpath, new_fpath) + fpath = new_fpath + + # Replace app4triqs and APP4TRIQS in all files + with open(fpath, 'r') as f: + s = f.read() + if "app4triqs" in s or "APP4TRIQS" in s: + with open(fpath, 'w') as f: + f.write(s.replace("app4triqs", app_name).replace("APP4TRIQS", capital_name)) diff --git a/share/replace_and_rename.sh b/share/replace_and_rename.sh deleted file mode 100755 index fd5c00b6..00000000 --- a/share/replace_and_rename.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env bash - -if [ $# -ne 1 ]; then - echo "Please pass the application name" - exit 1 -fi -app_name=$1 -capital_name=$(printf '%s' "$1" | awk '{ print toupper($0) }') - -# Move app4triqs directories if necessary -[ -d c++/app4triqs ] && mv c++/app4triqs c++/${app_name} -[ -d python/app4triqs ] && mv python/app4triqs python/${app_name} - -# Replace app4triqs and APP4TRIQS for our application in all files and filenames -if [ $(uname -s) == Linux ]; then - find . -type f \ - -not -path "./.git/*" \ - -not -path "*/replace_and_rename.sh" \ - -not -path "*/squash_history.sh" \ - -exec sed -i "s/app4triqs/${app_name}/g; s/APP4TRIQS/${capital_name}/g" {} \; - find . -type f -not -path "./.git/*" -exec rename app4triqs ${app_name} {} &> /dev/null \; -elif [ $(uname -s) == Darwin ]; then - LC_CTYPE=C LANG=C find . -type f \ - -not -path "./.git/*" \ - -not -path "*/replace_and_rename.sh" \ - -not -path "*/squash_history.sh" \ - -exec sed -i '' -e "s/app4triqs/${app_name}/g; s/APP4TRIQS/${capital_name}/g" {} \; - find . -type f -not -path "./.git/*" -exec rename "s/app4triqs/${app_name}/" {} \; &> /dev/null - -fi