From e8c5dc4da050a8a15574d662062ee31f34535d79 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 2 Aug 2019 00:06:23 +0200 Subject: [PATCH] [DEV] add dynamic links --- island/actions/islandAction_sync-local.py | 6 ++++ island/actions/islandAction_sync.py | 8 ++++- island/actions/update_links.py | 43 +++++++++++++++++++++++ island/config.py | 35 +++++++++++++++++- island/manifest.py | 34 +++++++++++++++++- 5 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 island/actions/update_links.py diff --git a/island/actions/islandAction_sync-local.py b/island/actions/islandAction_sync-local.py index 5eff128..4113e36 100644 --- a/island/actions/islandAction_sync-local.py +++ b/island/actions/islandAction_sync-local.py @@ -15,6 +15,7 @@ from island import config from island import multiprocess from island import manifest from island import commands +import update_links import os @@ -148,6 +149,11 @@ def execute(_arguments): debug.info(" ** local sync partial warning on " + str(count_error) + " repository") debug.info(" ***********************************************************") 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 diff --git a/island/actions/islandAction_sync.py b/island/actions/islandAction_sync.py index e8d9a77..d99b89b 100644 --- a/island/actions/islandAction_sync.py +++ b/island/actions/islandAction_sync.py @@ -15,6 +15,7 @@ from island import config from island import multiprocess from island import manifest from island import commands +import update_links import os ## @@ -179,4 +180,9 @@ def execute(_arguments): and os.path.exists(os.path.join(git_repo_path, ".gitmodules")) == True: debug.info(" ==> sync submodule") commands.submodule_sync(git_repo_path) - \ No newline at end of file + + ## Update the links: + have_error = update_links.update(configuration, mani, "sync-local") + if have_error == True: + return -1 + return None \ No newline at end of file diff --git a/island/actions/update_links.py b/island/actions/update_links.py new file mode 100644 index 0000000..a5c4141 --- /dev/null +++ b/island/actions/update_links.py @@ -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() \ No newline at end of file diff --git a/island/config.py b/island/config.py index 02db0ec..279340f 100644 --- a/island/config.py +++ b/island/config.py @@ -28,6 +28,7 @@ class Config(): self._branch = "master" self._manifest_name = "default.xml" self._volatiles = [] + self._curent_link = [] self.load() # set it deprecated at 2020/07 @@ -62,6 +63,7 @@ class Config(): if os.path.exists(env.get_island_path_config()) == False: return True self._volatiles = [] + self._curent_link = [] with open(env.get_island_path_config()) as json_file: data = json.load(json_file) if "repo" in data.keys(): @@ -74,6 +76,10 @@ class Config(): for elem in data["volatiles"]: if "git_address" in elem.keys() and "path" in elem.keys(): 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 False @@ -83,6 +89,7 @@ class Config(): data["branch"] = self._branch data["manifest_name"] = self._manifest_name data["volatiles"] = self._volatiles + data["link"] = self._curent_link with open(env.get_island_path_config(), 'w') as outfile: json.dump(data, outfile, indent=4) return True @@ -119,4 +126,30 @@ class Config(): def get_volatile(self): 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 = [] + + diff --git a/island/manifest.py b/island/manifest.py index 5655120..b7edf76 100644 --- a/island/manifest.py +++ b/island/manifest.py @@ -16,6 +16,7 @@ from realog import debug from . import tools from . import env from . import multiprocess +from . import config from lxml import etree @@ -28,6 +29,12 @@ class RepoConfig(): self.branch = "" self.volatile = False +class LinkConfig(): + def __init__(self): + self.source = "" + self.destination = "" + + def is_lutin_init(): if os.path.exists(env.get_island_path()) == False: debug.verbose("Lutin is not init: path does not exist: '" + env.get_island_path() + "'") @@ -59,11 +66,15 @@ class Manifest(): } self.remotes = [] self.includes = [] + self.links = [] # load the manifest self._load() # check error in manifest (double path ...) self._check_double_path([]) + def get_links(self): + return self.links + def _load(self): tree = etree.parse(self.manifest_xml) debug.debug("manifest : '" + self.manifest_xml + "'") @@ -224,8 +235,29 @@ class Manifest(): if child.tag == "option": # not managed ==> future use 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.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: for elem in self.includes: elem["manifest"] = Manifest(elem["path"])