Esempio n. 1
0
    def get(self,
            product,
            product_path,
            target_path,
            use_enclosing_directory,
            use_symlinks=None):
        if use_symlinks is None:
            use_symlinks = self._use_symlinks

        try:
            if use_symlinks:
                if use_enclosing_directory:
                    for basename in os.listdir(product_path):
                        os.symlink(os.path.join(product_path, basename),
                                   os.path.join(target_path, basename))
                else:
                    os.symlink(
                        product_path,
                        os.path.join(target_path,
                                     os.path.basename(product_path)))
            else:
                if use_enclosing_directory:
                    for basename in os.listdir(product_path):
                        util.copy_path(os.path.join(product_path, basename),
                                       target_path,
                                       resolve_root=True)
                else:
                    util.copy_path(product_path,
                                   target_path,
                                   resolve_root=True)

        except EnvironmentError as _error:
            raise Error("unable to retrieve product '%s' (%s) [%s]" %
                        (product.core.product_name, product.core.uuid, _error))
Esempio n. 2
0
    def put(self,
            paths,
            properties,
            use_enclosing_directory,
            use_symlinks=None,
            retrieve_files=None):
        if use_symlinks is None:
            use_symlinks = self._use_symlinks

        physical_name = properties.core.physical_name
        archive_path = properties.core.archive_path
        uuid = properties.core.uuid

        abs_archive_path = os.path.realpath(
            os.path.join(self._root, archive_path))
        abs_product_path = os.path.join(abs_archive_path, physical_name)

        # TODO separate this out like 'current_archive_path'
        if paths is not None and util.is_sub_path(os.path.realpath(paths[0]),
                                                  abs_product_path,
                                                  allow_equal=True):
            # Product should already be in the target location
            for path in paths:
                if not os.path.exists(path):
                    raise Error("product source path does not exist '%s'" %
                                (path, ))
                if not util.is_sub_path(os.path.realpath(path),
                                        abs_product_path,
                                        allow_equal=True):
                    raise Error(
                        "cannot ingest product where only part of the files are already at the "
                        "destination location")
        else:
            # Create destination location for product
            try:
                util.make_path(abs_archive_path)
            except EnvironmentError as _error:
                raise Error("cannot create parent destination path '%s' [%s]" %
                            (abs_archive_path, _error))

            # Create a temporary directory and transfer the product there, then move the product to its
            # destination within the archive.
            try:
                tmp_root = self.get_tmp_root(properties)
                with util.TemporaryDirectory(prefix=".put-",
                                             suffix="-%s" % uuid.hex,
                                             dir=tmp_root) as tmp_path:

                    # Create enclosing directory if required.
                    if use_enclosing_directory:
                        tmp_path = os.path.join(tmp_path, physical_name)
                        util.make_path(tmp_path)

                    # Transfer the product (parts).
                    if use_symlinks:
                        if use_enclosing_directory:
                            abs_path = abs_product_path
                        else:
                            abs_path = abs_archive_path

                        # Create symbolic link(s) for the product (parts).
                        for path in paths:
                            if util.is_sub_path(path, self._root):
                                # Create a relative symbolic link when the target is part of the archive
                                # (i.e. when creating an intra-archive symbolic link). This ensures the
                                # archive can be relocated without breaking intra-archive symbolic links.
                                os.symlink(
                                    os.path.relpath(path, abs_path),
                                    os.path.join(tmp_path,
                                                 os.path.basename(path)))
                            else:
                                os.symlink(
                                    path,
                                    os.path.join(tmp_path,
                                                 os.path.basename(path)))
                    else:
                        # Copy/retrieve product (parts).
                        if retrieve_files:
                            paths = retrieve_files(tmp_path)
                        else:
                            for path in paths:
                                util.copy_path(path,
                                               tmp_path,
                                               resolve_root=True)

                    # Move the transferred product into its destination within the archive.
                    if use_enclosing_directory:
                        os.rename(tmp_path, abs_product_path)
                    else:
                        assert (len(paths) == 1 and os.path.basename(paths[0])
                                == physical_name)
                        tmp_product_path = os.path.join(
                            tmp_path, physical_name)
                        os.rename(tmp_product_path, abs_product_path)

            except EnvironmentError as _error:
                raise Error(
                    "unable to transfer product to destination path '%s' [%s]"
                    % (abs_product_path, _error))
Esempio n. 3
0
 def pull(self, archive, product, target_dir):
     source_path = urlparse(product.core.remote_url).path
     target_path = os.path.join(target_dir, os.path.basename(source_path))
     util.copy_path(source_path, target_path)
     return self.auto_extract(target_path, product)