7
8
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/capistrano-virtualenv.rb', line 7
def self.extended(configuration)
configuration.load {
namespace(:virtualenv) {
_cset(:virtualenv_use_system, false) _cset(:virtualenv_script_url, "https://raw.github.com/pypa/virtualenv/master/virtualenv.py")
_cset(:virtualenv_script_file) { File.join(shared_path, "virtualenv", File.basename(URI.parse(virtualenv_script_url).path)) }
_cset(:virtualenv_bootstrap_python, "python") _cset(:virtualenv_cmd) { command }
_cset(:virtualenv_default_options) {
options = %w(--distribute --quiet)
options << "--system-site-packages" if virtualenv_use_system
options
}
_cset(:virtualenv_options) { virtualenv_default_options + fetch(:virtualenv_extra_options, []) }
_cset(:virtualenv_easy_install_options) { logger.info(":virtualenv_easy_install_options has been deprecated.")
%w(--quiet)
}
_cset(:virtualenv_pip_default_options, %w(--quiet))
_cset(:virtualenv_pip_options) { virtualenv_pip_default_options + fetch(:virtualenv_pip_extra_options, []) }
_cset(:virtualenv_pip_install_options, [])
_cset(:virtualenv_requirements, []) _cset(:virtualenv_requirements_file) { File.join(release_path, "requirements.txt") } _cset(:virtualenv_build_requirements, {})
_cset(:virtualenv_shared_path) { File.join(shared_path, "virtualenv", "shared") }
_cset(:virtualenv_shared_bin_path) { File.join(virtualenv_shared_path, "bin") }
_cset(:virtualenv_shared_python) { File.join(virtualenv_shared_bin_path, "python") }
_cset(:virtualenv_shared_easy_install) { logger.info(":virtualenv_shared_easy_install has been deprecated.")
File.join(virtualenv_shared_bin_path, "easy_install")
}
_cset(:virtualenv_shared_easy_install_cmd) { logger.info(":virtualenv_shared_easy_install_cmd has been deprecated.")
[
virtualenv_shared_python.dump,
virtualenv_shared_easy_install.dump,
virtualenv_easy_install_options.map { |x| x.dump }.join(" "),
].join(" ")
}
_cset(:virtualenv_shared_pip) { logger.info(":virtualenv_shared_pip has been deprecated.")
File.join(virtualenv_shared_bin_path, "pip")
}
_cset(:virtualenv_shared_pip_cmd) { logger.info(":virtualenv_shared_pip_cmd has been deprecated.")
[
virtualenv_shared_python.dump,
virtualenv_shared_pip.dump,
virtualenv_pip_options.map { |x| x.dump }.join(" "),
].join(" ")
}
_cset(:virtualenv_release_path) { File.join(release_path, "vendor", "virtualenv") } _cset(:virtualenv_release_bin_path) { File.join(virtualenv_release_path, "bin") }
_cset(:virtualenv_release_python) { File.join(virtualenv_release_bin_path, "python") } _cset(:virtualenv_release_easy_install) { logger.info(":virtualenv_release_easy_install has been deprecated.")
File.join(virtualenv_release_bin_path, "easy_install")
}
_cset(:virtualenv_release_easy_install_cmd) { logger.info(":virtualenv_release_easy_install_cmd has been deprecated.")
[
virtualenv_release_python.dump,
virtualenv_release_easy_install.dump,
virtualenv_easy_install_options.map { |x| x.dump }.join(" "),
].join(" ")
}
_cset(:virtualenv_release_pip) { logger.info(":virtualenv_release_pip has been deprecated.")
File.join(virtualenv_release_bin_path, "pip")
}
_cset(:virtualenv_release_pip_cmd) { logger.info(":virtualenv_release_pip_cmd has been deprecated.")
[
virtualenv_release_python.dump,
virtualenv_release_pip.dump,
virtualenv_pip_options.map { |x| x.dump }.join(" "),
].flatten.join(" ")
}
_cset(:virtualenv_current_path) { File.join(current_path, "vendor", "virtualenv") }
_cset(:virtualenv_current_bin_path) { File.join(virtualenv_current_path, "bin") }
_cset(:virtualenv_current_python) { File.join(virtualenv_current_bin_path, "python") }
_cset(:virtualenv_current_easy_install) { logger.info(":virtualenv_current_easy_install has been deprecated.")
File.join(virtualenv_current_bin_path, "easy_install")
}
_cset(:virtualenv_current_easy_install_cmd) { logger.info(":virtualenv_current_easy_install_cmd has been deprecated.")
[
virtualenv_current_python.dump,
virtualenv_current_easy_install.dump,
virtualenv_easy_install_options.map { |x| x.dump }.join(" "),
].join(" ")
}
_cset(:virtualenv_current_pip) {
logger.info(":virtualenv_current_pip has been deprecated.")
File.join(virtualenv_current_path, "bin", "pip")
}
_cset(:virtualenv_current_pip_cmd) {
logger.info(":virtualenv_current_pip_cmd has been deprecated.")
[
virtualenv_current_python.dump,
virtualenv_current_pip.dump,
virtualenv_pip_options.map { |x| x.dump }.join(" "),
].flatten.join(" ")
}
desc("Setup virtualenv.")
task(:setup, :except => { :no_release => true }) {
transaction {
dependencies if fetch(:virtualenv_setup_dependencies, true)
install
create_shared
}
}
after "deploy:setup", "virtualenv:setup"
desc("Install virtualenv.")
task(:install, :except => { :no_release => true }) {
run("mkdir -p #{File.dirname(virtualenv_script_file).dump}")
run("test -f #{virtualenv_script_file.dump} || wget --no-verbose -O #{virtualenv_script_file.dump} #{virtualenv_script_url.dump}")
}
_cset(:virtualenv_install_packages, %w(python rsync))
task(:dependencies, :except => { :no_release => true }) {
platform.packages.install(virtualenv_install_packages)
}
desc("Uninstall virtualenv.")
task(:uninstall, :except => { :no_release => true }) {
run("rm -f #{virtualenv_script_file.dump}")
}
def command(options={})
[
virtualenv_bootstrap_python,
virtualenv_script_file.dump,
virtualenv_options.map { |x| x.dump }.join(" "),
].join(" ")
end
def create(destination, options={})
execute = []
execute << "mkdir -p #{File.dirname(destination).dump}"
execute << "( test -d #{destination.dump} || #{command(options)} #{destination.dump} )"
invoke_command(execute.join(" && "), options)
end
def destroy(destination, options={})
invoke_command("rm -rf #{destination.dump}", options)
end
task(:create_shared, :except => { :no_release => true }) {
virtualenv.create(virtualenv_shared_path)
}
task(:destroy_shared, :except => { :no_release => true }) {
virtualenv.destroy(virtualenv_shared_path)
}
desc("Update virtualenv for project.")
task(:update, :except => { :no_release => true }) {
transaction {
update_shared
create_release
}
}
after "deploy:finalize_update", "virtualenv:update"
task(:update_shared, :except => { :no_release => true }) {
top.put(virtualenv_requirements.join("\n"), virtualenv_requirements_file) unless virtualenv_requirements.empty?
run("touch #{virtualenv_requirements_file.dump}")
pip_options = ( virtualenv_pip_options + virtualenv_pip_install_options ).map { |x| x.dump }.join(" ")
virtualenv.exec("pip install #{pip_options} -r #{virtualenv_requirements_file.dump}",
:virtualenv => virtualenv_shared_path)
virtualenv_build_requirements.each do |package, options|
options ||= []
virtualenv.exec("pip install #{pip_options} #{options.map { |x| x.dump }.join(" ")} #{package.dump}",
:virtualenv => virtualenv_shared_path)
end
}
def relocate(source, destination, options={})
execute = []
execute << "mkdir -p #{File.dirname(destination).dump}"
if fetch(:virtualenv_use_relocatable, false)
execute << %{#{command(options)} --relocatable #{source.dump}}
execute << %{cp -RPp #{source.dump} #{destination.dump}}
else
execute << %{( test -d #{destination.dump} || #{command(options)} #{destination.dump} )}
execute << %{rsync -lrpt #{File.join(source, "bin/").dump} #{File.join(destination, "bin/").dump}}
execute << %{sed -i -e 's|^#!#{source}/bin/python.*$|#!#{destination}/bin/python|' #{destination}/bin/*}
execute << %{rsync -lrpt #{File.join(source, "lib/").dump} #{File.join(destination, "lib/").dump}}
end
invoke_command(execute.join(" && "), options)
end
task(:create_release, :except => { :no_release => true }) {
virtualenv.relocate(virtualenv_shared_path, virtualenv_release_path)
}
def exec(cmdline, options={})
options = options.dup
virtualenv = ( options.delete(:virtualenv) || virtualenv_shared_path )
options[:env] = options.fetch(:env, {}).merge("PATH" => [ File.join(virtualenv, "bin"), "$PATH" ].join(":"))
invoke_command(cmdline, options)
end
}
}
end
|