Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit40181c2

Browse files
author
ksbao
committed
update scripts
1 parentacbbe06 commit40181c2

File tree

2 files changed

+168
-79
lines changed

2 files changed

+168
-79
lines changed

‎softcenter/gen_install.py‎

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#!/usr/bin/envpython
2-
#_*_ coding:utf-8_*_
1+
#!/usr/bin/envpython3
2+
#-*- coding:utf-8-*-
33

44
importos
55
importjson
66
importhashlib
7-
importcodecs
87
fromshutilimportcopyfile
98
importsys
109

@@ -23,28 +22,41 @@ def md5sum(full_path):
2322

2423
defwork_paths_by_walk():
2524
index=0
26-
forroot,subdirs,filesinos.walk(parent_path):
25+
forroot,subdirs,filesinos.walk(parent_path):
2726
index+=1
2827
forfilepathinfiles:
29-
printos.path.join(root,filepath)
28+
print(os.path.join(root,filepath))# 已添加括号
3029
forsubinsubdirs:
31-
printos.path.join(root,sub)
30+
print(os.path.join(root,sub))# 已添加括号
3231

3332
defwork_parent():
34-
ignore_paths=frozenset(["softcenter","appledns","speedtest"])
35-
forfnameinos.listdir(parent_path):
36-
33+
ignore_paths=frozenset(["koolcenter","softcenter","speedtest","dualwan","kcptun","koolnet","kuainiao","shadowvpn","tunnel","ssid","ssserver","qiaodao","uamas"])
34+
fnames=os.listdir(parent_path)
35+
fnames.sort()
36+
forfnameinfnames:
3737
iffname[0]=="."orfnameinignore_paths:
3838
continue
39+
path=os.path.join(parent_path,fname)
40+
ifos.path.isdir(path):
41+
yieldfname,path
3942

43+
defwork_parent_ext():
44+
contain_paths=frozenset(["cfetool"])
45+
fnames=os.listdir(parent_path)
46+
fnames.sort()
47+
forfnameinfnames:
48+
iffname[0]=="."orfnamenotincontain_paths:
49+
continue
4050
path=os.path.join(parent_path,fname)
4151
ifos.path.isdir(path):
42-
#printfname
43-
#printpath
52+
print(fname)# 已添加括号
53+
print(path)# 已添加括号
4454
yieldfname,path
4555

4656
defwork_files(parent,ext):
47-
forfnameinos.listdir(parent):
57+
fnames=os.listdir(parent)
58+
fnames.sort()
59+
forfnameinfnames:
4860
path=os.path.join(parent,fname)
4961
ifos.path.isfile(path):
5062
yieldpath
@@ -54,10 +66,9 @@ def check_subdir(module, path, name, ext, target_path):
5466
ifos.path.isdir(script_path):
5567
forfinwork_files(script_path,ext):
5668
target_file=os.path.join(target_path,os.path.basename(f))
57-
#print "copy", f, "-->", target_file
5869
copyfile(f,target_file)
5970
ifnottarget_file.endswith(".png")andto_remove:
60-
to_remove.write(target_file+"\n")
71+
to_remove.write(target_file+"\n")
6172

6273
defcheck_and_cp():
6374
formodule,pathinwork_parent():
@@ -69,10 +80,9 @@ def check_and_cp():
6980
defgen_modules(modules):
7081
formodule,pathinwork_parent():
7182
conf=os.path.join(path,"config.json.js")
72-
7383
m=None
7484
try:
75-
withcodecs.open(conf,"r","utf-8")asfc:
85+
withopen(conf,"r",encoding="utf-8")asfc:
7686
m=json.loads(fc.read())
7787
ifm:
7888
m["name"]=module
@@ -82,26 +92,46 @@ def gen_modules(modules):
8292
m["home_url"]="Module_"+module+".asp"
8393
except:
8494
pass
95+
ifnotm:
96+
m= {"name":module,"title":module,"tar_url":module+"/"+module+".tar.gz"}
97+
modules.append(m)
8598

99+
defgen_modules_ext(modules):
100+
formodule,pathinwork_parent_ext():
101+
conf=os.path.join(path,"config.json.js")
102+
m=None
103+
try:
104+
withopen(conf,"r",encoding="utf-8")asfc:
105+
m=json.loads(fc.read())
106+
ifm:
107+
m["name"]=module
108+
if"tar_url"notinm:
109+
m["tar_url"]=module+"/"+module+".tar.gz"
110+
if"home_url"notinm:
111+
m["home_url"]="Module_"+module+".asp"
112+
except:
113+
pass
86114
ifnotm:
87-
m= {"name":module,"title":module,"tar_url":module+"/"+module+".tar.gz"}
115+
m= {"name":module,"title":module,"tar_url":module+"/"+module+".tar.gz"}
88116
modules.append(m)
89117

