Skip to content

Commit

Permalink
Windows上已经可以配置Python
Browse files Browse the repository at this point in the history
  • Loading branch information
EricTianC committed Feb 22, 2021
1 parent 9292be7 commit 674995d
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 31 deletions.
13 changes: 12 additions & 1 deletion download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ type WriteCounter struct {
Bar *pgbar.Bar
}

var downNum = 0

func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Bar.Add(int(n))
return n, nil
}

func DownloadFile(filepath string, url string, title string) error {
if _, err := os.Stat(filepath); err == nil {
return nil
}
out, err := os.Create(filepath + ".tmp")
if err != nil {
return err
Expand All @@ -31,7 +36,9 @@ func DownloadFile(filepath string, url string, title string) error {
return err
}
defer resp.Body.Close()
bar := pgbar.NewBar(0, title, int(resp.ContentLength))
defer numMinus()
bar := pgbar.NewBar(downNum, title, int(resp.ContentLength))
downNum++
counter := &WriteCounter{bar}
if _, err = io.Copy(out, io.TeeReader(resp.Body, counter)); err != nil {
out.Close()
Expand All @@ -53,3 +60,7 @@ func Unpack(origin string, target string) error {
os.Remove(origin)
return nil
}

func numMinus() {
downNum--
}
19 changes: 17 additions & 2 deletions environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package environment

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -54,16 +55,29 @@ func (es *EnvSpace) CheckEnv() {
close(javaExits)
close(pyExits)
complete := make(chan struct{})
defer close(complete)
if js == false && yesorNot("未检测到Java环境,是否下载", true) {
gwg.Add(1)
es.DownloadJava(&gwg, complete)
} else {
go func() {
complete <- struct{}{}
}()
}
if pys == false && yesorNot("未检测到Python环境或版本过低,是否下载", true) {
gwg.Add(1)
es.DownloadPy(&gwg, complete)
} else {
go func() {
<-complete
}()
}
gwg.Wait()
es.CheckMcl()
err := es.CheckMcl()
if err != nil {
log.Panic(err)
}
CheckGraia()
}

//检查是否已安装
Expand Down Expand Up @@ -91,8 +105,9 @@ func (se *SimEnv) HasDirinEnvSpace(es *EnvSpace) bool {
}

//在空间中查找对应可执行文件
func (se *SimEnv) LookForExecFileInSpace(es *EnvSpace) bool {
func (se *SimEnv) LookForExecFileinSpace(es *EnvSpace) bool {
if !se.HasDirinEnvSpace(es) {
os.Mkdir(filepath.Join(es.BasePath, se.BasePath), os.ModePerm)
return false
}

Expand Down
17 changes: 17 additions & 0 deletions environment/graia.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package environment

import (
"os"
"os/exec"
)

func CheckGraia() {
_, err := exec.LookPath("graiax")
if err != nil {
cmd := exec.Command("pip", "install", "-U", "graiax")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
}
14 changes: 7 additions & 7 deletions environment/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
MIRROR = "https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/%s/%s/%s/%s/" //镜像地址,目前使用清华源,如要更换源需修改格式化代码
JAVA_MIRROR = "https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/%s/%s/%s/%s/" //镜像地址,目前使用清华源,如要更换源需修改格式化代码
JAVA_VERSION = "15" //要下载的Java版本
JDK_OR_JRE = "jre" //下载JDK还是JRE,然而清华原貌似没的选(Jre也有javac)
)
Expand Down Expand Up @@ -45,7 +45,7 @@ func (es *EnvSpace) CheckJava(js chan<- bool) {
}

//检查EnvSpace中是否有Java可执行文件
if jenv.LookForExecFileInSpace(es) {
if jenv.LookForExecFileinSpace(es) {
js <- true
return
}
Expand All @@ -62,7 +62,7 @@ func (es *EnvSpace) DownloadJava(gwg *sync.WaitGroup, complete chan<- struct{})
arch = "386"
}
//解析镜像列表Html
url := fmt.Sprintf(MIRROR, JAVA_VERSION, JDK_OR_JRE, ARCH[arch], OS[runtime.GOOS])
url := fmt.Sprintf(JAVA_MIRROR, JAVA_VERSION, JDK_OR_JRE, ARCH[arch], OS[runtime.GOOS])
doc, err := goquery.NewDocument(url)
if err != nil {
return err
Expand All @@ -75,18 +75,18 @@ func (es *EnvSpace) DownloadJava(gwg *sync.WaitGroup, complete chan<- struct{})
//正则筛选符合的文件
rt := fmt.Sprintf("^OpenJDK%sU-%s_%s_%s_hotspot_[0-9]{1,2}\\.[0-9]{1,2}\\.[0-9]{1,2}_[0-9]\\.(zip|tar\\.gz)", JAVA_VERSION, JDK_OR_JRE, ARCH[arch], OS[runtime.GOOS])
r, _ := regexp.Compile(rt)
var arch_url, name string
var archUrl, name string
for _, link := range links {
if r.MatchString(link) {
name = link
arch_url = url + link
archUrl = url + link
break
}
}
down.DownloadFile(name, arch_url, "下载Java")
down.DownloadFile(name, archUrl, "下载Java")
down.Unpack(name, filepath.Join(es.BasePath, jenv.BasePath))
go func() { complete <- struct{}{} }()
if jenv.LookForExecFileInSpace(es) {
if jenv.LookForExecFileinSpace(es) {
return nil
}
return fmt.Errorf("无法配置Java环境")
Expand Down
37 changes: 25 additions & 12 deletions environment/mcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"

down "github.com/EricTianC/GraiaOK/download"
)
Expand All @@ -16,11 +18,14 @@ const (
MCL_ZIP = "mcl.zip"
)

var mclse = &SimEnv{
Name: "mcl",
BasePath: "mcl",
ExecName: "mcl.jar",
}
var (
mclse = &SimEnv{
Name: "mcl",
BasePath: "mcl",
ExecName: "mcl.jar",
}
once sync.Once
)

func (es *EnvSpace) CheckMcl() error {
if _, err := os.Stat(filepath.Join(es.BasePath, mclse.BasePath, mclse.ExecName)); err == nil {
Expand Down Expand Up @@ -72,6 +77,7 @@ func firstRunMcl(es *EnvSpace) error {
if strings.Contains(text, "mirai-console started successfully.") {
finished <- struct{}{}
}
fmt.Println(text)
}
}()
go func() {
Expand All @@ -89,18 +95,25 @@ func firstRunMcl(es *EnvSpace) error {
return nil
}

//Stdin已强制设定
func (es *EnvSpace) MclCommand(args []string) *exec.Cmd {
if args == nil {
args = []string{"./mcl"}
args = []string{"-jar", mclse.ExecName}
} else {
args = append([]string{"-jar", mclse.ExecName}, args...)
}
cmd := exec.Command("java", args...)
cmd.Env = append(cmd.Env, es.Envs()...)
cmd.Stdin = os.Stdin
once.Do(func() {
var sep string //分隔符
switch runtime.GOOS {
case "windows":
sep = ";"
default:
sep = ":"
}
os.Setenv("PATH", os.Getenv("PATH")+strings.Join(es.Envs(), sep))
})
cmd.Dir = filepath.Join(es.BasePath, mclse.BasePath)
return cmd
}

//临时解决方法
func MakeMclBat() {

}
49 changes: 45 additions & 4 deletions environment/python.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package environment

import (
"fmt"
"log"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strconv"
"sync"

down "github.com/EricTianC/GraiaOK/download"
)

const (
PY_MIRROR = "https://npm.taobao.org/mirrors/python"
PY_VERSION = "3.9.2"
)

var pyse = &SimEnv{
Expand All @@ -14,11 +27,17 @@ var pyse = &SimEnv{

func (es *EnvSpace) CheckPython(pys chan<- bool) {
if pyse.IsInstalled() {
ver, _ := getPyVersion()
verNum, _ := strconv.ParseFloat(ver[:3], 32)
if verNum < 3.8 {
pys <- false
return
}
pys <- true
return
}

if pyse.LookForExecFileInSpace(es) {
if pyse.LookForExecFileinSpace(es) {
pys <- true
return
}
Expand All @@ -29,21 +48,43 @@ func (es *EnvSpace) CheckPython(pys chan<- bool) {
func (es *EnvSpace) DownloadPy(gwg *sync.WaitGroup, javacomplete <-chan struct{}) {
defer gwg.Done()
if runtime.GOOS == "windows" {
downloadPyWindows()
downloadPyWindows(es)
<-javacomplete
} else if runtime.GOOS == "linux" {
<-javacomplete
downloadPyLinux()
} else {
<-javacomplete
log.Print("您的系统暂不支持,请手动配置Python3.8以上版本")
log.Print("您的系统暂不支持,请手动配置Python3.8以上版本(含3.8)")
}
}

func downloadPyLinux() {
log.Println()
}

func downloadPyWindows() {
func downloadPyWindows(es *EnvSpace) {
var downUrl string
if runtime.GOARCH == "amd64" {
downUrl = fmt.Sprintf("%s/%s/python-%[2]s-amd64.exe", PY_MIRROR, PY_VERSION)
} else {
downUrl = fmt.Sprintf("%s/%s/python-%[2]s.exe", PY_MIRROR, PY_VERSION)
}
name := path.Base(downUrl)
down.DownloadFile(name, downUrl, "下载Py安装包")
targetDir, _ := filepath.Abs(filepath.Join(es.BasePath, pyse.BasePath))
cmd := exec.Command("./"+name, "/passive", "TargetDir="+targetDir, "PrependPath=1")
cmd.Run()
pyse.LookForExecFileinSpace(es)

}

func getPyVersion() (string, error) {
cmd := exec.Command(pyse.ExecName, "--version")
res, err := cmd.Output()
if err != nil {
return "", err
}
re, _ := regexp.Compile("\\d{1,2}\\.\\d{1,2}\\.\\d{1,2}")
return string(re.Find(res)), nil
}
17 changes: 12 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ func main() {
//检查环境
globalES := env.NewEnvSpace()
globalES.CheckEnv()
mcl := globalES.MclCommand(nil)
mcl.Stdin = os.Stdin
mcl.Stdout = os.Stdout
mcl.Stderr = os.Stderr
mcl.Run()

go func() {
mcl := globalES.MclCommand(nil)
mcl.Stdin = os.Stdin
mcl.Stdout = os.Stdout
mcl.Stderr = os.Stderr
mcl.Run()
}()

// go func() {
//
// }
}

0 comments on commit 674995d

Please sign in to comment.