You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2022/07/07 13:12:36 UTC

[GitHub] [skywalking-eyes] jmjoy commented on a diff in pull request #121: Add rust cargo support for dep command.

jmjoy commented on code in PR #121:
URL: https://github.com/apache/skywalking-eyes/pull/121#discussion_r915857235


##########
pkg/deps/cargo.go:
##########
@@ -0,0 +1,154 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package deps
+
+import (
+	"encoding/json"
+	"fmt"
+	"os"
+	"os/exec"
+	"path/filepath"
+	"regexp"
+
+	"github.com/apache/skywalking-eyes/internal/logger"
+)
+
+type CargoMetadata struct {
+	Packages []CargoPackage `json:"packages"`
+}
+
+type CargoPackage struct {
+	Name         string `json:"name"`
+	Version      string `json:"version"`
+	License      string `json:"license"`
+	LicenseFile  string `json:"license_file"`
+	ManifestPath string `json:"manifest_path"`
+}
+
+type CargoTomlResolver struct {
+	Resolver
+}
+
+func (resolver *CargoTomlResolver) CanResolve(file string) bool {
+	base := filepath.Base(file)
+	logger.Log.Debugln("Base name:", base)
+	return base == "Cargo.toml"
+}
+
+// Resolve resolves licenses of all dependencies declared in the Cargo.toml file.
+func (resolver *CargoTomlResolver) Resolve(cargoTomlFile string, config *ConfigDeps, report *Report) error {
+	dir := filepath.Dir(cargoTomlFile)
+
+	download := exec.Command("cargo", "fetch")
+	logger.Log.Debugf("Run command: %v, please wait", download.String())
+	download.Stdout = os.Stdout
+	download.Stderr = os.Stderr
+	download.Dir = dir
+	if err := download.Run(); err != nil {
+		return err
+	}
+
+	cmd := exec.Command("cargo", "metadata", "--format-version=1", "--all-features")
+	cmd.Dir = dir
+	output, err := cmd.Output()
+	if err != nil {
+		return err
+	}
+
+	var metadata CargoMetadata
+	if err := json.Unmarshal(output, &metadata); err != nil {
+		return err
+	}
+
+	logger.Log.Debugln("Package size:", len(metadata.Packages))
+
+	return resolver.ResolvePackages(metadata.Packages, config, report)
+}
+
+// ResolvePackages resolves the licenses of the given packages.
+func (resolver *CargoTomlResolver) ResolvePackages(packages []CargoPackage, config *ConfigDeps, report *Report) error {
+	for i := range packages {
+		pkg := packages[i]
+
+		if config.IsExcluded(pkg.Name, pkg.Version) {
+			continue
+		}
+		if l, ok := config.GetUserConfiguredLicense(pkg.Name, pkg.Version); ok {
+			report.Resolve(&Result{
+				Dependency:    pkg.Name,
+				LicenseSpdxID: l,
+				Version:       pkg.Version,
+			})
+			continue
+		}
+		err := resolver.ResolvePackageLicense(config, &pkg, report)
+		if err != nil {
+			logger.Log.Warnf("Failed to resolve the license of <%s@%s>: %v\n", pkg.Name, pkg.Version, err)
+			report.Skip(&Result{
+				Dependency:    pkg.Name,
+				LicenseSpdxID: Unknown,
+				Version:       pkg.Version,
+			})
+		}
+	}
+	return nil
+}
+
+var cargoPossibleLicenseFileName = regexp.MustCompile(`(?i)^LICENSE|LICENCE(\.txt)?|LICENSE-.+|COPYING(\.txt)?$`)
+
+// ResolvePackageLicense resolve the package license.
+// The CargoPackage.LicenseFile is generally used for non-standard licenses and is ignored now.
+func (resolver *CargoTomlResolver) ResolvePackageLicense(config *ConfigDeps, pkg *CargoPackage, report *Report) error {
+	if pkg.License == "" {
+		return fmt.Errorf("license is empty")
+	}

Review Comment:
   I wrote it with reference to `GoModResolver`, which returns an error, the calling place will warn and call `report.Skip`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org