[DEV] add dynamic links

This commit is contained in:
Edouard DUPIN 2019-08-02 00:06:23 +02:00
parent e604008a27
commit e8c5dc4da0
5 changed files with 123 additions and 3 deletions

View File

@ -15,6 +15,7 @@ from island import config
from island import multiprocess from island import multiprocess
from island import manifest from island import manifest
from island import commands from island import commands
import update_links
import os import os
@ -148,6 +149,11 @@ def execute(_arguments):
debug.info(" ** local sync partial warning on " + str(count_error) + " repository") debug.info(" ** local sync partial warning on " + str(count_error) + " repository")
debug.info(" ***********************************************************") debug.info(" ***********************************************************")
return env.ret_action_partial_done return env.ret_action_partial_done
## Update the links:
have_error = update_links.update(configuration, mani, "sync-local")
if have_error == True:
return -1
return None return None

View File

@ -15,6 +15,7 @@ from island import config
from island import multiprocess from island import multiprocess
from island import manifest from island import manifest
from island import commands from island import commands
import update_links
import os import os
## ##
@ -180,3 +181,8 @@ def execute(_arguments):
debug.info(" ==> sync submodule") debug.info(" ==> sync submodule")
commands.submodule_sync(git_repo_path) commands.submodule_sync(git_repo_path)
## Update the links:
have_error = update_links.update(configuration, mani, "sync-local")
if have_error == True:
return -1
return None

View File

@ -0,0 +1,43 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
##
## @author Edouard DUPIN
##
## @copyright 2012, Edouard DUPIN, all right reserved
##
## @license MPL v2.0 (see license file)
##
from realog import debug
from island import tools
from island import env
import os
## Update the links:
def update(configuration, mani, type_call):
# TODO: do not remove link when not needed
if len(configuration.get_links()) != 0 \
or len(mani.get_links()) != 0:
debug.info(type_call + ": remove old links ...")
for elem in configuration.get_links():
base_path = os.path.join(env.get_island_root_path(), elem["destination"])
debug.info(type_call + ": link: " + str(base_path))
if os.path.islink(base_path) == True:
os.unlink(base_path)
else:
debug.error(type_call + ": remove link is not authorised ==> not a link", crash=False)
have_error = True
configuration.clear_links()
debug.info(type_call + ": add new links ...")
for elem in mani.get_links():
base_path = os.path.join(env.get_island_root_path(), elem["destination"])
source_path = os.path.join(env.get_island_root_path(), elem["source"])
debug.info(type_call + ": link: " + str(base_path))
if os.path.exists(base_path) == True:
debug.error(type_call + ": create link is not possible ==> path already exist", crash=False)
have_error = True
else:
tools.create_directory_of_file(base_path)
os.symlink(source_path, base_path)
configuration.add_link(elem["source"], elem["destination"])
configuration.store()

View File

@ -28,6 +28,7 @@ class Config():
self._branch = "master" self._branch = "master"
self._manifest_name = "default.xml" self._manifest_name = "default.xml"
self._volatiles = [] self._volatiles = []
self._curent_link = []
self.load() self.load()
# set it deprecated at 2020/07 # set it deprecated at 2020/07
@ -62,6 +63,7 @@ class Config():
if os.path.exists(env.get_island_path_config()) == False: if os.path.exists(env.get_island_path_config()) == False:
return True return True
self._volatiles = [] self._volatiles = []
self._curent_link = []
with open(env.get_island_path_config()) as json_file: with open(env.get_island_path_config()) as json_file:
data = json.load(json_file) data = json.load(json_file)
if "repo" in data.keys(): if "repo" in data.keys():
@ -74,6 +76,10 @@ class Config():
for elem in data["volatiles"]: for elem in data["volatiles"]:
if "git_address" in elem.keys() and "path" in elem.keys(): if "git_address" in elem.keys() and "path" in elem.keys():
self.add_volatile(elem["git_address"], elem["path"]) self.add_volatile(elem["git_address"], elem["path"])
if "link" in data.keys():
for elem in data["link"]:
if "source" in elem.keys() and "destination" in elem.keys():
self.add_link(elem["source"], elem["destination"])
return True return True
return False return False
@ -83,6 +89,7 @@ class Config():
data["branch"] = self._branch data["branch"] = self._branch
data["manifest_name"] = self._manifest_name data["manifest_name"] = self._manifest_name
data["volatiles"] = self._volatiles data["volatiles"] = self._volatiles
data["link"] = self._curent_link
with open(env.get_island_path_config(), 'w') as outfile: with open(env.get_island_path_config(), 'w') as outfile:
json.dump(data, outfile, indent=4) json.dump(data, outfile, indent=4)
return True return True
@ -120,3 +127,29 @@ class Config():
def get_volatile(self): def get_volatile(self):
return copy.deepcopy(self._volatiles) return copy.deepcopy(self._volatiles)
def get_links(self):
return self._curent_link
def add_link(self, source, destination):
for elem in self._curent_link:
if elem["destination"] == destination:
debug.error("can not have multiple destination folder in link " + destination, crash=False)
return False
self._curent_link.append( {
"source": source,
"destination": destination
})
return True
def remove_link(self, destination):
for elem in self._curent_link:
if elem["destination"] == destination:
del self._curent_link[elem]
return
debug.warning("Request remove link that does not exist")
def clear_links(self):
self._curent_link = []

