Skip to content

Commit

Permalink
scripts: fix log msg
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Mar 29, 2023
1 parent 6e92a76 commit 571b2a2
Showing 1 changed file with 80 additions and 176 deletions.
256 changes: 80 additions & 176 deletions scripts/translations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type textLabel string
// locales is a map, where key is text label and value is translation.
type locales map[textLabel]string

// localePath is a cleaned path to the locale.
type localePath string

func main() {
if len(os.Args) == 1 {
usage("need a command")
Expand Down Expand Up @@ -79,27 +82,21 @@ func main() {
}

func command(uri *url.URL, projectID string, conf twoskyConf) (err error) {
var flagSet *flag.FlagSet
var numWorker int
var verbose bool

dlFlagSet := flag.NewFlagSet("download", flag.ExitOnError)
dlFlagSet.Usage = func() {
usage("download command error")
}
dlFlagSet.IntVar(&numWorker, "n", 1, "number of concurrent downloads")

aaFlagSet := flag.NewFlagSet("auto-add", flag.ExitOnError)
aaFlagSet.Usage = func() {
usage("autod-add command error")
}
aaFlagSet.IntVar(&numWorker, "n", 1, "number of concurrent downloads")
aaFlagSet.BoolVar(&verbose, "v", false, "enable verbose output")

switch os.Args[1] {
case "summary":
err = summary(conf.Languages)
case "download":
err = dlFlagSet.Parse(os.Args[2:])
flagSet = flag.NewFlagSet("download", flag.ExitOnError)
flagSet.Usage = func() {
usage("download command error")
}
flagSet.IntVar(&numWorker, "n", 1, "number of concurrent downloads")

err = flagSet.Parse(os.Args[2:])
if err != nil {
// Don't wrap the error since there is exit on error.
return err
Expand All @@ -115,7 +112,14 @@ func command(uri *url.URL, projectID string, conf twoskyConf) (err error) {
case "upload":
err = upload(uri, projectID, conf.BaseLangcode)
case "auto-add":
err = aaFlagSet.Parse(os.Args[2:])
flagSet = flag.NewFlagSet("auto-add", flag.ExitOnError)
flagSet.Usage = func() {
usage("auto-add command error")
}
flagSet.IntVar(&numWorker, "n", 1, "number of concurrent downloads")
flagSet.BoolVar(&verbose, "v", false, "enable verbose output")

err = flagSet.Parse(os.Args[2:])
if err != nil {
// Don't wrap the error since there is exit on error.
return err
Expand All @@ -142,17 +146,14 @@ func check(err error) {

// ask reads line from STDIN, returns true if line is 'y', otherwise false.
func ask(s string) (a bool) {
const q = "y/[n]:"

fmt.Printf("%s %s ", s, q)
fmt.Printf("%s y/[n]: ", s)

scanner := bufio.NewScanner(os.Stdin)

scanner.Scan()
line := scanner.Text()
line = strings.ToLower(line)

return line == "y"
return strings.ToLower(line) == "y"
}

// usage prints usage. If addStr is not empty print addStr and exit with code
Expand Down Expand Up @@ -531,80 +532,42 @@ func autoAdd(
baseLang langCode,
numWorker int,
) (err error) {
defer func() { err = errors.Annotate(err, "update: %w") }()

log.Debug("Downloading the locales.")
defer func() { err = errors.Annotate(err, "auto add: %w") }()

log.Debug("downloading the locales")
download(uri, projectID, langs, numWorker)

log.Debug("Checking if the English locale is up to date.")

ch, err := checkBaseLocale()
log.Debug("checking if the base locale is up to date")
adds, dels, err := getChangedLocales()
if err != nil {
// Don't wrap the error since it's informative enough as is and there
// is an annotation deferred already.
return err
}

switch ch {
case none:
log.Debug("There are no changes.")
case deletion:
log.Debug("There are deletions.")
basePath := localePath(filepath.Join(localesDir, defaultBaseFile))

return baseLocaleDeletions(uri, projectID, baseLang)
case addition:
log.Debug("There are additions.")
err = baseLocaleAdditions()
default:
panic("undefined change")
}

if err != nil {
// Don't wrap the error since it's informative enough as is and
// there is an annotation deferred already.
return err
}

return autoAddChanges(langs)
}
if slices.Contains(adds, basePath) {
log.Debug("base locale has additions")

// autoAddChanges adds translation changes to the git.
func autoAddChanges(langs languages) (err error) {
log.Debug("Checking if the release-blocker locales are fully translated")

err = checkReleaseBlockerLocales(langs)
if err != nil {
// Don't wrap the error since it's informative enough as is and there
// is an annotation deferred already.
return err
}

log.Debug("Adding non-deletion changes.")

err = addAdditions()
if err != nil {
// Don't wrap the error since it's informative enough as is and there
// is an annotation deferred already.
return err
}

log.Debug("Printing the list of files with deletion changes.")

printDeletions()

if !ask("Proceed to restore files with deletion changes?") {
return nil
}
// If there are additions, then someone probably unuploaded changes
// from unmerged branches. This isn't critical, so just add this
// change.
err = addBaseLocale()
if err != nil {
// Don't wrap the error since it's informative enough as is and
// there is an annotation deferred already.
return err
}
} else if slices.Contains(dels, basePath) {
log.Debug("base locale has deletions")

err = restoreLocales()
if err != nil {
// Don't wrap the error since it's informative enough as is and there
// is an annotation deferred already.
return err
return baseLocaleDeletions(uri, projectID, baseLang)
} else {
log.Debug("base locale has no changes")
}

return nil
return autoAddChanges(langs, adds)
}

// baseLocaleDeletions handles deletion changes to the base locale.
Expand All @@ -623,65 +586,35 @@ func baseLocaleDeletions(uri *url.URL, projectID string, baseLang langCode) (err
return err
}

log.Debug("Make sure that the new text has appeared on Crowdin.")

return nil
}

// baseLocaleAdditions handles addition changes to the base locale.
func baseLocaleAdditions() (err error) {
// If there are additions, then someone probably unuploaded changes
// from unmerged branches. This isn't critical, so just add this
// change.
log.Debug("Adding base locale.")

return addBaseLocale()
}

type change int

const (
wrong change = iota - 2
deletion
none
addition
)

// checkBaseLocale returns change or error. change is 0 if there are no
// changes, 1 if there are at elast one addition, and -1 if there are no
// additions.
func checkBaseLocale() (ch change, err error) {
path := filepath.Join(localesDir, defaultBaseFile)

cmd := exec.Command("git", "diff", "-U0", path)
stdout, err := cmd.StdoutPipe()
// autoAddChanges adds translation changes to the git.
func autoAddChanges(langs languages, adds []localePath) (err error) {
log.Debug("checking if the release-blocker locales are fully translated")
err = checkReleaseBlockerLocales(langs)
if err != nil {
return wrong, fmt.Errorf("getting command: %w", err)
// Don't wrap the error since it's informative enough as is and there
// is an annotation deferred already.
return err
}

err = cmd.Start()
log.Debug("adding non-deletion changes")
err = addAdditions(adds)
if err != nil {
return wrong, fmt.Errorf("starting command: %w", err)
}

scanner := bufio.NewScanner(stdout)

for scanner.Scan() {
line := scanner.Text()

if strings.HasPrefix(line, "+++") {
// continue
} else if strings.HasPrefix(line, "+") {
return addition, nil
}
// Don't wrap the error since it's informative enough as is and there
// is an annotation deferred already.
return err
}

err = cmd.Wait()
err = restoreLocales()
if err != nil {
return wrong, fmt.Errorf("waiting command: %w", err)
// Don't wrap the error since it's informative enough as is and there
// is an annotation deferred already.
return err
}

return deletion, nil
return nil
}

// checkReleaseBlockerLocales returns nil if translations of the release
Expand Down Expand Up @@ -717,6 +650,12 @@ func checkReleaseBlockerLocales(langs languages) (err error) {

// restoreLocales reverts changes to the locales.
func restoreLocales() (err error) {
if !ask("Proceed to restore locales with unstaged changes?") {
return nil
}

log.Debug("restoring locales with unstaged changes")

_, _, err = aghos.RunCommand("git", "restore", localesDir)

return errors.Annotate(err, "restoring locales: %w")
Expand Down Expand Up @@ -746,30 +685,23 @@ func addBaseLocale() (err error) {

// addAdditions adds locales, except base localse, with additional changes to
// the git.
func addAdditions() (err error) {
adds, _, err := getChangedLocales()
if err != nil {
return fmt.Errorf("adding changes: %w", err)
}

func addAdditions(adds []localePath) (err error) {
for _, v := range adds {
path := filepath.Join(localesDir, v)

err = gitAddPatch(path)
err = gitAddPatch(string(v))

if err != nil {
return fmt.Errorf("adding locale %q: %w", path, err)
return fmt.Errorf("adding locale %q: %w", v, err)
}
}

return nil
}

// getChangedLocales returns locales with changes, except base locale, or
// error. adds is the list of locales with at least single addition. dels is
// the list of locales with deletions.
func getChangedLocales() (adds, dels []string, err error) {
cmd := exec.Command("git", "diff", "-U0", localesDir)
// getChangedLocales returns cleaned paths of locales with changes or error.
// adds is the list of locales with at least single addition. dels is the list
// of locales with deletions.
func getChangedLocales() (adds, dels []localePath, err error) {
cmd := exec.Command("git", "diff", "--numstat", localesDir)

stdout, err := cmd.StdoutPipe()
if err != nil {
Expand All @@ -782,23 +714,18 @@ func getChangedLocales() (adds, dels []string, err error) {
}

scanner := bufio.NewScanner(stdout)
key := ""
var all []string

for scanner.Scan() {
line := scanner.Text()

if strings.HasPrefix(line, "+++") {
i := strings.LastIndex(line, "/")
key = line[i+1:]
fields := strings.Fields(line)

all = append(all, key)
} else if strings.HasPrefix(line, "+") {
if key == defaultBaseFile {
continue
}
path := localePath(fields[2])

adds = append(adds, key)
if fields[0] != "0" {
adds = append(adds, path)
} else {
dels = append(dels, path)
}
}

Expand All @@ -807,28 +734,5 @@ func getChangedLocales() (adds, dels []string, err error) {
return nil, nil, fmt.Errorf("waiting command: %w", err)
}

adds = slices.Compact(adds)

for _, v := range all {
if !slices.Contains(adds, v) {
dels = append(dels, v)
}
}

return adds, dels, nil
}

// printDeletions prints list of translations with non-additional changes.
func printDeletions() {
_, dels, err := getChangedLocales()
if err != nil {
log.Info("printing locales with deletions: %s", err)

return
}

for _, v := range dels {
path := filepath.Join(localesDir, v)
fmt.Println(path)
}
}

0 comments on commit 571b2a2

Please sign in to comment.