Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __share_remote_file(self, tmpdir, rpath, srpath, fast):
rdir, rfile = posixpath.split(rpath)
lpath = joinpath(tmpdir, rfile)
subr = self.__downfile(rpath, lpath)
if subr != const.ENoError:
perr("Fatal: Error {} while downloading remote file '{}'".format(subr, rpath))
return subr
return self.__share_local_file(lpath, srpath, fast)
def __share_local(self, lpath, rpath, fast):
if not os.path.exists(lpath):
perr("Local path '{}' does not exist.".format(lpath))
return const.EParameter
if not self.__ok_to_use_remote_temp_dir():
perr("Can't continue unless you allow the program to use the remote temporary directory '{}'".format(const.RemoteTempDir))
return const.EUserRejected
if fast:
pr("# fast (unverified) sharing, no network I/O needed (for local sharing), but the other person may not be able to accept some of your files")
if os.path.isfile(lpath):
# keep the same file name
lname = os.path.basename(lpath)
rfile = joinpath(rpath, lname, '/')
return self.__share_local_file(lpath, rfile, fast)
elif os.path.isdir(lpath):
return self.__share_local_dir(lpath, rpath, fast)
else:
perr("Local path '{}' is not a file or directory.".format(lpath))
return const.EParameter
if it specifies an existing directory, it is the local directory
if not specified, the local directory is the current directory '.'
otherwise, it specifies the local file name
To stream a file using downfile, you can use the 'mkfifo' trick with omxplayer etc.:
mkfifo /tmp/omx
bypy.py downfile /tmp/omx &
omxplayer /tmp/omx
'''
localfile = localpath
if not localpath:
localfile = os.path.basename(remotefile)
elif localpath[-1] == '\\' or \
localpath[-1] == '/' or \
os.path.isdir(localpath):
#localfile = os.path.join(localpath, os.path.basename(remotefile))
localfile = joinpath(localpath, os.path.basename(remotefile))
else:
localfile = localpath
pcsrpath = get_pcs_path(remotefile)
return self.__downfile(pcsrpath, localfile)
def __share_local_dir(self, lpath, rpath, fast):
result = const.ENoError
for walk in self.__walk_normal_file(lpath):
(dirpath, dirnames, filenames) = walk
for filename in filenames:
rpart = os.path.relpath(dirpath, lpath)
if rpart == '.':
rpart = ''
subr = self.__share_local_file(
joinpath(dirpath, filename),
posixpath.join(rpath, rpart, filename),
fast)
if subr != const.ENoError:
result = subr
return result
def __syncup_diff_one(self, rpath, localdir, d):
result = const.ENoError
t = d[0] # type
p = d[1] # path
#lcpath = os.path.join(localdir, p) # local complete path
lcpath = joinpath(localdir, p) # local complete path
rcpath = rpath + '/' + p # remote complete path
if self.shalloverwrite("Do you want to overwrite '{}' at Baidu Yun? [y/N]".format(p)):
# this path is before get_pcs_path() since delete() expects so.
#result = self.delete(rpartialdir + '/' + p)
result = self.__delete(rcpath)
# self.pd("diff type: {}".format(t))
# self.__isrev = True
# if t != 'F':
# result = self.move(remotedir + '/' + p, remotedir + '/' + p + '.moved_by_bypy.' + time.strftime("%Y%m%d%H%M%S"))
# self.__isrev = False
if t == 'F' or t == 'FD':
subresult = self.__upload_file(lcpath, rcpath)
if subresult != const.ENoError:
result = subresult
else: # " t == 'DF' " must be true
subresult = self.__mkdir(rcpath)
def __walk_upload(self, localpath, remotepath, ondup, walk):
(dirpath, dirnames, filenames) = walk
rdir = os.path.relpath(dirpath, localpath)
if rdir == '.':
rdir = ''
else:
rdir = rdir.replace('\\', '/')
rdir = (remotepath + '/' + rdir).rstrip('/') # '/' bites
result = const.ENoError
for name in filenames:
#lfile = os.path.join(dirpath, name)
lfile = joinpath(dirpath, name)
self.__current_file = lfile
self.__current_file_size = getfilesize(lfile)
rfile = rdir + '/' + name.replace('\\', '/')
# if the corresponding file matches at Baidu Yun, then don't upload
upload = True
self.__isrev = False
self.__remote_json = {}
subresult = self.__get_file_info(rfile, dumpex = False)
if subresult == const.ENoError: # same-name remote file exists
self.__isrev = True
if const.ENoError == self.__verify_current_file(self.__remote_json, False):
# the two files are the same
upload = False
self.pv("Remote file '{}' already exists, skip uploading".format(rfile))
else: # the two files are different
if not self.shalloverwrite("Remote file '{}' exists but is different, "
def cleancache():
if cached.loadcache():
for absdir in cached.cache.keys():
if not os.path.exists(absdir):
if cached.verbose:
pr("Directory: '{}' no longer exists, removing the cache entries".format(absdir))
cached.dirty = True
del cached.cache[absdir]
else:
oldfiles = cached.cache[absdir]
files = {}
needclean = False
for f in oldfiles.keys():
#p = os.path.join(absdir, f)
p = joinpath(absdir, f)
if os.path.exists(p):
files[f] = oldfiles[f]
else:
if cached.verbose:
needclean = True
pr("File '{}' no longer exists, removing the cache entry".format(p))
if needclean:
cached.dirty = True
cached.cache[absdir] = files
cached.savecache()
def __syncdown_delete_local(self, localdir, local):
result = const.ENoError
for l in local:
# use os.path.isfile()/isdir() instead of l[0], because we need to check file/dir existence.
# as we may have removed the parent dir previously during the iteration
#p = os.path.join(localdir, l[1])
p = joinpath(localdir, l[1])
if os.path.isfile(p):
subresult = removefile(p, self.verbose)
if subresult != const.ENoError:
result = subresult
elif os.path.isdir(p):
subresult = removedir(p, self.verbose)
if subresult != const.ENoError:
result = subresult
return result
def __proceed_share_remote(self, walkresult, rpath, dirjs, filejs, args):
remoterootlen, tmpdir, srpath, fast = args
result = walkresult
for filej in filejs:
rfile = filej['path']
subr = self.__share_remote_file(tmpdir, rfile, joinpath(srpath, rfile[remoterootlen:], sep = '/'), fast)
if subr != const.ENoError:
result = subr
return result