From 136b70ccf8b6887d961a5665d3356592444be12a Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 26 Dec 2022 12:33:09 +0800 Subject: [PATCH] Add db_password --- example.yaml | 1 + game_backuper/config.py | 5 +++++ game_backuper/db.py | 14 ++++++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/example.yaml b/example.yaml index ab5ca46..979c176 100644 --- a/example.yaml +++ b/example.yaml @@ -7,6 +7,7 @@ compress_method: "bzip2" # Optional. Default value: null. Supported value: "bzi # zstd support 0-22 (Default: 3). brotli support 0-11 (Default: unset). compress_level: 6 encrypt_db: false # Optional. Default value: false. Encrypt the database. Warning: The default python sqlite library don't support encrypt, it just ignore encrypt phases. +db_password: "Password" # Specify the password of the encryped database. encrypt_files: false # Optional. Default value: false. Encrypt backup files. The key information will stored in database. programs: - name: Your program name # This name is used to identify different application. diff --git a/game_backuper/config.py b/game_backuper/config.py index a096ad5..252dd5e 100644 --- a/game_backuper/config.py +++ b/game_backuper/config.py @@ -452,6 +452,7 @@ class Program(BasicOption, NFBasicOption): class Config(BasicOption, NFBasicOption): dest = '' encrypt_db = False + db_password = None progs = [] progs_name = [] @@ -471,6 +472,10 @@ class Config(BasicOption, NFBasicOption): self.encrypt_db = t['encrypt_db'] else: self.encrypt_db = False + if 'db_password' in t: + if not isinstance(t['db_password'], str): + raise ValueError('db_password should be a string.') + self.db_password = t['db_password'] if 'programs' not in t: raise ValueError("No programs found.") self.parse_all(t) diff --git a/game_backuper/db.py b/game_backuper/db.py index 00fe873..0cf87f7 100644 --- a/game_backuper/db.py +++ b/game_backuper/db.py @@ -1,4 +1,4 @@ -from getpass import getpass +from getpass import getpass as _getpass from os import close from os.path import join from shutil import move @@ -45,6 +45,12 @@ PRIMARY KEY(id) );''' +def getpass(prompt, cfg: Config) -> str: + if cfg.db_password is not None: + return cfg.db_password + return _getpass(prompt) + + class Db: VERSION = [1, 0, 0, 2] @@ -83,7 +89,7 @@ class Db: hydrate_file_if_needed(fn) self.db = connect(fn, check_same_thread=False) if config.encrypt_db: - passpharse = getpass('Please input the password of the database:') + passpharse = getpass('Please input the password of the database:', config) # noqa: E501 if not self.encrypted: tfn = mkstemp() close(tfn[0]) @@ -98,7 +104,7 @@ class Db: self.db = connect(fn, check_same_thread=False) elif opts.change_key: self.__set_encrypt_key(passpharse) - passpharse = getpass('Please input new password of the database:') # noqa: E501 + passpharse = getpass('Please input new password of the database:', config) # noqa: E501 tfn = mkstemp() close(tfn[0]) tfn = tfn[1] @@ -113,7 +119,7 @@ class Db: self.__set_encrypt_key(passpharse) else: if self.encrypted: - passpharse = getpass('Please input the password of the database:') # noqa: E501 + passpharse = getpass('Please input the password of the database:', config) # noqa: E501 self.__set_encrypt_key(passpharse) tfn = mkstemp() close(tfn[0])