1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/pythonBuild.go
TheShifter 188e743f7b
PythonBuild: Implementation of pythonBuild step (#3483)
* Implementation of pythonBuild step

* minor update and refactoring

* minor update

* add integration test and test project to testdata dir

* remove generated build data dir

* Rewrite some logic. Minor fix in integration tests for python

* Add new input parameters to pythonBuild.yaml

* rewrite logic
remove some checks

* rollback

* resolve merge conflict in piper.go
Update logic in python build. Create bom now works fine

* remove duplicate line

* refactoring fix

* resolve comment. Remove install build and change build command. Change twine upload command

* add groovy wrapper for pythonBuild step

* Rewrite tests. Remove some cheks from pythonBuild.go

* add some test to pythonBuild_test.go

* Add some parameters and credentials to the pythonBuild.groovy

* fix issue in unit tests

* add pythonBuild to fieldRelatedWhitelist

* update integration test for pythonBuild

* add imports

* update integration tests and add a new one

* minor fix

* fix some issues in integration tests

* update integration tests. Make it works again

Co-authored-by: Anil Keshav <anil.keshav@sap.com>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-03-29 19:01:44 +02:00

108 lines
2.8 KiB
Go

package cmd
import (
"fmt"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
)
const (
PyBomFilename = "bom.xml"
)
type pythonBuildUtils interface {
command.ExecRunner
FileExists(filename string) (bool, error)
piperutils.FileUtils
}
type pythonBuildUtilsBundle struct {
*command.Command
*piperutils.Files
}
func newPythonBuildUtils() pythonBuildUtils {
utils := pythonBuildUtilsBundle{
Command: &command.Command{},
Files: &piperutils.Files{},
}
// Reroute command output to logging framework
utils.Stdout(log.Writer())
utils.Stderr(log.Writer())
return &utils
}
func pythonBuild(config pythonBuildOptions, telemetryData *telemetry.CustomData) {
utils := newPythonBuildUtils()
err := runPythonBuild(&config, telemetryData, utils)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runPythonBuild(config *pythonBuildOptions, telemetryData *telemetry.CustomData, utils pythonBuildUtils) error {
installFlags := []string{"-m", "pip", "install", "--upgrade"}
err := buildExecute(config, utils, installFlags)
if err != nil {
return fmt.Errorf("Python build failed with error: %w", err)
}
if config.CreateBOM {
if err := runBOMCreationForPy(utils, installFlags); err != nil {
return fmt.Errorf("BOM creation failed: %w", err)
}
}
if config.Publish {
if err := publishWithTwine(config, utils, installFlags); err != nil {
return fmt.Errorf("failed to publish: %w", err)
}
}
return nil
}
func buildExecute(config *pythonBuildOptions, utils pythonBuildUtils, installFlags []string) error {
var flags []string
flags = append(flags, config.BuildFlags...)
flags = append(flags, "setup.py", "sdist", "bdist_wheel")
log.Entry().Info("starting building python project:")
err := utils.RunExecutable("python3", flags...)
if err != nil {
return err
}
return nil
}
func runBOMCreationForPy(utils pythonBuildUtils, installFlags []string) error {
installFlags = append(installFlags, "cyclonedx-bom")
if err := utils.RunExecutable("python3", installFlags...); err != nil {
return err
}
if err := utils.RunExecutable("cyclonedx-bom", "--e", "--output", PyBomFilename); err != nil {
return err
}
return nil
}
func publishWithTwine(config *pythonBuildOptions, utils pythonBuildUtils, installFlags []string) error {
installFlags = append(installFlags, "twine")
if err := utils.RunExecutable("python3", installFlags...); err != nil {
return err
}
if err := utils.RunExecutable("twine", "upload", "--username", config.TargetRepositoryUser,
"--password", config.TargetRepositoryPassword, "--repository-url", config.TargetRepositoryURL,
"dist/*"); err != nil {
return err
}
return nil
}