Skip to content

Commit f61362f

Browse files
committed
2.023: auto update
1 parent 8f57160 commit f61362f

File tree

17 files changed

+289
-12
lines changed

17 files changed

+289
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.023
4+
- 自动更新
5+
- 修复保存任务时logger无法序列化的bug (感谢 @9chu)
6+
37
## 2.022
48
- 增加下载速度显示
59
- 增加低速自动重试 `low_speed_threshold`, 默认为`10KB/s`以下重试

README.chs.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ xeH
5555
- **download_timeout** 设置下载图片的超时,默认为`10`
5656
- **low_speed_threshold** 设置最低下载速度,低于此值将换源重新下载,单位为KB/s,默认为`10`
5757
- **ignored_errors** 设置忽略的错误码,默认为空,错误码可以从`const.py`中获得
58+
- **auto_update** 自动检查更新,`check` 仅检查更新,`download` 下载更新,`off` 关闭检查;默认为`download`
59+
- **update_beta_channel** 设置是否更新到测试版,默认为否
5860
- **log_path** 日志路径,默认为`eh.log`
5961
- **log_verbose** 日志等级,可选1-3,值越大输出越详细,默认为`2`
6062
- **save_tasks** 是否保存任务到`h.json`,可用于断点续传,默认为否
@@ -67,7 +69,8 @@ xeH
6769
[--rpc-interface ADDR] [--rpc-port PORT] [--rpc-secret ...]
6870
[--rpc-open-browser BOOL] [--delete-task-files BOOL] [-a BOOL]
6971
[--download-range a-b,c-d,e] [-t N] [--timeout N]
70-
[--low-speed-threshold N] [-f] [-l /path/to/eh.log] [-v] [-h]
72+
[--low-speed-threshold N] [-f] [--auto-update {check,download,off}]
73+
[--update-beta-channel BOOL] [-l /path/to/eh.log] [-v] [-h]
7174
[--version]
7275
[url [url ...]]
7376
@@ -112,6 +115,10 @@ xeH
112115
-t N, --thread N 下载线程数 (默认: 5)
113116
--timeout N 设置下载图片的超时 (默认: 10秒)
114117
-f, --force 忽略配额判断, 继续下载 (默认: False)
118+
--auto-update {check,download,off}
119+
检查并自动下载更新
120+
--update-beta-channel BOOL
121+
是否更新到测试分支
115122
-l /path/to/eh.log, --logpath /path/to/eh.log
116123
保存日志的路径 (默认: eh.log)
117124
-v, --verbose 设置日志装逼等级 (默认: 2)

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Configuration keys:
5353
- **download_timeout** Timeout of download images. Default to `10`s.
5454
- **low_speed_threshold** Retry download if speed is lower than specified value. Default to `10` KB/s.
5555
- **ignored_errors** Set the error codes to ignore and continue downloading. Default to *empty*. Error codes can be obtained from [const.py](xeHentai/const.py).
56+
- **auto_update** turn on auto update of program `check` for check only and `download` for download; `off` to turn off. Default to `download`.
57+
- **update_beta_channel** set to true to update to dev branch
5658
- **log_path** Set log file path. Default to `eh.log`.
5759
- **log_verbose** Set log level with integer from 1 to 3. Bigger value means more verbose output. Default to `2`.
5860
- **save_tasks** Set to save uncompleted tasks in `h.json`. Default to `False`.
@@ -65,7 +67,8 @@ Usage: xeh [-u USERNAME] [-k KEY] [-c COOKIE] [-i] [--daemon] [-d DIR] [-o]
6567
[--rpc-interface ADDR] [--rpc-port PORT] [--rpc-secret ...]
6668
[--rpc-open-browser BOOL] [--delete-task-files BOOL] [-a BOOL]
6769
[--download-range a-b,c-d,e] [-t N] [--timeout N]
68-
[--low-speed-threshold N] [-f] [-l /path/to/eh.log] [-v] [-h]
70+
[--low-speed-threshold N] [-f] [--auto-update {check,download,off}]
71+
[--update-beta-channel BOOL] [-l /path/to/eh.log] [-v] [-h]
6972
[--version]
7073
[url [url ...]]
7174
@@ -126,9 +129,14 @@ optional arguments:
126129
(default: 10 KB/s)
127130
-f, --force download regardless of quota exceeded warning
128131
(default: False)
132+
--auto-update {check,download,off}
133+
check or download update automatically
134+
(default: download)
135+
--update-beta-channel BOOL
136+
check update upon beta channel
137+
(default: True)
129138
-l /path/to/eh.log, --logpath /path/to/eh.log
130-
define log path (current:
131-
/Users/fffonion/Dev/Python/xeHentai/eh.log)
139+
define log path (default: eh.log)
132140
-v, --verbose show more detailed log (default: 3)
133141
-h, --help show this help message and exit
134142
--version show program's version number and exit

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'xeHentai',
2323
'xeHentai.util',
2424
'xeHentai.i18n',
25+
'xeHentai.updater',
2526
]
2627
requires = ['requests']
2728

