forked from poise/poise-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_execute.rb
More file actions
102 lines (90 loc) · 2.79 KB
/
Copy pathpython_execute.rb
File metadata and controls
102 lines (90 loc) · 2.79 KB
1
2
3
4
5
6
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
#
# Copyright 2015, Noah Kantrowitz
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/mixin/which'
require 'chef/provider/execute'
require 'chef/resource/execute'
require 'poise'
require 'poise_python/resources/python_runtime'
module PoisePython
module Resources
# (see PythonExecute::Resource)
# @since 1.0.0
module PythonExecute
# A `python_execute` resource to run Python scripts and commands.
#
# @provides python_execute
# @action run
# @example
# python_execute 'myapp.py' do
# user 'myuser'
# end
class Resource < Chef::Resource::Execute
include Poise(parent: true)
provides(:python_execute)
actions(:run)
# @!attribute parent_python
# Parent Python installation.
# @return [PoisePython::Resources::Python::Resource, nil]
parent_attribute(:python, type: :python_runtime, optional: true)
# Nicer name for the DSL.
alias_method :python, :parent_python
end
# The default provider for `python_execute`.
#
# @see Resource
# @provides python_execute
class Provider < Chef::Provider::Execute
include Chef::Mixin::Which
provides(:python_execute)
private
# The Python binary to use for this command.
#
# @return [String]
def python_binary
if new_resource.parent_python
new_resource.parent_python.python_binary
else
which('python')
end
end
# Command to pass to shell_out.
#
# @return [String, Array<String>]
def command
if new_resource.command.is_a?(Array)
[python_binary] + new_resource.command
else
"#{python_binary} #{new_resource.command}"
end
end
# Environment variables to pass to shell_out.
#
# @return [Hash]
def environment
if new_resource.parent_python
environment = new_resource.parent_python.python_environment
if new_resource.environment
environment = environment.merge(new_resource.environment)
end
environment
else
new_resource.environment
end
end
end
end
end
end