add list and list_leveldb_key
This commit is contained in:
@@ -7,7 +7,7 @@ from game_backuper.config import (
|
|||||||
)
|
)
|
||||||
from game_backuper.cml import Opts, OptAction
|
from game_backuper.cml import Opts, OptAction
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from os.path import exists, join
|
from os.path import exists, join, isdir
|
||||||
from os import mkdir, remove
|
from os import mkdir, remove
|
||||||
from game_backuper.file import new_file, copy_file, File, mkdir_for_file
|
from game_backuper.file import new_file, copy_file, File, mkdir_for_file
|
||||||
from game_backuper.filetype import FileType
|
from game_backuper.filetype import FileType
|
||||||
@@ -46,7 +46,7 @@ class BackupTask(Thread):
|
|||||||
elif isinstance(f, ConfigLeveldb):
|
elif isinstance(f, ConfigLeveldb):
|
||||||
from game_backuper.leveldb import have_leveldb
|
from game_backuper.leveldb import have_leveldb
|
||||||
if not have_leveldb:
|
if not have_leveldb:
|
||||||
raise ValueError('Leveldb is not supported.')
|
raise NotImplementedError('Leveldb is not supported.')
|
||||||
if not exists(f.full_path):
|
if not exists(f.full_path):
|
||||||
continue
|
continue
|
||||||
from game_backuper.leveldb import (
|
from game_backuper.leveldb import (
|
||||||
@@ -93,9 +93,26 @@ class Backuper:
|
|||||||
t = BackupTask(prog, self.db, self.conf)
|
t = BackupTask(prog, self.db, self.conf)
|
||||||
self.tasks.append(t)
|
self.tasks.append(t)
|
||||||
t.start()
|
t.start()
|
||||||
|
elif self.opts.action == OptAction.LIST:
|
||||||
|
print(prog.name)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.opts.programs_list is None:
|
if self.opts.action == OptAction.LIST_LEVELDB_KEY:
|
||||||
|
from game_backuper.leveldb import have_leveldb
|
||||||
|
if not have_leveldb:
|
||||||
|
raise NotImplementedError('Leveldb is not supported.')
|
||||||
|
from game_backuper.leveldb import list_leveldb_entries
|
||||||
|
for db in self.opts.programs_list:
|
||||||
|
if exists(db):
|
||||||
|
if isdir(db):
|
||||||
|
print(f'Keys in "{db}":')
|
||||||
|
for i in list_leveldb_entries(db):
|
||||||
|
print(i)
|
||||||
|
else:
|
||||||
|
raise FileExistsError(f'"{db}" should be a directory.')
|
||||||
|
else:
|
||||||
|
raise FileNotFoundError(f'Can not find "{db}"')
|
||||||
|
elif self.opts.programs_list is None:
|
||||||
for prog in self.conf.progs:
|
for prog in self.conf.progs:
|
||||||
self.deal_prog(prog)
|
self.deal_prog(prog)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ else:
|
|||||||
class OptAction(IntEnum):
|
class OptAction(IntEnum):
|
||||||
BACKUP = 0
|
BACKUP = 0
|
||||||
RESTORE = 1
|
RESTORE = 1
|
||||||
|
LIST = 2
|
||||||
|
LIST_LEVELDB_KEY = 3
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_str(v: str) -> IntEnum:
|
def from_str(v: str) -> IntEnum:
|
||||||
@@ -22,6 +24,10 @@ class OptAction(IntEnum):
|
|||||||
return OptAction.BACKUP
|
return OptAction.BACKUP
|
||||||
elif t == 'restore':
|
elif t == 'restore':
|
||||||
return OptAction.RESTORE
|
return OptAction.RESTORE
|
||||||
|
elif t == 'list':
|
||||||
|
return OptAction.LIST
|
||||||
|
elif t == 'list_leveldb_key':
|
||||||
|
return OptAction.LIST_LEVELDB_KEY
|
||||||
else:
|
else:
|
||||||
raise TypeError('Must be str.')
|
raise TypeError('Must be str.')
|
||||||
|
|
||||||
@@ -33,19 +39,26 @@ class Opts:
|
|||||||
|
|
||||||
def __init__(self, cml: List[str]):
|
def __init__(self, cml: List[str]):
|
||||||
try:
|
try:
|
||||||
r = getopt(cml, 'hc:', [])
|
r = getopt(cml, 'hc:', ['help', 'config='])
|
||||||
for i in r[0]:
|
for i in r[0]:
|
||||||
if i[0] == '-h':
|
if i[0] == '-h' or i[0] == '--help':
|
||||||
self.print_help()
|
self.print_help()
|
||||||
import sys
|
import sys
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif i[0] == '-c':
|
elif i[0] == '-c' or i[0] == '--config':
|
||||||
self.config_file = i[1]
|
self.config_file = i[1]
|
||||||
if len(r[1]) > 0:
|
if len(r[1]) > 0:
|
||||||
cm = r[1]
|
cm = r[1]
|
||||||
re = OptAction.from_str(cm[0])
|
re = OptAction.from_str(cm[0])
|
||||||
if re is not None:
|
if re is not None:
|
||||||
self.action = re
|
self.action = re
|
||||||
|
if re == OptAction.LIST:
|
||||||
|
return
|
||||||
|
elif re == OptAction.LIST_LEVELDB_KEY:
|
||||||
|
if len(cm) == 1:
|
||||||
|
raise GetoptError('list_leveldb_key need at least one db_path.') # noqa: E501
|
||||||
|
self.programs_list = cm[1:]
|
||||||
|
return
|
||||||
li = cm if re is None else cm[1:]
|
li = cm if re is None else cm[1:]
|
||||||
if len(li) > 0:
|
if len(li) > 0:
|
||||||
self.programs_list = li
|
self.programs_list = li
|
||||||
@@ -56,4 +69,9 @@ class Opts:
|
|||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
def print_help(self):
|
def print_help(self):
|
||||||
print('''game-backuper [options] [backup|restore] [game names]''')
|
print('''game-backuper [options] [backup|restore] [<game names> [...]]
|
||||||
|
game-backuper [options] list
|
||||||
|
game-backuper [options] list_leveldb_key [<db_path> [...]]
|
||||||
|
Options:
|
||||||
|
-h, --help Print help message.
|
||||||
|
-c, --config <path> Set config file.''')
|
||||||
|
|||||||
Reference in New Issue
Block a user