10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-10-19 22:41:48 +02:00
quantum_package/scripts/qp_plugins

300 lines
9.4 KiB
Plaintext
Raw Normal View History

2018-04-30 18:43:13 +02:00
#!/usr/bin/env python2
2015-06-04 16:40:00 +02:00
# -*- coding: utf-8 -*-
"""
2015-07-16 14:25:20 +02:00
Usage:
2019-01-14 15:20:51 +01:00
qp_plugins list [-iuq]
2018-12-21 11:41:28 +01:00
qp_plugins download <url>
qp_plugins install <name>...
qp_plugins uninstall <name>
2019-01-14 15:20:51 +01:00
qp_plugins create -n <name> [-r <repo>] [<needed_modules>...]
2015-06-05 09:50:24 +02:00
Options:
2019-01-14 15:20:51 +01:00
list List
-i --installed only the installed plugins
-u --uninstalled only the uninstalled plugins
-q --repositories the external repositories
2018-12-21 11:41:28 +01:00
2019-01-14 15:20:51 +01:00
download <url> Download an external repository.
The URL points to a tar.gz file or a git repository:
http://example.com/site/example.tar.gz
git@gitlab.com:user/example_repository
2018-12-21 11:41:28 +01:00
2019-01-14 15:20:51 +01:00
install Install a plugin
2018-12-21 11:41:28 +01:00
2019-01-14 15:20:51 +01:00
uninstall Uninstall a plugin
2018-12-21 11:41:28 +01:00
2019-01-14 15:20:51 +01:00
create
-n --name=<name> Create a new plugin named <name>
-r --repository=<repo> Name of the repository in which to create the plugin
2018-12-21 12:38:44 +01:00
2019-01-14 15:20:51 +01:00
"""
2015-06-04 16:40:00 +02:00
import sys
import os
2015-07-15 11:00:23 +02:00
import subprocess
2015-06-04 16:40:00 +02:00
try:
from docopt import docopt
from module_handler import ModuleHandler, get_dict_child
from module_handler import get_l_module_descendant
2019-01-14 15:20:51 +01:00
from qp_path import QP_SRC, QP_PLUGINS, QP_DATA
2015-06-04 16:40:00 +02:00
except ImportError:
2015-10-06 15:33:11 +02:00
print "Please check if you have sourced the ${QP_ROOT}/quantum_package.rc"
print "(`source ${QP_ROOT}/quantum_package.rc`)"
print sys.exit(1)
2015-06-04 16:40:00 +02:00
def save_new_module(path, l_child):
2019-01-14 15:20:51 +01:00
"""Creates a new module"""
2015-06-04 16:40:00 +02:00
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
# N E E D E D _ C H I L D R E N _ M O D U L E S #
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
try:
os.makedirs(path)
except OSError:
2015-10-06 15:33:11 +02:00
print "The module ({0}) already exists...".format(path)
2015-06-04 16:40:00 +02:00
sys.exit(1)
2018-10-19 16:33:58 +02:00
with open(os.path.join(path, "NEED"), "w") as f:
2015-06-04 16:40:00 +02:00
f.write(" ".join(l_child))
f.write("\n")
# ~#~#~#~#~#~#~ #
# R E A D _ M E #
# ~#~#~#~#~#~#~ #
module_name = os.path.basename(path)
header = "{0}\n{1}\n{0}\n".format("=" * len(module_name), module_name)
with open(os.path.join(path, "README.rst"), "w") as f:
f.write(header + "\n")
2018-12-28 16:09:36 +01:00
with open(os.path.join(path, ".gitignore"), "w") as f:
with open(os.path.join(QP_DATA, "module_gitignore"), "r") as g:
data = g.read()
f.write(data)
2019-01-14 15:20:51 +01:00
with open(os.path.join(path, "%s.irp.f"%(module_name)), "w") as f:
f.write("program {0}".format(module_name))
2016-02-23 09:21:29 +01:00
f.write("""
implicit none
BEGIN_DOC
! TODO : Put the documentation of the program here
END_DOC
2018-10-19 16:33:58 +02:00
print *, 'Hello world'
end
""")
def main(arguments):
2019-01-14 15:20:51 +01:00
"""Main function"""
arguments["<name>"] = [os.path.normpath(name) for name in arguments["<name>"]]
2018-12-28 15:06:27 +01:00
if arguments["list"]:
2019-01-14 22:37:24 +01:00
if arguments["--repositories"]:
l_result = [f for f in os.listdir(QP_PLUGINS) if f not in [".gitignore", "local"]]
2018-12-21 15:19:28 +01:00
else:
2019-01-15 00:48:28 +01:00
# Search in QP_PLUGINS all directories with a NEED file
2019-01-14 15:20:51 +01:00
l_tmp = [dirname for (dirname, _, filenames) in \
os.walk(QP_PLUGINS, followlinks=False) \
2019-01-15 00:48:28 +01:00
for f in filenames if f == 'NEED' and \
"IRPF90_temp" not in dirname]
2018-12-21 15:19:28 +01:00
# Find directories which contain modules
2019-01-14 15:20:51 +01:00
l_tmp = [os.path.split(f) for f in l_tmp]
2018-12-21 15:19:28 +01:00
d_tmp = {}
2019-01-14 15:20:51 +01:00
for (x, _) in l_tmp:
2018-12-21 15:19:28 +01:00
d_tmp[x] = 1
l_repository = d_tmp.keys()
2019-01-14 15:20:51 +01:00
m_instance = ModuleHandler(l_repository)
l_plugins = [module for module in m_instance.l_module]
2018-12-21 15:19:28 +01:00
l_result = l_plugins
2019-01-14 22:37:24 +01:00
if arguments["--installed"] or arguments["--uninstalled"]:
2018-12-21 15:19:28 +01:00
# Search in src all symbolic links that are modules
2019-01-14 22:37:24 +01:00
l_installed = [f for f in os.listdir(QP_SRC) \
2019-01-14 15:20:51 +01:00
if (os.path.islink(os.path.join(QP_SRC, f)) \
and f != ".gitignore")]
2018-12-21 15:19:28 +01:00
2019-01-14 22:37:24 +01:00
if arguments["--installed"]:
2019-01-14 15:20:51 +01:00
l_result = [f for f in l_plugins if f in l_installed]
2018-12-21 15:19:28 +01:00
2019-01-14 22:37:24 +01:00
elif arguments["--uninstalled"]:
2019-01-14 15:20:51 +01:00
l_result = [f for f in l_plugins if f not in l_installed]
for module in sorted(l_result):
2019-01-14 22:37:24 +01:00
print module
2019-01-14 15:20:51 +01:00
if arguments["create"]:
m_instance = ModuleHandler([QP_SRC])
l_children = arguments["<needed_modules>"]
name = arguments["<name>"][0]
2019-01-14 22:37:24 +01:00
if arguments["--repository"]:
repository = arguments["--repository"]
2018-12-21 15:13:33 +01:00
else:
repository = "local"
path = os.path.join(QP_PLUGINS, repository, name)
2015-06-04 16:40:00 +02:00
2018-12-21 11:41:28 +01:00
print "Created plugin:"
2015-10-06 15:33:11 +02:00
print path, '\n'
2015-06-04 16:40:00 +02:00
for children in l_children:
if children not in m_instance.dict_descendant:
2018-12-21 11:41:28 +01:00
print "Error: {0} is not a valid module.".format(children)
2015-06-04 16:40:00 +02:00
sys.exit(1)
2018-12-21 11:41:28 +01:00
print "Needed modules:"
2015-10-06 15:33:11 +02:00
print l_children, '\n'
2015-06-04 16:40:00 +02:00
2015-10-06 15:33:11 +02:00
print "This corresponds to using the following modules:"
print l_children + m_instance.l_descendant_unique(l_children), '\n'
2015-06-04 16:40:00 +02:00
2015-10-06 15:33:11 +02:00
print "Which is reduced to:"
l_child_reduce = m_instance.l_reduce_tree(l_children)
2015-10-06 15:33:11 +02:00
print l_child_reduce, '\n'
2015-07-16 11:39:52 +02:00
print "Installation",
2015-06-04 16:40:00 +02:00
save_new_module(path, l_child_reduce)
2015-07-16 11:39:52 +02:00
print " [ OK ]"
2015-10-06 15:33:11 +02:00
print ""
2019-01-14 15:20:51 +01:00
arguments["create"] = False
arguments["install"] = True
main(arguments)
2015-07-16 11:39:52 +02:00
elif arguments["download"]:
2018-12-21 11:41:28 +01:00
url = arguments["<url>"]
2019-01-14 15:20:51 +01:00
is_repo = not(url.endswith(".tar.gz") or \
url.endswith(".tgz") or \
url.endswith(".zip"))
2018-12-21 11:41:28 +01:00
os.chdir(QP_PLUGINS)
if is_repo:
2018-12-21 12:38:44 +01:00
subprocess.check_call(["git", "clone", url])
2018-12-21 11:41:28 +01:00
else:
filename = url.split('/')[-1]
import requests, shutil
try:
2019-01-14 15:20:51 +01:00
r = requests.get(url, verify=True, stream=True)
2018-12-21 11:41:28 +01:00
except:
2019-01-14 15:20:51 +01:00
r = requests.get(url, verify=False, stream=True)
2018-12-21 11:41:28 +01:00
r.raw.decode_content = True
with open(filename, 'wb') as f:
2019-01-14 15:20:51 +01:00
shutil.copyfileobj(r.raw, f)
2018-12-21 11:41:28 +01:00
if filename.endswith(".tar.gz") or \
2019-01-14 15:20:51 +01:00
filename.endswith(".tgz") or \
filename.endswith(".tar.bz2") or \
filename.endswith(".tar"):
subprocess.check_call(["tar", "xf", filename])
os.remove(filename)
2015-06-17 19:00:31 +02:00
elif arguments["install"]:
2015-06-23 10:04:59 +02:00
d_local = get_dict_child([QP_SRC])
2019-01-14 15:20:51 +01:00
l_tmp = [dirname for (dirname, _, filenames) in \
os.walk(QP_PLUGINS, followlinks=False) \
for f in filenames if f == 'NEED']
2018-12-21 18:35:56 +01:00
d_repo_of_plugin = {}
d_repo = {}
2019-01-14 15:20:51 +01:00
for (x, y) in [os.path.split(f) for f in l_tmp]:
2018-12-21 18:35:56 +01:00
d_repo_of_plugin[y] = x
d_repo[x] = None
l_repository = d_repo.keys()
d_plugin = get_dict_child(l_repository)
d_child = d_local.copy()
2015-06-17 19:00:31 +02:00
d_child.update(d_plugin)
2016-01-27 17:15:57 +01:00
normalize_case = {}
for name in d_local.keys() + d_plugin.keys():
2019-01-14 15:20:51 +01:00
normalize_case[name.lower()] = name
2016-01-27 17:15:57 +01:00
2019-01-14 15:20:51 +01:00
l_name = [normalize_case[name.lower()] for name in arguments["<name>"]]
2015-06-17 19:00:31 +02:00
for name in l_name:
2018-12-28 15:06:27 +01:00
2015-06-17 19:00:31 +02:00
if name in d_local:
print "{0} Is already installed".format(name)
2015-06-17 19:00:31 +02:00
l_module_descendant = get_l_module_descendant(d_child, l_name)
2015-06-17 19:00:31 +02:00
l_module_to_cp = [module for module in l_module_descendant if module not in d_local]
2015-06-17 19:00:31 +02:00
if l_module_to_cp:
2018-12-21 11:41:28 +01:00
print "Required dependencies:"
2015-06-17 19:00:31 +02:00
print l_module_to_cp
2015-06-17 19:00:31 +02:00
print "Installation...",
2015-06-17 19:00:31 +02:00
for module_to_cp in l_module_to_cp:
2019-01-14 15:20:51 +01:00
src = os.path.join(d_repo_of_plugin[module_to_cp], module_to_cp)
2018-12-21 18:35:56 +01:00
des = os.path.join(QP_SRC, module_to_cp)
try:
os.symlink(src, des)
2019-01-14 15:20:51 +01:00
install = os.path.join(src, "install")
2018-12-21 18:35:56 +01:00
if os.path.isfile(install):
wd = os.getcwd()
os.chdir(src)
subprocess.check_call([install])
os.chdir(wd)
except OSError:
print "The src directory is broken. Please remove %s" % des
raise
2015-07-16 17:45:42 +02:00
print "[ OK ]"
2015-06-22 10:37:17 +02:00
elif arguments["uninstall"]:
2015-06-23 10:04:59 +02:00
m_instance = ModuleHandler([QP_SRC])
d_descendant = m_instance.dict_descendant
d_local = get_dict_child([QP_SRC])
2015-06-22 10:37:17 +02:00
l_name = arguments["<name>"]
2015-06-23 10:04:59 +02:00
l_failed = [name for name in l_name if name not in d_local]
2015-07-15 11:00:23 +02:00
2015-06-22 10:37:17 +02:00
if l_failed:
2018-12-21 11:41:28 +01:00
print "Plugins not installed:"
2015-06-22 10:37:17 +02:00
for name in sorted(l_failed):
2019-01-14 22:37:24 +01:00
print "%s" % name
2015-06-22 10:37:17 +02:00
sys.exit(1)
2015-07-15 11:00:23 +02:00
2019-01-14 15:20:51 +01:00
l_name_to_remove = l_name + \
[module for module in m_instance.l_module \
for name in l_name if name in d_descendant[module]]
2015-07-15 11:00:23 +02:00
2018-12-21 11:41:28 +01:00
print "Removing plugins:"
2015-07-16 14:41:39 +02:00
print l_name_to_remove
2015-06-23 10:04:59 +02:00
2015-07-16 14:41:39 +02:00
for module in set(l_name_to_remove):
2015-06-23 10:04:59 +02:00
2018-12-21 12:38:44 +01:00
subprocess.check_call(["module_handler.py", "clean", module])
2015-06-23 10:04:59 +02:00
2015-07-16 14:41:39 +02:00
for module in set(l_name_to_remove):
2019-01-14 15:20:51 +01:00
uninstall = os.path.join(QP_SRC, module, "uninstall")
2018-12-21 12:38:44 +01:00
if os.path.isfile(uninstall):
subprocess.check_call([uninstall])
2015-07-15 11:00:23 +02:00
try:
os.unlink(os.path.join(QP_SRC, module))
except OSError:
2015-10-06 15:33:11 +02:00
print "%s is a core module which can't be removed" % module
if __name__ == '__main__':
2019-01-14 15:20:51 +01:00
ARG = docopt(__doc__)
main(ARG)