Esempio n. 1
0
    def _make_links(self, links=None, hard=False):
        """
        Try to create the links (symbolic or hard)

        :param links: List of links to create
        :type links: list
        :param hard: Create hard link
        :type hard: boole
        :return: Number of created link(s)
        :rtype: int
        :raises SystemExit: If link(s) cannot be created
        """
        if not links or not len(links):
            return 0

        for slink, tlink in links:
            if not os.path.exists(tlink) and not os.path.islink(tlink):
                if Manager.get_simulate() and Manager.get_verbose():
                    Utils.verbose("Linking %s -> %s" % (tlink, os.path.relpath(slink, start=self.target)))
                else:
                    try:
                        if not Manager.get_simulate():
                            source_link = os.path.relpath(slink, start=self.target)
                            if hard:
                                os.link(source_link, tlink)
                            else:
                                os.symlink(source_link, tlink)
                    except OSError as err:
                        Utils.error("[%s] Can't create %slink %s: %s" %
                                    (self.manager.bank.name, 'hard ' if hard else 'sym', tlink, str(err)))
                    self.add_link()
        return self.created_links
Esempio n. 2
0
    def _generate_dir_link(self, source=None, target=None, hard=False, fallback=None, requires=None, limit=0):
        """
        Create a symbolic link between 'source' and 'target' for a directory

        :param source: Source directory to link
        :type source: str
        :param target: Destination directory name (relative to config param 'production.dir')
        :type target: str
        :param hard: Create hard link instead of symlink
        :type hard: bool (default False)
        :param fallback: Alternative source if source does not exist
        :type fallback: str
        :param requires: A required directory
        :type requires: str
        :param limit: Limit deepest search to `limit` depth, default 0, no limit
        :type limit: int
        :return: Number of created link(s)
        :rtype: int
        """
        if not self._prepare_links(source=source, target=target, fallback=fallback,
                                   requires=requires, get_deepest=True, limit=limit):
            return 0

        slink = os.path.join(self.source)
        tlink = os.path.join(self.target, self.manager.bank.name)

        self._make_links(links=[(slink, tlink)], hard=hard)

        if Manager.get_simulate() and Manager.get_verbose():
            Utils.verbose("%s -> %s directory link done" % (self.target, self.source))
        return self.created_links
Esempio n. 3
0
    def _generate_files_link(self, source=None, target=None, remove_ext=False):
        """
        Links list of file from 'source' to 'target' directory.

        If remove_ext is set to True, then another link is created. This link is the same as the
        target link, without the file extension

        :param source: Source directory to link
        :type source: str
        :param target: Destination directory name (relative to config param 'production_dir')
        :type target: str
        :param remove_ext: Create another link of the file without the file name extension
        :type remove_ext: bool (default False)
        :return: Number of created link(s)
        :rtype: int
        """
        if not self._prepare_links(source=source, target=target, get_deepest=True):
            return 0

        # Get files in the source directory
        files = Utils.get_files(path=self.source)
        links = []

        for ffile in files:
            # Source file link
            slink = os.path.join(self.source, ffile)
            tlink = os.path.join(self.target, ffile)
            links.append((slink, tlink))
            if Manager.get_verbose():
                Utils.verbose("[_generate_files_link] append slink %s" % slink)
                Utils.verbose("[_generate_files_link] append tlink %s" % tlink)

            # If asked to create another symbolic link without extension name
            if remove_ext:
                new_file = os.path.splitext(os.path.basename(ffile))[0]
                tlink = os.path.join(self.target, new_file)
                links.append((slink, tlink))
                if Manager.get_verbose():
                    Utils.verbose("[_generate_files_link] [rm_ext=%s] append slink %s" % (str(remove_ext), slink))
                    Utils.verbose("[_generate_files_link] [rm_ext=%s] append tlink %s" % (str(remove_ext), tlink))

        self._make_links(links=links)

        if Manager.get_simulate() and Manager.get_verbose():
            Utils.verbose("%s -> %s file link done" % (self.target, self.source))
        return self.created_links
