找回密码
 会员注册
查看: 21|回复: 0

使用mirror.ghproxy.com加速raw.githubusercontent.com以解决brewupgradepython@3.12慢的问题

[复制链接]

2万

主题

0

回帖

6万

积分

超级版主

积分
65028
发表于 2024-9-10 19:52:57 | 显示全部楼层 |阅读模式
MacOS系统上,升级python3.12时,超级慢,而且最后还失败了。看了日志,发现是用curl从raw.githubusercontent.com上下载Python安装包超时了。解决方案一:开启VPN工具,穿越围墙解决方案二:使用mirror.ghproxy.com加速raw.githubusercontent.com穿墙有风险,操作需谨慎,这里我采用的是方案二。1.查看curl路径whichcurl#/usr/bin/curl2.编写自定义脚本,并赋予执行权限ln-s`whichcurl`/usr/local/bin/curl[-d~/.local/bin]||mkdir-p~/.local/bin/cd~/.local/bintouchcurlchmod+xcurl#注:PATH要配置到.bashrc或.zshrc里,别的窗口才能起作用exportPATH=$HOME/.local/binPATHvicurlwhichcurl#~/.local/bin/curl3.脚本内容#!/usr/bin/envpythonimportosimportreimportsystry:fromtypingimportList#NOQA:F401exceptImportError:passRE_SKIP=re.compile(r'\.(zip|rar|7z|apk|ipa|exe|msi|m3u|m3u8|mp4|mp3))defrewrite_url(i,scheme):#typestr,str)->strproxy="mirror.ghproxy.com"domains=("raw.githubusercontent.com","gist.githubusercontent.com")#TODO:usepersonalCloudflareWorkerinstead.j=i[len(scheme):]fordomainindomains:ifj.startswith(domain):ifnotRE_SKIP.search(j):i=scheme+proxy+'/'+ibreakreturnidefgen_cmd(sys_argv):#typeList[str])->strargs=[]scheme="https://"foriinsys_argv[1:]:ifi.startswith(scheme):i=rewrite_url(i,scheme)elif""ini:i=repr(i)args.append(i)tool="/usr/local/bin/"+sys_argv[0].split("/")[-1]cmd=tool+""+"".join(args)returncmddefmain():#type)->intsys_argv=sys.argvif"--dry"insys_argv:sys_argv=[iforiinsys_argvifi!="--dry"]print("-->",gen_cmd(sys_argv))return0returnos.system(gen_cmd(sys_argv))if__name__=="__main__":sys.exit(main())4.测试效果curlhttps://raw.githubusercontent.com/Homebrew/homebrew-core/a775cbd0967da13128293d71fb26431fdedee6fb/Formula/m/mpdecimal.rb5.重新执行升级命令(这时候就很快了,不到一分钟就下载完毕,十分钟内就完成升级)brewupgradepython@3.12注:wget也可以用类似的方法加速#################2024.02.08补充:如果有外网服务器,还可以配个中转请求:1.本地curl文件内容如下(需exportJUMPER_IP='')#!/usr/bin/envpythonimportosimportsystry:fromtypingimportList#NOQA:F401exceptImportError:passdefgen_cmd(sys_argv):#typeList[str])->strargs=[]scheme="https://"origin,target="raw.githubusercontent.com","raw.gitmirror.com"domains=("objects.githubusercontent.com","github.com")redirect=os.getenv("JUMPER_IP")host="http://{}:9337/".format(redirect)foriinsys_argv[1:]:ifi.startswith(scheme):j=i[len(scheme):]ifj.startswith(origin):i=i.replace(origin,target)elifredirect:fordomainindomains:ifj.startswith(domain):i=host+jbreakelif""ini:i=repr(i)args.append(i)tool="/usr/local/bin/"+sys_argv[0].split("/")[-1]cmd=tool+""+"".join(args)returncmddefmain():#type)->intsys_argv=sys.argvif"--dry"insys_argv:sys_argv=[iforiinsys_argvifi!="--dry"]print("-->"+gen_cmd(sys_argv))return0returnos.system(gen_cmd(sys_argv))if__name__=="__main__":sys.exit(main())2.服务器上的app.py文件如下:#!/usr/bin/envpythonimportre#pipinstallhttpxorjsonlogurusanicgunicorn'uvicorn[standard]'fromhttpximportAsyncClientfromloguruimportloggerfromorjsonimportdumps,loadsfromsanicimportSanic,rawapp=Sanic("Jumper",dumps=dumps,loads=loads)@app.route("",methods=["GET","POST","PUT","PATCH","DELETE"])asyncdefindex(request):returnraw(b"Welcome.Example>>http:8000/http.127.0.0.1:9000/home")@app.route("/",methods=["GET","POST","PUT","PATCH","DELETE"])asyncdefhandler(request,full:str):host,url=full.lstrip("/"),""ifm:=re.search(r"(https?)[^0-9a-zA-Z]+(.+)",host):scheme,host=m.groups()else:scheme="https"try:domain,url=host.split("/",1)exceptValueError:domain=hostbase_url=scheme+"://"+domaintarget_path=urlifqs:=request.query_string:target_path+="?"+qsifnottarget_path:target_path=base_urlifnottarget_path.startswith("/")andnottarget_path.startswith("http"):target_path="/"+target_pathlogger.debug(f"{base_url=};{target_path=}")asyncwithAsyncClient(base_url=base_url,follow_redirects=True,timeout=20)asclient:method,body=request.method,request.bodyr=awaitclient.request(method,target_path,content=body)ifr.status_code==302and(next_url:=r.headers.get("location")):r=awaitclient.request(method,next_url,content=body)returnraw(r.content,status=r.status_code)if__name__=="__main__":#pragma:nocoverapp.run(debug=True,host="0.0.0.0")3.后台启动服务:gunicornapp:app--bind0.0.0.0:9337--worker-classuvicorn.workers.UvicornWorker--daemon4.本地brew对应的install文件修改如下:cd$(brew--prefix)/Homebrew/Library/Homebrewgrep-rnDownloading*.rb#download_strategy.rb:426:vidownload_strategy.rb#给download的url增加前缀gitdiff增加这几行:if(domain=Homebrew::EnvConfig.artifact_domain)url=url.sub(%r{^https?://#{GitHubPackages::URL_DOMAIN}/}o,"#{domain.chomp("/")}/")+elsifurl.start_with?("https://cdn.")+puts"Leave#{url}tobeitself."+elsif!url.start_with?("https://mirrors.")+url="http://your-server-ip-or-domain/"+urlendohai"Downloading#{url}" 即除了mirrors和cdn开头的域名,全都使用自建域名加速elsifurl.start_with?("https://cdn.")puts"Leave#{url}tobeitself."elsif!url.start_with?("https://mirrors.")url="https://your.domain.com/"+url################################2024.03.06补充(Updated@2024.08.29)Thanksto https://github.com/hunshcn/gh-proxy步骤4可以用如下Python脚本来实现:#!/usr/bin/envpython"""Thisscriptisusedtoimprovedownloadspeedfor`brewinstall`Usage:pythonpad_brew_download_url.py"""importosimportsubprocessHOST="https://mirror.ghproxy.com/"PY_HOST="https://mirrors.huaweicloud.com/python/"PAD="""elsif(url.start_with?("https://cdn.")||url.start_with?("https://desktop.docker.com"))puts"Leave#{url}tobeitself."elsif(url.start_with?("https://ftpmirror.")||url.start_with?("https://ftp.gnu.org"))puts"Skip#{url}padding."elsifurl.start_with?("https://www.python.org/ftp/python/3.")url="%s"+url[34,url.length]elsif!url.start_with?("https://mirror")url="%s"+url"""defsay_done():#type)->Nonetry:fromrich.consoleimportConsoleexceptImportError:print("\nDone~\n")else:console=Console()console.log("[boldmagenta]Done.[/boldmagenta]",":vampire:")defcapture_output(cmd):#typestr)->strtry:r=subprocess.run(cmd,shell=True,capture_output=True)except(TypeError,AttributeError):#Forpythonstrs_index=text.index(s)pre=text[:s_index]lines=pre.splitlines()length=len(lines)if_index=lengthforiinrange(length-1,-1,-1):line=linesifline.strip().startswith("if"):if_index=ibreakelif_index=0start=if_index+2fori,oneinenumerate(lines[start:],start):ifone.strip().startswith("elsif"):elif_index=ibreakifelif_index:cut="\n".join(lines[:elif_index])ifpre.endswith("\n"):cut+="\n"text=cut+text[s_index:]returntextdefmain():#type)->Nonebrew_root=capture_output("brew--prefix")folder="Homebrew/Library/Homebrew"name="download_strategy.rb"file=os.path.join(brew_root,folder,name)withopen(file)asf:text=f.read()pad=(PAD%(PY_HOST,HOST)).lstrip("\n")ifpadintext:print("{}alreadyin{}\nNothingtodo!".format(HOST,file))returnbak_file=file+".bak"ifnotos.path.exists(bak_file):withopen(bak_file,"w")asfp:fp.write(text)print("Createbackupfileat:{}".format(bak_file))s='end\n\nohai"Downloading#{url}"'text=remove_old_pad(text,s)new_text=text.replace(s,pad+s)withopen(file,"w")asf:f.write(new_text)print("Insert\n{}into{}".format(pad,file))say_done()if__name__=="__main__":main()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-29 13:25 , Processed in 0.646996 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表