diff --git a/cliv2/scripts/prepare_licenses.go b/cliv2/scripts/prepare_licenses.go new file mode 100644 index 0000000000..64640b9980 --- /dev/null +++ b/cliv2/scripts/prepare_licenses.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "io" + "net/http" + "os" + "path/filepath" + "regexp" +) + +func downloadLicense(url, packageName string) error { + folderPath := filepath.Join(".", "internal", "embedded", "_data", "licenses", packageName) + licenseFileName := filepath.Join(folderPath, "LICENSE") + + err := os.MkdirAll(folderPath, 0755) + if err != nil { + return err + } + + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("failed to download   license: %s (status code: %d)", url, resp.StatusCode) + } + + licenseFile, err := os.Create(licenseFileName) + if err != nil { + return err + } + defer licenseFile.Close() + + _, err = io.Copy(licenseFile, resp.Body) + return err +} + +func main() { + pattern := regexp.MustCompile("(?i)COPYING|LICENSE|NOTICE.*") + + licenses := []struct { + url string + packageName string + }{ + {"https://raw.githubusercontent.com/davecgh/go-spew/master/LICENSE", "github.com/davecgh/go-spew"}, + {"https://raw.githubusercontent.com/alexbrainman/sspi/master/LICENSE", "github.com/alexbrainman/sspi"}, + {"https://raw.githubusercontent.com/pmezard/go-difflib/master/LICENSE", "github.com/pmezard/go-difflib"}, + {"https://go.dev/LICENSE?m=text", "go.dev"}, + } + + for _, license := range licenses { + err := downloadLicense(license.url, license.packageName) + if err != nil { + fmt.Printf("Error downloading license for %s: %v\n", license.packageName, err) + } + } + + licensePath := filepath.Join(".", "internal", "embedded", "_data", "licenses") + err := filepath.Walk(licensePath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && !pattern.MatchString(path) { + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { + return err + } + } else { + fmt.Println(path) + } + return nil + }) + + if err != nil { + fmt.Println("Error cleaning up licenses:", err) + } +} diff --git a/cliv2/scripts/prepare_licenses.py b/cliv2/scripts/prepare_licenses.py deleted file mode 100755 index 971a536e21..0000000000 --- a/cliv2/scripts/prepare_licenses.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 -import os -import requests -import re - -def manual_license_download(url, package_name): - folder_path = os.path.join(".", "internal", "embedded", "_data", "licenses", package_name) - license_file_name = os.path.normpath(os.path.join(folder_path, "LICENSE")) - - if not os.path.exists(license_file_name): - os.makedirs(folder_path, exist_ok=True) - with requests.get(url, stream=True, allow_redirects=True) as response: - response.raise_for_status() - with open(license_file_name, "wb") as license_file: - for chunk in response.iter_content(chunk_size=8192): - license_file.write(chunk) - -def main(): - # Try to find all licenses via the go.mod file - go_bin_path = os.path.join(os.getcwd(), "_cache") - os.environ["GOBIN"] = go_bin_path - os.system(f"go install github.com/google/go-licenses@latest") - os.environ["PATH"] += os.pathsep + go_bin_path - os.system(f"go-licenses save ./... --save_path=./internal/embedded/_data/licenses --force --ignore github.com/snyk/cli/cliv2/") - - manual_license_download("https://raw.githubusercontent.com/davecgh/go-spew/master/LICENSE", "github.com/davecgh/go-spew") - manual_license_download("https://raw.githubusercontent.com/alexbrainman/sspi/master/LICENSE", "github.com/alexbrainman/sspi") - manual_license_download("https://raw.githubusercontent.com/pmezard/go-difflib/master/LICENSE", "github.com/pmezard/go-difflib") - manual_license_download("https://go.dev/LICENSE?m=text", "go.dev") - - # Clean up and print result - pattern = re.compile("COPYING|LICENSE|NOTICE.*", flags=re.IGNORECASE) - for root, dirs, files in os.walk(os.path.join(".", "internal", "embedded", "_data", "licenses")): - for entry in files: - p = os.path.join(root, entry) - if not pattern.match(entry): - try: - if os.access(p, os.W_OK): - os.remove(p) - except: - pass - else: - print(f" {p}") - -if __name__ == "__main__": - main() diff --git a/cliv2/scripts/prepare_licenses.sh b/cliv2/scripts/prepare_licenses.sh index c50503bc2f..02aa779dd4 100755 --- a/cliv2/scripts/prepare_licenses.sh +++ b/cliv2/scripts/prepare_licenses.sh @@ -1,10 +1,5 @@ #!/usr/bin/env bash set -euo pipefail BASEDIR=$(dirname "$0") -PYTHON_VERSION="" -if python3 -c 'print("python3")' > /dev/null 2>&1; then - PYTHON_VERSION="3" -fi - -python$PYTHON_VERSION $BASEDIR/prepare_licenses.py +go run $BASEDIR/prepare_licenses.go