Skip to content

Commit

Permalink
Merge pull request #169 from unarxiv/feat/init
Browse files Browse the repository at this point in the history
#48 Feat/init
  • Loading branch information
Xiaozhe Yao committed Dec 19, 2018
2 parents bb5615e + 0c2b09f commit b0d9f54
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 45 deletions.
4 changes: 3 additions & 1 deletion cli/config.go
Expand Up @@ -115,7 +115,6 @@ func createFileIfNotExist(filePath string) {
func validateConfig() {
if !isRoot() {
homepath := getHomeDir()
log.Println(homepath)
// Validate CVPM Path
cvpmPath := filepath.Join(homepath, "cvpm")
createFolderIfNotExist(cvpmPath)
Expand All @@ -125,6 +124,9 @@ func validateConfig() {
// create logs folder
logsFolder := filepath.Join(cvpmPath, "logs")
createFolderIfNotExist(logsFolder)
// create webui folder
webuiFolder := filepath.Join(cvpmPath, "webui")
createFolderIfNotExist(webuiFolder)
// check if system log file exists
cvpmLogPath := filepath.Join(cvpmPath, "logs", "system.log")
createFileIfNotExist(cvpmLogPath)
Expand Down
4 changes: 4 additions & 0 deletions cli/daemon.go
Expand Up @@ -5,6 +5,7 @@ package main

import (
"github.com/fatih/color"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/googollee/go-socket.io"
"github.com/hpcloud/tail"
Expand Down Expand Up @@ -154,6 +155,8 @@ func BeforeResponse() gin.HandlerFunc {
/repos -> Get to fetch Running Repos
*/
func runServer(port string) {
config := readConfig()
webuiFolder := filepath.Join(config.Local.LocalFolder, "webui")
color.Red("Initiating")
var err error
socketServer, err = socketio.NewServer(nil)
Expand All @@ -163,6 +166,7 @@ func runServer(port string) {
r := gin.Default()
r.Use(BeforeResponse())
watchLogs(socketServer)
r.Use(static.Serve("/", static.LocalFile(webuiFolder, false)))
// Status Related Handlers
r.GET("/status", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
Expand Down
70 changes: 56 additions & 14 deletions cli/handler.go
Expand Up @@ -8,9 +8,11 @@ package main

import (
"bufio"
"errors"
"fmt"
"github.com/fatih/color"
"github.com/getsentry/raven-go"
"github.com/manifoldco/promptui"
"github.com/mitchellh/go-homedir"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
Expand Down Expand Up @@ -52,6 +54,8 @@ func InstallHandler(c *cli.Context) {
color.Cyan("Installing... Please wait patiently")
pip([]string{"install", "cvpm", "--user"})
return
} else if remoteURL == "webui" {
InstallWebUi()
} else {
color.Cyan("Installing to " + localFolder)
}
Expand Down Expand Up @@ -87,6 +91,7 @@ func DaemonHandler(c *cli.Context) {
}
}

// Handle Repo Related Command
func RepoHandler(c *cli.Context) {
taskParams := c.Args().Get(0)
switch taskParams {
Expand All @@ -108,11 +113,44 @@ func RepoHandler(c *cli.Context) {
case "ps":
requestParams := map[string]string{}
ClientGet("repos", requestParams)
case "init":
InitHandler(c)
default:
color.Red("Command Not Supported!")
}
}

// Handle Config Related Command

// validate if python/pip/others exists or does not change

func validateIfProgramAllowed(rawInput string) error {
input := strings.TrimSpace(rawInput)
if input == "y" || input == "Y" || input == "Yes" || input == "" {
return nil
} else {
if _, err := os.Stat(input); os.IsNotExist(err) {
return errors.New(input + " not exists")
} else {
return nil
}
}
}

// trigger and parse input filepath
func InputAndParseConfigContent(label string, validate promptui.ValidateFunc) string {
prompt := promptui.Prompt{
Label: label,
Validate: validate,
}
result, err := prompt.Run()
if err != nil {
fmt.Printf("%v\n", err)
return ""
}
return result
}

func ConfigHandler(c *cli.Context) {
homepath, _ := homedir.Dir()
configFilePath := filepath.Join(homepath, "cvpm", "config.toml")
Expand All @@ -131,33 +169,37 @@ func ConfigHandler(c *cli.Context) {
var nextConfig cvpmConfig
nextConfig.Local.LocalFolder = prevConfig.Local.LocalFolder
// Handle Python Location
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Python Location[" + prevConfig.Local.Python + "]")
newPyLocation, _ := reader.ReadString('\n')
fmt.Println("Original Python Location: " + prevConfig.Local.Python)
newPyLocation := InputAndParseConfigContent("Python(3)", validateIfProgramAllowed)
newPyLocation = strings.TrimSpace(newPyLocation)
if newPyLocation == "y" || newPyLocation == "Y" || newPyLocation == "Yes" || newPyLocation == "" {
newPyLocation = prevConfig.Local.Python
} else {
if _, err := os.Stat(newPyLocation); os.IsNotExist(err) {
log.Fatal("Python executable file not found: No such file")
}
}
nextConfig.Local.Python = newPyLocation
// Handle Pypi Location
fmt.Printf("Pip Location[" + prevConfig.Local.Pip + "]")
newPipLocation, _ := reader.ReadString('\n')
fmt.Println("Original Pip Location: " + prevConfig.Local.Pip)
newPipLocation := InputAndParseConfigContent("Pip(3)", validateIfProgramAllowed)
newPipLocation = strings.TrimSpace(newPipLocation)
if newPipLocation == "y" || newPipLocation == "Y" || newPipLocation == "Yes" || newPipLocation == "" {
newPipLocation = prevConfig.Local.Pip
} else {
if _, err := os.Stat(newPipLocation); os.IsNotExist(err) {
log.Fatal("Pip executable file not found: No such file")
}
}
nextConfig.Local.Pip = newPipLocation
writeConfig(nextConfig)
}

func InitHandler(c *cli.Context) {

prompt := promptui.Prompt{
Label: "Your Package Name",
}
result, err := prompt.Run()
if err != nil {
panic(err)
}
InitNewRepo(result)
// rename {package_name} to real package name
pyFolderName := filepath.Join(result, "{package_name}")
err = os.Rename(pyFolderName, filepath.Join(result, result))
if err != nil {
fmt.Println(err)
}
}
52 changes: 24 additions & 28 deletions cli/repository.go
Expand Up @@ -100,22 +100,16 @@ func runRepo(Vendor string, Name string, Solver string, Port string) {
}
}

func CloneFromGit(remoteURL string, targetFolder string) Repository {
localFolderName := strings.Split(remoteURL, "/")
vendorName := localFolderName[len(localFolderName)-2]
repoName := localFolderName[len(localFolderName)-1]
localFolder := filepath.Join(targetFolder, vendorName, repoName)
color.Cyan("Cloning " + remoteURL + " into " + localFolder)
_, err := git.PlainClone(localFolder, false, &git.CloneOptions{
func CloneFromGit(remoteURL string, targetFolder string) {
color.Cyan("Cloning " + remoteURL + " into " + targetFolder)
_, err := git.PlainClone(targetFolder, false, &git.CloneOptions{
URL: remoteURL,
Progress: os.Stdout,
})
if err != nil {
raven.CaptureErrorAndWait(err, nil)
fmt.Println(err)
}
repo := Repository{Name: repoName, Vendor: vendorName, LocalFolder: localFolder}
return repo
}

func InstallDependencies(localFolder string) {
Expand Down Expand Up @@ -149,30 +143,19 @@ func PostInstallation(repoFolder string) {
}

// Return Repository Meta Info: Dependency, Config, Disk Size and Readme

func GetMetaInfo(Vendor string, Name string) RepositoryMetaInfo {
repos := readRepos()
repositoryMeta := RepositoryMetaInfo{}
for _, existed_repo := range repos {
if existed_repo.Name == Name && existed_repo.Vendor == Vendor {
// Read config file etc
byte_config, err := ioutil.ReadFile(filepath.Join(existed_repo.LocalFolder, "cvpm.toml"))
if err != nil {
repositoryMeta.Config = "Read cvpm.toml failed"
} else {
repositoryMeta.Config = string(byte_config)
}
byte_dependency, err := ioutil.ReadFile(filepath.Join(existed_repo.LocalFolder, "requirements.txt"))
if err != nil {
repositoryMeta.Dependency = "Read requirements.txt failed"
} else {
repositoryMeta.Dependency = string(byte_dependency)
}
byte_readme, err := ioutil.ReadFile(filepath.Join(existed_repo.LocalFolder, "README.md"))
if err != nil {
repositoryMeta.Readme = "Read Readme.md failed"
} else {
repositoryMeta.Readme = string(byte_readme)
}
readmeFilePath := filepath.Join(existed_repo.LocalFolder, "README.md")
cvpmConfigFilePath := filepath.Join(existed_repo.LocalFolder, "cvpm.toml")
requirementsFilePath := filepath.Join(existed_repo.LocalFolder, "requirements.txt")
repositoryMeta.Config = readFileContent(cvpmConfigFilePath)
repositoryMeta.Dependency = readFileContent(requirementsFilePath)
repositoryMeta.Readme = readFileContent(readmeFilePath)
packageSize := getDirSizeMB(existed_repo.LocalFolder)
repositoryMeta.DiskSize = packageSize
}
Expand All @@ -184,11 +167,24 @@ func GetMetaInfo(Vendor string, Name string) RepositoryMetaInfo {
func InstallFromGit(remoteURL string) {
config := readConfig()
var repo Repository
repo = CloneFromGit(remoteURL, config.Local.LocalFolder)
// prepare local folder
localFolderName := strings.Split(remoteURL, "/")
vendorName := localFolderName[len(localFolderName)-2]
repoName := localFolderName[len(localFolderName)-1]
localFolder := filepath.Join(config.Local.LocalFolder, vendorName, repoName)
CloneFromGit(remoteURL, localFolder)
repo = Repository{Name: repoName, Vendor: vendorName, LocalFolder: localFolder}

repoFolder := repo.LocalFolder
InstallDependencies(repoFolder)
GeneratingRunners(repoFolder)
config.Repositories = addRepo(config.Repositories, repo)
writeConfig(config)
PostInstallation(repoFolder)
}

// Init a new repoo by using bolierplate
func InitNewRepo(repoName string) {
bolierplateURL := "https://github.com/cvmodel/bolierplate.git"
CloneFromGit(bolierplateURL, repoName)
}
12 changes: 12 additions & 0 deletions cli/utils.go
@@ -1,6 +1,7 @@
package main

import (
"io/ioutil"
"net"
"os"
"os/user"
Expand Down Expand Up @@ -76,3 +77,14 @@ func findNextOpenPort(port int) string {
}
return strPort
}

func readFileContent(filename string) string {
var content string
byte_content, err := ioutil.ReadFile(filename)
if err != nil {
content = "Read " + filename + "Failed!"
} else {
content = string(byte_content)
}
return content
}
6 changes: 6 additions & 0 deletions cli/webui.go
@@ -0,0 +1,6 @@
package main

// Install Web UI -> Download Latest and Unzip
func InstallWebUi() {

}
2 changes: 1 addition & 1 deletion dashboard/config/index.js
Expand Up @@ -13,7 +13,7 @@ module.exports = {
proxyTable: {},

// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
host: '0.0.0.0', // can be overwritten by process.env.HOST
port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/services/config.js
Expand Up @@ -30,7 +30,7 @@ class ConfigService {
const configService = new ConfigService()

const discoveryConfig = {
'endpoint': 'http://127.0.0.1:3000'
'endpoint': 'http://192.168.1.4:3000'
}

export {
Expand Down
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Expand Up @@ -126,6 +126,7 @@ function genSidebarConfig(title) {
'getting-started',
'install-package',
'write-package',
'developer',
'discovery',
'contributing',
'credits',
Expand Down
1 change: 1 addition & 0 deletions docs/en-US/guide/developer.md
@@ -0,0 +1 @@
# Developer Guide
1 change: 1 addition & 0 deletions docs/zh-CN/guide/developer.md
@@ -0,0 +1 @@
# 开发指南

0 comments on commit b0d9f54

Please sign in to comment.