github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/python/poetry.go (about) 1 package python 2 3 import ( 4 "errors" 5 "fmt" 6 "github.com/jfrog/build-info-go/build" 7 "github.com/jfrog/build-info-go/entities" 8 "github.com/jfrog/build-info-go/utils/pythonutils" 9 gofrogcmd "github.com/jfrog/gofrog/io" 10 "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/python/dependencies" 11 "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" 12 buildUtils "github.com/jfrog/jfrog-cli-core/v2/common/build" 13 "github.com/jfrog/jfrog-cli-core/v2/utils/config" 14 python "github.com/jfrog/jfrog-cli-core/v2/utils/python" 15 "github.com/jfrog/jfrog-client-go/utils/errorutils" 16 "github.com/jfrog/jfrog-client-go/utils/log" 17 "golang.org/x/exp/slices" 18 "io" 19 "os/exec" 20 ) 21 22 type PoetryCommand struct { 23 PythonCommand 24 // The uniq Artifactory repository name for poetry config file. 25 poetryConfigRepoName string 26 } 27 28 const baseConfigRepoName = "jfrog-server" 29 30 func NewPoetryCommand() *PoetryCommand { 31 return &PoetryCommand{ 32 PythonCommand: *NewPythonCommand(pythonutils.Poetry), 33 poetryConfigRepoName: baseConfigRepoName, 34 } 35 } 36 37 func (pc *PoetryCommand) Run() (err error) { 38 log.Info(fmt.Sprintf("Running Poetry %s.", pc.commandName)) 39 var buildConfiguration *buildUtils.BuildConfiguration 40 pc.args, buildConfiguration, err = buildUtils.ExtractBuildDetailsFromArgs(pc.args) 41 if err != nil { 42 return err 43 } 44 pythonBuildInfo, err := buildUtils.PrepareBuildPrerequisites(buildConfiguration) 45 if err != nil { 46 return 47 } 48 defer func() { 49 if pythonBuildInfo != nil && err != nil { 50 err = errors.Join(err, pythonBuildInfo.Clean()) 51 } 52 }() 53 err = pc.SetPypiRepoUrlWithCredentials() 54 if err != nil { 55 return err 56 } 57 if pythonBuildInfo != nil { 58 switch pc.commandName { 59 case "install": 60 return pc.install(buildConfiguration, pythonBuildInfo) 61 case "publish": 62 return pc.publish(buildConfiguration, pythonBuildInfo) 63 default: 64 // poetry native command 65 return gofrogcmd.RunCmd(pc) 66 67 } 68 } 69 return gofrogcmd.RunCmd(pc) 70 } 71 72 func (pc *PoetryCommand) install(buildConfiguration *buildUtils.BuildConfiguration, pythonBuildInfo *build.Build) (err error) { 73 var pythonModule *build.PythonModule 74 pythonModule, err = pythonBuildInfo.AddPythonModule("", pc.pythonTool) 75 if err != nil { 76 return 77 } 78 if buildConfiguration.GetModule() != "" { 79 pythonModule.SetName(buildConfiguration.GetModule()) 80 } 81 var localDependenciesPath string 82 localDependenciesPath, err = config.GetJfrogDependenciesPath() 83 if err != nil { 84 return 85 } 86 pythonModule.SetLocalDependenciesPath(localDependenciesPath) 87 pythonModule.SetUpdateDepsChecksumInfoFunc(pc.UpdateDepsChecksumInfoFunc) 88 89 return errorutils.CheckError(pythonModule.RunInstallAndCollectDependencies(pc.args)) 90 } 91 92 func (pc *PoetryCommand) publish(buildConfiguration *buildUtils.BuildConfiguration, pythonBuildInfo *build.Build) error { 93 publishCmdArgs := append(slices.Clone(pc.args), "-r "+pc.poetryConfigRepoName) 94 // Collect build info by running the jf poetry install cmd 95 pc.args = []string{} 96 err := pc.install(buildConfiguration, pythonBuildInfo) 97 if err != nil { 98 return err 99 } 100 // Run the publish cmd 101 pc.args = publishCmdArgs 102 return gofrogcmd.RunCmd(pc) 103 } 104 105 func (pc *PoetryCommand) UpdateDepsChecksumInfoFunc(dependenciesMap map[string]entities.Dependency, srcPath string) error { 106 servicesManager, err := utils.CreateServiceManager(pc.serverDetails, -1, 0, false) 107 if err != nil { 108 return err 109 } 110 return dependencies.UpdateDepsChecksumInfo(dependenciesMap, srcPath, servicesManager, pc.repository) 111 } 112 113 func (pc *PoetryCommand) SetRepo(repo string) *PoetryCommand { 114 pc.repository = repo 115 return pc 116 } 117 118 func (pc *PoetryCommand) SetArgs(arguments []string) *PoetryCommand { 119 pc.args = arguments 120 return pc 121 } 122 123 func (pc *PoetryCommand) SetCommandName(commandName string) *PoetryCommand { 124 pc.commandName = commandName 125 return pc 126 } 127 128 func (pc *PoetryCommand) SetPypiRepoUrlWithCredentials() error { 129 rtUrl, username, password, err := python.GetPypiRepoUrlWithCredentials(pc.serverDetails, pc.repository, false) 130 if err != nil { 131 return err 132 } 133 if password != "" { 134 return python.ConfigPoetryRepo( 135 rtUrl.Scheme+"://"+rtUrl.Host+rtUrl.Path, 136 username, 137 password, 138 pc.poetryConfigRepoName) 139 } 140 return nil 141 } 142 143 func (pc *PoetryCommand) CommandName() string { 144 return "rt_python_poetry" 145 } 146 147 func (pc *PoetryCommand) SetServerDetails(serverDetails *config.ServerDetails) *PoetryCommand { 148 pc.serverDetails = serverDetails 149 return pc 150 } 151 152 func (pc *PoetryCommand) ServerDetails() (*config.ServerDetails, error) { 153 return pc.serverDetails, nil 154 } 155 156 func (pc *PoetryCommand) GetCmd() *exec.Cmd { 157 var cmd []string 158 cmd = append(cmd, string(pc.pythonTool)) 159 cmd = append(cmd, pc.commandName) 160 cmd = append(cmd, pc.args...) 161 return exec.Command(cmd[0], cmd[1:]...) 162 } 163 164 func (pc *PoetryCommand) GetEnv() map[string]string { 165 return map[string]string{} 166 } 167 168 func (pc *PoetryCommand) GetStdWriter() io.WriteCloser { 169 return nil 170 } 171 172 func (pc *PoetryCommand) GetErrWriter() io.WriteCloser { 173 return nil 174 }