From 11c1601e498dc15adb93f914a6cc9f1299024f61 Mon Sep 17 00:00:00 2001 From: wxy1343 <1343890272@qq.com> Date: Tue, 10 Aug 2021 17:11:20 +0800 Subject: [PATCH] [Fix] #41 --- aliyunpan/about.py | 2 +- aliyunpan/api/models.py | 36 ++++++++++++++++++++++++++++++++++++ aliyunpan/cli/cli.py | 35 +---------------------------------- aliyunpan/common.py | 7 +++++++ 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/aliyunpan/about.py b/aliyunpan/about.py index 3036ade..95a2595 100644 --- a/aliyunpan/about.py +++ b/aliyunpan/about.py @@ -1 +1 @@ -__version__ = '2.7.5' +__version__ = '2.7.6' diff --git a/aliyunpan/api/models.py b/aliyunpan/api/models.py index 77cfb60..182c6f7 100644 --- a/aliyunpan/api/models.py +++ b/aliyunpan/api/models.py @@ -6,6 +6,7 @@ from treelib.exceptions import NodeIDAbsentError from aliyunpan.api.type import FileInfo, ShareInfo +from aliyunpan.api.utils import get_sha1 from aliyunpan.common import GetFileListBar _all_ = ['PathList', 'parse_share_url', 'AliyunpanPath'] @@ -35,6 +36,10 @@ def update_path_list(self, file_id='root', depth=None, is_fid=True, **kwargs): if depth == max_depth: get_file_list_bar.refresh_line() return False + old_file_list = self._tree.children(file_id) + for i in old_file_list: + if i.identifier not in [j['file_id'] for j in file_list]: + self._tree.remove_node(i.identifier) for i, info in enumerate(file_list): if depth == max_depth: ratio = (i + 1) / len(file_list) if file_list else None @@ -51,6 +56,37 @@ def update_path_list(self, file_id='root', depth=None, is_fid=True, **kwargs): get_file_list_bar.refresh_line() return True + def check_path_diff(self, local_path, disk_path_list): + p = Path(local_path) + change_file_list = [] + for path in p.iterdir(): + flag = False + for i, path_ in enumerate(disk_path_list, 1): + name, file_info = list(path_.items())[0] + if p / name not in p.iterdir(): + change_file_list.append(p / name) + if Path(path) == p / name: + if Path(path).is_dir() and file_info['data'] and path.is_dir() != file_info['data'].type: + if 'children' in file_info: + children = file_info['children'] + change_file_list.extend(self.check_path_diff(p / name, children)) + elif list(path.iterdir()): + change_file_list.extend(list(path.iterdir())) + if file_info and file_info['data'] and path.is_file() == file_info['data'].type: + if path.is_file() and get_sha1(path).lower() != file_info['data'].content_hash.lower(): + continue + flag = True + if not flag and i == len(disk_path_list): + change_file_list.append(path) + if not len(list(p.iterdir())): + for path_ in disk_path_list: + name, file_info = list(path_.items())[0] + change_file_list.append(p / name) + if not len(disk_path_list): + for path_ in p.iterdir(): + change_file_list.append(path_) + return list(set(change_file_list)) + @staticmethod def get_file_info(info): file_info_list = [] diff --git a/aliyunpan/cli/cli.py b/aliyunpan/cli/cli.py index 698cf42..70d9661 100644 --- a/aliyunpan/cli/cli.py +++ b/aliyunpan/cli/cli.py @@ -548,8 +548,7 @@ def sync(self, path, upload_path, sync_time, time_out, chunk_size, retry, first= self._path_list.update_path_list(p, is_fid=False) file_id = self._path_list.get_path_fid(p, update=False) path_ = self._path_list._tree.to_dict(file_id, with_data=True)[str(relative_path)] - change_file_list = self.check_path_diff(path, path_['children'] if 'children' in path_ else []) - self._print.refresh_line() + change_file_list = self._path_list.check_path_diff(path, path_['children'] if 'children' in path_ else []) for path_ in change_file_list: relative_path = path.name / (path - path_) if path_.exists(): @@ -557,43 +556,11 @@ def sync(self, path, upload_path, sync_time, time_out, chunk_size, retry, first= chunk_size=chunk_size, retry=retry) else: self.rm(upload_path / relative_path) - self._print.print_line() if sync_time: self._print.wait_info('等待{time}秒后再次同步', t=sync_time, refresh_line=True) self._print.refresh_line() self.sync(path, upload_path, sync_time, time_out, chunk_size, retry, first=first) - def check_path_diff(self, local_path, disk_path_list): - p = Path(local_path) - change_file_list = [] - for path in p.iterdir(): - flag = False - for i, path_ in enumerate(disk_path_list, 1): - name, file_info = list(path_.items())[0] - if p / name not in p.iterdir(): - change_file_list.append(p / name) - if Path(path) == p / name: - if Path(path).is_dir() and file_info['data'] and path.is_dir() != file_info['data'].type: - if 'children' in file_info: - children = file_info['children'] - change_file_list.extend(self.check_path_diff(p / name, children)) - elif list(path.iterdir()): - change_file_list.extend(list(path.iterdir())) - if file_info and file_info['data'] and path.is_file() == file_info['data'].type: - if path.is_file() and get_sha1(path).lower() != file_info['data'].content_hash.lower(): - continue - flag = True - if not flag and i == len(disk_path_list): - change_file_list.append(path) - if not len(list(p.iterdir())): - for path_ in disk_path_list: - name, file_info = list(path_.items())[0] - change_file_list.append(p / name) - if not len(disk_path_list): - for path_ in p.iterdir(): - change_file_list.append(path_) - return list(set(change_file_list)) - def share_link(self, path_list, file_id_list=None, expiration=None): t = '' if expiration is None else time.time() + expiration if not file_id_list: diff --git a/aliyunpan/common.py b/aliyunpan/common.py index 5193d1f..2b91784 100644 --- a/aliyunpan/common.py +++ b/aliyunpan/common.py @@ -234,6 +234,7 @@ def wait_info(self, title=None, t=3, refresh_line=False): def upload_info(self, path, status=None, refresh_line=False, rapid_upload=False, t=None, average_speed=None, *args, **kwargs): + self._output = True t = self._time_info.format(t) if t else None average_speed = self.avg_info.format(*str_of_size(average_speed, tuple_=True)) if average_speed else None rapid_title = self._rapid_upload_title if rapid_upload else None @@ -242,6 +243,7 @@ def upload_info(self, path, status=None, refresh_line=False, rapid_upload=False, self.output = info def download_info(self, path, status=None, refresh_line=False, t=None, average_speed=None, aria2=False, *args): + self._output = True t = self._time_info.format(t) if t else None aria2 = self._aria2_title if aria2 else aria2 average_speed = self.avg_info.format(*str_of_size(average_speed, tuple_=True)) if average_speed else None @@ -250,22 +252,26 @@ def download_info(self, path, status=None, refresh_line=False, t=None, average_s self.output = info def mkdir_info(self, path, status=None, *args, **kwargs): + self._output = True info = self.get_info(status, path, self._mkdir_title, *args, **kwargs) info.color = self._mkdir_color or info.color self.output = info def move_info(self, path, target_path, status=None, *args, **kwargs): + self._output = True info = self.get_info(status, path, self._move_title, *args, target_path=target_path, **kwargs) info.color = self._move_color or info.color self.output = info def remove_info(self, path, status=None, *args, **kwargs): + self._output = True info = self.get_info(status, path, self._remove_title, *args, **kwargs) info.error = False info.color = self._remove_color or info.color self.output = info def rename_info(self, path, name, status=None, *args, **kwargs): + self._output = True info = self.get_info(status, path, self._rename_title, *args, target_path=name, **kwargs) info.color = self._rename_color or info.color self.output = info @@ -284,6 +290,7 @@ def print_line(self): self._print.print_line = True def print_info(self, info, error=False, refresh_line=False, *args, **kwargs): + self._output = True self.output = Info(info, error, refresh_line, *args, **kwargs)