Upload command line

This commit is contained in:
2021-09-06 13:21:38 +08:00
parent adacf5a883
commit c96d31b30a
6 changed files with 66 additions and 7 deletions

3
game-backuper.py Normal file
View File

@@ -0,0 +1,3 @@
# You can use `pyinstaller -c game-backuper.py` to get a package version
from game_backuper import start
start()

View File

@@ -1,3 +1,4 @@
__version__ = "1.0.0"
from game_backuper.main import main from game_backuper.main import main
@@ -6,4 +7,6 @@ def start():
try: try:
sys.exit(main()) sys.exit(main())
except Exception: except Exception:
from traceback import print_exc
print_exc()
sys.exit(-1) sys.exit(-1)

View File

@@ -5,7 +5,7 @@ from game_backuper.config import (
ConfigNormalFile, ConfigNormalFile,
ConfigLeveldb, ConfigLeveldb,
) )
from game_backuper.cml import Opts 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
from os import mkdir from os import mkdir
@@ -58,11 +58,23 @@ class Backuper:
self.opts = opts self.opts = opts
self.tasks = [] self.tasks = []
def run(self): def deal_prog(self, prog: Program):
for prog in self.conf.progs: if self.opts.action == OptAction.BACKUP:
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()
def run(self):
if self.opts.programs_list is None:
for prog in self.conf.progs:
self.deal_prog(prog)
else:
for n in self.opts.programs_list:
if n not in self.conf.progs_name:
raise ValueError(f'Can not find "{n}" in config file.')
for n in self.opts.programs_list:
prog = self.conf.progs[self.conf.progs_name.index(n)]
self.deal_prog(prog)
self.wait() self.wait()
return 0 return 0

View File

@@ -1,6 +1,7 @@
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
from typing import List from typing import List
from platform import system from platform import system
from enum import IntEnum, unique
if system() == "Windows": if system() == "Windows":
import os import os
DEFAULT_CONFIG = f'{os.environ["APPDATA"]}\\game-backuper.yaml' DEFAULT_CONFIG = f'{os.environ["APPDATA"]}\\game-backuper.yaml'
@@ -8,8 +9,27 @@ else:
DEFAULT_CONFIG = '/etc/game-backuper.yaml' DEFAULT_CONFIG = '/etc/game-backuper.yaml'
@unique
class OptAction(IntEnum):
BACKUP = 0
RESTORE = 1
@staticmethod
def from_str(v: str) -> IntEnum:
if isinstance(v, str):
t = v.lower()
if t == 'backup':
return OptAction.BACKUP
elif t == 'restore':
return OptAction.RESTORE
else:
raise TypeError('Must be str.')
class Opts: class Opts:
config_file: str = DEFAULT_CONFIG config_file: str = DEFAULT_CONFIG
action = OptAction.BACKUP
programs_list = None
def __init__(self, cml: List[str]): def __init__(self, cml: List[str]):
try: try:
@@ -21,6 +41,14 @@ class Opts:
sys.exit(0) sys.exit(0)
elif i[0] == '-c': elif i[0] == '-c':
self.config_file = i[1] self.config_file = i[1]
if len(r[1]) > 0:
cm = r[1]
re = OptAction.from_str(cm[0])
if re is not None:
self.action = re
li = cm if re is None else cm[1:]
if len(li) > 0:
self.programs_list = li
except GetoptError: except GetoptError:
from traceback import print_exc from traceback import print_exc
print_exc() print_exc()
@@ -28,4 +56,4 @@ class Opts:
sys.exit(-1) sys.exit(-1)
def print_help(self): def print_help(self):
print('''game-backuper [options] [game names]''') print('''game-backuper [options] [backup|restore] [game names]''')

View File

@@ -79,6 +79,7 @@ class Program:
class Config: class Config:
dest = '' dest = ''
progs = [] progs = []
progs_name = []
def __init__(self, fn: str): def __init__(self, fn: str):
with open(fn, 'r', encoding='UTF-8') as f: with open(fn, 'r', encoding='UTF-8') as f:
@@ -99,4 +100,8 @@ class Config:
p = Program(prog) p = Program(prog)
if not p.check(): if not p.check():
raise ValueError('Config error: program information error') raise ValueError('Config error: program information error')
self.progs.append(p) if p.name not in self.progs_name:
self.progs_name.append(p.name)
self.progs.append(p)
else:
raise ValueError(f'have same name "{p.name}" in programs.')

View File

@@ -1,16 +1,24 @@
# flake8: noqa # flake8: noqa
import sys import sys
from game_backuper import __version__
if len(sys.argv) == 2 and sys.argv[1] == "py2exe": if len(sys.argv) == 2 and sys.argv[1] == "py2exe":
from distutils.core import setup from distutils.core import setup
import py2exe import py2exe
params = { params = {
"console": [{ "console": [{
'script': "game_backuper/__main__.py", 'script': "game_backuper/__main__.py",
"dest_base": 'game-backuper' "dest_base": 'game-backuper',
'version': __version__,
'product_name': 'game-backuper',
'product_version': __version__,
'company_name': 'lifegpc',
'description': 'A game backuper',
}], }],
"options": { "options": {
"py2exe": { "py2exe": {
"optimize": 2, "optimize": 2,
"compressed": 1,
"excludes": ["pydoc"]
} }
}, },
"zipfile": None, "zipfile": None,
@@ -25,7 +33,7 @@ else:
} }
setup( setup(
name="game-backuper", name="game-backuper",
version="1.0.0", version=__version__,
url="https://github.com/lifegpc/game-backuper", url="https://github.com/lifegpc/game-backuper",
author="lifegpc", author="lifegpc",
author_email="[email protected]", author_email="[email protected]",