2021-10-01 00:21:50 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-01 21:51:21 +08:00
|
|
|
"bufio"
|
2021-10-01 00:21:50 +08:00
|
|
|
"fmt"
|
2022-04-11 13:43:02 +08:00
|
|
|
"io"
|
2021-10-01 00:21:50 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-10-01 22:04:29 +08:00
|
|
|
"strings"
|
2021-10-01 00:21:50 +08:00
|
|
|
"time"
|
|
|
|
|
2021-10-01 18:28:57 +08:00
|
|
|
"github.com/BurntSushi/toml"
|
2021-10-01 00:21:50 +08:00
|
|
|
"github.com/go-git/go-git/v5"
|
2021-10-02 09:13:45 +08:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2022-04-11 13:43:02 +08:00
|
|
|
cp "github.com/otiai10/copy"
|
2021-10-01 00:21:50 +08:00
|
|
|
)
|
|
|
|
|
2021-10-01 22:23:20 +08:00
|
|
|
var isShowSize = false
|
|
|
|
|
|
|
|
type Requerment_t struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
2021-10-02 08:49:35 +08:00
|
|
|
Commit string
|
2021-10-01 22:23:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Package_t struct {
|
|
|
|
Name string
|
|
|
|
Releases []string
|
|
|
|
ReleasesName []string
|
|
|
|
ReleasesCommit []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config_t struct {
|
|
|
|
Packages []Package_t
|
|
|
|
}
|
|
|
|
|
2021-10-01 23:03:00 +08:00
|
|
|
func main() {
|
|
|
|
superPath := "/tmp"
|
|
|
|
path := "/pikascript"
|
|
|
|
|
|
|
|
go readPathSize(superPath + path)
|
2021-10-02 08:54:41 +08:00
|
|
|
repo := updatePikascript(superPath + path)
|
2021-10-01 23:03:00 +08:00
|
|
|
|
|
|
|
packages, res := getPackages(superPath + path)
|
|
|
|
if !res {
|
|
|
|
fmt.Printf("[error]: get package info faild.\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
|
|
|
|
|
|
|
requerments, res := getRequestment("requestment.txt")
|
|
|
|
if !res {
|
|
|
|
fmt.Printf("[error]: get requerment info faild.\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
|
|
|
|
2021-10-02 08:49:35 +08:00
|
|
|
requerments, res = matchRequestments(packages, requerments)
|
2021-10-01 23:03:00 +08:00
|
|
|
if !res {
|
|
|
|
fmt.Printf("[error]: match requestment faild.\n")
|
|
|
|
return
|
|
|
|
}
|
2021-10-01 23:05:06 +08:00
|
|
|
fmt.Printf("\n")
|
2021-10-01 23:03:00 +08:00
|
|
|
|
2021-10-02 09:42:29 +08:00
|
|
|
checkOutRequsetments(superPath+path, repo, requerments)
|
2021-10-02 08:49:35 +08:00
|
|
|
|
2021-10-01 23:03:00 +08:00
|
|
|
fmt.Println("update OK !")
|
|
|
|
for i := 3; i >= 0; i-- {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
fmt.Println("this window will auto close after", i, "s...")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 13:43:02 +08:00
|
|
|
func FilterDirsGlob(dir, suffix string) ([]string, error) {
|
|
|
|
return filepath.Glob(filepath.Join(dir, suffix))
|
|
|
|
}
|
|
|
|
|
|
|
|
func MoveFile(sourcePath, destPath string) error {
|
|
|
|
inputFile, err := os.Open(sourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Couldn't open source file: %s", err)
|
|
|
|
}
|
|
|
|
outputFile, err := os.Create(destPath)
|
|
|
|
if err != nil {
|
|
|
|
inputFile.Close()
|
|
|
|
return fmt.Errorf("Couldn't open dest file: %s", err)
|
|
|
|
}
|
|
|
|
defer outputFile.Close()
|
|
|
|
_, err = io.Copy(outputFile, inputFile)
|
|
|
|
inputFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Writing to output file failed: %s", err)
|
|
|
|
}
|
|
|
|
// The copy was successful, so now delete the original file
|
|
|
|
err = os.Remove(sourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed removing original file: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-02 09:42:29 +08:00
|
|
|
func checkOutRequsetments(path string, repo *git.Repository, requerments []Requerment_t) {
|
2022-04-11 13:43:02 +08:00
|
|
|
os.Mkdir("pikascript-lib", os.ModePerm)
|
|
|
|
os.Mkdir("pikascript-core", os.ModePerm)
|
|
|
|
os.Mkdir("pikascript-api", os.ModePerm)
|
2021-10-12 17:25:38 +08:00
|
|
|
workTree, _ := repo.Worktree()
|
2021-10-02 08:49:35 +08:00
|
|
|
for _, requerment := range requerments {
|
2021-10-02 09:52:15 +08:00
|
|
|
/* checkout commit */
|
2021-10-02 08:54:41 +08:00
|
|
|
fmt.Printf("checking out: %s\n", requerment.Commit)
|
2021-10-02 09:13:45 +08:00
|
|
|
err := workTree.Checkout(&git.CheckoutOptions{
|
2021-10-12 17:25:38 +08:00
|
|
|
Hash: plumbing.NewHash(requerment.Commit),
|
|
|
|
Force: true,
|
2021-10-02 09:13:45 +08:00
|
|
|
})
|
2021-10-02 08:54:41 +08:00
|
|
|
CheckIfError(err)
|
2021-10-02 09:52:15 +08:00
|
|
|
/* update file */
|
2021-10-02 09:42:29 +08:00
|
|
|
var packagePath string = path + "/package/" + requerment.Name
|
2022-04-11 13:43:02 +08:00
|
|
|
var dirPath string = "pikascript-lib/" + requerment.Name
|
2021-10-02 09:52:15 +08:00
|
|
|
if requerment.Name == "pikascript-core" {
|
2021-10-04 01:50:52 +08:00
|
|
|
packagePath = path + "/src"
|
2022-04-11 13:43:02 +08:00
|
|
|
dirPath = "pikascript-core"
|
2022-05-05 10:23:56 +08:00
|
|
|
CheckIfError(cp.Copy(packagePath+"/../tools/pikaCompiler/rust-msc-latest-win10.exe", "./rust-msc-latest-win10.exe"))
|
2021-10-02 10:18:27 +08:00
|
|
|
CheckIfError(err)
|
2021-10-02 09:52:15 +08:00
|
|
|
}
|
2022-04-11 13:43:02 +08:00
|
|
|
// fmt.Printf(" copy" + " " + packagePath + " " + dirPath + "\n")
|
|
|
|
CheckIfError(cp.Copy(packagePath, dirPath))
|
2022-05-15 13:40:48 +08:00
|
|
|
pyFileList, _ := FilterDirsGlob(dirPath, "*.py")
|
2022-04-11 13:43:02 +08:00
|
|
|
for i := range pyFileList {
|
2022-05-05 10:23:56 +08:00
|
|
|
pyFileSource := strings.ReplaceAll(pyFileList[i], "\\", "/")
|
2022-04-11 13:43:02 +08:00
|
|
|
pyFilePath := strings.Split(pyFileSource, "/")
|
|
|
|
pyFileName := pyFilePath[len(pyFilePath)-1]
|
|
|
|
fmt.Println(" Installed: " + pyFileName)
|
2022-05-05 10:23:56 +08:00
|
|
|
CheckIfError(os.Rename(pyFileSource, pyFileName))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-15 13:40:48 +08:00
|
|
|
pyiFileList, _ := FilterDirsGlob(dirPath, "*.pyi")
|
2022-05-05 10:23:56 +08:00
|
|
|
for i := range pyiFileList {
|
|
|
|
pyFileSource := strings.ReplaceAll(pyiFileList[i], "\\", "/")
|
|
|
|
pyFilePath := strings.Split(pyFileSource, "/")
|
|
|
|
pyFileName := pyFilePath[len(pyFilePath)-1]
|
|
|
|
fmt.Println(" Installed: " + pyFileName)
|
|
|
|
CheckIfError(os.Rename(pyFileSource, pyFileName))
|
2022-04-11 13:43:02 +08:00
|
|
|
}
|
2021-10-02 09:42:29 +08:00
|
|
|
|
2021-10-02 08:49:35 +08:00
|
|
|
}
|
2021-10-12 17:25:38 +08:00
|
|
|
err := workTree.Checkout(&git.CheckoutOptions{
|
|
|
|
Hash: plumbing.NewHash("master"),
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
CheckIfError(err)
|
2021-10-02 08:49:35 +08:00
|
|
|
}
|
2021-10-02 08:54:41 +08:00
|
|
|
func CheckIfError(err error) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-10-02 08:49:35 +08:00
|
|
|
|
2021-10-02 08:54:41 +08:00
|
|
|
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
|
|
|
|
}
|
2021-10-02 08:49:35 +08:00
|
|
|
func getMatchedPackage(requerment Requerment_t, packages []Package_t) (Package_t, bool) {
|
|
|
|
var pkg Package_t
|
|
|
|
for _, pkg = range packages {
|
|
|
|
if requerment.Name == pkg.Name {
|
|
|
|
return pkg, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pkg, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMatchedCommit(requestVersion string, pkg Package_t) (string, bool) {
|
|
|
|
for i, packageVersion := range pkg.ReleasesName {
|
|
|
|
if packageVersion == requestVersion {
|
|
|
|
return pkg.ReleasesCommit[i], true
|
|
|
|
}
|
|
|
|
if "latest" == requestVersion {
|
|
|
|
return "master", true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchRequestments(packages []Package_t, requerments []Requerment_t) ([]Requerment_t, bool) {
|
|
|
|
for i, requerment := range requerments {
|
|
|
|
pkg, res := getMatchedPackage(requerment, packages)
|
|
|
|
if !res {
|
|
|
|
fmt.Printf("[error]: match package for %s faild.\n", requerment.Name)
|
|
|
|
return requerments, false
|
|
|
|
}
|
|
|
|
commit, res := getMatchedCommit(requerment.Version, pkg)
|
|
|
|
if !res {
|
|
|
|
fmt.Printf("[error]: match commit for %s faild.\n", requerment.Name)
|
|
|
|
return requerments, false
|
|
|
|
}
|
|
|
|
fmt.Printf("matched: %s %s\n", pkg.Name, commit)
|
|
|
|
requerments[i].Commit = commit
|
|
|
|
}
|
|
|
|
return requerments, true
|
|
|
|
}
|
|
|
|
|
2021-10-01 22:04:29 +08:00
|
|
|
func getRequestment(path string) ([]Requerment_t, bool) {
|
|
|
|
var requestments []Requerment_t
|
2021-10-01 21:51:21 +08:00
|
|
|
requestment_file, _ := os.Open(path)
|
|
|
|
defer requestment_file.Close()
|
|
|
|
scanner := bufio.NewScanner(requestment_file)
|
|
|
|
var count int
|
|
|
|
for scanner.Scan() {
|
2021-10-01 22:04:29 +08:00
|
|
|
var requerment Requerment_t
|
2021-10-01 21:51:21 +08:00
|
|
|
count++
|
|
|
|
line := scanner.Text()
|
2021-10-01 22:32:07 +08:00
|
|
|
line = strings.ReplaceAll(line, " ", "")
|
2021-10-01 22:04:29 +08:00
|
|
|
requerment.Name = strings.Split(line, "==")[0]
|
|
|
|
requerment.Version = strings.Split(line, "==")[1]
|
|
|
|
fmt.Printf("request: %s %s\n", requerment.Name, requerment.Version)
|
|
|
|
requestments = append(requestments, requerment)
|
2021-10-01 21:51:21 +08:00
|
|
|
}
|
2021-10-01 22:04:29 +08:00
|
|
|
return requestments, true
|
2021-10-01 21:51:21 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 22:23:20 +08:00
|
|
|
func getPackages(path string) ([]Package_t, bool) {
|
2021-10-01 19:29:38 +08:00
|
|
|
var config Config_t
|
2021-10-04 17:26:02 +08:00
|
|
|
if _, err := toml.DecodeFile(path+"/packages.toml", &config); err != nil {
|
2021-10-01 18:28:57 +08:00
|
|
|
fmt.Println(err)
|
2021-10-01 22:23:20 +08:00
|
|
|
return config.Packages, false
|
2021-10-01 18:28:57 +08:00
|
|
|
}
|
2021-10-01 23:03:00 +08:00
|
|
|
for i_package, pkg := range config.Packages {
|
2021-10-01 22:23:20 +08:00
|
|
|
i := 0
|
2021-10-01 19:43:40 +08:00
|
|
|
for _, release := range pkg.Releases {
|
2021-10-01 22:23:20 +08:00
|
|
|
pkg.ReleasesName = append(pkg.ReleasesName, strings.Split(release, " ")[0])
|
|
|
|
pkg.ReleasesCommit = append(pkg.ReleasesCommit, strings.Split(release, " ")[1])
|
|
|
|
i++
|
2021-10-01 19:43:40 +08:00
|
|
|
}
|
2021-10-01 23:03:00 +08:00
|
|
|
config.Packages[i_package] = pkg
|
2021-10-01 18:28:57 +08:00
|
|
|
}
|
2021-10-01 22:23:20 +08:00
|
|
|
return config.Packages, true
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func readPathSize(path string) {
|
|
|
|
for {
|
|
|
|
if !isShowSize {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
size, _ := pathSize(path)
|
|
|
|
fmt.Printf("recived : %2f MB \n", float64(size)/1024/1024)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 08:49:35 +08:00
|
|
|
func updatePikascript(path string) *git.Repository {
|
2021-10-01 00:21:50 +08:00
|
|
|
pathExist, err := PathExists(path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("PathExists(%s),err(%v)\n", path, err)
|
|
|
|
}
|
|
|
|
if !pathExist {
|
|
|
|
/* clone the pikascript repo */
|
|
|
|
fmt.Printf("downloading pikascript to %s...\n", path)
|
|
|
|
fmt.Printf("need about 10 min(s)\n")
|
|
|
|
fmt.Printf("please wait...\n")
|
|
|
|
isShowSize = true
|
|
|
|
_, err = git.PlainClone(path, false, &git.CloneOptions{
|
|
|
|
URL: "https://gitee.com/lyon1998/pikascript",
|
|
|
|
Progress: os.Stdout,
|
|
|
|
})
|
|
|
|
if nil != err {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pull the pikascript repo */
|
2021-10-02 08:49:35 +08:00
|
|
|
repo, _ := git.PlainOpen(path)
|
|
|
|
w, _ := repo.Worktree()
|
2021-10-01 00:21:50 +08:00
|
|
|
fmt.Println("updating pikascript...")
|
|
|
|
w.Pull(&git.PullOptions{RemoteName: "origin"})
|
2021-10-02 08:49:35 +08:00
|
|
|
ref, _ := repo.Head()
|
|
|
|
commit, _ := repo.CommitObject(ref.Hash())
|
2021-10-01 00:21:50 +08:00
|
|
|
fmt.Println(commit)
|
|
|
|
|
|
|
|
isShowSize = false
|
2021-10-01 21:51:21 +08:00
|
|
|
|
2021-10-02 09:52:15 +08:00
|
|
|
err = w.Checkout(&git.CheckoutOptions{
|
2021-10-12 17:25:38 +08:00
|
|
|
Hash: plumbing.NewHash("master"),
|
|
|
|
Force: true,
|
2021-10-02 09:52:15 +08:00
|
|
|
})
|
|
|
|
CheckIfError(err)
|
|
|
|
|
2021-10-02 08:49:35 +08:00
|
|
|
return repo
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 17:30:04 +08:00
|
|
|
func PathExists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
|
2021-10-01 17:30:04 +08:00
|
|
|
func pathSize(path string) (int64, error) {
|
|
|
|
var size int64
|
|
|
|
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
size += info.Size()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return size, err
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|