9
1
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-11-08 22:43:38 +01:00
qp2/bin/qp_name

105 lines
2.8 KiB
Plaintext
Raw Normal View History

2020-03-17 16:39:43 +01:00
#!/usr/bin/env python3
2019-01-25 11:39:31 +01:00
"""
Displays the names of all the files in which the provider/subroutine/function
given as argument is used. With the -r flag, the name can be changed in the
whole quantum package.
Usage:
qp_name <name> [-r <new_name> | --rename=<new_name>]
Options:
-h Prints the help message
-r <new_name> --rename=<new_name> Renames the provider /
subroutine / function and all
its occurences
Note:
It is safe to create a commit before renaming a provider, and then to
check what has changed using git diff.
"""
import re
import sys
import os
try:
from docopt import docopt
from qp_path import QP_SRC, QP_ROOT
except ImportError:
2020-12-04 10:40:42 +01:00
print("source quantum_package.rc")
2019-01-25 11:39:31 +01:00
raise
def main(arguments):
"""Main function"""
# Check that name exist in */IRPF90_man
2020-03-17 16:39:43 +01:00
print("Checking that name exists...")
2019-01-25 11:39:31 +01:00
all_modules = os.listdir(QP_SRC)
f = arguments["<name>"]+".l"
found = False
for mod in all_modules:
if os.path.isdir(os.path.join(QP_SRC, mod, "IRPF90_man")):
for filename in os.listdir(os.path.join(QP_SRC, mod, "IRPF90_man")):
if filename == f:
found = True
break
if found: break
if not found:
2020-03-17 16:39:43 +01:00
print("Error:")
print("The variable/subroutine/function \""+arguments["<name>"] \
+ "\" was not found in the sources.")
print("Did you compile the code at the root?")
print("Continue? [y/N] ", end=' ')
2019-01-25 11:39:31 +01:00
cont = sys.stdin.read(1).strip() in ["y", "Y"]
if not cont:
2020-03-17 16:39:43 +01:00
print("Aborted")
2019-01-25 11:39:31 +01:00
sys.exit(1)
# Now search in all the files
if arguments["--rename"]:
2020-03-17 16:39:43 +01:00
print("Replacing...")
2019-01-25 11:39:31 +01:00
else:
2020-03-17 16:39:43 +01:00
print("Searching...")
2019-01-25 11:39:31 +01:00
name = re.compile(r"\b"+arguments["<name>"]+r"\b", re.IGNORECASE)
for mod in all_modules:
dirname = os.path.join(QP_SRC, mod)
if not os.path.isdir(dirname):
continue
for filename in os.listdir(dirname):
if "." not in filename:
continue
filename = os.path.join(dirname, filename)
if not os.path.isfile(filename):
continue
with open(filename, "r") as f:
f_in = f.read()
if name.search(f_in):
2020-03-17 16:39:43 +01:00
print(filename)
2019-01-25 11:39:31 +01:00
if arguments["--rename"]:
f_new = name.sub(arguments["--rename"], f_in)
with open(filename, "w") as f:
f.write(f_new)
2020-03-17 16:39:43 +01:00
print("Done")
2019-01-25 11:39:31 +01:00
with open(os.path.join(QP_ROOT, "REPLACE"), 'a') as f:
2020-03-17 16:39:43 +01:00
print("qp_name "+" ".join(sys.argv[1:]), file=f)
2019-01-25 11:39:31 +01:00
if __name__ == '__main__':
ARGS = docopt(__doc__)
main(ARGS)