Class: DPL::Provider::PyPI

Inherits:
DPL::Provider show all
Defined in:
lib/dpl/provider/pypi.rb

Constant Summary collapse

DEFAULT_SERVER =
false
PYPIRC_FILE =
'~/.pypirc'

Instance Method Summary collapse

Instance Method Details

#check_appObject


122
123
# File 'lib/dpl/provider/pypi.rb', line 122

def check_app
end

#check_authObject


115
116
117
118
119
120
# File 'lib/dpl/provider/pypi.rb', line 115

def check_auth
  error "missing PyPI username" unless pypi_user
  error "missing PyPI password" unless pypi_password
  write_config
  log "Authenticated as #{pypi_user}"
end

#configObject


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dpl/provider/pypi.rb', line 78

def config
  servers = {
      'pypi' => [
                  "username: #{pypi_user}",
                  "password: #{pypi_password}",
                ]
    }
  if pypi_server
    servers['pypi'].insert(0, "repository: #{pypi_server}")
  end
  {
    :header => '[distutils]',
    :servers_line => 'index-servers = pypi',
    :servers => servers
  }
end

#install_deploy_dependenciesObject


68
69
70
71
72
73
74
75
76
# File 'lib/dpl/provider/pypi.rb', line 68

def install_deploy_dependencies
  # --user likely fails inside virtualenvs but is needed outside to avoid needing sudo.
  # `--upgrade-strategy eager` prevents the old resolver from keeping an outdated dependency if it's preinstalled
  unless context.shell "if [ -z ${VIRTUAL_ENV+x} ]; then export PIP_USER=yes; fi && " \
                       "wget -nv -O - https://bootstrap.pypa.io/get-pip.py | python - --no-setuptools --no-wheel && " \
                       "pip install --upgrade --upgrade-strategy eager #{pypi_setuptools_arg} #{pypi_twine_arg} #{pypi_wheel_arg}"
    error "Couldn't install pip, setuptools, twine or wheel."
  end
end

#needs_key?Boolean

Returns:

  • (Boolean)

125
126
127
# File 'lib/dpl/provider/pypi.rb', line 125

def needs_key?
  false
end

#push_appObject


129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dpl/provider/pypi.rb', line 129

def push_app
  context.shell "python setup.py #{pypi_distributions}"
  unless context.shell "twine upload#{pypi_skip_existing_option} -r pypi dist/*"
    error 'PyPI upload failed.'
  end
  context.shell "rm -rf dist/*"
  unless skip_upload_docs?
    log "Uploading documentation (skip with \"skip_upload_docs: true\")"
    context.shell "python setup.py upload_docs #{pypi_docs_dir_option} -r #{pypi_server}"
  end
end

#pypi_distributionsObject


19
20
21
# File 'lib/dpl/provider/pypi.rb', line 19

def pypi_distributions
  options[:distributions] || context.env['PYPI_DISTRIBUTIONS'] || 'sdist'
end

#pypi_docs_dir_optionObject


23
24
25
26
27
28
# File 'lib/dpl/provider/pypi.rb', line 23

def pypi_docs_dir_option
  docs_dir = options[:docs_dir] || context.env['PYPI_DOCS_DIR'] || ''
  if !docs_dir.empty?
    '--upload-dir ' + docs_dir
  end
end

#pypi_passwordObject


11
12
13
# File 'lib/dpl/provider/pypi.rb', line 11

def pypi_password
  options[:password] || context.env['PYPI_PASSWORD']
end

#pypi_serverObject


15
16
17
# File 'lib/dpl/provider/pypi.rb', line 15

def pypi_server
  options[:server] || context.env['PYPI_SERVER'] || DEFAULT_SERVER
end

#pypi_setuptools_argObject


41
42
43
44
45
46
47
48
# File 'lib/dpl/provider/pypi.rb', line 41

def pypi_setuptools_arg
  setuptools_version = options[:setuptools_version] || context.env['SETUPTOOLS_VERSION'] || ''
  if setuptools_version[/\A\d+(?:\.\d+)*\z/]
    'setuptools==' + setuptools_version
  else
    'setuptools'
  end
end

#pypi_skip_existing_optionObject


35
36
37
38
39
# File 'lib/dpl/provider/pypi.rb', line 35

def pypi_skip_existing_option
    if options.fetch(:skip_existing, false)
      ' --skip-existing'
    end
end

#pypi_twine_argObject


50
51
52
53
54
55
56
57
# File 'lib/dpl/provider/pypi.rb', line 50

def pypi_twine_arg
  twine_version = options[:twine_version] || context.env['TWINE_VERSION'] || ''
  if twine_version[/\A\d+(?:\.\d+)*\z/]
    'twine==' + twine_version
  else
    'twine'
  end
end

#pypi_userObject


7
8
9
# File 'lib/dpl/provider/pypi.rb', line 7

def pypi_user
  option(:username, :user) || context.env['PYPI_USER'] || context.env['PYPI_USERNAME']
end

#pypi_wheel_argObject


59
60
61
62
63
64
65
66
# File 'lib/dpl/provider/pypi.rb', line 59

def pypi_wheel_arg
  wheel_version = options[:wheel_version] || context.env['WHEEL_VERSION'] || ''
  if wheel_version[/\A\d+(?:\.\d+)*\z/]
    'wheel==' + wheel_version
  else
    'wheel'
  end
end

#skip_upload_docs?Boolean

Returns:

  • (Boolean)

30
31
32
33
# File 'lib/dpl/provider/pypi.rb', line 30

def skip_upload_docs?
  ! options.has_key?(:skip_upload_docs) ||
    (options.has_key?(:skip_upload_docs) && options[:skip_upload_docs])
end

#write_configObject


106
107
108
109
110
111
112
113
# File 'lib/dpl/provider/pypi.rb', line 106

def write_config
  File.open(File.expand_path(PYPIRC_FILE), 'w') do |f|
    config.each do |key, val|
      f.puts(val) if val.is_a? String or val.is_a? Array
    end
    write_servers(f)
  end
end

#write_servers(f) ⇒ Object


95
96
97
98
99
100
101
102
103
104
# File 'lib/dpl/provider/pypi.rb', line 95

def write_servers(f)
  config[:servers].each do |key, val|
    f.puts " " * 4 + key
  end

  config[:servers].each do |key, val|
    f.puts "[#{key}]"
    f.puts val
  end
end