xeH

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
#!/usr/bin/env python
22

3-
from xeHentai import cli
3+
import os
4+
import sys
5+
import json
6+
import zipfile
7+
from threading import Thread
8+
import xeHentai.const as const
49

5-
cli.start()
10+
SRC_UPDATE_FILE = const.SRC_UPDATE_FILE
11+
if const.PY3K:
12+
from importlib import reload
13+
14+
def load_update():
15+
if os.path.exists(SRC_UPDATE_FILE):
16+
try:
17+
need_remove = False
18+
update_id = ""
19+
with zipfile.ZipFile(SRC_UPDATE_FILE, 'r') as z:
20+
try:
21+
r = json.loads(z.read("info.json"))
22+
except:
23+
need_remove = True
24+
else:
25+
if 'v' not in r and r['v'] != SRC_UPDATE_VERSION:
26+
# ignoring legacy file
27+
need_remove = True
28+
else:
29+
update_id = r["update_id"]
30+
if need_remove:
31+
os.remove(SRC_UPDATE_FILE)
32+
return
33+
v = const.__version__
34+
sys.path.insert(0, SRC_UPDATE_FILE)
35+
import xeHentai
36+
reload(xeHentai)
37+
xeHentai.const.VERSION_UPDATE = update_id
38+
xeHentai.const.VERSION_UPDATE_LOADER = v
39+
except:
40+
if sys.path[0] == SRC_UPDATE_FILE:
41+
sys.path.pop(0)
42+
os.remove(SRC_UPDATE_FILE)
43+
44+
45+
if __name__ == "__main__":
46+
load_update()
47+
48+
from xeHentai import cli, i18n
49+
cli.start()

xeH.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
#!/usr/bin/env python
22

3-
from xeHentai import cli
3+
import sys
4+
import json
5+
import zipfile
6+
from threading import Thread
7+
from xeHentai.const import *
48

5-
cli.start()
9+
def load_update():
10+
if os.path.exists(SRC_UPDATE_FILE):
11+
try:
12+
need_remove = False
13+
update_id = ""
14+
with zipfile.ZipFile(SRC_UPDATE_FILE, 'r') as z:
15+
try:
16+
r = json.loads(z.read("info.json"))
17+
except:
18+
need_remove = True
19+
else:
20+
if 'v' not in r and r['v'] != SRC_UPDATE_VERSION:
21+
# ignoring legacy file
22+
need_remove = True
23+
else:
24+
update_id = r["update_id"]
25+
if need_remove:
26+
os.remove(SRC_UPDATE_FILE)
27+
return
28+
sys.path.insert(0, SRC_UPDATE_FILE)
29+
import xeHentai
30+
reload(xeHentai)
31+
xeHentai.const.VERSION_UPDATE = update_id
32+
except ImportError:
33+
if sys.path[0] == SRC_UPDATE_FILE:
34+
sys.path.pop(0)
35+
os.remove(SRC_UPDATE_FILE)
36+
except:
37+
pass
38+
return { "v": SRC_UPDATE_VERSION }
39+
40+
41+
if __name__ == "__main__":
42+
load_update()
43+
44+
from xeHentai import cli, i18n
45+
cli.start()

