Class: CapsuleCD::Python::PythonEngine

Inherits:
Engine
  • Object
show all
Defined in:
lib/capsulecd/python/python_engine.rb

Instance Attribute Summary

Attributes inherited from Engine

#config

Instance Method Summary collapse

Methods inherited from Engine

#initialize, #post_build_step, #post_package_step, #post_release_step, #post_runner_retrieve_payload, #post_source_configure, #post_source_process_pull_request_payload, #post_source_process_push_payload, #post_source_release, #post_test_step, #pre_build_step, #pre_package_step, #pre_release_step, #pre_runner_retrieve_payload, #pre_source_configure, #pre_source_process_pull_request_payload, #pre_source_process_push_payload, #pre_source_release, #pre_test_step, #start

Constructor Details

This class inherits a constructor from CapsuleCD::Engine

Instance Method Details

#build_stepObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/capsulecd/python/python_engine.rb', line 9

def build_step
  super
  unless File.exist?(@source_git_local_path + '/setup.py')
    fail CapsuleCD::Error::BuildPackageInvalid, 'setup.py file is required to process Python package'
  end

  # check for/create required VERSION file
  unless File.exist?(@source_git_local_path + '/VERSION')
    File.open(@source_git_local_path + '/VERSION', 'w') { |file| file.write('0.0.0') }
  end

  # bump up the version here.
  # since there's no standardized way to bump up the version in the setup.py file, we're going to assume that the version
  # is specified in a VERSION file in the root of the source repository
  # this is option #4 in the python packaging guide:
  # https://packaging.python.org/en/latest/single_source_version/#single-sourcing-the-version
  #
  # additional packaging structures, like those listed below, may also be supported in the future.
  # http://stackoverflow.com/a/7071358/1157633

  version = File.read(@source_git_local_path + '/VERSION').strip
  next_version = bump_version(SemVer.parse(version))
  File.open(@source_git_local_path + '/VERSION', 'w') do |file|
    file.write(next_version)
  end

  # make sure the package testing manager is available.
  # there is a standardized way to test packages (python setup.py tests), however for automation tox is preferred
  # because of virtualenv and its support for multiple interpreters.
  unless File.exist?(@source_git_local_path + '/tox.ini')
    # if a tox.ini file is not present, we'll create a default one and specify 'python setup.py test' as the test
    # runner command, and requirements.txt as the dependencies for this package.
    File.open(@source_git_local_path + '/tox.ini', 'w') { |file|
      file.write(<<-TOX.gsub(/^\s+/, '')
# Tox (http://tox.testrun.org/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.

[tox]
envlist = py27
usedevelop = True

[testenv]
commands = python setup.py test
deps =
  -rrequirements.txt
TOX
      )
    }
  end

  # check for/create any required missing folders/files
  unless File.exist?(@source_git_local_path + '/requirements.txt')
    File.open(@source_git_local_path + '/requirements.txt', 'w') { |file| file.write('') }
  end

  unless File.exist?(@source_git_local_path + '/tests')
    FileUtils.mkdir(@source_git_local_path + '/tests')
  end
  unless File.exist?(@source_git_local_path + '/tests/__init__.py')
    File.open(@source_git_local_path + '/tests/__init__.py', 'w') { |file| file.write('') }
  end
  unless File.exist?(@source_git_local_path + '/.gitignore')
    CapsuleCD::GitUtils.create_gitignore(@source_git_local_path, ['Python'])
  end
end

#package_stepObject

run npm publish



103
104
105
106
107
108
109
110
111
112
# File 'lib/capsulecd/python/python_engine.rb', line 103

def package_step
  super

  # commit changes to the cookbook. (test run occurs before this, and it should clean up any instrumentation files, created,
  # as they will be included in the commmit and any release artifacts)
  version = File.read(@source_git_local_path + '/VERSION').strip
  next_version = SemVer.parse(version)
  CapsuleCD::GitUtils.commit(@source_git_local_path, "(v#{next_version}) Automated packaging of release by CapsuleCD")
  @source_release_commit = CapsuleCD::GitUtils.tag(@source_git_local_path, "v#{next_version}")
end

#release_stepObject

this step should push the release to the package repository (ie. npm, chef supermarket, rubygems)



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/capsulecd/python/python_engine.rb', line 115

def release_step
  super
  pypirc_path = File.expand_path('~/.pypirc')

  unless @config.pypi_username || @config.pypi_password
    fail CapsuleCD::Error::ReleaseCredentialsMissing, 'cannot deploy package to pip, credentials missing'
  end

  # write the knife.rb config file.
  File.open(pypirc_path, 'w+') do |file|
    file.write(<<-EOT.gsub(/^\s+/, '')
      [distutils]
      index-servers=pypi

      [pypi]
      repository = https://upload.pypi.org/legacy/
      username = #{@config.pypi_username}
      password = #{@config.pypi_password}
    EOT
              )
  end

  # run python setup.py sdist
  Open3.popen3('python setup.py sdist', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
    { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
      Thread.new do
        until (line = stream_buffer.gets).nil?
          puts "#{name} -> #{line}"
        end
      end
    end
    # wait for process
    external.join
    unless external.value.success?
      fail CapsuleCD::Error::ReleasePackageError, 'python setup.py sdist failed'
    end
  end

  #using twine instead (it supports HTTPS.)https://python-packaging-user-guide.readthedocs.org/en/latest/distributing/#uploading-your-project-to-pypi
  Open3.popen3("twine upload --config-file #{pypirc_path}  dist/*", chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
    { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
      Thread.new do
        until (line = stream_buffer.gets).nil?
          puts "#{name} -> #{line}"
        end
      end
    end
    # wait for process
    external.join
    unless external.value.success?
      fail CapsuleCD::Error::ReleasePackageError, 'twine package upload failed. Check log for exact error'
    end
  end
end

#test_stepObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/capsulecd/python/python_engine.rb', line 77

def test_step
  super


  # download the package dependencies and register it in the virtualenv using tox (which will do pip install -e .)
  # https://packaging.python.org/en/latest/distributing/
  # once that's done, tox will run tests
  # run test command
  test_cmd = @config.engine_cmd_test || 'tox'
  Open3.popen3(test_cmd, chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
    { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
      Thread.new do
        until (line = stream_buffer.gets).nil?
          puts "#{name} -> #{line}"
        end
      end
    end
    # wait for process
    external.join
    unless external.value.success?
      fail CapsuleCD::Error::TestDependenciesError, test_cmd + ' failed to test package.'
    end
  end unless @config.engine_disable_test
end