capistrano-pyenv 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -37,6 +37,8 @@ Following options are available to manage your pyenv.
37
37
  * `:pyenv_use_virtualenv` - create new virtualenv from `:pyenv_virtualenv_python_version`. `false` by default. `:pyenv_python_version` will be treated as the name of the virtualenv if this is turned `true`.
38
38
  * `:pyenv_virtualenv_python_version` - the python version to create virtualenv. `2.7.3` by default.
39
39
  * `:pyenv_virtualenv_options` - command-line options for virtualenv.
40
+ * `:pyenv_install_dependencies` - controls whether installing dependencies or not. `true` by default.
41
+ * `:pyenv_define_default_environment` - define `PYENV_ROOT` and update `PATH` to use pyenv over capistrano. `true` by default.
40
42
 
41
43
  ## Contributing
42
44
 
@@ -7,13 +7,17 @@ module Capistrano
7
7
  def self.extended(configuration)
8
8
  configuration.load {
9
9
  namespace(:pyenv) {
10
+ _cset(:pyenv_root, "$HOME/.pyenv")
10
11
  _cset(:pyenv_path) {
11
- capture("echo $HOME/.pyenv").chomp()
12
+ # expand to actual path to use this value since pyenv may be executed by users other than `:user`.
13
+ capture("echo #{pyenv_root.dump}").strip
12
14
  }
15
+ _cset(:pyenv_bin_path) { File.join(pyenv_path, "bin") }
16
+ _cset(:pyenv_shims_path) { File.join(pyenv_path, "shims") }
13
17
  _cset(:pyenv_bin) {
14
- File.join(pyenv_path, 'bin', 'pyenv')
18
+ File.join(pyenv_bin_path, "pyenv")
15
19
  }
16
- _cset(:pyenv_cmd) { # to use custom pyenv_path, we use `env` instead of cap's default_environment.
20
+ _cset(:pyenv_cmd) {
17
21
  "env PYENV_VERSION=#{pyenv_python_version.dump} #{pyenv_bin}"
18
22
  }
19
23
  _cset(:pyenv_repository, 'git://github.com/yyuu/pyenv.git')
@@ -32,9 +36,11 @@ module Capistrano
32
36
  _cset(:pyenv_virtualenv_python_version, '2.7.3')
33
37
  _cset(:pyenv_virtualenv_options, %w(--distribute --quiet --system-site-packages))
34
38
 
39
+ _cset(:pyenv_install_dependencies, true)
40
+
35
41
  desc("Setup pyenv.")
36
42
  task(:setup, :except => { :no_release => true }) {
37
- dependencies
43
+ dependencies if pyenv_install_dependencies
38
44
  update
39
45
  configure
40
46
  build
@@ -78,9 +84,29 @@ module Capistrano
78
84
  plugins.update
79
85
  }
80
86
 
87
+ def setup_default_environment
88
+ env = fetch(:default_environment, {}).dup
89
+ env["PYENV_ROOT"] = pyenv_path
90
+ env["PATH"] = [ pyenv_shims_path, pyenv_bin_path, env.fetch("PATH", "$PATH") ].join(":")
91
+ set(:default_environment, env)
92
+ end
93
+
94
+ _cset(:pyenv_define_default_environment, true)
95
+ # workaround for `multistage` of capistrano-ext.
96
+ # https://github.com/yyuu/capistrano-rbenv/pull/5
97
+ if top.namespaces.key?(:multistage)
98
+ after "multistage:ensure" do
99
+ setup_default_environment if pyenv_define_default_environment
100
+ end
101
+ else
102
+ on :start do
103
+ setup_default_environment if pyenv_define_default_environment
104
+ end
105
+ end
106
+
81
107
  desc("Purge pyenv.")
82
108
  task(:purge, :except => { :no_release => true }) {
83
- run("rm -rf #{pyenv_path}")
109
+ run("rm -rf #{pyenv_path.dump}")
84
110
  }
85
111
 
86
112
  namespace(:plugins) {
@@ -118,35 +144,49 @@ module Capistrano
118
144
  _cset(:pyenv_configure_script) {
119
145
  (<<-EOS).gsub(/^\s*/, '')
120
146
  # Configured by capistrano-pyenv. Do not edit directly.
121
- export PATH="#{pyenv_path}/bin:$PATH"
147
+ export PATH=#{[ pyenv_bin_path, "$PATH"].join(":").dump}
122
148
  eval "$(pyenv init -)"
123
149
  EOS
124
150
  }
151
+
152
+ def _update_config(script_file, file, tempfile)
153
+ execute = []
154
+ ## (1) ensure copy source file exists
155
+ execute << "( test -f #{file.dump} || touch #{file.dump} )"
156
+ ## (2) copy originao config to temporary file
157
+ execute << "rm -f #{tempfile.dump}" # remove tempfile to preserve permissions of original file
158
+ execute << "cp -fp #{file.dump} #{tempfile.dump}"
159
+ ## (3) modify temporary file
160
+ execute << "sed -i -e '/^#{Regexp.escape(pyenv_configure_signature)}/,/^#{Regexp.escape(pyenv_configure_signature)}/d' #{tempfile.dump}"
161
+ execute << "echo #{pyenv_configure_signature.dump} >> #{tempfile.dump}"
162
+ execute << "cat #{script_file.dump} >> #{tempfile.dump}"
163
+ execute << "echo #{pyenv_configure_signature.dump} >> #{tempfile.dump}"
164
+ ## (4) update config only if it is needed
165
+ execute << "cp -fp #{file.dump} #{(file + ".orig").dump}"
166
+ execute << "( diff -u #{file.dump} #{tempfile.dump} || mv -f #{tempfile.dump} #{file.dump} )"
167
+ run(execute.join(" && "))
168
+ end
169
+
170
+ def update_config(script_file, file)
171
+ begin
172
+ tempfile = capture("mktemp /tmp/pyenv.XXXXXXXXXX").strip
173
+ _update_config(script_file, file, tempfile)
174
+ ensure
175
+ run("rm -f #{tempfile.dump}") rescue nil
176
+ end
177
+ end
178
+
125
179
  _cset(:pyenv_configure_signature, '##pyenv:configure')
126
180
  task(:configure, :except => { :no_release => true }) {
127
181
  if fetch(:pyenv_use_configure, true)
128
- script = File.join('/tmp', "pyenv.#{$$}")
129
- config = [ pyenv_configure_files ].flatten
130
- config_map = Hash[ config.map { |f| [f, File.join('/tmp', "#{File.basename(f)}.#{$$}")] } ]
131
182
  begin
132
- execute = []
133
- put(pyenv_configure_script, script)
134
- config_map.each { |file, temp|
135
- ## (1) copy original config to temporaly file and then modify
136
- execute << "( test -f #{file} || touch #{file} )"
137
- execute << "cp -fp #{file} #{temp}"
138
- execute << "sed -i -e '/^#{Regexp.escape(pyenv_configure_signature)}/,/^#{Regexp.escape(pyenv_configure_signature)}/d' #{temp}"
139
- execute << "echo #{pyenv_configure_signature.dump} >> #{temp}"
140
- execute << "cat #{script} >> #{temp}"
141
- execute << "echo #{pyenv_configure_signature.dump} >> #{temp}"
142
- ## (2) update config only if it is needed
143
- execute << "cp -fp #{file} #{file}.orig"
144
- execute << "( diff -u #{file} #{temp} || mv -f #{temp} #{file} )"
145
- }
146
- run(execute.join(' && '))
183
+ script_file = capture("mktemp /tmp/pyenv.XXXXXXXXXX").strip
184
+ top.put(pyenv_configure_script, script_file)
185
+ [ pyenv_configure_files ].flatten.each do |file|
186
+ update_config(script_file, file)
187
+ end
147
188
  ensure
148
- remove = [ script ] + config_map.values
149
- run("rm -f #{remove.join(' ')}") rescue nil
189
+ run("rm -f #{script_file.dump}") rescue nil
150
190
  end
151
191
  end
152
192
  }
@@ -219,6 +259,11 @@ module Capistrano
219
259
 
220
260
  run("#{pyenv_cmd} exec #{python} --version && #{pyenv_cmd} global #{pyenv_python_version}")
221
261
  }
262
+
263
+ # call `pyenv rehash` to update shims.
264
+ def rehash()
265
+ run("#{pyenv_cmd} rehash")
266
+ end
222
267
  }
223
268
  }
224
269
  end
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module PyEnv
3
- VERSION = "0.0.10"
3
+ VERSION = "0.0.11"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-pyenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-28 00:00:00.000000000 Z
12
+ date: 2013-03-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capistrano