github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/python/python.go (about)

     1  package python
     2  
     3  import (
     4  	"errors"
     5  	python "github.com/jfrog/jfrog-cli-core/v2/utils/python"
     6  	"io"
     7  	"os/exec"
     8  
     9  	"github.com/jfrog/build-info-go/build"
    10  	"github.com/jfrog/build-info-go/entities"
    11  	"github.com/jfrog/build-info-go/utils/pythonutils"
    12  	gofrogcmd "github.com/jfrog/gofrog/io"
    13  	"github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/python/dependencies"
    14  	"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
    15  	buildUtils "github.com/jfrog/jfrog-cli-core/v2/common/build"
    16  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
    17  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    18  	"github.com/jfrog/jfrog-client-go/utils/log"
    19  )
    20  
    21  type PythonCommand struct {
    22  	serverDetails *config.ServerDetails
    23  	pythonTool    pythonutils.PythonTool
    24  	commandName   string
    25  	args          []string
    26  	repository    string
    27  }
    28  
    29  func NewPythonCommand(pythonTool pythonutils.PythonTool) *PythonCommand {
    30  	return &PythonCommand{pythonTool: pythonTool}
    31  }
    32  
    33  func (pc *PythonCommand) Run() (err error) {
    34  	log.Info("Running", string(pc.pythonTool), pc.commandName)
    35  	var buildConfiguration *buildUtils.BuildConfiguration
    36  	pc.args, buildConfiguration, err = buildUtils.ExtractBuildDetailsFromArgs(pc.args)
    37  	if err != nil {
    38  		return
    39  	}
    40  	pythonBuildInfo, err := buildUtils.PrepareBuildPrerequisites(buildConfiguration)
    41  	if err != nil {
    42  		return
    43  	}
    44  	defer func() {
    45  		if pythonBuildInfo != nil && err != nil {
    46  			err = errors.Join(err, pythonBuildInfo.Clean())
    47  		}
    48  	}()
    49  	err = pc.SetPypiRepoUrlWithCredentials()
    50  	if err != nil {
    51  		return
    52  	}
    53  
    54  	if pythonBuildInfo != nil && pc.commandName == "install" {
    55  		// Need to collect build info
    56  		var pythonModule *build.PythonModule
    57  		pythonModule, err = pythonBuildInfo.AddPythonModule("", pc.pythonTool)
    58  		if err != nil {
    59  			return
    60  		}
    61  		if buildConfiguration.GetModule() != "" {
    62  			pythonModule.SetName(buildConfiguration.GetModule())
    63  		}
    64  		var localDependenciesPath string
    65  		localDependenciesPath, err = config.GetJfrogDependenciesPath()
    66  		if err != nil {
    67  			return
    68  		}
    69  		pythonModule.SetLocalDependenciesPath(localDependenciesPath)
    70  		pythonModule.SetUpdateDepsChecksumInfoFunc(pc.UpdateDepsChecksumInfoFunc)
    71  		err = errorutils.CheckError(pythonModule.RunInstallAndCollectDependencies(pc.args))
    72  	} else {
    73  		// Python native command
    74  		err = gofrogcmd.RunCmd(pc)
    75  	}
    76  	return
    77  }
    78  
    79  func (pc *PythonCommand) UpdateDepsChecksumInfoFunc(dependenciesMap map[string]entities.Dependency, srcPath string) error {
    80  	servicesManager, err := utils.CreateServiceManager(pc.serverDetails, -1, 0, false)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	return dependencies.UpdateDepsChecksumInfo(dependenciesMap, srcPath, servicesManager, pc.repository)
    85  }
    86  
    87  func (pc *PythonCommand) SetRepo(repo string) *PythonCommand {
    88  	pc.repository = repo
    89  	return pc
    90  }
    91  
    92  func (pc *PythonCommand) SetArgs(arguments []string) *PythonCommand {
    93  	pc.args = arguments
    94  	return pc
    95  }
    96  
    97  func (pc *PythonCommand) SetCommandName(commandName string) *PythonCommand {
    98  	pc.commandName = commandName
    99  	return pc
   100  }
   101  
   102  func (pc *PythonCommand) SetPypiRepoUrlWithCredentials() error {
   103  	rtUrl, err := python.GetPypiRepoUrl(pc.serverDetails, pc.repository, false)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	pc.args = append(pc.args, python.GetPypiRemoteRegistryFlag(pc.pythonTool), rtUrl)
   108  	return nil
   109  }
   110  
   111  func (pc *PythonCommand) SetServerDetails(serverDetails *config.ServerDetails) *PythonCommand {
   112  	pc.serverDetails = serverDetails
   113  	return pc
   114  }
   115  
   116  func (pc *PythonCommand) ServerDetails() (*config.ServerDetails, error) {
   117  	return pc.serverDetails, nil
   118  }
   119  
   120  func (pc *PythonCommand) GetCmd() *exec.Cmd {
   121  	var cmd []string
   122  	cmd = append(cmd, string(pc.pythonTool))
   123  	cmd = append(cmd, pc.commandName)
   124  	cmd = append(cmd, pc.args...)
   125  	return exec.Command(cmd[0], cmd[1:]...)
   126  }
   127  
   128  func (pc *PythonCommand) GetEnv() map[string]string {
   129  	return map[string]string{}
   130  }
   131  
   132  func (pc *PythonCommand) GetStdWriter() io.WriteCloser {
   133  	return nil
   134  }
   135  
   136  func (pc *PythonCommand) GetErrWriter() io.WriteCloser {
   137  	return nil
   138  }