Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Remote item exists. Solve for potential type conflict.
item_is_folder = item.folder is not None
item_is_file = False if item_is_folder else item.file is not None
event_is_dir = _inotify_flags.ISDIR not in to_flags
if (item_is_folder and not event_is_dir) or (item_is_file and event_is_dir):
# Path is a dir remotely but a file locally, or a file remotely but a dir locally.
# To solve the type conflict we try renaming the remote item, and if it succeeds, proceed as if
# the remote item does not exist; otherwise fall back to dir merge.
new_name = get_filename_with_incremented_count(item.name)
can_upload = False
try:
can_upload = move_item.MoveItemTask(
repo=to_repo, task_pool=self.task_pool,
parent_relpath=to_parent_relpath, item_name=item.name, item_id=item.id,
new_parent_relpath=to_parent_relpath, new_name=new_name, is_folder=item_is_folder).handle()
except onedrivesdk.error.OneDriveError as e:
logging.error('API error renaming remote item "%s/%s" to "%s/%s": %s. Fallback to dir merge.',
to_parent_relpath, item.name, to_parent_relpath, new_name, e)
can_upload = False
finally:
if not can_upload:
self._add_merge_dir_task(to_repo, to_parent_relpath)
return
elif item_is_folder and event_is_dir:
# A dir of same name already exists remotely but we don't know if it has been synced before or
# was created on another machine. Merge the two directories.
self._add_merge_dir_task(to_repo, item_relpath)
return
elif item_is_file and not event_is_dir:
if hash_match(item_local_abspath, item) and update_mtime.UpdateTimestampTask(
repo=to_repo, task_pool=self.task_pool,
parent_relpath=to_parent_relpath, item_name=to_ev.name).handle():
item_request = self.repo.authenticator.client.item(drive=self.repo.drive.id, path=self.rel_path)
returned_item = item_request_call(self.repo, item_request.upload_async,
local_path=self.local_abspath, upload_status=self.update_progress)
if not isinstance(returned_item, onedrivesdk.Item):
if hasattr(returned_item, '_prop_dict'):
returned_item = onedrivesdk.Item(returned_item._prop_dict)
else:
returned_item = item_request_call(self.repo, item_request.get)
self.update_timestamp_and_record(returned_item, item_stat)
self.task_pool.release_path(self.local_abspath)
logging.info('Finished uploading file "%s".', self.local_abspath)
return True
except (onedrivesdk.error.OneDriveError, OSError) as e:
logging.error('Error uploading file "%s": %s.', self.local_abspath, e)
# TODO: what if quota is exceeded?
if (isinstance(e, onedrivesdk.error.OneDriveError) and
e.code == onedrivesdk.error.ErrorCode.MalwareDetected):
logging.warning('File "%s" was detected as malware by OneDrive. '
'Do not upload during program session.', self.local_abspath)
self.task_pool.occupy_path(self.local_abspath, None)
return False
self.task_pool.release_path(self.local_abspath)
return False
try:
if self.abort_if_local_gone and not os.path.isdir(self.local_abspath):
logging.warning('Local dir "%s" is gone. Skip creating remote item for it.', self.local_abspath)
return
item = self._get_folder_pseudo_item(self.item_name)
item_request = self._get_item_request()
item = od_api_helper.item_request_call(self.repo, item_request.children.add, item)
self.repo.update_item(item, self.parent_relpath, 0)
logging.info('Created remote item for local dir "%s".', self.local_abspath)
if self.upload_if_success:
logging.info('Adding task to merge "%s" after remote item was created.', self.local_abspath)
self.task_pool.add_task(MergeDirectoryTask(
self.repo, self.task_pool, self.parent_relpath + '/' + self.item_name,
self.repo.authenticator.client.item(drive=self.repo.drive.id, id=item.id)))
return True
except (onedrivesdk.error.OneDriveError, OSError) as e:
logging.error('Error when creating remote dir of "%s": %s.', self.local_abspath, e)
return False