View File

@ -16,6 +16,7 @@ from realog import debug
from . import tools from . import tools
from . import env from . import env
from . import multiprocess from . import multiprocess
from . import config
from lxml import etree from lxml import etree
@ -28,6 +29,12 @@ class RepoConfig():
self.branch = "" self.branch = ""
self.volatile = False self.volatile = False
class LinkConfig():
def __init__(self):
self.source = ""
self.destination = ""
def is_lutin_init(): def is_lutin_init():
if os.path.exists(env.get_island_path()) == False: if os.path.exists(env.get_island_path()) == False:
debug.verbose("Lutin is not init: path does not exist: '" + env.get_island_path() + "'") debug.verbose("Lutin is not init: path does not exist: '" + env.get_island_path() + "'")
@ -59,11 +66,15 @@ class Manifest():
} }
self.remotes = [] self.remotes = []
self.includes = [] self.includes = []
self.links = []
# load the manifest # load the manifest
self._load() self._load()
# check error in manifest (double path ...) # check error in manifest (double path ...)
self._check_double_path([]) self._check_double_path([])
def get_links(self):
return self.links
def _load(self): def _load(self):
tree = etree.parse(self.manifest_xml) tree = etree.parse(self.manifest_xml)
debug.debug("manifest : '" + self.manifest_xml + "'") debug.debug("manifest : '" + self.manifest_xml + "'")
@ -224,8 +235,29 @@ class Manifest():
if child.tag == "option": if child.tag == "option":
# not managed ==> future use # not managed ==> future use
continue continue
if child.tag == "link":
# not managed ==> future use
source = ""
destination = ""
for attr in child.attrib:
if attr == "source":
source = child.attrib[attr]
elif attr == "destination":
destination = child.attrib[attr]
else:
debug.error("(l:" + str(child.sourceline) + ") Parsing the manifest: Unknow '" + child.tag + "' attibute : '" + attr + "', availlable:[source,destination]")
if source == "":
debug.error("(l:" + str(child.sourceline) + ") Parsing the manifest: '" + child.tag + "' missing attribute: 'source' ==> specify the git to clone ...")
if destination == "":
debug.error("(l:" + str(child.sourceline) + ") Parsing the manifest: '" + child.tag + "' missing attribute: 'destination' ==> specify the git to clone ...")
self.links.append({
"source":source,
"destination":destination,
})
debug.warning("Add link: '" + str(destination) + "' ==> '" + str(source) + "'")
continue
debug.info("(l:" + str(child.sourceline) + ") '" + str(child.tag) + "' values=" + str(child.attrib)); debug.info("(l:" + str(child.sourceline) + ") '" + str(child.tag) + "' values=" + str(child.attrib));
debug.error("(l:" + str(child.sourceline) + ") Parsing error Unknow NODE : '" + str(child.tag) + "' availlable:[remote,include,default,project]") debug.error("(l:" + str(child.sourceline) + ") Parsing error Unknow NODE : '" + str(child.tag) + "' availlable:[remote,include,default,project,option,link]")
# now we parse all sub repo: # now we parse all sub repo:
for elem in self.includes: for elem in self.includes:
elem["manifest"] = Manifest(elem["path"]) elem["manifest"] = Manifest(elem["path"])