2024-10-01 21:25:08 +02:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-08-24 18:47:09 +02:00
|
|
|
//go:build ignore
|
2017-03-15 13:30:16 +01:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"compress/gzip"
|
2024-10-01 21:25:08 +02:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2017-03-15 13:30:16 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-08-11 22:05:34 +02:00
|
|
|
|
2024-10-01 21:25:08 +02:00
|
|
|
"code.gitea.io/gitea/build/license"
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
2020-08-11 22:05:34 +02:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2017-03-15 13:30:16 +01:00
|
|
|
)
|
|
|
|
|
2017-03-15 16:11:26 +01:00
|
|
|
func main() {
|
|
|
|
var (
|
2020-09-04 16:02:37 +02:00
|
|
|
prefix = "gitea-licenses"
|
|
|
|
url = "https://api.github.com/repos/spdx/license-list-data/tarball"
|
|
|
|
githubApiToken = ""
|
|
|
|
githubUsername = ""
|
|
|
|
destination = ""
|
2017-03-15 16:11:26 +01:00
|
|
|
)
|
2017-03-15 13:30:16 +01:00
|
|
|
|
|
|
|
flag.StringVar(&destination, "dest", "options/license/", "destination for the licenses")
|
2020-09-04 16:02:37 +02:00
|
|
|
flag.StringVar(&githubUsername, "username", "", "github username")
|
|
|
|
flag.StringVar(&githubApiToken, "token", "", "github api token")
|
2017-03-15 13:30:16 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2021-09-22 07:38:34 +02:00
|
|
|
file, err := os.CreateTemp(os.TempDir(), prefix)
|
2017-03-15 13:30:16 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create temp file. %s", err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:05:34 +02:00
|
|
|
defer util.Remove(file.Name())
|
2017-03-15 13:30:16 +01:00
|
|
|
|
2022-08-22 06:33:01 +02:00
|
|
|
if err := os.RemoveAll(destination); err != nil {
|
|
|
|
log.Fatalf("Cannot clean destination folder: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(destination, 0o755); err != nil {
|
|
|
|
log.Fatalf("Cannot create destination: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 16:02:37 +02:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to download archive. %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(githubApiToken) > 0 && len(githubUsername) > 0 {
|
|
|
|
req.SetBasicAuth(githubUsername, githubApiToken)
|
|
|
|
}
|
2017-03-15 13:30:16 +01:00
|
|
|
|
2020-09-04 16:02:37 +02:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2017-03-15 13:30:16 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to download archive. %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(file, resp.Body); err != nil {
|
|
|
|
log.Fatalf("Failed to copy archive to file. %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := file.Seek(0, 0); err != nil {
|
|
|
|
log.Fatalf("Failed to reset seek on archive. %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gz, err := gzip.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to gunzip the archive. %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := tar.NewReader(gz)
|
2024-10-01 21:25:08 +02:00
|
|
|
aliasesFiles := make(map[string][]string)
|
2017-03-15 13:30:16 +01:00
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to iterate archive. %s", err)
|
|
|
|
}
|
|
|
|
|
2018-02-12 19:20:30 +01:00
|
|
|
if !strings.Contains(hdr.Name, "/text/") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-15 13:30:16 +01:00
|
|
|
if filepath.Ext(hdr.Name) != ".txt" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-01 21:25:08 +02:00
|
|
|
fileBaseName := filepath.Base(hdr.Name)
|
|
|
|
licenseName := strings.TrimSuffix(fileBaseName, ".txt")
|
|
|
|
|
|
|
|
if strings.HasPrefix(fileBaseName, "README") {
|
2017-03-15 13:30:16 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-01 21:25:08 +02:00
|
|
|
if strings.HasPrefix(fileBaseName, "deprecated_") {
|
2017-03-15 13:30:16 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-10-01 21:25:08 +02:00
|
|
|
out, err := os.Create(path.Join(destination, licenseName))
|
2017-03-15 13:30:16 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create new file. %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer out.Close()
|
|
|
|
|
2024-10-01 21:25:08 +02:00
|
|
|
// some license files have same content, so we need to detect these files and create a convert map into a json file
|
|
|
|
// Later we use this convert map to avoid adding same license content with different license name
|
|
|
|
h := md5.New()
|
|
|
|
// calculate md5 and write file in the same time
|
|
|
|
r := io.TeeReader(tr, h)
|
|
|
|
if _, err := io.Copy(out, r); err != nil {
|
2017-03-15 13:30:16 +01:00
|
|
|
log.Fatalf("Failed to write new file. %s", err)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Written %s\n", out.Name())
|
2024-10-01 21:25:08 +02:00
|
|
|
|
|
|
|
md5 := hex.EncodeToString(h.Sum(nil))
|
|
|
|
aliasesFiles[md5] = append(aliasesFiles[md5], licenseName)
|
2017-03-15 13:30:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-01 21:25:08 +02:00
|
|
|
// generate convert license name map
|
|
|
|
licenseAliases := make(map[string]string)
|
|
|
|
for _, fileNames := range aliasesFiles {
|
|
|
|
if len(fileNames) > 1 {
|
|
|
|
licenseName := license.GetLicenseNameFromAliases(fileNames)
|
|
|
|
if licenseName == "" {
|
|
|
|
// license name should not be empty as expected
|
|
|
|
// if it is empty, we need to rewrite the logic of GetLicenseNameFromAliases
|
|
|
|
log.Fatalf("GetLicenseNameFromAliases: license name is empty")
|
|
|
|
}
|
|
|
|
for _, fileName := range fileNames {
|
|
|
|
licenseAliases[fileName] = licenseName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// save convert license name map to file
|
|
|
|
b, err := json.Marshal(licenseAliases)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create json bytes. %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
licenseAliasesDestination := filepath.Join(destination, "etc", "license-aliases.json")
|
|
|
|
if err := os.MkdirAll(filepath.Dir(licenseAliasesDestination), 0o755); err != nil {
|
|
|
|
log.Fatalf("Failed to create directory for license aliases json file. %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(licenseAliasesDestination)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create license aliases json file. %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err = f.Write(b); err != nil {
|
|
|
|
log.Fatalf("Failed to write license aliases json file. %s", err)
|
|
|
|
}
|
|
|
|
|
2017-03-15 13:30:16 +01:00
|
|
|
fmt.Println("Done")
|
|
|
|
}
|