33import shutil
44import logging
55from configparser import ConfigParser
6- from distutils .util import strtobool
76
87logging .basicConfig (level = logging .DEBUG , format = '%(levelname)s: %(message)s' )
98
9+ APP_ID = 'io.github.richardchien.coolqhttpapi'
10+
1011COOLQ_DIR = '/home/user/coolq'
1112COOLQ_CONFIG_FILE = os .path .join (COOLQ_DIR , 'conf' , 'CQP.cfg' )
1213COOLQ_APP_DIR = os .path .join (COOLQ_DIR , 'app' )
13- APP_ID = 'io.github.richardchien.coolqhttpapi'
14- APP_DIR = os .path .join (COOLQ_APP_DIR , APP_ID )
14+ COOLQ_APP_DATA_DIR = os .path .join (COOLQ_DIR , 'data' , 'app' )
15+
16+ APP_DIR_OLD = os .path .join (COOLQ_APP_DIR , APP_ID )
17+ APP_DIR = os .path .join (COOLQ_APP_DATA_DIR , APP_ID )
1518APP_CONFIG_DIR = os .path .join (APP_DIR , 'config' )
1619VERSION_LOCK_FILE = os .path .join (APP_DIR , 'version.lock' )
1720
18- CPK_FILE = '/home/user/io.github.richardchien.coolqhttpapi.cpk'
21+ CPK_NAME = f'{ APP_ID } .cpk'
22+ CPK_FILE = f'/home/user/{ CPK_NAME } '
23+
24+
25+ def strtobool (s ):
26+ return s .lower () in {'1' , 'true' , 'yes' , 'on' }
1927
2028
2129def touch (path ):
2230 with open (path , 'a' ):
2331 os .utime (path , None )
2432
2533
34+ def makedirs (dir , mode = 0o755 , exist_ok = True ):
35+ os .makedirs (dir , mode = mode , exist_ok = exist_ok )
36+
37+
2638def copy_cpk ():
27- shutil .copyfile (
28- CPK_FILE ,
29- os .path .join (COOLQ_APP_DIR , APP_ID + '.cpk' )
30- )
39+ makedirs (COOLQ_APP_DIR )
40+ shutil .copyfile (CPK_FILE , os .path .join (COOLQ_APP_DIR , CPK_NAME ))
3141
3242
3343def enable_plugin ():
@@ -97,10 +107,9 @@ def app_config_format():
97107
98108
99109def write_app_config (config_name , app_config ):
100- os . makedirs (APP_CONFIG_DIR , mode = 0o755 , exist_ok = True )
110+ makedirs (APP_CONFIG_DIR )
101111 file_format = app_config_format ()
102- with open (os .path .join (APP_CONFIG_DIR ,
103- config_name + '.' + file_format ),
112+ with open (os .path .join (APP_CONFIG_DIR , config_name + '.' + file_format ),
104113 'w' ) as config_file :
105114 if file_format == 'json' :
106115 json .dump (dict (app_config [config_name ].items ()),
@@ -109,17 +118,29 @@ def write_app_config(config_name, app_config):
109118 app_config .write (config_file )
110119
111120
121+ def move_old_app_dir ():
122+ if os .path .exists (APP_DIR_OLD ) and not os .path .exists (APP_DIR ):
123+ logging .debug (f'检测到旧的插件数据,正在复制到新位置 { APP_DIR } ……' )
124+ makedirs (os .path .dirname (APP_DIR ))
125+ shutil .copytree (APP_DIR_OLD , APP_DIR )
126+ app_data_backup_dir = f'{ APP_DIR_OLD .rstrip (os .path .sep )} .bak'
127+ shutil .move (APP_DIR_OLD , app_data_backup_dir )
128+ logging .debug (f'复制插件数据成功,旧的插件数据已备份在 { app_data_backup_dir } 。' )
129+
130+
112131def bootstrap ():
132+ move_old_app_dir ()
133+
113134 if is_first_start ():
114135 logging .debug ('容器首次启动,开始初始化……' )
115- os . makedirs (APP_DIR , exist_ok = True )
136+ makedirs (APP_DIR )
116137 logging .debug ('正在安装插件……' )
117138 copy_cpk ()
118139 touch (VERSION_LOCK_FILE )
119140 logging .debug ('正在启用插件……' )
120141 enable_plugin ()
121142 elif version_locked ():
122- logging .debug ('插件版本已锁定,开始覆盖 cpk 文件……' )
143+ logging .debug ('插件版本已锁定,正在覆盖 cpk 文件……' )
123144 copy_cpk ()
124145
125146 config_name = os .getenv ('COOLQ_ACCOUNT' , 'general' )
@@ -140,8 +161,12 @@ def bootstrap():
140161 remove_app_config (config_name , include_general = False )
141162 write_app_config (config_name , app_config )
142163
143- logging .info ('CoolQ HTTP API 插件 bootstrap 完成 。' )
164+ logging .info ('CQHTTP 插件 bootstrap 成功 。' )
144165
145166
146167if __name__ == '__main__' :
147- bootstrap ()
168+ try :
169+ bootstrap ()
170+ except Exception as e :
171+ logging .exception (e )
172+ logging .error ('CQHTTP 插件 bootstrap 失败。' )
0 commit comments