10
0
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-11-14 10:03:47 +01:00
QuantumPackage/scripts/module/module_handler.py

287 lines
7.9 KiB
Python
Raw Normal View History

2020-03-17 16:39:43 +01:00
#!/usr/bin/env python3
2019-01-25 11:39:31 +01:00
# -*- coding: utf-8 -*-
"""
Module utilitary
Usage:
module_handler.py print_descendant [<module_name>...]
module_handler.py clean [ --all | <module_name>...]
2021-06-18 15:02:23 +02:00
module_handler.py tidy [ --all | <module_name>...]
2021-06-18 15:08:07 +02:00
module_handler.py create_git_ignore [ --all | <module_name>...]
2019-01-25 11:39:31 +01:00
Options:
print_descendant Print the genealogy of the needed modules
2021-06-18 15:02:23 +02:00
clean Used for ninja clean
tidy A light version of clean, where only the intermediate
files are removed
2021-06-18 15:08:07 +02:00
create_git_ignore deprecated
2019-01-25 11:39:31 +01:00
NEED The path of NEED file.
by default try to open the file in the current path
2021-06-18 15:08:07 +02:00
2019-01-25 11:39:31 +01:00
"""
import os
import sys
import os.path
from collections import namedtuple
import shutil
try:
from docopt import docopt
from qp_path import QP_SRC, QP_ROOT, QP_PLUGINS, QP_EZFIO
except ImportError:
2021-07-06 15:02:47 +02:00
print("source quantum_package.rc")
2019-01-25 11:39:31 +01:00
raise
def is_module(path_module_rel):
return os.path.isfile(os.path.join(QP_SRC, path_module_rel, "NEED"))
def is_plugin(path_module_rel):
return os.path.isfile(os.path.join(QP_PLUGINS, path_module_rel, "NEED"))
def get_binaries(path_module):
"""
Return the list of binaries
"""
import subprocess
try:
cmd = 'grep -l -i --regexp="^\\s*program\\s" {0}/*.irp.f'.format(path_module)
process = subprocess.Popen([cmd],
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
2020-03-17 16:39:43 +01:00
stdout = stdout.decode()
2019-01-25 11:39:31 +01:00
except OSError:
return []
else:
if not stdout:
return []
elif "No such file or directory" not in stdout:
l_bin = [i.replace(".irp.f", "", 1) for i in stdout.split()]
return [os.path.realpath(bin_) for bin_ in l_bin]
else:
return []
def get_dict_child(l_root_abs=None):
"""Loop over MODULE in QP_ROOT/src, open all the NEED
and create a dict[MODULE] = [sub module needed, ...]
"""
d_ref = dict()
if not l_root_abs:
l_root_abs = [QP_SRC]
for root_abs in l_root_abs:
for module_rel in os.listdir(root_abs):
module_abs = os.path.join(root_abs, module_rel)
try:
path_file = os.path.join(module_abs, "NEED")
with open(path_file, "r") as f:
l_children = f.read().split()
except IOError:
pass
else:
if module_rel not in d_ref:
d_ref[module_rel] = l_children
#else:
# print "Module {0} alredy defined"
# print "Abort"
# sys.exit(1)
return d_ref
def get_l_module_descendant(d_child, l_module):
"""
From a list of module return the module and descendant
"""
l = []
for module in l_module:
if module not in l:
l.append(module)
try:
l.extend(get_l_module_descendant(d_child, d_child[module]))
except KeyError:
2020-03-17 16:39:43 +01:00
print("Error: ", file=sys.stderr)
print("`{0}` is not a submodule".format(module), file=sys.stderr)
print("Check the typo (spelling, case, '/', etc.) ", file=sys.stderr)
2022-01-28 20:50:10 +01:00
# pass
2019-01-25 11:39:31 +01:00
sys.exit(1)
return list(set(l))
class ModuleHandler():
def __init__(self, l_root_abs=None):
self.dict_child = get_dict_child(l_root_abs)
@property
def l_module(self):
2020-03-17 16:39:43 +01:00
return list(self.dict_child.keys())
2019-01-25 11:39:31 +01:00
@property
def dict_parent(self):
"""
Get a dic of the first parent
"""
d_child = self.dict_child
d = {}
for module_name in d_child:
2020-03-17 16:39:43 +01:00
d[module_name] = [i for i in list(d_child.keys())
2019-01-25 11:39:31 +01:00
if module_name in d_child[i]]
return d
@property
def dict_descendant(self):
"""
Get a dic of all the genealogy desc (children and all_children)
"""
d = {}
d_child = self.dict_child
for module_name in d_child:
try:
d[module_name] = get_l_module_descendant(d_child,
d_child[module_name])
except KeyError:
2020-03-17 16:39:43 +01:00
print("Check NEED for {0}".format(
module_name))
2019-01-25 11:39:31 +01:00
sys.exit(1)
return d
@property
def dict_root(self):
"""
Return a dict(module_name) = module_boss
The top node in a tree.
"""
d_asc = self.dict_parent
d_desc = self.dict_descendant
l_all_module = self.l_module
dict_root = {}
for module in l_all_module:
dict_root[module] = [p for p in l_all_module
if module in [p] + d_desc[p] and not d_asc[p]
][0]
return dict_root
def l_descendant_unique(self, l_module):
d_desc = self.dict_descendant
d = {}
for module in l_module:
for e in d_desc[module]:
d[e] = 1
2020-03-17 16:39:43 +01:00
return list(d.keys())
2019-01-25 11:39:31 +01:00
def l_reduce_tree(self, l_module):
"""For a list of module in input return only the root"""
l_d_u = self.l_descendant_unique(l_module)
l_module_reduce = []
for module in l_module:
if module not in l_d_u:
l_module_reduce.append(module)
return l_module_reduce
if __name__ == '__main__':
arguments = docopt(__doc__)
if arguments['--all']:
l_module = [f for f in os.listdir(QP_SRC)
if os.path.isdir(os.path.join(QP_SRC, f))]
2023-02-06 11:46:04 +01:00
l_non_module = [f for f in l_module if not is_module(f) ]
if l_non_module:
print("Ignoring invalid modules:")
print(" ".join(l_non_module))
# Filter out all non-modules
l_module = [f for f in l_module if is_module(f) ]
2019-01-25 11:39:31 +01:00
# Remove all produced ezfio_config files
for filename in os.listdir( os.path.join(QP_EZFIO, "config") ):
os.remove( os.path.join(QP_EZFIO, "config", filename) )
2021-06-18 15:08:07 +02:00
2019-01-25 11:39:31 +01:00
elif not arguments['<module_name>']:
dir_ = os.getcwd()
l_module = [os.path.basename(dir_)]
else:
l_module = arguments['<module_name>']
for module in l_module:
if not is_module(module):
2020-03-17 16:39:43 +01:00
print("{0} is not a valid module. Abort".format(module))
print("No NEED in it")
2019-01-25 11:39:31 +01:00
sys.exit(1)
m = ModuleHandler()
if arguments['print_descendant']:
for module in l_module:
2020-03-17 16:39:43 +01:00
print(" ".join(sorted(m.l_descendant_unique([module]))))
2019-01-25 11:39:31 +01:00
2021-06-18 15:02:23 +02:00
if arguments["clean"] or arguments["tidy"]:
2019-01-25 11:39:31 +01:00
l_dir = ['IRPF90_temp', 'IRPF90_man']
l_file = ["irpf90_entities", "tags", "irpf90.make", "Makefile",
2021-06-18 15:08:07 +02:00
"Makefile.depend", ".ninja_log", ".ninja_deps",
2019-01-25 11:39:31 +01:00
"ezfio_interface.irp.f"]
for module in l_module:
module_abs = os.path.realpath(os.path.join(QP_SRC, module))
l_symlink = m.l_descendant_unique([module])
l_exe = get_binaries(module_abs)
2021-06-18 15:02:23 +02:00
for f in l_dir:
try:
shutil.rmtree(os.path.join(module_abs, f))
except:
pass
2019-01-25 11:39:31 +01:00
2021-06-18 15:02:23 +02:00
for symlink in l_symlink:
try:
os.unlink(os.path.join(module_abs, symlink))
except:
pass
2019-01-25 11:39:31 +01:00
2021-06-18 15:02:23 +02:00
for f in l_file:
try:
os.remove(os.path.join(module_abs, f))
except:
pass
2019-01-25 11:39:31 +01:00
2021-06-18 15:02:23 +02:00
if arguments["clean"]:
2019-01-25 11:39:31 +01:00
for f in l_exe:
try:
os.remove(os.path.join(module_abs, f))
except:
pass