3
0
mirror of https://github.com/triqs/dft_tools synced 2024-06-29 16:34:53 +02:00

Use python instead of bash for the replace and rename operation

This commit is contained in:
Nils Wentzell 2019-10-31 17:26:31 -04:00
parent b7dafbd2e6
commit 64bbc3ce48
3 changed files with 47 additions and 32 deletions

View File

@ -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
```

45
share/replace_and_rename.py Executable file
View File

@ -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))

View File

@ -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