github.com/robryk/drone@v0.2.1-0.20140602202253-40fe4305815d/pkg/plugin/publish/pypi.go (about)

     1  package publish
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/drone/drone/pkg/build/buildfile"
     7  )
     8  
     9  // set up the .pypirc file
    10  var pypirc = `
    11  cat <<EOF > $HOME/.pypirc
    12  [distutils]
    13  index-servers = 
    14      %s
    15  
    16  [%s]
    17  username:%s
    18  password:%s
    19  %s
    20  EOF`
    21  
    22  var deployCmd = `
    23  if [ -n "$_PYPI_SETUP_PY" ]
    24  then
    25      python $_PYPI_SETUP_PY sdist %s upload -r %s
    26      if [ $? -ne 0 ]
    27      then
    28          echo "Deploy to PyPI failed - perhaps due to the version number not being incremented. Continuing..."
    29      fi
    30  else
    31      echo "Failed to find setup.py file"
    32  fi
    33  `
    34  
    35  type PyPI struct {
    36  	Username   string   `yaml:"username,omitempty"`
    37  	Password   string   `yaml:"password,omitempty"`
    38  	Formats    []string `yaml:"formats,omitempty"`
    39  	Repository string   `yaml:"repository,omitempty"`
    40  	Branch     string   `yaml:"branch,omitempty"`
    41  }
    42  
    43  func (p *PyPI) Write(f *buildfile.Buildfile) {
    44  	var indexServer string
    45  	var repository string
    46  
    47  	if len(p.Username) == 0 || len(p.Password) == 0 {
    48  		// nothing to do if the config is fundamentally flawed
    49  		return
    50  	}
    51  
    52  	// Handle the setting a custom pypi server/repository
    53  	if len(p.Repository) == 0 {
    54  		indexServer = "pypi"
    55  		repository = ""
    56  	} else {
    57  		indexServer = "custom"
    58  		repository = fmt.Sprintf("repository:%s", p.Repository)
    59  	}
    60  
    61  	f.WriteCmdSilent("echo 'publishing to PyPI...'")
    62  
    63  	// find the setup.py file
    64  	f.WriteCmdSilent("_PYPI_SETUP_PY=$(find . -name 'setup.py')")
    65  
    66  	// build the .pypirc file that pypi expects
    67  	f.WriteCmdSilent(fmt.Sprintf(pypirc, indexServer, indexServer, p.Username, p.Password, repository))
    68  	formatStr := p.BuildFormatStr()
    69  
    70  	// if we found the setup.py file use it to deploy
    71  	f.WriteCmdSilent(fmt.Sprintf(deployCmd, formatStr, indexServer))
    72  }
    73  
    74  func (p *PyPI) BuildFormatStr() string {
    75  	if len(p.Formats) == 0 {
    76  		// the format parameter is optional - if it's not here,
    77  		// omit the format string completely.
    78  		return ""
    79  	}
    80  	fmtStr := "--formats "
    81  	for i := range p.Formats {
    82  		fmtStr += p.Formats[i] + ","
    83  	}
    84  	return fmtStr[:len(fmtStr)-1]
    85  }