From 73117b34a5d15d0d3cc69dfdea026bc9f3be8b0d Mon Sep 17 00:00:00 2001 From: Giacomo Olivero Date: Sun, 20 Nov 2022 12:01:20 +0100 Subject: [PATCH 1/2] Better implementation for .driveignore --- drive_cli/utils.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/drive_cli/utils.py b/drive_cli/utils.py index 06ca2a0..94b6eb9 100644 --- a/drive_cli/utils.py +++ b/drive_cli/utils.py @@ -170,12 +170,14 @@ def push_needed(drive, item_path): local_time = os.path.getmtime(item_path) - float(19801.00) data = drive_data() sync_time = data[item_path]['time'] + if (type(sync_time) is not float): + sync_time = sync_time['time'] if sync_time < local_time: if sync_time < drive_time: input = '' while(input != 's' and input != 'o'): input = click.prompt("Conflict: both local and online copy of " + - dir_name + " has been modified\npress o to OVERWRITE s to SKIP") + item_path + " has been modified\npress o to OVERWRITE s to SKIP") if(input == 'o'): return True else: @@ -437,16 +439,34 @@ def pull_content(cwd, fid): drive_data(data) -def list_local(cwd): +def list_local(cwd, untracked = []): local_lis = os.listdir(cwd) drive_ignore_path = os.path.join(cwd, '.driveignore') if os.path.isfile(drive_ignore_path): file = open(drive_ignore_path, 'r') - untracked_files = file.readlines() - for f in untracked_files: - local_lis.remove(f[:-1]) + untracked_files = [os.path.join(cwd, exp_to_regex(x)) for x in file.readlines()] + untracked = untracked + untracked_files file.close() - return local_lis + for uf in untracked: + for lf in local_lis: + if exp_matchfile(uf, os.path.join(cwd, lf)): + todelete = True + for neguf in untracked: + if neguf[0] == '!': + if exp_matchfile(neguf[1:], os.path.join(cwd, lf)): + todelete = False + break + if todelete: + local_lis.remove(lf) + return local_lis, untracked + + +def exp_to_regex(exp): + return exp[:-1].replace('*', '([^/]*)').replace('([^/]*)([^/]*)', '(.*)') + + +def exp_matchfile(exp, filename): + return re.search(exp, filename) is not None def list_status(cwd, sync_time): @@ -469,9 +489,9 @@ def list_status(cwd, sync_time): click.secho("No changes made since the last sync") -def push_content(cwd, fid): +def push_content(cwd, fid, untracked = []): drive_lis = get_child(cwd) - local_lis = list_local(cwd) + local_lis, untracked = list_local(cwd, untracked) data = drive_data() for item in local_lis: item_path = os.path.join(cwd, item) @@ -484,7 +504,7 @@ def push_content(cwd, fid): if child_cwd not in data.keys(): data[child_cwd] = {'id': child_id, 'time': time.time()} data = drive_data(data) - push_content(child_cwd, child_id) + push_content(child_cwd, child_id, untracked) else: item_path = os.path.join(cwd, item) if item not in drive_lis.keys(): From 3515f26c63fdc51c9da033774460eda44db18bd2 Mon Sep 17 00:00:00 2001 From: Giacomo Olivero Date: Fri, 14 Apr 2023 21:20:07 +0200 Subject: [PATCH 2/2] timezone setting and check for file in trash --- drive_cli/utils.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/drive_cli/utils.py b/drive_cli/utils.py index 94b6eb9..25c1479 100644 --- a/drive_cli/utils.py +++ b/drive_cli/utils.py @@ -9,6 +9,7 @@ from mimetypes import MimeTypes from pick import Picker from datetime import datetime +from dateutil import tz from googleapiclient.discovery import build from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload from httplib2 import Http @@ -166,8 +167,12 @@ def write_needed(dir_name, item): def push_needed(drive, item_path): drive_time = time.mktime(time.strptime( - drive['modifiedTime'], '%Y-%m-%dT%H:%M:%S.%fZ')) + float(19800.00) - local_time = os.path.getmtime(item_path) - float(19801.00) + drive['modifiedTime'], '%Y-%m-%dT%H:%M:%S.%fZ')) #+ float(19800.00) + # utc to local time + dt = datetime.strptime(drive['modifiedTime'], '%Y-%m-%dT%H:%M:%S.%fZ') + dt = dt.replace(tzinfo=tz.tzutc()).astimezone(tz.tzlocal()) + drive_time = time.mktime(dt.timetuple()) + local_time = os.path.getmtime(item_path) #- float(19801.00) data = drive_data() sync_time = data[item_path]['time'] if (type(sync_time) is not float): @@ -256,7 +261,7 @@ def get_child(cwd): service = build('drive', 'v3', http=creds.authorize(Http())) page_token = None drive_lis = {} - query = "'" + data[cwd]['id'] + "' in parents" + query = "'" + data[cwd]['id'] + "' in parents and trashed = false" while True: children = service.files().list(q=query, spaces='drive', @@ -462,7 +467,7 @@ def list_local(cwd, untracked = []): def exp_to_regex(exp): - return exp[:-1].replace('*', '([^/]*)').replace('([^/]*)([^/]*)', '(.*)') + return exp[:-1].replace('**', '(...)').replace('*', '([^/]*)').replace('(...)', '(.*)') def exp_matchfile(exp, filename):