Class: Mortar::Local::Python
Constant Summary
collapse
- PYTHON_OSX_TGZ_NAME =
"mortar-python-osx.tgz"
- PYTHON_OSX_TGZ_DEFAULT_URL_PATH =
"resource/python_osx"
- PYPI_URL_PATH =
"resource/mortar_pypi"
- MORTAR_PYTHON_PACKAGES =
["luigi", "mortar-luigi", "stillson"]
Instance Method Summary
collapse
Methods included from Params
#automatic_parameters, #merge_parameters
#download_file, #ensure_mortar_local_directory, #extract_tgz, #get_resource, #gitignore_template_path, #head_resource, #http_date_to_epoch, #install_date, #install_file_for, #is_newer_version, #jython_cache_directory, #jython_directory, #local_install_directory, #local_install_directory_name, #local_log_dir, #local_project_gitignore, #local_udf_log_dir, #make_call, #make_call_sleep_seconds, #note_install, #osx?, #project_root, #render_script_template, #reset_local_logs, #run_templated_script, #unset_hadoop_env_vars, #url_date
Methods included from Helpers
#action, #ask, #confirm, #copy_if_not_present_at_dest, #default_host, #deprecate, #display, #display_header, #display_object, #display_row, #display_table, #display_with_indent, #download_to_file, #ensure_dir_exists, #error, error_with_failure, error_with_failure=, extended, extended_into, #format_bytes, #format_date, #format_with_bang, #get_terminal_environment, #home_directory, #host, #hprint, #hputs, included, included_into, #installed_with_omnibus?, #json_decode, #json_encode, #line_formatter, #longest, #output_with_bang, #pending_github_team_state_message, #quantify, #redisplay, #retry_on_exception, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #test_name, #ticking, #time_ago, #truncate, #warning, #with_tty, #write_to_file
Instance Method Details
#candidates ⇒ Object
99
100
101
|
# File 'lib/mortar/local/python.rb', line 99
def candidates
@candidate_pythons.dup
end
|
#check_or_install ⇒ Object
Execute either an installation of python or an inspection of the local system to see if a usable python is available
39
40
41
42
43
44
45
46
47
|
# File 'lib/mortar/local/python.rb', line 39
def check_or_install
if osx?
install_or_update_osx
else
check_system_python
end
end
|
#check_pythons_for_virtenv ⇒ Object
Inspects the list of found python installations and checks if they have virtualenv installed. The first one found will be used.
112
113
114
115
116
117
118
119
120
|
# File 'lib/mortar/local/python.rb', line 112
def check_pythons_for_virtenv
@candidate_pythons.each{ |py|
if has_virtualenv_installed(py)
@command = py
return true
end
}
return false
end
|
#check_system_python ⇒ Object
Checks if there is a usable versionpython already installed
104
105
106
107
|
# File 'lib/mortar/local/python.rb', line 104
def check_system_python
@candidate_pythons = lookup_local_pythons
return 0 != @candidate_pythons.length
end
|
#check_virtualenv ⇒ Object
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/mortar/local/python.rb', line 49
def check_virtualenv
if osx?
return true
else
return check_pythons_for_virtenv
end
end
|
#desired_python_minor_version ⇒ Object
146
147
148
|
# File 'lib/mortar/local/python.rb', line 146
def desired_python_minor_version
return "2.7"
end
|
#has_python_requirements ⇒ Object
154
155
156
|
# File 'lib/mortar/local/python.rb', line 154
def has_python_requirements
return File.exists?(pip_requirements_path)
end
|
#has_valid_virtualenv? ⇒ Boolean
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/mortar/local/python.rb', line 172
def has_valid_virtualenv?
output = `#{@command} -m virtualenv #{python_env_dir} 2>&1`
if 0 != $?.to_i
File.open(virtualenv_error_log_path, 'w') { |f|
f.write(output)
}
return false
end
return true
end
|
#has_virtualenv_installed(python) ⇒ Object
Checks if the specified python command has virtualenv installed
124
125
126
127
128
129
130
131
|
# File 'lib/mortar/local/python.rb', line 124
def has_virtualenv_installed(python)
`#{python} -m virtualenv --help 2>&1`
if (0 != $?.to_i)
false
else
true
end
end
|
#install_mortar_python_package(package_name) ⇒ Object
313
314
315
316
317
318
319
|
# File 'lib/mortar/local/python.rb', line 313
def install_mortar_python_package(package_name)
unless pip_install mortar_package_url(package_name)
return false
end
ensure_mortar_local_directory mortar_package_dir(package_name)
note_install mortar_package_dir(package_name)
end
|
#install_or_update_osx ⇒ Object
Performs an installation of python specific to this project, this install includes pip and virtualenv
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/mortar/local/python.rb', line 66
def install_or_update_osx
@command = "#{local_install_directory}/python/bin/python"
if should_do_python_install?
action "Installing python to #{local_install_directory_name}" do
install_osx
end
elsif should_do_update?
action "Updating to latest python in #{local_install_directory_name}" do
install_osx
end
end
true
end
|
#install_osx ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/mortar/local/python.rb', line 80
def install_osx
FileUtils.mkdir_p(local_install_directory)
python_tgz_path = File.join(local_install_directory, PYTHON_OSX_TGZ_NAME)
download_file(python_archive_url, python_tgz_path)
(python_tgz_path, local_install_directory)
FileUtils.chmod(0755, @command)
File.delete(python_tgz_path)
note_install("python")
end
|
#install_python_dependencies ⇒ Object
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/mortar/local/python.rb', line 264
def install_python_dependencies
action "Installing python dependencies to #{local_install_directory_name}" do
ensure_mortar_local_directory mortar_packages_dir
MORTAR_PYTHON_PACKAGES.each{ |package_name|
unless install_mortar_python_package(package_name)
return false
end
}
end
return true
end
|
#install_user_python_dependencies ⇒ Object
305
306
307
|
# File 'lib/mortar/local/python.rb', line 305
def install_user_python_dependencies
return run_pip_command "install --requirement #{pip_requirements_path}"
end
|
#local_activate_path ⇒ Object
276
277
278
|
# File 'lib/mortar/local/python.rb', line 276
def local_activate_path
return "#{python_env_dir}/bin/activate"
end
|
#local_pip_bin ⇒ Object
284
285
286
|
# File 'lib/mortar/local/python.rb', line 284
def local_pip_bin
return "#{python_env_dir}/bin/pip"
end
|
#local_python_bin ⇒ Object
280
281
282
|
# File 'lib/mortar/local/python.rb', line 280
def local_python_bin
return "#{python_env_dir}/bin/python"
end
|
#lookup_local_pythons ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/mortar/local/python.rb', line 133
def lookup_local_pythons
found_bins = []
[ "python#{desired_python_minor_version}", "python" ].each{ |cmd|
path_to_python = `which #{cmd}`.to_s.strip
if path_to_python != ''
found_bins << path_to_python
end
}
return found_bins
end
|
#luigi_command_template_parameters(luigi_script, user_script_args) ⇒ Object
352
353
354
355
356
357
358
359
360
361
362
363
|
# File 'lib/mortar/local/python.rb', line 352
def luigi_command_template_parameters(luigi_script, user_script_args)
script_args = [
"--local-scheduler",
"--logging-conf-file #{luigi_logging_config_file_path}",
user_script_args.join(" "),
]
return {
:python_arugments => "",
:python_script => luigi_script.executable_path(),
:script_arguments => script_args.join(" ")
}
end
|
#luigi_logging_config_file_path ⇒ Object
348
349
350
|
# File 'lib/mortar/local/python.rb', line 348
def luigi_logging_config_file_path
File.expand_path("../../conf/luigi/logging.ini", __FILE__)
end
|
#mortar_package_dir(package) ⇒ Object
251
252
253
|
# File 'lib/mortar/local/python.rb', line 251
def mortar_package_dir(package)
package_dir = "#{mortar_packages_dir}/#{package}"
end
|
#mortar_package_url(package) ⇒ Object
237
238
239
240
241
|
# File 'lib/mortar/local/python.rb', line 237
def mortar_package_url(package)
full_host = (host =~ /^http/) ? host : "https://api.#{host}"
default_url = full_host + "/" + PYPI_URL_PATH
"#{ENV.fetch('MORTAR_PACKAGE_URL', default_url)}/#{package}"
end
|
#mortar_packages_dir ⇒ Object
247
248
249
|
# File 'lib/mortar/local/python.rb', line 247
def mortar_packages_dir
return "pythonenv/mortar-packages"
end
|
#pip_error_log_path ⇒ Object
205
206
207
|
# File 'lib/mortar/local/python.rb', line 205
def pip_error_log_path
return ENV.fetch('PIP_ERROR_LOG', "#{local_install_directory}/pip_dependency_install.log")
end
|
#pip_install(package_url) ⇒ Object
309
310
311
|
# File 'lib/mortar/local/python.rb', line 309
def pip_install package_url
return run_pip_command "install #{package_url} --use-mirrors;"
end
|
#pip_requirements_path ⇒ Object
150
151
152
|
# File 'lib/mortar/local/python.rb', line 150
def pip_requirements_path
return ENV.fetch('PIP_REQ_FILE', File.join(Dir.getwd, "requirements.txt"))
end
|
#python_archive_url ⇒ Object
166
167
168
169
170
|
# File 'lib/mortar/local/python.rb', line 166
def python_archive_url
full_host = (host =~ /^http/) ? host : "https://api.#{host}"
default_url = full_host + "/" + PYTHON_OSX_TGZ_DEFAULT_URL_PATH
return ENV.fetch('PYTHON_DISTRO_URL', default_url)
end
|
#python_command_script_template_path ⇒ Object
Path to the template which generates the bash script for running python
339
340
341
|
# File 'lib/mortar/local/python.rb', line 339
def python_command_script_template_path
File.expand_path("../../templates/script/runpython.sh", __FILE__)
end
|
#python_directory ⇒ Object
162
163
164
|
# File 'lib/mortar/local/python.rb', line 162
def python_directory
return "#{local_install_directory}/python"
end
|
#python_env_dir ⇒ Object
158
159
160
|
# File 'lib/mortar/local/python.rb', line 158
def python_env_dir
return "#{local_install_directory}/pythonenv"
end
|
#requirements_edit_date ⇒ Object
Date of last change to the requirements file
229
230
231
232
233
234
235
|
# File 'lib/mortar/local/python.rb', line 229
def requirements_edit_date
if has_python_requirements
return File.mtime(pip_requirements_path).to_i
else
return nil
end
end
|
#run_luigi_script(luigi_script, user_script_args) ⇒ Object
321
322
323
324
|
# File 'lib/mortar/local/python.rb', line 321
def run_luigi_script(luigi_script, user_script_args)
template_params = luigi_command_template_parameters(luigi_script, user_script_args)
run_templated_script(python_command_script_template_path, template_params)
end
|
#run_pip_command(subcmd) ⇒ Object
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# File 'lib/mortar/local/python.rb', line 288
def run_pip_command subcmd
pip_output = `. #{local_activate_path} && #{local_python_bin} #{local_pip_bin} --log #{pip_error_log_path} #{subcmd}`
if 0 != $?.to_i
return false
else
return true
end
end
|
#run_stillson_luigi_client_cfg_expansion(luigi_script, project_config_parameters) ⇒ Object
326
327
328
329
330
331
332
333
334
335
336
|
# File 'lib/mortar/local/python.rb', line 326
def run_stillson_luigi_client_cfg_expansion(luigi_script, project_config_parameters)
auto_params = automatic_parameters()
parameters = merge_parameters(auto_params, project_config_parameters)
stillson_template_params = {
:parameters => parameters,
:luigi_script => luigi_script.executable_path()
}
run_templated_script(stillson_command_script_template_path, stillson_template_params)
end
|
#setup_project_python_environment ⇒ Object
Creates a virtualenv in a well known location and installs any packages necessary for the users python udf
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/mortar/local/python.rb', line 185
def setup_project_python_environment
if not has_valid_virtualenv?
return false
end
if should_do_requirements_install
action "Installing user defined python dependencies" do
unless install_user_python_dependencies()
return false
end
note_install("pythonenv")
end
end
if should_install_python_dependencies?
unless install_python_dependencies()
return false
end
end
return true
end
|
#should_do_python_install? ⇒ Boolean
Determines if a python install needs to occur, true if no python install present or a newer version is available
95
96
97
|
# File 'lib/mortar/local/python.rb', line 95
def should_do_python_install?
return (osx? and (not (File.exists?(python_directory))))
end
|
#should_do_requirements_install ⇒ Object
Whether or not we need to do a ‘pip install -r requirements.txt` because we’ve never done one before or the dependencies have changed
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/mortar/local/python.rb', line 215
def should_do_requirements_install
if has_python_requirements
if not install_date('pythonenv')
return true
else
return (requirements_edit_date > install_date('pythonenv'))
end
else
return false
end
end
|
#should_do_update? ⇒ Boolean
60
61
62
|
# File 'lib/mortar/local/python.rb', line 60
def should_do_update?
return is_newer_version('python', python_archive_url)
end
|
#should_install_python_dependencies? ⇒ Boolean
255
256
257
258
259
260
261
262
|
# File 'lib/mortar/local/python.rb', line 255
def should_install_python_dependencies?
MORTAR_PYTHON_PACKAGES.each{ |package|
if update_mortar_package? package
return true
end
}
return false
end
|
#stillson_command_script_template_path ⇒ Object
Path to the template which generates the bash script for running stillson
344
345
346
|
# File 'lib/mortar/local/python.rb', line 344
def stillson_command_script_template_path
File.expand_path("../../templates/script/runstillson.sh", __FILE__)
end
|
#update_mortar_package?(package) ⇒ Boolean
243
244
245
|
# File 'lib/mortar/local/python.rb', line 243
def update_mortar_package?(package)
return is_newer_version(mortar_package_dir(package), mortar_package_url(package))
end
|
#virtualenv_error_log_path ⇒ Object
209
210
211
|
# File 'lib/mortar/local/python.rb', line 209
def virtualenv_error_log_path
return ENV.fetch('VE_ERROR_LOG', "virtualenv.log")
end
|