Skip to content

Commit

Permalink
feat: Make upload dir use local copy - Windows. #982
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Jan 20, 2023
1 parent 8f8ef8d commit bbe16f7
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions py/h2o_wave/core.py
Expand Up @@ -687,7 +687,7 @@ def upload(self, files: List[str]) -> List[str]:

if is_windows and shutil.which('robocopy'):
src = os.path.dirname(f) or os.getcwd()
args = ['robocopy', src, dst, os.path.basename(f), '/J/W:0']
args = ['robocopy', src, dst, os.path.basename(f), '/J', '/W:0']
else:
args = [cp_command, f, dst]

Expand Down Expand Up @@ -729,26 +729,22 @@ def upload_dir(self, directory: str) -> str:
if not os.path.isdir(directory):
raise ValueError(f'{directory} is not a directory.')

# TODO: Keep dir structure.
if _use_local_upload:
try:
is_windows = 'Windows' in platform.system()
# TODO: https://stackoverflow.com/questions/4601161/copying-all-contents-of-folder-to-another-folder-using-batch-file
cp_command = 'xcopy' if is_windows else 'cp'
uuid = str(uuid4())
dst = os.path.join(_waved_dir, _data_dir, 'f', uuid)
os.makedirs(dst, exist_ok=True)

if is_windows:
args = [cp_command, os.path.join(directory, '.'), dst, '/K/O/X']
if 'Windows' in platform.system():
args = ['robocopy', directory, dst, '/S', '/J', '/W:0', '*.*']
else:
args = [cp_command, '-a', os.path.join(directory, '.'), dst]
args = ['rsync', '-a', os.path.join(directory, '.'), dst]

_, err = subprocess.Popen(args, stderr=subprocess.PIPE).communicate()
if err:
raise ValueError(err.decode())

return [f'/_f/{uuid}']
return [f'{_base_url}_f/{uuid}']
except Exception as e:
print(f'Error during local copy, falling back to HTTP upload: {e}')

Expand Down Expand Up @@ -906,26 +902,22 @@ async def upload_dir(self, directory: str) -> str:
if not os.path.isdir(directory):
raise ValueError(f'{directory} is not a directory.')

# TODO: Keep dir structure.
if _use_local_upload:
try:
is_windows = 'Windows' in platform.system()
# TODO: https://stackoverflow.com/questions/4601161/copying-all-contents-of-folder-to-another-folder-using-batch-file
cp_command = 'xcopy' if is_windows else 'cp'
uuid = str(uuid4())
dst = os.path.join(_waved_dir, _data_dir, 'f', uuid)
os.makedirs(dst, exist_ok=True)

if is_windows:
args = [cp_command, os.path.join(directory, '.'), dst, '/K/O/X']
if 'Windows' in platform.system():
args = ['robocopy', directory, dst, '/S', '/J', '/W:0', '*.*']
else:
args = [cp_command, '-a', os.path.join(directory, '.'), dst]
args = ['rsync', '-a', os.path.join(directory, '.'), dst]

_, err = subprocess.Popen(args, stderr=subprocess.PIPE).communicate()
if err:
raise ValueError(err.decode())

return [f'/_f/{uuid}']
return [f'{_base_url}_f/{uuid}']
except Exception as e:
print(f'Error during local copy, falling back to HTTP upload: {e}')

Expand Down Expand Up @@ -961,19 +953,17 @@ async def upload(self, files: List[str]) -> List[str]:

if _use_local_upload:
try:
is_windows = 'Windows' in platform.system()
cp_command = 'xcopy' if is_windows else 'cp'
uploaded_files = []
for f in files:
uuid = str(uuid4())
dst = os.path.join(_waved_dir, _data_dir, 'f', uuid)
os.makedirs(dst, exist_ok=True)

if is_windows and shutil.which('robocopy'):
if 'Windows' in platform.system():
src = os.path.dirname(f) or os.getcwd()
args = ['robocopy', src, dst, os.path.basename(f), '/J/W:0']
args = ['robocopy', src, dst, os.path.basename(f), '/J', '/W:0']
else:
args = [cp_command, f, dst]
args = ['cp', f, dst]

_, err = subprocess.Popen(args, stderr=subprocess.PIPE, stdout=subprocess.DEVNULL).communicate()
if err:
Expand Down

0 comments on commit bbe16f7

Please sign in to comment.