xeHentai/cli.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
def start():
2727
opt = parse_opt()
2828
xeH = xeHentai()
29+
if opt.auto_update != "off":
30+
check_update(xeH.logger, {
31+
"auto_update": opt.auto_update,
32+
"update_beta_channel": opt.update_beta_channel,
33+
})
2934
if opt.daemon:
3035
if opt.interactive:
3136
xeH.logger.warning(i18n.XEH_OPT_IGNORING_I)
@@ -44,6 +49,13 @@ def start():
4449
else:
4550
main(xeH, opt)
4651

52+
def check_update(l, cfg):
53+
from .updater.updater import check_update
54+
t = Thread(name="updater", target=check_update, args=(l, cfg))
55+
t.setDaemon(True)
56+
t.start()
57+
return t
58+
4759
def main(xeH, opt):
4860
xeH.update_config(**vars(opt))
4961
log = xeH.logger
@@ -181,6 +193,10 @@ def parse_opt():
181193
current = ERR_QUOTA_EXCEEDED in _def['ignored_errors'],
182194
add_value = ERR_QUOTA_EXCEEDED, dest='ignored_errors',
183195
help = i18n.XEH_OPT_f)
196+
parser.add_argument('--auto-update', default = _def['auto_update'], choices = ("check", "download", "off"),
197+
dest = 'auto_update', help = i18n.XEH_OPT_auto_update)
198+
parser.add_argument('--update-beta-channel', type = bool, metavar = "BOOL", default = _def['update_beta_channel'],
199+
dest = 'update_beta_channel', help = i18n.XEH_OPT_update_beta_channel)
184200

185201
parser.add_argument('-l', '--logpath', metavar = '/path/to/eh.log',
186202
default = os.path.abspath(_def['log_path']), help = i18n.XEH_OPT_l)

xeHentai/config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@
6666
delete_task_files = False
6767

6868
# retry a connection if per thread speed is lower than this value, unit is KB per second
69-
low_speed_threshold = 10
69+
low_speed_threshold = 10
70+
71+
# turn on auto update of program "check" for check only and "download" for download
72+
auto_update = "download"
73+
# set to true to update to dev branch
74+
update_beta_channel = False

xeHentai/const.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
CODEPAGE = locale.getdefaultlocale()[1] or 'ascii'
1717
ANDROID = 'ANDROID_ARGUMENT' in os.environ
1818

19-
__version__ = 2.022
20-
DEVELOPMENT = False
19+
__version__ = 2.023
20+
VERSION_UPDATE = ""
21+
DEVELOPMENT = True
2122

2223
SCRIPT_NAME = "xeHentai"
2324

@@ -29,6 +30,9 @@
2930
# The application is not frozen
3031
# Change this bit to match where you store your data files:
3132
FILEPATH = sys.path[0]
33+
# if update is being injected
34+
if FILEPATH.endswith(".zip"):
35+
FILEPATH = sys.path[1]
3236

3337
DUMMY_FILENAME = "-dummy-"
3438
RENAME_TMPDIR = "-xeh-conflict-"
@@ -37,6 +41,9 @@
3741
STATIC_CACHE_TTL = 3600
3842
STATIC_CACHE_VERSION = 1
3943

44+
SRC_UPDATE_FILE = os.path.join(FILEPATH, "src.zip")
45+
SRC_UPDATE_VERSION = 1
46+
4047
RE_INDEX = re.compile('.+/(\d+)/([^\/]+)/*')
4148
RE_GALLERY = re.compile('/([a-f0-9]{10})/[^\-]+\-(\d+)')
4249
RE_IMGHASH = re.compile('/([a-f0-9]{40})-(\d+)-(\d+)-(\d+)-([a-z]{,4})')

xeHentai/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
class xeHentai(object):
3838
def __init__(self):
3939
self.verstr = "%.3f%s" % (__version__, '-dev' if DEVELOPMENT else "")
40+
if VERSION_UPDATE:
41+
self.verstr = "%s-%s(%s)" % (self.verstr, VERSION_UPDATE[:7], VERSION_UPDATE_LOADER)
4042
self.logger = logger.Logger()
4143
self._exit = False
4244
self.tasks = Queue() # for queueing, stores gid only

0 commit comments

Comments
 (0)