def upload_bundle_zip(self, file_uuid, construct_args, worksheet_uuid, follow_symlinks, add_to_worksheet):
     '''
     |file_uuid| specifies a pointer to the temporary file X.
     - If X is a non-zip file, then just upload X as an ordinary file.
     - If X is a zip file containing one file/directory Y representing bundle, then upload Y.
     - If X is a zip file containing multiple files/directories, then upload X.
     Return the new bundle's uuid.
     Note: delete the file_uuid file and X if needed (these are temporary files).
     '''
     if file_uuid:
         orig_path = self.file_paths[file_uuid]  # Note: cheat and look at file_server's data
         precondition(orig_path, 'Unexpected file uuid: %s' % (file_uuid,))
         if zip_util.is_zip_file(orig_path):
             container_path = tempfile.mkdtemp()  # Make temporary directory
             zip_util.unzip(orig_path, container_path, file_name=None)  # Unzip into a directory
             # If the container path only has one item, then make that the final path
             sub_files = os.listdir(container_path)
             if len(sub_files) == 1:
                 final_path = os.path.join(container_path, sub_files[0])
             else:  # Otherwise, use the container path
                 final_path = container_path
                 container_path = None
         else:
             # Not a zip file!  Just upload it normally as a file.
             final_path = orig_path
             container_path = None  # Don't need to delete
     else:
         final_path = None
     result = self.client.upload_bundle(final_path, construct_args, worksheet_uuid, follow_symlinks, exclude_patterns=[], add_to_worksheet=add_to_worksheet)
     if file_uuid:
         if container_path:
             path_util.remove(container_path)  # Remove temporary directory
         self.finalize_file(file_uuid, final_path != orig_path)  # Remove temporary file
     return result
Beispiel #2
0
 def upload_zip(self, bundle_type, file_uuid, metadata, worksheet_uuid=None):
   '''
   Unzip the zip in the temp file identified by the given file uuid and then
   upload the unzipped directory. Return the new bundle's id.
   '''
   zip_path = self.temp_file_paths.pop(file_uuid, None)
   precondition(zip_path, 'Unexpected file uuid: %s' % (file_uuid,))
   path = zip_util.unzip(zip_path)
   return self.client.upload(bundle_type, path, metadata, worksheet_uuid)
 def upload_bundle_zip(self, file_uuid, construct_args, worksheet_uuid,
                       follow_symlinks, add_to_worksheet):
     '''
     |file_uuid| specifies a pointer to the temporary file X.
     - If X is a non-zip file, then just upload X as an ordinary file.
     - If X is a zip file containing one file/directory Y representing bundle, then upload Y.
     - If X is a zip file containing multiple files/directories, then upload X.
     Return the new bundle's uuid.
     Note: delete the file_uuid file and X if needed (these are temporary files).
     '''
     if file_uuid:
         orig_path = self.file_paths[
             file_uuid]  # Note: cheat and look at file_server's data
         precondition(orig_path, 'Unexpected file uuid: %s' % (file_uuid, ))
         if zip_util.is_zip_file(orig_path):
             container_path = tempfile.mkdtemp()  # Make temporary directory
             zip_util.unzip(orig_path, container_path,
                            file_name=None)  # Unzip into a directory
             # If the container path only has one item, then make that the final path
             sub_files = os.listdir(container_path)
             if len(sub_files) == 1:
                 final_path = os.path.join(container_path, sub_files[0])
             else:  # Otherwise, use the container path
                 final_path = container_path
                 container_path = None
         else:
             # Not a zip file!  Just upload it normally as a file.
             final_path = orig_path
             container_path = None  # Don't need to delete
     else:
         final_path = None
     result = self.client.upload_bundle(final_path,
                                        construct_args,
                                        worksheet_uuid,
                                        follow_symlinks,
                                        exclude_patterns=[],
                                        add_to_worksheet=add_to_worksheet)
     if file_uuid:
         if container_path:
             path_util.remove(container_path)  # Remove temporary directory
         self.finalize_file(
             file_uuid, final_path != orig_path)  # Remove temporary file
     return result
Beispiel #4
0
 def upload_bundle_zip(self, file_uuid, construct_args, worksheet_uuid, follow_symlinks):
     '''
     Unzip the zip in the temp file identified by the given file uuid and then
     upload the unzipped directory. Return the new bundle's id.
     Note: delete the file_uuid file, because it's temporary!
     '''
     if file_uuid:
         zip_path = self.file_paths[file_uuid]  # Note: cheat and look at file_server's data
         precondition(zip_path, 'Unexpected file uuid: %s' % (file_uuid,))
         container_path = tempfile.mkdtemp()  # Make temporary directory
         path = zip_util.unzip(zip_path, container_path)  # Unzip
     else:
         path = None
     result = self.client.upload_bundle(path, construct_args, worksheet_uuid, follow_symlinks)
     if file_uuid:
         path_util.remove(container_path)  # Remove temporary directory
         self.finalize_file(file_uuid, True)  # Remove temporary zip
     return result
    def download_target(self, target, follow_symlinks, return_zip=False):
        # Create remote zip file, download to local zip file
        (fd, zip_path) = tempfile.mkstemp(dir=tempfile.gettempdir())
        os.close(fd)
        source_uuid, name = self.open_target_zip(target, follow_symlinks)
        source = RPCFileHandle(source_uuid, self.proxy)
        with open(zip_path, 'wb') as dest:
            with contextlib.closing(source):
                file_util.copy(source, dest, autoflush=False, print_status='Downloading %s on %s to %s' % ('/'.join(target), self.address, zip_path))

        self.finalize_file(source_uuid, True)  # Delete remote zip file
        # Unpack the local zip file
        container_path = tempfile.mkdtemp()
        if return_zip:
            return zip_path, container_path

        result_path = zip_util.unzip(zip_path, container_path, name)
        path_util.remove(zip_path)  # Delete local zip file

        return (result_path, container_path)
    def download_target(self, target, follow_symlinks, return_zip=False):
        # Create remote zip file, download to local zip file
        (fd, zip_path) = tempfile.mkstemp(dir=tempfile.gettempdir())
        os.close(fd)
        source_uuid, name = self.open_target_zip(target, follow_symlinks)
        source = RPCFileHandle(source_uuid, self.proxy)
        with open(zip_path, 'wb') as dest:
            with contextlib.closing(source):
                file_util.copy(source,
                               dest,
                               autoflush=False,
                               print_status='Downloading %s on %s to %s' %
                               ('/'.join(target), self.address, zip_path))

        self.finalize_file(source_uuid, True)  # Delete remote zip file
        # Unpack the local zip file
        container_path = tempfile.mkdtemp()
        if return_zip:
            return zip_path, container_path

        result_path = zip_util.unzip(zip_path, container_path, name)
        path_util.remove(zip_path)  # Delete local zip file

        return (result_path, container_path)