Esempio n. 4
0
    def _prepare_links(self, source=None, target=None, get_deepest=False, fallback=None, requires=None, limit=0):
        """
        Prepare stuff to create links

        :param source: Source path
        :type source: str
        :param target: Destination path
        :type target: str
        :param get_deepest: Try to find deepest directory(ies) from source
        :type get_deepest: bool
        :param fallback: Alternative source if source does not exist
        :type fallback: str
        :param requires: A required file or directory
        :type requires: str
        :param limit: Limit deepest search to `limit` depth, default 0, no limit
        :type limit: int
        :return: Boolean
        :rtype: bool
        :raises SystemExit: If 'source' or 'target' are None
        :raises SystemExit: If 'data.dir' not set in :py:data:`global.properties`
        :raises SystemExit: If 'production.dir' not set in :py:data:`manager.properties`
        :raises SystemExit: If 'target' directory cannot be created
        """
        self._check_source_target_parameters(source=source, target=target)
        data_dir = self.bank_data_dir
        source = os.path.join(data_dir, source)
        target_dir = self.manager.config.get('MANAGER', 'production.dir')
        bank_name = self.manager.bank.name

        if requires is not None:
            if not os.path.exists(os.path.join(data_dir, requires)):
                Utils.warn("[%s] Can't create %s, requires param %s not here." % (bank_name, source, requires))
                return False

        if not os.path.isdir(source):
            if fallback is None:
                if self.manager.get_verbose():
                    Utils.warn("[%s] %s does not exist" % (bank_name, source))
                return False
            else:
                if self.manager.get_verbose():
                    Utils.verbose("[%s] %s does not exist. Fallback to %s" % (bank_name, source, fallback))
                source = os.path.join(data_dir, fallback)
                if not os.path.isdir(source):
                    if self.manager.get_verbose():
                        Utils.warn("[%s] Fallback %s does not exist" % (bank_name, source))
                        return False

        if get_deepest:
            source = Utils.get_deepest_dir(source, full=get_deepest, limit=limit)
        target = os.path.join(target_dir, target)

        # Check destination directory where to create link(s)
        if not os.path.exists(target) and not os.path.isdir(target):
            if Manager.get_simulate() and Manager.get_verbose():
                Utils.verbose("[_prepare_links] [%s] Creating directory %s" % (bank_name, target))
            else:
                try:
                    if not Manager.get_simulate():
                        os.makedirs(target)
                except OSError as err:
                    Utils.error("[%s] Can't create %s dir: %s" % (bank_name, target, str(err)))

        self.source = source
        self.target = target
        if Manager.get_verbose():
            Utils.verbose("[prepare_links] source %s" % self.source)
            Utils.verbose("[prepare_links] target %s" % self.target)
        return True
Esempio n. 5
0
    def _clone_structure(self, source=None, target=None, remove_ext=False, limit=0):
        """
        Create a directory structure from a source to a target point and link all files from source inside target

        :param source: Source directory to clone
        :type source: str
        :param target: Destination directory to create if does not exist
        :type target: str
        :param remove_ext: Create another link of the file without the file name extension
        :type remove_ext: bool
        :param limit: Limit subtree seach to value, default 0, no limit
        :tpye limit: int
        :return: True if structure cloning build OK, throws otherwise
        :rtype: bool
        :raise SystemExit: If error occurred during directory structure building
        """
        self._check_source_target_parameters(source=source, target=target)
        # Check do_links.clone_dirs. As we want to recreate the same architecture as for the source,
        # we need to recreate the target because Utils.get_subtree removes the source path which contains
        # the target name
        target = os.path.join(target, source)
        source = os.path.join(self.bank_data_dir, source)
        subtrees = Utils.get_subtree(path=source, limit=limit)

        try:
            for subtree in subtrees:
                end_target = os.path.join(self.prod_dir, target, subtree)
                if not os.path.exists(end_target) and not os.path.isdir(end_target):
                    if Manager.get_simulate() and Manager.get_verbose():
                        Utils.verbose("[_clone_structure] [%s] Creating directory %s" % (self.bank_name, end_target))
                    else:
                        if not Manager.get_simulate():
                            os.makedirs(end_target)

                sub_files = Utils.get_files(path=os.path.join(source, subtree))
                if len(sub_files) == 0:
                    continue

                links = []
                for ffile in sub_files:
                    # Source file link
                    slink = os.path.join(source, subtree, ffile)
                    tlink = os.path.join(end_target, ffile)
                    links.append((slink, tlink))
                    if Manager.get_verbose():
                        Utils.verbose("[_generate_files_link] append slink %s" % slink)
                        Utils.verbose("[_generate_files_link] append tlink %s" % tlink)
                        # If asked to create another symbolic link without extension name
                    if remove_ext:
                        new_file = os.path.splitext(os.path.basename(ffile))[0]
                        tlink = os.path.join(end_target, new_file)
                        links.append((slink, tlink))
                        if Manager.get_verbose():
                            Utils.verbose("[_generate_files_link] [rm_ext=%s] append slink %s"
                                          % (str(remove_ext), slink))
                            Utils.verbose("[_generate_files_link] [rm_ext=%s] append tlink %s"
                                          % (str(remove_ext), tlink))
                # Set self.target for _make_links
                self.target = end_target
                self._make_links(links=links)
        except OSError as err:
            Utils.error("[%s] Can't create %s dir: %s (%s)" % (self.bank_name, end_target, str(err),
                                                               os.access(end_target, os.W_OK)))

        return True