iterative / PyDrive2

Google Drive API Python wrapper library. Maintained fork of PyDrive.
https://docs.iterative.ai/PyDrive2
Other
580 stars 69 forks source link

rebuild `cp_file` in `GDriveFileSystem` to use `GoogleDriveFile.Copy` #222

Closed simone-viozzi closed 10 months ago

simone-viozzi commented 2 years ago

Can copy single files and folders, the recursive implementation is in AbstractFileSystem.copy.

Is compatible with AbstractFileSystem.copy so for example:

fs = GDriveFileSystem("root/tmp", auth)

fs.copy('root/tmp/a.pdf', 'root/tmp/folder/b.pdf', recursive=False)
> will copy the file file a.pdf in the folder and rename it to b.pdf

fs.copy('root/tmp/folder1', 'root/tmp/folder/new_folder1', recursive=True)
> will copy the folder folder1 into folder, renaming it new_folder1. will also copy the content 
> of folder1 into new_folder1

There are no tests for copy, but there is one for the move method, that right now is implemented as copy + rm, and the test passes.

todo list:

@shcheklein can you please review the code? Also check if this is backward compatible with the previous implementation.

Thank you.

shcheklein commented 2 years ago

@simone-viozzi hey, thanks. I'll try to find some time this week!

simone-viozzi commented 2 years ago

@shcheklein I noticed that there was no mkdir method and I needed one, so I implemented it.

I also noticed that fs.expand_path only works if the path is one level under self.path. Example:

then:

print(fs.expand_path('root/tmp/fo1', recursive=True))
> ['root/tmp/fo1', 'root/tmp/fo1/file2.pdf', 'root/tmp/fo1/fo2/file3.pdf', 'root/tmp/fo1/fo2/fo3/file4.pdf'] 
# ( correct )

print(fs.expand_path('root/tmp/fo1/fo2/', recursive=True))
> ['root/tmp/fo1/fo2']
# ( wrong! Correct answer is: ['root/tmp/fo1/fo2', 'root/tmp/fo1/fo2/file3.pdf', 'root/tmp/fo1/fo2/fo3/file4.pdf'] )

expand_path use find:

https://github.com/iterative/PyDrive2/blob/27bbf4c8b4ef7fc3cce97f645b0e79d6795bf73c/pydrive2/fs/spec.py#L469-L509

The same bug happens in find as well.

I lost a good amount of time to understand why copy suddenly stopped working 😅