Skip to content

Commit

Permalink
Merge pull request #21 from RimoChan/自動更新強化
Browse files Browse the repository at this point in the history
現在自動更新除了git pull外,也爲release版安裝pip和下載二進制文件。
  • Loading branch information
RimoChan authored Aug 1, 2020
2 parents 9770cc7 + 989c01e commit f056d78
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 17 deletions.
4 changes: 4 additions & 0 deletions librian/librian_util/release信息.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import 路徑


是release = (路徑.librian外層 / '.librian_release_info').is_file()
69 changes: 69 additions & 0 deletions librian/librian_util/更新器.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import logging
import subprocess
import base64
import hashlib

import requests

from . import 路徑, release信息


嵌入的python路徑 = 路徑.librian外層 / 'python36/python.exe'

嵌入的git路徑 = 路徑.librian外層 / 'MinGit-2.25.0-busybox-64-bit/cmd/git.exe'


def 自我更新():
if release信息.是release:
git路徑 = 嵌入的git路徑
else:
try:
git路徑 = 'git'
subprocess.check_call('git --version', stdout=subprocess.DEVNULL)
except FileNotFoundError:
raise FileNotFoundError('需要一個Git。更新功能需要你有Git命令可用,或者使用release版本中嵌入的Git。')
subprocess.run(
[str(git路徑), 'pull', 'origin', 'master'],
shell=True,
check=True,
stderr=subprocess.PIPE,
cwd=路徑.librian外層,
)

if release信息.是release:
subprocess.run(
[str(嵌入的python路徑), '-m', 'pip', 'install', '-r', 'requirements_release.txt'],
shell=True,
check=True,
stderr=subprocess.PIPE,
cwd=路徑.librian外層,
)
build文件更新()


def build文件更新():
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return base64.b64encode(hash_md5.digest()).decode('utf-8')

def 下載(文件名, 網址):
r = requests.get(網址)
with open(文件名, 'wb') as f:
f.write(r.content)

文件表 = {
'Librian面板.exe': 'https://rimosto-cdn.azureedge.net/librian/Librian面板.exe'
}
for 文件名, 網址 in 文件表.items():
文件真名 = 路徑.librian外層 / 文件名
if 文件真名.is_file():
本地md5 = md5(文件真名)
在線md5 = requests.head(網址).headers['Content-MD5']
if 本地md5 == 在線md5:
continue
logging.warning(f'{文件名}過期了。')
下載(文件名, 網址)
logging.warning(f'下載{文件名}。')
21 changes: 8 additions & 13 deletions librian/librian面板/librian_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from librian.librian本體.帶有vue的山彥 import 帶有vue的山彥
from librian.librian本體.librian虛擬機 import 虛擬機環境

from librian.librian_util import 加載器, 文件, 路徑
from librian.librian_util import 加載器, 文件, 路徑, 更新器


rp = dulwich.repo.Repo('.')
Expand Down Expand Up @@ -169,19 +169,14 @@ def 瀏覽器打開(self, s):
webbrowser.open(s)

def 自我更新(self, callback):
git路徑 = 'git'
try:
subprocess.check_call('git --version', stdout=subprocess.DEVNULL)
except FileNotFoundError:
嵌入的git路徑 = 路徑.librian外層 / 'MinGit-2.25.0-busybox-64-bit/cmd/git.exe'
if Path(嵌入的git路徑).is_file():
git路徑 = 嵌入的git路徑
else:
self.alert('需要一個Git', 'info', '更新功能需要你有Git命令可用,或者使用release版本中嵌入的Git。')
return
def t():
r = subprocess.run(f'{git路徑} pull origin master', stderr=subprocess.PIPE, encoding='utf8', check=False)
callback.Call([r.returncode, r.stderr])
try:
更新器.自我更新()
except subprocess.CalledProcessError as e:
callback.Call([e.returncode, e.stderr.decode('gbk')])
except Exception as e:
callback.Call([1, e.__repr__()])
callback.Call([0, ''])
threading.Thread(target=t).start()

def 退出(self):
Expand Down
4 changes: 2 additions & 2 deletions librian/雜物/構建用/Librian面板.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#include <cassert>

int main(int argc, char *argv[]) {
system("\"\"./python36/python\"\" -m librian.librianÃæ°å.librian_panel");
system("pause");
// system("\"\"./python36/python\"\" -m librian.librianÃæ°å.librian_panel");
system("start \"LibrianÃæ°å\" cmd /C \"\"./python36/python.exe\" -m librian.librianÃæ°å.librian_panel & pause\"");
}
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ weasyprint>=50
PyYAML>=5.2
fire>=0.2.1
dulwich>=0.19.14
AppKit==0.2.8 ; sys_platform == 'darwin'
requests>=2.24.0
AppKit>=0.2.8 ; sys_platform == 'darwin'
3 changes: 2 additions & 1 deletion requirements_release.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ Pillow==5.2.0
opencc==1.1.1
PyYAML==5.1
fire==0.2.1
dulwich==0.19.14
dulwich==0.19.14
requests==2.24.0

0 comments on commit f056d78

Please sign in to comment.