90118
ifstage=="stage1":
91-
to_remove=open(os.path.join(curr_path,"to_remove.txt"),"w")
119+
to_remove=open(os.path.join(curr_path,"to_remove.txt"),"w",encoding="utf-8")
92120
check_and_cp()
93121
to_remove.close()
94-
else:
122+
elifstage=="stage2":
95123
gmodules=None
96-
withcodecs.open(os.path.join(curr_path,"app.template.json.js"),"r","utf-8")asfg:
124+
withopen(os.path.join(curr_path,"app.template.json.js"),"r",encoding="utf-8")asfg:
97125
gmodules=json.loads(fg.read())
98126
gmodules["apps"]= []
127+
#gmodules["apps1"] = []
99128
gen_modules(gmodules["apps"])
129+
#gen_modules_ext(gmodules["apps1"])
100130

101-
withcodecs.open(os.path.join(curr_path,"config.json.js"),"r","utf-8")asfc:
131+
withopen(os.path.join(curr_path,"config.json.js"),"r",encoding="utf-8")asfc:
102132
conf=json.loads(fc.read())
103133
gmodules["version"]=conf["version"]
104134
gmodules["md5"]=conf["md5"]
105135

106-
withcodecs.open(os.path.join(curr_path,"app.json.js"),"w","utf-8")asfw:
107-
json.dump(gmodules,fw,sort_keys=True,indent=4,ensure_ascii=False,encoding='utf8')
136+
withopen(os.path.join(curr_path,"app.json.js"),"w",encoding="utf-8")asfw:
137+
json.dump(gmodules,fw,sort_keys=True,indent=4,ensure_ascii=False)

‎softcenter/sync.py‎

Lines changed: 115 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
11
#!/usr/bin/env python
22
# _*_ coding:utf-8 _*_
33

4+
45
importos
5-
importurlparse
6-
importhttplib
6+
importurllib.parse
7+
importhttp.client
78
importjson
89
importhashlib
910
importcodecs
1011
fromshutilimportcopyfile
1112
importsys
1213
importtraceback
13-
fromdistutils.versionimportLooseVersion
14-
fromstringimportTemplate
14+
importre# 用于正则解析 git_path 和 JS 预处理
15+
frompackagingimportversion# 新增:用于版本比较(替换 LooseVersion)
16+
importtarfile
17+
fromstringimportTemplate
18+
19+
try:
20+
importrequests
21+
exceptImportError:
22+
raiseImportError("The 'requests' library is required but not installed. Please install it before running this script.")
1523

1624
#https://docs.python.org/2.4/lib/httplib-examples.html
1725

26+
1827
curr_path=os.path.dirname(os.path.realpath(__file__))
1928
parent_path=os.path.realpath(os.path.join(curr_path,".."))
2029
git_bin="git"
2130

2231
defhttp_request(url,depth=0):
2332
ifdepth>10:
24-
raiseException("Redirected "+depth+" times, giving up.")
25-
o=urlparse.urlparse(url,allow_fragments=True)
26-
ifo.scheme=='https':
27-
conn=httplib.HTTPSConnection(o.netloc)
28-
else:
29-
conn=httplib.HTTPConnection(o.netloc)
30-
path=o.path
31-
ifo.query:
32-
path+='?'+o.query
33-
conn.request("GET",path,"", {"Cache-Control":"max-age=0"})
34-
response=conn.getresponse()
35-
#print response.status, response.reason
36-
37-
ifresponse.status>300andresponse.status<400:
38-
headers=dict(response.getheaders())
39-
ifheaders.has_key('location')andheaders['location']!=url:
40-
#print headers['location']
41-
returnhttp_request(headers['location'],depth+1)
42-
43-
data=response.read()
44-
returndata
33+
raiseException("Redirected {} times, giving up.".format(depth))
34+
try:
35+
resp=requests.get(url,headers={"Cache-Control":"max-age=0"},timeout=10,allow_redirects=False)
36+
# 处理重定向
37+
if300<resp.status_code<400and"location"inresp.headersandresp.headers["location"]!=url:
38+
returnhttp_request(resp.headers["location"],depth+1)
39+
returnresp.content
40+
exceptExceptionase:
41+
print(f"[http_request] requests error:{e}")
42+
raise
4543

