Skip to content

Commit

Permalink
Fix previous circular import (Close #359)
Browse files Browse the repository at this point in the history
  • Loading branch information
houtianze committed May 10, 2017
1 parent fd38640 commit 3316555
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 43 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Version History:

- 1.5.2: Defuse the circular import bomb brought in the previous version...
- 1.5.1: Improve multiprocess (and fix filter() for Python3)
- 1.5.0: Multi-Process for directory download / upload / sync up/down

Expand Down
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Version History:
~~~~~~~~~~~~~~~~

- 1.5.2: Defuse the circular import bomb brought in the previous
version...
- 1.5.1: Improve multiprocess (and fix filter() for Python3)
- 1.5.0: Multi-Process for directory download / upload / sync up/down

Expand Down
61 changes: 51 additions & 10 deletions bypy/bypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
from . import const
from . import gvar
from . import printer_console
from . import monkey # TODO: circular import
from . import cached as cachedm
from . import util
from . import printer
from .cached import (cached, stringifypickle, md5, crc32, slice_md5)
from .struct import PathDictTree
from .util import (
Expand Down Expand Up @@ -134,6 +136,46 @@ def UPool(*args, **kwargs):
# global instance for non-member function to access
gbypyinst = None

# This crap is here to avoid circular imports
# What a fantastic packing architecture Python has!
def makemppr(pr):
def mppr(msg, *args, **kwargs):
return pr(mp.current_process().name + ': ' + msg, *args, **kwargs)
return mppr

def makemppprgr(pprgr):
def mppprgr(finish, total, start_time = None, existing = 0,
prefix = '', suffix = '', seg = 20):
prefix = mp.current_process().name + ': ' + str(prefix)
pprgr(finish, total, start_time, existing, prefix, suffix, seg)
return mppprgr

def set_mp_print():
global pr
global prcolor
global ask
global pprgr
opr = pr
oprcolor = prcolor
oask = ask
opprgr = pprgr
def restoremp():
global pr
global prcolor
global ask
global pprgr
pr = cachedm.pr = util.pr = opr
prcolor = util.prcolor = printer.prcolor = oprcolor
ask = util.ask = oask
pprgr = util.pprgr = opprgr

pr = cachedm.pr = util.pr = makemppr(opr)
prcolor = util.prcolor = printer.prcolor = makemppr(oprcolor)
ask = util.ask = makemppr(oask)
pprgr = util.pprgr = makemppprgr(opprgr)

return restoremp

class ByPy(object):
'''The main class of the bypy program'''
# TODO: Apply to configdir instead of ~/.bypy
Expand Down Expand Up @@ -502,7 +544,7 @@ def __multi_process(self, worker, iter, process = "dl / ul"):
return const.EArgument
self.__warn_multi_processes(process)
# patch the printer
restoremp = monkey.setmultiprocess()
restoremp = set_mp_print()
with UPool(self.processes) as pool:
# http://stackoverflow.com/a/35134329/404271
# On Python 2.x, Ctrl-C won't work if we use pool.map(), wait() or get()
Expand Down Expand Up @@ -650,7 +692,7 @@ def __request_work(self, url, pars, act, method, actargs = None, addtoken = True
result = ec
# errors that make retrying meaningless
elif (
#ec == 31061 or # sc == 400 file already exists
ec == 31061 or # sc == 400 file already exists
ec == 31062 or # sc == 400 file name is invalid
ec == 31063 or # sc == 400 file parent path does not exist
ec == 31064 or # sc == 403 file is not authorized
Expand Down Expand Up @@ -1109,23 +1151,23 @@ def __verify_current_file(self, j, gotlmd5):
self.pd("Remote file size: {}".format(rsize))

if self.__current_file_size == rsize:
self.pd("Local file and remote file sizes match")
self.pd("Local and remote file size matches")
if self.__verify:
if not gotlmd5:
self.__current_file_md5 = md5(self.__current_file)
self.pd("Local file MD5 : {}".format(self.__current_file_md5))
self.pd("Remote file MD5: {}".format(rmd5))

if self.__current_file_md5 == rmd5:
self.pd("Local file and remote file hashes match")
self.pd("Local and remote file hash matches")
return const.ENoError
else:
pinfo("Local file and remote file hashes DON'T match")
pinfo("Local and remote file hash DOESN'T match")
return const.EHashMismatch
else:
return const.ENoError
else:
pinfo("Local file and remote file sizes DON'T match")
pinfo("Local and remote file size DOESN'T match")
return const.EHashMismatch

def __get_file_info_act(self, r, args):
Expand Down Expand Up @@ -2166,7 +2208,6 @@ def __mkdir(self, rpath, **kwargs):
'path' : rpath }
return self.__post(pcsurl + 'file', pars, self.__mkdir_act, **kwargs)


def mkdir(self, remotepath):
''' Usage: mkdir <remotedir> - \
create a directory at Baidu Yun
Expand Down Expand Up @@ -2653,7 +2694,7 @@ def __syncup_diff_one(self, rpath, localdir, d):
result = subresult
else: # " t == 'DF' " must be true
subresult = self.__mkdir(rcpath)
if subresult != const.ENoError:
if subresult != const.ENoError and subresult != const.IEFileAlreadyExists:
result = subresult
else:
pinfo("Uploading '{}' skipped".format(lcpath))
Expand All @@ -2675,7 +2716,7 @@ def __syncup_local_one(self, rpath, localdir, l):
result = subresult
else: # " t == 'D' " must be true
subresult = self.__mkdir(rcpath)
if subresult != const.ENoError:
if subresult != const.ENoError and subresult != const.IEFileAlreadyExists:
result = subresult

return result
Expand Down
2 changes: 1 addition & 1 deletion bypy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# https://packaging.python.org/single_source_version/
__title__ = 'bypy'
__version__ = '1.5.1'
__version__ = '1.5.2'
__author__ = 'Hou Tianze'
__license__ = 'MIT'
__desc__ = 'Python client for Baidu Yun (Personal Cloud Storage) 百度云/百度网盘 Python 客户端'
Expand Down
31 changes: 0 additions & 31 deletions bypy/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import sys
from functools import partial
import multiprocess as mp

from . import printer_console
from . import bypy
Expand Down Expand Up @@ -38,36 +37,6 @@ def setgui(*arg):
bypy.ask = util.ask = partial(printer_gui.ask, inst)
bypy.pprgr = util.pprgr = partial(printer_gui.pprgr, inst)

def makemppr(pr):
def mppr(msg, *args, **kwargs):
return pr(mp.current_process().name + ': ' + msg, *args, **kwargs)
return mppr

def makemppprgr(pprgr):
def mppprgr(finish, total, start_time = None, existing = 0,
prefix = '', suffix = '', seg = 20):
prefix = mp.current_process().name + ': ' + str(prefix)
pprgr(finish, total, start_time, existing, prefix, suffix, seg)
return mppprgr

def setmultiprocess():
opr = bypy.pr
oprcolor = bypy.prcolor
oask = bypy.ask
opprgr = util.pprgr
def restoremp():
bypy.pr = cached.pr = util.pr = opr
bypy.prcolor = util.prcolor = printer.prcolor = oprcolor
bypy.ask = util.ask = oask
bypy.pprgr = util.pprgr = opprgr

bypy.pr = cached.pr = util.pr = makemppr(opr)
bypy.prcolor = util.prcolor = printer.prcolor = makemppr(oprcolor)
bypy.ask = util.ask = printer_console.ask = makemppr(oask)
bypy.pprgr = util.pprgr = printer_console.pprgr = makemppprgr(opprgr)

return restoremp

def patchpr(func):
bypy.pr = cached.pr = util.pr = func

Expand Down
3 changes: 2 additions & 1 deletion bypy/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ def main():
if '1' in sys.argv:
return

by = bypy.ByPy(configdir=ConfigDir, processes=4, debug=1, verbose=1)
time.sleep(10)
by = bypy.ByPy(configdir=ConfigDir, processes=2, debug=1, verbose=1)
runTests(by)
if '2' in sys.argv:
return
Expand Down

0 comments on commit 3316555

Please sign in to comment.