def _copyFile(self, sourceFile, destinationFile): if not sourceFile.exists: raise FileNotFoundError def request(): start = time.time() bytesTransferred = 0 with io.open(sourceFile.path, 'rb') as source: with io.open(destinationFile.path, 'wb') as dest: while True: buffer = source.read(transfermanager.CHUNKSIZE) if not buffer: break dest.write(buffer) elapsed = self.elapsed(start) bytesTransferred += len(buffer) self.logProgress(destinationFile.path, elapsed, bytesTransferred, sourceFile.size, float(bytesTransferred) / sourceFile.size) elapsed = self.elapsed(start) self.updateSummary(self._summary, sourceFile.size, elapsed) self.logEnd(destinationFile.path, elapsed, sourceFile.size, self._summary.copiedFiles) requestexecutor.execute(request) return self.touch(sourceFile, destinationFile)
def _copyNonEmptyFile(self, sourceFile, destinationFile): def createMedia(fileObject): request = (self._drive.files() .get_media(fileId = sourceFile.delegate['id'], fields = driveutils.FIELDS)) return apiclient.http.MediaIoBaseDownload(fileObject, request, chunksize = transfermanager.CHUNKSIZE) def request(): start = time.time() with io.open(destinationFile.path, 'wb') as dest: media = createMedia(dest) while True: (progress, file) = media.next_chunk() elapsed = self.elapsed(start) if file is not None: self.updateSummary(self._summary, sourceFile.size, elapsed) self.logEnd(destinationFile.location, elapsed, sourceFile.size, self._summary.copiedFiles) return file self.logProgress(destinationFile.location, elapsed, progress.resumable_progress, progress.total_size, progress.progress()) requestexecutor.execute(request)
def create(self, file): if not isinstance(file, remotefile.RemoteFile): if not file.remote: raise RuntimeError('Expected a remote location: %s' % file) remoteFileFactory = remotefile.Factory(self._drive) return self.create(remoteFileFactory.create(file)) query = CHILDREN_QUERY % {'parents': file.delegate['id']} def request(): remoteFolder = RemoteFolder(file) pageToken = None while True: list = (self._drive.files().list(q = query, fields = CHILDREN_FIELDS, pageToken = pageToken, maxResults = LIST_MAX_RESULTS)) files = list.execute() for child in files['items']: remoteFolder.addChild(remotefile.fromParent(file, child)) pageToken = files.get('nextPageToken') if pageToken is None: break return remoteFolder return requestexecutor.execute(request)
def remove(self, destinationFile): def request(): return (self._drive.files() .trash(fileId = destinationFile.delegate['id'], fields = driveutils.FIELDS) .execute()) file = requestexecutor.execute(request) return destinationFile.withDelegate(file)
def insertFolder(self, sourceFile, destinationFile): body = destinationFile.delegate.copy() body['modifiedDate'] = str(sourceFile.modified) def request(): return (self._drive.files().insert(body = body, fields = driveutils.FIELDS).execute()) file = requestexecutor.execute(request) return destinationFile.withDelegate(file)
def create(self, path): fileId = self.retrieveFileId(path) if fileId is None: raise RuntimeError('%s not found' % path) def request(): return (self.drive.files().get(fileId = fileId, fields = driveutils.FIELDS).execute()) file = requestexecutor.execute(request) return RemoteFile(path, file)
def trashFile(self, remoteFile): LOGGER.info('%s: Trashing file...', remoteFile.path) if self.args.dryRun: return remoteFile def request(): return (self.drive.files() .trash(fileId = remoteFile.delegate['id'], fields = driveutils.FIELDS) .execute()) file = requestexecutor.execute(request) return remoteFile.withDelegate(file)
def insertFolder(self, localFile, remoteFile): LOGGER.info('%s: Inserting folder... (Checked %d/%d files)', remoteFile.path, self.checkedFiles, self.totalFiles) if self.args.dryRun: return remoteFile body = remoteFile.delegate.copy() body['modifiedDate'] = str(localFile.modified) def request(): return (self.drive.files().insert(body = body, fields = driveutils.FIELDS).execute()) file = requestexecutor.execute(request) return remoteFile.withDelegate(file)
def create(self, location): if not location.remote: raise RuntimeError('Expected a remote location: %s' % location) fileId = self.retrieveFileId(location) if fileId is None: raise RuntimeError('%s not found' % location) def request(): return (self.drive.files().get(fileId = fileId, fields = driveutils.FIELDS).execute()) file = requestexecutor.execute(request) return RemoteFile(location, file)
def touch(self, sourceFile, destinationFile): body = {'modifiedDate': str(sourceFile.modified)} def request(): request = (self._drive.files() .patch(fileId = destinationFile.delegate['id'], body = body, setModifiedDate = True, fields = driveutils.FIELDS)) # Ignore Etags request.headers['If-Match'] = '*' return request.execute() file = requestexecutor.execute(request) return destinationFile.withDelegate(file)
def touch(self, localFile, remoteFile): LOGGER.info('%s: Updating modified date... (Checked %d/%d files)', remoteFile.path, self.checkedFiles, self.totalFiles) if self.args.dryRun: return remoteFile body = {'modifiedDate': str(localFile.modified)} def request(): return (self.drive.files() .patch(fileId = remoteFile.delegate['id'], body = body, setModifiedDate = True, fields = driveutils.FIELDS) .execute()) file = requestexecutor.execute(request) return remoteFile.withDelegate(file)
def _copyFile(self, sourceFile, destinationFile, createRequest): body = destinationFile.delegate.copy() body['modifiedDate'] = str(sourceFile.modified) (mimeType, encoding) = mimetypes.guess_type(sourceFile.location.path) if mimeType is None: mimeType = DEFAULT_MIME_TYPE resumable = (sourceFile.size > 0) media = apiclient.http.MediaFileUpload(sourceFile.location.path, mimetype = mimeType, chunksize = transfermanager.CHUNKSIZE, resumable = resumable) def request(): start = time.time() request = createRequest(body, media) if not resumable: file = request.execute() elapsed = self.elapsed(start) self.updateSummary(self._summary, sourceFile.size, elapsed) self.logEnd(destinationFile.location, elapsed, sourceFile.size, self._summary.copiedFiles) return file while True: (progress, file) = request.next_chunk() elapsed = self.elapsed(start) if file is not None: self.updateSummary(self._summary, sourceFile.size, elapsed) self.logEnd(destinationFile.location, elapsed, sourceFile.size, self._summary.copiedFiles) return file self.logProgress(destinationFile.location, elapsed, progress.resumable_progress, progress.total_size, progress.progress()) file = requestexecutor.execute(request) return destinationFile.withDelegate(file)
def retrieveFileId(self, location): parent = location.parent if parent is None: return 'root' parentId = self.retrieveFileId(parent) if parentId is None: return None query = (FILE_ID_QUERY % {'title' : driveutils.escapeQueryParameter(location.name)}) def request(): return (self.drive.children().list(folderId = parentId, q = query, maxResults = 1, fields = 'items(id)') .execute()) children = requestexecutor.execute(request) for child in children.get('items'): return child['id'] return None
def retrieveFileId(self, path): (parent, name) = posixpath.split(path) if parent == path: return 'root' if name == '': return self.retrieveFileId(parent) parentId = self.retrieveFileId(parent) if parentId is None: return None query = (FILE_ID_QUERY % {'title' : driveutils.escapeQueryParameter(name)}) def request(): return (self.drive.children().list(folderId = parentId, q = query, maxResults = 1, fields = 'items(id)') .execute()) children = requestexecutor.execute(request) for child in children.get('items'): return child['id'] return None
def _copyFile(self, sourceFile, destinationFile): body = sourceFile.delegate.copy() body['parents'] = destinationFile.delegate['parents'] def request(): start = time.time() request = (self._drive.files() .copy(fileId = sourceFile.delegate['id'], body = body, fields = driveutils.FIELDS)) file = request.execute() elapsed = self.elapsed(start) self.updateSummary(self._summary, sourceFile.size, elapsed) self.logEnd(destinationFile.path, elapsed, sourceFile.size, self._summary.copiedFiles) return file file = requestexecutor.execute(request) return destinationFile.withDelegate(file)
def copyFile(self, localFile, remoteFile, createRequest): body = remoteFile.delegate.copy() body['modifiedDate'] = str(localFile.modified) (mimeType, encoding) = mimetypes.guess_type(localFile.delegate) if mimeType is None: mimeType = DEFAULT_MIME_TYPE resumable = (localFile.size > CHUNKSIZE) media = apiclient.http.MediaFileUpload(localFile.delegate, mimetype = mimeType, chunksize = CHUNKSIZE, resumable = resumable) def request(): request = createRequest(body, media) start = time.time() if not resumable: file = request.execute() self.logProgress(remoteFile.path, start, localFile.size) return file while True: (progress, file) = request.next_chunk() if file is not None: self.logProgress(remoteFile.path, start, localFile.size) return file self.logProgress(remoteFile.path, start, progress.resumable_progress, progress.total_size, progress.progress(), False) file = requestexecutor.execute(request) return remoteFile.withDelegate(file)