Skip to content

Commit

Permalink
closer to fully running (perm error if protoc already exists in the c…
Browse files Browse the repository at this point in the history
…ache dir)
  • Loading branch information
jwasinger committed Jun 24, 2024
1 parent f8c45bf commit 68697a3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
2 changes: 0 additions & 2 deletions accounts/usbwallet/trezor/trezor.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
// - Grab the latest Go plugin `go get -u github.com/golang/protobuf/protoc-gen-go`
// - Vendor in the latest Go plugin `govendor fetch github.com/golang/protobuf/...`

//go:generate protoc -I/usr/local/include:. --go_out=import_path=trezor:. messages.proto messages-common.proto messages-management.proto messages-ethereum.proto

// Package trezor contains the wire protocol.
package trezor

Expand Down
23 changes: 15 additions & 8 deletions build/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ func generateCheck() {
if strings.HasPrefix(path, filepath.FromSlash("tests/testdata")) {
return nil
}

if d.IsDir() {
return nil
}
Expand Down Expand Up @@ -470,11 +471,16 @@ func downloadProtoc(cachedir string) (string, error) {
operatingSystem := runtime.GOOS
var base string

csdb := build.MustLoadChecksums("build/checksums.txt")
version, err := build.Version(csdb, "protoc")
if err != nil {
log.Fatal(err)
}
if runtime.GOOS == "windows" {
if arch == "amd64" {
base = "win64.zip"
base = "win64"
} else if arch == "386" {
base = "win32.zip"
base = "win32"
} else {
return "", errNoReleaseAvailable
}
Expand All @@ -494,19 +500,20 @@ func downloadProtoc(cachedir string) (string, error) {
} else {
return "", errNoReleaseAvailable
}
base = operatingSystem + "-" + arch + ".zip"
base = operatingSystem + "-" + arch
}

fileName := fmt.Sprintf("protoc-%s-%s", version, base)
url := fmt.Sprintf("https://github.com/protocolbuffers/protobuf/releases/download/v%s/%s", version, fileName)
archivePath := filepath.Join(cachedir, fileName)
if err := build.Download(url, archivePath); err != nil {
archiveFileName := fileName + ".zip"
url := fmt.Sprintf("https://github.com/protocolbuffers/protobuf/releases/download/v%s/%s", version, archiveFileName)
archivePath := filepath.Join(cachedir, archiveFileName)
if err := csdb.DownloadFile(url, archivePath); err != nil {
log.Fatal(err)
}
if err := build.ExtractArchive(archivePath, cachedir); err != nil {
if err := build.ExtractArchive(archivePath, filepath.Join(cachedir, fileName)); err != nil {
log.Fatal(err)
}
return filepath.Join(cachedir, base, "protoc"), nil
return filepath.Join(cachedir, fileName), nil
}

// Release Packaging
Expand Down
48 changes: 21 additions & 27 deletions internal/build/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,29 @@ func (db *ChecksumDB) DownloadFile(url, dstPath string) error {
fmt.Printf("%s is stale\n", dstPath)
fmt.Printf("downloading from %s\n", url)

if err := Download(url, dstPath); err != nil {
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("download error: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("download error: status %d", resp.StatusCode)
}
if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil {
return err
}
fd, err := os.OpenFile(dstPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
dst := newDownloadWriter(fd, resp.ContentLength)
_, err = io.Copy(dst, resp.Body)
dst.Close()
if err != nil {
return err
}

return db.Verify(dstPath)
}

Expand Down Expand Up @@ -130,29 +150,3 @@ func (w *downloadWriter) Close() error {
}
return closeErr
}

func Download(url, dstPath string) error {
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("download error: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("download error: status %d", resp.StatusCode)
}
if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil {
return err
}
fd, err := os.OpenFile(dstPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
dst := newDownloadWriter(fd, resp.ContentLength)
_, err = io.Copy(dst, resp.Body)
dst.Close()
if err != nil {
return err
}
return nil
}

0 comments on commit 68697a3

Please sign in to comment.