You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
1.9 KiB

  1. // +build ignore
  2. package main
  3. import (
  4. "archive/tar"
  5. "compress/gzip"
  6. "flag"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "log"
  11. "net/http"
  12. "os"
  13. "path"
  14. "path/filepath"
  15. "strings"
  16. "code.gitea.io/gitea/modules/util"
  17. )
  18. func main() {
  19. var (
  20. prefix = "gitea-licenses"
  21. url = "https://api.github.com/repos/spdx/license-list-data/tarball"
  22. destination = ""
  23. )
  24. flag.StringVar(&destination, "dest", "options/license/", "destination for the licenses")
  25. flag.Parse()
  26. file, err := ioutil.TempFile(os.TempDir(), prefix)
  27. if err != nil {
  28. log.Fatalf("Failed to create temp file. %s", err)
  29. }
  30. defer util.Remove(file.Name())
  31. resp, err := http.Get(url)
  32. if err != nil {
  33. log.Fatalf("Failed to download archive. %s", err)
  34. }
  35. defer resp.Body.Close()
  36. if _, err := io.Copy(file, resp.Body); err != nil {
  37. log.Fatalf("Failed to copy archive to file. %s", err)
  38. }
  39. if _, err := file.Seek(0, 0); err != nil {
  40. log.Fatalf("Failed to reset seek on archive. %s", err)
  41. }
  42. gz, err := gzip.NewReader(file)
  43. if err != nil {
  44. log.Fatalf("Failed to gunzip the archive. %s", err)
  45. }
  46. tr := tar.NewReader(gz)
  47. for {
  48. hdr, err := tr.Next()
  49. if err == io.EOF {
  50. break
  51. }
  52. if err != nil {
  53. log.Fatalf("Failed to iterate archive. %s", err)
  54. }
  55. if !strings.Contains(hdr.Name, "/text/") {
  56. continue
  57. }
  58. if filepath.Ext(hdr.Name) != ".txt" {
  59. continue
  60. }
  61. if strings.HasPrefix(filepath.Base(hdr.Name), "README") {
  62. continue
  63. }
  64. if strings.HasPrefix(filepath.Base(hdr.Name), "deprecated_") {
  65. continue
  66. }
  67. out, err := os.Create(path.Join(destination, strings.TrimSuffix(filepath.Base(hdr.Name), ".txt")))
  68. if err != nil {
  69. log.Fatalf("Failed to create new file. %s", err)
  70. }
  71. defer out.Close()
  72. if _, err := io.Copy(out, tr); err != nil {
  73. log.Fatalf("Failed to write new file. %s", err)
  74. } else {
  75. fmt.Printf("Written %s\n", out.Name())
  76. }
  77. }
  78. fmt.Println("Done")
  79. }