10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-09-27 12:00:56 +02:00
quantum_package/scripts/qp_plugins

297 lines
9.7 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:
2018-12-21 15:19:28 +01:00
qp_plugins list [ -i | -u | -q ]
2018-12-21 11:41:28 +01:00
qp_plugins download <url>
qp_plugins install <name>...
qp_plugins uninstall <name>
2018-12-21 15:13:33 +01:00
qp_plugins create -n <name> [-r <repository>] [<needed_modules>...]
2015-06-05 09:50:24 +02:00
Options:
2018-12-21 11:41:28 +01:00
list List all the plugins
-i List only the installed plugins
-u List only the uninstalled plugins
2018-12-21 15:19:28 +01:00
-q List the external repositories
2018-12-21 11:41:28 +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
install Install a plugin
uninstall Uninstall a plugin
create -n <name> Create a new plugin named <name>
2018-12-21 15:13:33 +01:00
-r <repository> Name of the repository in which to create the plugin
2018-12-21 12:38:44 +01:00
2018-12-21 11:41:28 +01:00
"""
2015-06-04 16:40:00 +02:00
import sys
import os
2015-07-15 11:00:23 +02:00
import subprocess
from os import listdir
from os.path import isdir, join
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
2015-07-16 17:45:42 +02:00
from qp_path import QP_SRC, QP_PLUGINS, QP_ROOT
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):
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
# 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-10-19 16:33:58 +02: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):
2018-12-28 15:06:27 +01:00
arguments["<name>"] = [ os.path.normpath(name) for name in arguments["<name>"] ]
if arguments["list"]:
2018-12-21 15:19:28 +01:00
if arguments["-q"]:
l_result = [ f for f in listdir(QP_PLUGINS) if f not in [ ".gitignore", "local" ] ]
else:
# Search in src all directories with a NEED file
l_tmp = [ dirname for (dirname, _, filenames) in os.walk(QP_PLUGINS, followlinks=False) for f in filenames if f == 'NEED']
# Find directories which contain modules
l_tmp = [ os.path.split(f) for f in l_tmp ]
d_tmp = {}
for (x,_) in l_tmp:
d_tmp[x] = 1
l_repository = d_tmp.keys()
m_all_instances = ModuleHandler(l_repository)
m_instance = ModuleHandler(l_repository)
l_plugins = [ module for module in m_instance.l_module ]
l_result = l_plugins
if arguments["-i"] or arguments["-u"]:
# Search in src all symbolic links that are modules
l_installed = [ f for f in listdir(QP_SRC) if (os.path.islink(os.path.join(QP_SRC,f)) and f != ".gitignore") ]
if arguments["-i"]:
l_result = [ f for f in l_plugins if f in l_installed ]
elif arguments["-u"]:
l_result = [ f for f in l_plugins if f not in l_installed ]
2018-12-21 11:41:28 +01:00
for module in sorted(l_result):
print "* {0}".format(module)
if arguments["create"]:
m_instance = ModuleHandler([QP_SRC])
2018-12-21 15:05:27 +01:00
print arguments
l_children = arguments["<needed_modules>"]
name = arguments["<name>"][0]
2018-12-21 15:13:33 +01:00
if arguments["-r"]:
repository = arguments["-r"]
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 ""
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>"]
is_repo = not( url.endswith(".tar.gz") or \
url.endswith(".tgz") or \
url.endswith(".zip") \
)
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:
r = requests.get(url,verify=True,stream=True)
except:
r = requests.get(url,verify=False,stream=True)
r.raw.decode_content = True
with open(filename, 'wb') as f:
shutil.copyfileobj(r.raw, f)
if filename.endswith(".tar.gz") or \
filename.endswith(".tgz") or \
filename.endswith(".tar.bz2") or \
filename.endswith(".tar"):
2018-12-21 12:38:44 +01:00
subprocess.check_call(["tar", "xf", filename])
2018-12-21 11:41:28 +01:00
os.remove(filename)
2015-06-17 19:00:31 +02:00
elif arguments["install"]:
# Python 2.6 ...
d_module_location= dict()
2015-06-23 10:04:59 +02:00
d_local = get_dict_child([QP_SRC])
2018-12-21 18:35:56 +01:00
# d_plugin = get_dict_child([join(QP_PLUGINS, f) for f in listdir(QP_PLUGINS) if isdir(join(QP_PLUGINS, f))])
l_tmp = [ dirname for (dirname, _, filenames) in os.walk(QP_PLUGINS, followlinks=False) for f in filenames if f == 'NEED']
d_repo_of_plugin = {}
d_repo = {}
for (x,y) in [ os.path.split(f) for f in l_tmp ]:
d_repo_of_plugin[y] = x
d_repo[x] = None
l_repository = d_repo.keys()
# m_all_instances = ModuleHandler(l_repository)
# m_instance = ModuleHandler(l_repository)
# l_plugins = [ module for module in m_instance.l_module ]
# print l_plugins
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():
normalize_case [ name.lower() ] = name
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
print name
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:
2018-12-21 18:35:56 +01:00
src = os.path.join( d_repo_of_plugin[module_to_cp], module_to_cp )
des = os.path.join(QP_SRC, module_to_cp)
try:
os.symlink(src, des)
install = os.path.join(src,"install")
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):
2015-06-23 10:04:59 +02:00
print "* %s" % name
2015-06-22 10:37:17 +02:00
sys.exit(1)
2015-07-15 11:00:23 +02:00
2015-07-16 14:41:39 +02: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):
2018-12-21 12:38:44 +01:00
uninstall = os.path.join(QP_SRC,module,"uninstall")
print uninstall
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__':
arguments = docopt(__doc__)
main(arguments)