4644
defwork_modules():
4745
module_path=os.path.join(curr_path,"modules.json")
@@ -52,58 +50,101 @@ def work_modules():
5250
forminmodules:
5351
if"module"inm:
5452
try:
55-
up=sync_module(m["module"],m["git_source"])
53+
up=sync_module(m["module"],m["git_source"],m["branch"])
5654
ifnotupdated:
5755
updated=up
58-
exceptException,e:
56+
exceptExceptionase:
5957
traceback.print_exc()
6058
returnupdated
6159

62-
defsync_module(module,git_path):
60+
defsync_module(module,git_path,branch):
6361
module_path=os.path.join(parent_path,module)
6462
conf_path=os.path.join(module_path,"config.json.js")
65-
rconf=get_remote_js(git_path)
63+
rconf=get_remote_js(git_path,branch)
64+
ifrconfisNone:
65+
print(f"[sync_module] Skipping module '{module}' due to invalid config URL.")
66+
returnFalse# 不更新,返回 False
6667
lconf=get_local_js(conf_path)
6768
update=False
6869
ifnotrconf:
69-
return
70-
printrconf
70+
returnFalse# 修改:如果 rconf 无效,也返回 False
71+
print(rconf)
7172
ifnotlconf:
7273
update=True
7374
else:
74-
ifLooseVersion(rconf["version"])>LooseVersion(lconf["version"]):
75+
# 修改:使用 packaging.version.parse 替换 LooseVersion
76+
ifversion.parse(rconf["version"])>version.parse(lconf["version"]):
7577
update=True
7678
ifupdate:
77-
print"updating",git_path
79+
print("updating",git_path)
7880
cmd=""
79-
tar_path=os.path.join(module_path,"%s.tar.gz"%module);
81+
tar_path=os.path.join(module_path,"%s.tar.gz"%module)
8082
ifos.path.isdir(module_path):
81-
cmd="cd $module_path && git reset --hard && $git_bin pull && rm -f $module.tar.gz && tar -zcf $module.tar.gz $module"
83+
#cmd = "cd $module_path && $git_bin reset --hard && $git_bin clean -fdqx && $git_bin pull && rm -f $module.tar.gz && tar -zcf $module.tar.gz $module"
84+
cmd="cd $module_path && $git_bin reset --hard && $git_bin clean -fdqx && $git_bin fetch --depth 1 && rm -f $module.tar.gz"
8285
else:
83-
cmd="cd $parent_path && $git_bin clone $git_path $module_path && cd $module_path && tar -zcf $module.tar.gz $module"
86+
# 修改:添加 --depth 1 和 --branch $branch 以实现浅克隆指定分支
87+
cmd="cd $parent_path && $git_bin clone --depth 1 --branch $branch $git_path $module_path && cd $module_path && tar -zcf $module.tar.gz $module"
8488
t=Template(cmd)
85-
params= {"parent_path":parent_path,"git_path":git_path,"module_path":module_path,"module":module,"git_bin":git_bin}
89+
params= {"parent_path":parent_path,"git_path":git_path,"module_path":module_path,"module":module,"git_bin":git_bin,"branch":branch}
8690
s=t.substitute(params)
8791
os.system(s)
8892
rconf["md5"]=md5sum(tar_path)
8993
withcodecs.open(conf_path,"w","utf-8")asfw:
90-
json.dump(rconf,fw,sort_keys=True,indent=4,ensure_ascii=False,encoding='utf8')
91-
os.system("cd %s && chown -R www:www ."%module_path)
94+
json.dump(rconf,fw,sort_keys=True,indent=4,ensure_ascii=False)
95+
#os.system("cd %s && chown -R www:www ." % module_path)
9296
returnupdate
9397

