def testRaisesErrorIfGsutilNotFound(self):
     mock_paths = mock.MagicMock()
     mock_paths.sdk_bin_path = None
     self.StartObjectPatch(config, 'Paths', return_value=mock_paths)
     self._MockFindExecutableOnPath(return_value=None)
     with self.assertRaisesRegex(
             storage_util.GsutilError,
             'A path to the storage client `gsutil` could not be found. Please '
             'check your SDK installation.'):
         storage_util.RunGsutilCommand('cp', [])
 def testUploadFileWithBackslashesInFilePath(self):
     popen_mock = self._MockPopen()
     self._MockFindExecutableOnPath()
     storage_util.RunGsutilCommand(
         'cp', ['C:\\Users\\foo\\file.txt', 'gs://bucket/my-file.txt'])
     self.assertEqual(popen_mock.call_count, 1)
     args, _ = popen_mock.call_args
     self.assertEqual(
         args[0][-3:],
         ['cp', 'C:\\Users\\foo\\file.txt', 'gs://bucket/my-file.txt'])
 def testUploadFileWithSpacesInFilename(self):
     popen_mock = self._MockPopen()
     self._MockFindExecutableOnPath()
     storage_util.RunGsutilCommand(
         'cp', ['my local file.txt', 'gs://bucket/my-file.txt'])
     self.assertEqual(popen_mock.call_count, 1)
     args, _ = popen_mock.call_args
     self.assertEqual(
         args[0][-3:],
         ['cp', 'my local file.txt', 'gs://bucket/my-file.txt'])
예제 #4
0
def _ImportGsutil(gcs_bucket, source, destination):
    """Imports files and directories into a bucket."""
    destination_ref = storage_util.ObjectReference(gcs_bucket, destination)
    try:
        retval = storage_util.RunGsutilCommand(
            'cp',
            command_args=(['-r', source, destination_ref.ToUrl()]),
            run_concurrent=True,
            out_func=log.out.write,
            err_func=log.err.write)
    except (execution_utils.PermissionError,
            execution_utils.InvalidCommandError) as e:
        raise command_util.GsutilError(six.text_type(e))
    if retval:
        raise command_util.GsutilError('gsutil returned non-zero status code.')
예제 #5
0
def _IsLocalFile(file_name):
    return not (file_name.startswith('gs://')
                or file_name.startswith('https://'))


def _UploadToGcs(async, local_path, daisy_bucket, image_uuid):
    """Uploads a local file to GCS. Returns the gs:// URI to that file."""
    file_name = os.path.basename(local_path).replace(' ', '-')
    dest_path = 'gs://{0}/tmpimage/{1}-{2}'.format(daisy_bucket, image_uuid,
                                                   file_name)
    log.status.Print('\nCopying [{0}] to [{1}]'.format(local_path, dest_path))
    if async:
        log.status.Print(
            'Once completed, your image will be imported from Cloud'
            ' Storage asynchronously.')
    storage_util.RunGsutilCommand('cp',
                                  '"{0}" {1}'.format(local_path, dest_path))
    return dest_path


def _CopyToScratchBucket(source_uri, image_uuid, storage_client, daisy_bucket):
    """Copy image from source_uri to daisy scratch bucket."""
    image_file = os.path.basename(source_uri)
    dest_uri = 'gs://{0}/tmpimage/{1}-{2}'.format(daisy_bucket, image_uuid,
                                                  image_file)
    src_object = resources.REGISTRY.Parse(source_uri,
                                          collection='storage.objects')
    dest_object = resources.REGISTRY.Parse(dest_uri,
                                           collection='storage.objects')
    log.status.Print('\nCopying [{0}] to [{1}]'.format(source_uri, dest_uri))
    storage_client.Rewrite(src_object, dest_object)
    return dest_uri