Browse Source

Output SHA1 checksum for released packages

v2ray 10 years ago
parent
commit
639bcad78d
4 changed files with 53 additions and 9 deletions
  1. 1 0
      .travis.yml
  2. 18 6
      tools/build/build.go
  3. 3 3
      tools/build/build_test.go
  4. 31 0
      tools/build/metadata.go

+ 1 - 0
.travis.yml

@@ -31,6 +31,7 @@ deploy:
     - "$GOPATH/bin/v2ray-linux-32.zip"
     - "$GOPATH/bin/v2ray-linux-arm.zip"
     - "$GOPATH/bin/v2ray-linux-arm64.zip"
+    - "$GOPATH/bin/metadata.txt"
   skip_cleanup: true
   on:
     tags: true

+ 18 - 6
tools/build/build.go

@@ -11,9 +11,10 @@ import (
 )
 
 var (
-	flagTargetOS   = flag.String("os", runtime.GOOS, "Target OS of this build.")
-	flagTargetArch = flag.String("arch", runtime.GOARCH, "Target CPU arch of this build.")
-	flagArchive    = flag.Bool("zip", false, "Whether to make an archive of files or not.")
+	flagTargetOS     = flag.String("os", runtime.GOOS, "Target OS of this build.")
+	flagTargetArch   = flag.String("arch", runtime.GOARCH, "Target CPU arch of this build.")
+	flagArchive      = flag.Bool("zip", false, "Whether to make an archive of files or not.")
+	flagMetadataFile = flag.String("metadata", "metadata.txt", "File to store metadata info of released packages.")
 
 	binPath string
 )
@@ -45,10 +46,10 @@ func getBinPath() string {
 func main() {
 	flag.Parse()
 	binPath = getBinPath()
-	build(*flagTargetOS, *flagTargetArch, *flagArchive, "")
+	build(*flagTargetOS, *flagTargetArch, *flagArchive, "", *flagMetadataFile)
 }
 
-func build(targetOS, targetArch string, archive bool, version string) {
+func build(targetOS, targetArch string, archive bool, version string, metadataFile string) {
 	v2rayOS := parseOS(targetOS)
 	v2rayArch := parseArch(targetArch)
 
@@ -91,7 +92,18 @@ func build(targetOS, targetArch string, archive bool, version string) {
 		root := filepath.Base(targetDir)
 		err = zipFolder(root, zipFile)
 		if err != nil {
-			fmt.Println("Unable to create archive (%s): %v\n", zipFile, err)
+			fmt.Printf("Unable to create archive (%s): %v\n", zipFile, err)
+		}
+
+		metadataWriter, err := os.OpenFile(filepath.Join(binPath, metadataFile), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
+		if err != nil {
+			fmt.Printf("Unable to create metadata file (%s): %v\n", metadataFile, err)
+		}
+		defer metadataWriter.Close()
+
+		err = CalcMetadata(zipFile, metadataWriter)
+		if err != nil {
+			fmt.Printf("Failed to calculate metadata for file (%s): %v", zipFile, err)
 		}
 	}
 }

+ 3 - 3
tools/build/build_test.go

@@ -36,21 +36,21 @@ func TestBuildMacOS(t *testing.T) {
 	binPath = filepath.Join(os.Getenv("GOPATH"), "testing")
 	cleanBinPath()
 
-	build("macos", "amd64", true, "test")
+	build("macos", "amd64", true, "test", "metadata.txt")
 	assert.Bool(allFilesExists(
 		"v2ray-macos.zip",
 		"v2ray-test-macos",
 		filepath.Join("v2ray-test-macos", "config.json"),
 		filepath.Join("v2ray-test-macos", "v2ray"))).IsTrue()
 
-	build("windows", "amd64", true, "test")
+	build("windows", "amd64", true, "test", "metadata.txt")
 	assert.Bool(allFilesExists(
 		"v2ray-windows-64.zip",
 		"v2ray-test-windows-64",
 		filepath.Join("v2ray-test-windows-64", "config.json"),
 		filepath.Join("v2ray-test-windows-64", "v2ray.exe"))).IsTrue()
 
-	build("linux", "amd64", true, "test")
+	build("linux", "amd64", true, "test", "metadata.txt")
 	assert.Bool(allFilesExists(
 		"v2ray-linux-64.zip",
 		"v2ray-test-linux-64",

+ 31 - 0
tools/build/metadata.go

@@ -0,0 +1,31 @@
+package main
+
+import (
+	"crypto/sha1"
+	"encoding/hex"
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+)
+
+func CalcMetadata(file string, writer io.Writer) error {
+	fileReader, err := os.Open(file)
+	if err != nil {
+		return err
+	}
+	defer fileReader.Close()
+
+	hasher := sha1.New()
+	nBytes, err := io.Copy(hasher, fileReader)
+	if err != nil {
+		return err
+	}
+	sha1sum := hasher.Sum(nil)
+	filename := filepath.Base(file)
+	fmt.Fprintf(writer, "File: %s\n", filename)
+	fmt.Fprintf(writer, "Size: %d\n", nBytes)
+	fmt.Fprintf(writer, "SHA1: %s\n", hex.EncodeToString(sha1sum))
+	fmt.Fprintln(writer)
+	return nil
+}