94-
defget_config_js(git_path):
95-
#https://github.com/koolshare/merlin_tunnel.git
96-
#git@github.com:koolshare/merlin_tunnel.git
97-
98-
ifgit_path.startswith("https://"):
99-
returngit_path[0:-4]+"/raw/master/config.json.js"
98+
defget_config_js(git_path,branch):
99+
ifnotgit_path:
100+
print("[get_config_js] Warning: git_source is empty for this module.")
101+
returnNone
102+
103+
# 解析 git_path 以提取 owner 和 repo
104+
# 支持格式:
105+
# - https://github.com/owner/repo.git
106+
# - git@github.com:owner/repo.git
107+
# - owner/repo.git 或 owner/repo
108+
owner=None
109+
repo=None
110+
111+
# 去除可能的 .git 后缀
112+
ifgit_path.endswith(".git"):
113+
git_path=git_path[:-4]
114+
115+
# 正则匹配常见格式
116+
match_https=re.match(r"https?://github\.com/([^/]+)/([^/]+)",git_path)
117+
match_ssh=re.match(r"git@github\.com:([^/]+)/([^/]+)",git_path)
118+
match_short=re.match(r"([^/]+)/([^/]+)",git_path)
119+
120+
ifmatch_https:
121+
owner,repo=match_https.groups()
122+
elifmatch_ssh:
123+
owner,repo=match_ssh.groups()
124+
elifmatch_short:
125+
owner,repo=match_short.groups()
100126
else:
101-
index=git_path.find(":")
102-
return"https://github.com/"+git_path[index+1:-4]+"/raw/master/config.json.js"
103-
104-
defget_remote_js(git_path):
105-
data=http_request(get_config_js(git_path))
106-
conf=json.loads(data)
127+
print(f"[get_config_js] Error: Invalid git_source format:{git_path}")
128+
returnNone
129+
130+
# 构建正确的 GitHub raw URL
131+
raw_url=f"https://raw.githubusercontent.com/{owner}/{repo}/{branch}/config.json.js"
132+
returnraw_url
133+
134+
defget_remote_js(git_path,branch):
135+
url=get_config_js(git_path,branch)
136+
ifnoturl:
137+
returnNone
138+
data=http_request(url)
139+
ifnotdata:
140+
print(f"[get_remote_js] Warning: No data received from{url}")
141+
returnNone
142+
try:
143+
conf=json.loads(data.decode('utf-8'))
144+
exceptExceptionase:
145+
print(f"[get_remote_js] Error decoding JSON from{url}:{e}")
146+
print(f"[get_remote_js] Raw data:{data}")
147+
returnNone
107148
returnconf
108149

109150
defget_local_js(conf_path):
@@ -130,20 +171,39 @@ def work_parent():
130171
ifos.path.isdir(path):
131172
yieldfname,path
132173

174+
defparse_js_config(content):
175+
# """辅助函数:预处理 .js 文件内容,提取纯 JSON 部分"""
176+
# 去除注释(单行 // 和多行 /* */)
177+
content=re.sub(r'//.*?$|/\*.*?\*/','',content,flags=re.MULTILINE|re.DOTALL)
178+
# 去除 var ... = 和结尾 ;
179+
content=re.sub(r'^\s*(var\s+\w+\s*=\s*)?\{','{',content)
180+
content=re.sub(r';\s*$','',content)
181+
# 如果键没有引号,添加(简单正则替换,假设键是字母数字)
182+
content=re.sub(r'(\s*)([a-zA-Z0-9_]+)\s*:',r'\1"\2":',content)
183+
returncontent.strip()
184+
133185
defgen_modules(modules):
134186
formodule,pathinwork_parent():
135187
conf=os.path.join(path,"config.json.js")
136188
m=None
137189
try:
138190
withcodecs.open(conf,"r","utf-8")asfc:
139-
m=json.loads(fc.read())
191+
raw_content=fc.read()
192+
# 预处理为纯 JSON
193+
json_content=parse_js_config(raw_content)
194+
m=json.loads(json_content)
140195
ifm:
141196
m["name"]=module
142197
if"tar_url"notinm:
143198
m["tar_url"]=module+"/"+module+".tar.gz"
144199
if"home_url"notinm:
145200
m["home_url"]="Module_"+module+".asp"
146-
except:
201+
exceptjson.JSONDecodeErrorase:
202+
print(f"[gen_modules] JSON decode error in{conf}:{e}")
203+
print(f"[gen_modules] Raw content:{raw_content}")
204+
# 跳过无效模块,使用默认值
205+
m=None
206+
exceptExceptionase:
147207
traceback.print_exc()
148208

149209
ifnotm:
@@ -163,10 +223,9 @@ def refresh_gmodules():
163223
gmodules["md5"]=conf["md5"]
164224

165225
withcodecs.open(os.path.join(curr_path,"app.json.js"),"w","utf-8")asfw:
166-
json.dump(gmodules,fw,sort_keys=True,indent=4,ensure_ascii=False,encoding='utf8')
226+
json.dump(gmodules,fw,sort_keys=True,indent=4,ensure_ascii=False)
167227

168228
updated=work_modules()
169229
ifupdated:
170230
refresh_gmodules()
171-
os.system("chown -R www:www %s/softcenter/app.json.js"%parent_path)
172-
os.system("sh %s/softcenter/build.sh"%parent_path)
231+
#os.system("chown -R www:www %s/softcenter/app.json.js" % parent_path)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp