Ejemplo n.º 1
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
Ejemplo n.º 2
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