From: Jakub Czajka Date: Sun, 19 Nov 2023 17:20:36 +0000 (+0100) Subject: Permit reading token and encryption key from command line. X-Git-Url: https://git.ekhem.eu.org/?a=commitdiff_plain;h=8f212c5f435694cc7d4e2c0d44c88353a4e799c5;p=gdrive_knife.git Permit reading token and encryption key from command line. --- diff --git a/gdrive_knife.py b/gdrive_knife.py index 55939e9..24238ae 100644 --- a/gdrive_knife.py +++ b/gdrive_knife.py @@ -3,6 +3,7 @@ import argparse import io +import json import os.path import shutil import sys @@ -28,20 +29,30 @@ def file_path(parser, path): return parser.error(f'{path} does not exist!') return path -def fernet_key(parser, path): - if not os.path.isfile(path): - return parser.error(f'{path} does not exist!') - with open(path, 'rb') as f: - return Fernet(f.read()) +def fernet_key(parser, path_or_string): + if os.path.isfile(path_or_string): + with open(path_or_string, 'rb') as f: + return Fernet(f.read()) + return Fernet(path_or_string) -def get_drive_client(authentication_token): - creds = Credentials.from_authorized_user_file(authentication_token, SCOPES) +def auth_token(parser, path_or_string): + creds = None + if os.path.isfile(path_or_string): + creds = Credentials.from_authorized_user_file(path_or_string, SCOPES) + else: + as_json = json.loads(path_or_string) + creds = Credentials( + token=as_json['token'], + refresh_token=as_json['refresh_token'], + token_uri=as_json['token_uri'], + client_id=as_json['client_id'], + client_secret=as_json['client_secret'], + scopes=as_json['scopes'], + ) if not creds or not creds.valid: - print(f'{authentication_token} is not valid. Use `auth` to obtain a new ' - 'one.') + print(f'Token is not valid. Use `auth` to obtain a new one.') sys.exit(1) - - return build('drive', 'v3', credentials=creds) + return creds def get_path_on_drive(file_path): if os.path.isabs(file_path): @@ -163,7 +174,7 @@ def auth(args): print('Authentication successful.') def list(args): - drive = get_drive_client(args.token) + drive = build('drive', 'v3', credentials=args.token) files_on_drive = drive.files().list(q='trashed=false', fields='files(id, originalFilename)').execute() @@ -172,7 +183,7 @@ def list(args): print(file_on_drive['originalFilename']) def download(args): - drive = get_drive_client(args.token) + drive = build('drive', 'v3', credentials=args.token) maybe_id = get_file_id(drive, args.path) if not maybe_id: @@ -206,7 +217,7 @@ def download(args): print(f'Removed {path_in_tmp}.') def upload(args): - drive = get_drive_client(args.token) + drive = build('drive', 'v3', credentials=args.token) path = args.name if args.name else args.file path_in_tmp = get_path_on_drive(path) @@ -241,7 +252,7 @@ def upload(args): print(f'Removed {path_in_tmp}.') def delete(args): - drive = get_drive_client(args.token) + drive = build('drive', 'v3', credentials=args.token) maybe_id = get_file_id(drive, args.path) if not maybe_id: @@ -274,7 +285,7 @@ if __name__ == '__main__': list_parser = subparsers.add_parser('list', help='List files.') list_parser.add_argument('-t', '--token', required=True, - type=lambda x : file_path(parser, x), help='File with the ' + type=lambda x : auth_token(parser, x), help='File with the ' 'authentication token.') list_parser.set_defaults(func=list) @@ -285,7 +296,7 @@ if __name__ == '__main__': type=lambda x : fernet_key(parser, x), help='File with the decryption ' 'key.') download_parser.add_argument('-t', '--token', required=True, - type=lambda x : file_path(parser, x), help='File with the ' + type=lambda x : auth_token(parser, x), help='File with the ' 'authentication token.') download_parser.add_argument('--leave-as-archive', action='store_true', help='Skip unarchiving for directories.') @@ -299,14 +310,14 @@ if __name__ == '__main__': type=lambda x : fernet_key(parser, x), help='File with the decryption ' 'key.') upload_parser.add_argument('-t', '--token', required=True, - type=lambda x : file_path(parser, x), help='File with the ' + type=lambda x : auth_token(parser, x), help='File with the ' 'authentication token.') upload_parser.set_defaults(func=upload) delete_parser = subparsers.add_parser('delete', help='Delete a file.') delete_parser.add_argument('path', help='File to delete.') delete_parser.add_argument('-t', '--token', required=True, - type=lambda x : file_path(parser, x), help='File with the ' + type=lambda x : auth_token(parser, x), help='File with the ' 'authentication token.') delete_parser.set_defaults(func=delete)