Skip to content
This repository has been archived by the owner on Dec 14, 2024. It is now read-only.

More configurable path #70

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
9 changes: 7 additions & 2 deletions client/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"strings"
"sync"
"os"
"time"

"github.com/fatih/color"
Expand Down Expand Up @@ -110,6 +111,10 @@ func (c *Client) Clone(handle, rootPath string, ac bool) (err error) {
}
}()
}

os.MkdirAll(handle, os.ModePerm)
os.Chdir(handle)

for _, _submission := range submissions {
func() {
defer func() {
Expand Down Expand Up @@ -142,7 +147,7 @@ func (c *Client) Clone(handle, rootPath string, ac bool) (err error) {
mu.Unlock()
return
}
ext, ok := LangsExt[lang]
ext, ok := util.LangsExt[lang]
if !ok {
mu.Lock()
count++
Expand All @@ -155,7 +160,7 @@ func (c *Client) Clone(handle, rootPath string, ac bool) (err error) {
testCount := int64(submission["passedTestCount"].(float64))
filename = fmt.Sprintf("%v_%v_%v", submissionID, strings.ToLower(verdict), testCount)
}
info.RootPath = filepath.Join(rootPath, handle, info.ProblemType)

URL, _ := info.SubmissionURL(c.host)
data := cloneData{URL, filepath.Join(info.Path(), filename), "." + ext}
ch <- data
Expand Down
53 changes: 43 additions & 10 deletions client/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package client
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/xalanq/cf-tool/config"
)

// ProblemTypes problem types
Expand All @@ -22,7 +25,6 @@ type Info struct {
GroupID string `json:"group_id"`
ProblemID string `json:"problem_id"`
SubmissionID string `json:"submission_id"`
RootPath string
}

// ErrorNeedProblemID error
Expand Down Expand Up @@ -75,17 +77,48 @@ func (info *Info) Hint() string {
return text
}

// Path path
func (info *Info) Path() string {
path := info.RootPath
if info.GroupID != "" {
path = filepath.Join(path, info.GroupID)
// PathMayError get directory for problem, check for configuration error
func (info *Info) PathMayError() (path string, err error) {
// this function must recompute the result every time it's called,
// because `Parse` is implemented by modifying the info struct and recompute the path
cfg := config.Instance

currentDirectory, err := os.Getwd()
if err != nil { return }

path = ""
if info.ProblemID == "" {
panic("Internal error: cannot get problem path from incomplete info")
}
if info.ProblemType != "acmsguru" && info.ContestID != "" {
path = filepath.Join(path, info.ContestID)
for _, value := range cfg.PathSpecifier {
if value.Type == info.ProblemType {
expectedPath := strings.NewReplacer(
"%%", "%",
"%contestID%", info.ContestID,
"%problemID%", info.ProblemID,
"%groupID%", info.GroupID,
).Replace(value.Pattern)
components := strings.Split(expectedPath, "/")
for length := len(components); length >= 0; length-- {
if strings.HasSuffix(currentDirectory, filepath.Join(components[:length]...)) {
path = filepath.Join(append([]string {currentDirectory}, components[length:]...)...)
break
}
}
break
}
}
if info.ProblemID != "" {
path = filepath.Join(path, strings.ToLower(info.ProblemID))
if path == "" {
return "", errors.New("Invalid configuration! Need to specify path specifier for " + info.ProblemType)
}
return
}

// Path get directory for problem, panic if the configuration is incorrect
func (info *Info) Path() string {
path, err := info.PathMayError()
if err != nil {
panic(err)
}
return path
}
Expand Down
103 changes: 0 additions & 103 deletions client/langs.go

This file was deleted.

5 changes: 3 additions & 2 deletions client/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (c *Client) Parse(info Info) (problems []string, paths []string, err error)
if err != nil {
return
}
info.ProblemID = ""
info.ProblemID = "<problem>"
if problemID == "" {
statics, err := c.Statis(info)
if err != nil {
Expand All @@ -119,7 +119,8 @@ func (c *Client) Parse(info Info) (problems []string, paths []string, err error)
mu := sync.Mutex{}
paths = make([]string, len(problems))
for i, problemID := range problems {
paths[i] = filepath.Join(contestPath, strings.ToLower(problemID))
info.ProblemID = strings.ToLower(problemID)
paths[i] = info.Path()
go func(problemID, path string) {
defer wg.Done()
mu.Lock()
Expand Down
2 changes: 1 addition & 1 deletion client/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *Client) Pull(info Info, rootPath string, ac bool) (err error) {
if ac && !(strings.Contains(submission.status, "Accepted") || strings.Contains(submission.status, "Pretests passed")) {
continue
}
ext, ok := LangsExt[submission.lang]
ext, ok := util.LangsExt[submission.lang]
if !ok {
continue
}
Expand Down
Loading