wright 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/NEWS +5 -0
- data/README.md +3 -3
- data/Rakefile +0 -14
- data/lib/wright.rb +2 -1
- data/lib/wright/config.rb +11 -14
- data/lib/wright/dry_run.rb +7 -8
- data/lib/wright/dsl.rb +13 -11
- data/lib/wright/logger.rb +17 -16
- data/lib/wright/provider.rb +8 -8
- data/lib/wright/provider/directory.rb +5 -5
- data/lib/wright/provider/file.rb +5 -5
- data/lib/wright/provider/package.rb +9 -8
- data/lib/wright/provider/package/apt.rb +7 -9
- data/lib/wright/provider/symlink.rb +9 -9
- data/lib/wright/resource.rb +24 -24
- data/lib/wright/resource/directory.rb +15 -14
- data/lib/wright/resource/file.rb +15 -14
- data/lib/wright/resource/package.rb +14 -13
- data/lib/wright/resource/symlink.rb +11 -10
- data/lib/wright/util.rb +17 -19
- data/lib/wright/util/color.rb +14 -15
- data/lib/wright/util/file.rb +50 -55
- data/lib/wright/util/file_permissions.rb +30 -23
- data/lib/wright/util/recursive_autoloader.rb +10 -12
- data/lib/wright/util/stolen_from_activesupport.rb +42 -43
- data/lib/wright/util/user.rb +19 -22
- data/lib/wright/version.rb +2 -2
- metadata +6 -5
data/lib/wright/util/color.rb
CHANGED
@@ -1,49 +1,48 @@
|
|
1
1
|
module Wright
|
2
2
|
module Util
|
3
|
-
#
|
3
|
+
# ANSI color helpers.
|
4
4
|
module Color
|
5
|
-
#
|
5
|
+
# Colorizes a string (red).
|
6
6
|
#
|
7
|
-
# string
|
7
|
+
# @param string [String] the string to colorize
|
8
8
|
#
|
9
|
-
#
|
9
|
+
# @return [String] the colorized string
|
10
10
|
def self.red(string)
|
11
11
|
colorize(string, :red)
|
12
12
|
end
|
13
13
|
|
14
|
-
#
|
14
|
+
# Colorizes a string (yellow).
|
15
15
|
#
|
16
|
-
# string
|
16
|
+
# @param string [String] the string to colorize
|
17
17
|
#
|
18
|
-
#
|
18
|
+
# @return [String] the colorized string
|
19
19
|
def self.yellow(string)
|
20
20
|
colorize(string, :yellow)
|
21
21
|
end
|
22
22
|
|
23
|
-
#
|
23
|
+
# Colorizes a string.
|
24
24
|
#
|
25
|
-
# string
|
26
|
-
# color
|
27
|
-
#
|
28
|
-
# Examples
|
25
|
+
# @param string [String] the string to colorize
|
26
|
+
# @param color [String] the color that should be used
|
29
27
|
#
|
28
|
+
# @example
|
30
29
|
# Wright::Util::Color.colorize('Hello world', :red)
|
31
30
|
# # => "\e[31mHello world\e[0m"
|
32
31
|
#
|
33
32
|
# Wright::Util::Color.colorize('Hello world', :yellow)
|
34
33
|
# # => "\e[32mHello world\e[0m"
|
35
34
|
#
|
36
|
-
#
|
35
|
+
# @return [String] the colorized string
|
37
36
|
def self.colorize(string, color)
|
38
37
|
no_color = COLOR_MAP[:none]
|
39
38
|
color = COLOR_MAP.fetch(color, no_color)
|
40
39
|
"#{color}#{string}#{no_color}"
|
41
40
|
end
|
42
41
|
|
43
|
-
COLOR_MAP = {
|
42
|
+
COLOR_MAP = {
|
44
43
|
none: "\e[0m",
|
45
44
|
red: "\e[31m",
|
46
|
-
yellow: "\e[
|
45
|
+
yellow: "\e[33m"
|
47
46
|
}
|
48
47
|
private_constant :COLOR_MAP
|
49
48
|
end
|
data/lib/wright/util/file.rb
CHANGED
@@ -27,11 +27,11 @@
|
|
27
27
|
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
28
28
|
# SUCH DAMAGE.
|
29
29
|
|
30
|
-
module Wright
|
30
|
+
module Wright
|
31
31
|
module Util
|
32
|
-
#
|
32
|
+
# Various file methods.
|
33
33
|
module File
|
34
|
-
USER_MAP = {
|
34
|
+
USER_MAP = {
|
35
35
|
'u' => 04700,
|
36
36
|
'g' => 02070,
|
37
37
|
'o' => 01007,
|
@@ -44,7 +44,7 @@ module Wright #:nodoc:
|
|
44
44
|
end
|
45
45
|
private_class_method :user_mask
|
46
46
|
|
47
|
-
MODE_MAP = {
|
47
|
+
MODE_MAP = {
|
48
48
|
'r' => 0444,
|
49
49
|
'w' => 0222,
|
50
50
|
'x' => 0111,
|
@@ -59,22 +59,20 @@ module Wright #:nodoc:
|
|
59
59
|
end
|
60
60
|
private_class_method :mode_mask
|
61
61
|
|
62
|
-
#
|
63
|
-
# value.
|
64
|
-
#
|
65
|
-
# mode - The symbolic mode string.
|
66
|
-
# base_mode - The integer base mode.
|
67
|
-
# filetype - The filetype. Defaults to :file.
|
62
|
+
# Converts a symbolic mode string to an integer mode value.
|
68
63
|
#
|
69
|
-
#
|
64
|
+
# @param mode [String] the symbolic mode string
|
65
|
+
# @param base_mode [Integer] the base mode
|
66
|
+
# @param filetype [Symbol] the filetype
|
70
67
|
#
|
68
|
+
# @example
|
71
69
|
# Wright::Util::File.symbolic_mode_to_i('u=rw,go=r', 0400).to_s(8)
|
72
70
|
# # => "644"
|
73
71
|
#
|
74
72
|
# Wright::Util::File.symbolic_mode_to_i('u=rw,g+r', 0200).to_s(8)
|
75
73
|
# # => "640"
|
76
74
|
#
|
77
|
-
#
|
75
|
+
# @return [Integer] the integer mode
|
78
76
|
def self.symbolic_mode_to_i(mode, base_mode, filetype = :file)
|
79
77
|
is_directory = (filetype == :directory)
|
80
78
|
unless symbolic_mode?(mode)
|
@@ -87,23 +85,22 @@ module Wright #:nodoc:
|
|
87
85
|
mode_i
|
88
86
|
end
|
89
87
|
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
# mode_clause - The symbolic mode clause.
|
94
|
-
# base_mode_i - The integer base mode.
|
95
|
-
# is_directory - Denotes whether the mode_clause should be
|
96
|
-
# treated as a symbolic directory mode clause.
|
88
|
+
# Converts a single symbolic mode clause to an integer mode
|
89
|
+
# value.
|
97
90
|
#
|
98
|
-
#
|
91
|
+
# @param mode_clause [String] the symbolic mode clause
|
92
|
+
# @param base_mode_i [Integer] the integer base mode
|
93
|
+
# @param is_directory [Bool] denotes whether the mode_clause
|
94
|
+
# should be treated as a symbolic directory mode clause
|
99
95
|
#
|
96
|
+
# @example
|
100
97
|
# Wright::Util::File.mode_clause_to_i('g+r', 0600, false).to_s(8)
|
101
98
|
# # => "640"
|
102
99
|
#
|
103
100
|
# Wright::Util::File.mode_clause_to_i('+rw', 0600, false).to_s(8)
|
104
101
|
# # => "666"
|
105
102
|
#
|
106
|
-
#
|
103
|
+
# @return [Integer] the mode clause as an integer
|
107
104
|
def self.mode_clause_to_i(mode_clause, base_mode_i, is_directory)
|
108
105
|
mode_clause = "a#{mode_clause}" if mode_clause =~ /\A[+-=]/
|
109
106
|
who, op, perm = mode_clause.split(/([+-=])/)
|
@@ -126,12 +123,11 @@ module Wright #:nodoc:
|
|
126
123
|
end
|
127
124
|
private_class_method :apply_user_mode_masks
|
128
125
|
|
129
|
-
#
|
130
|
-
#
|
131
|
-
# mode - The numeric mode string.
|
126
|
+
# Converts a numeric mode string to an integer mode.
|
132
127
|
#
|
133
|
-
#
|
128
|
+
# @param mode [String, #to_i] the numeric mode string
|
134
129
|
#
|
130
|
+
# @example
|
135
131
|
# Wright::Util::File.numeric_mode_to_i('0600').to_s(8)
|
136
132
|
# # => "600"
|
137
133
|
#
|
@@ -144,8 +140,8 @@ module Wright #:nodoc:
|
|
144
140
|
# Wright::Util::File.numeric_mode_to_i('invalid_mode').to_s(8)
|
145
141
|
# # => nil
|
146
142
|
#
|
147
|
-
#
|
148
|
-
# be converted
|
143
|
+
# @return [Integer] the mode in integer form or +nil+ if the
|
144
|
+
# mode could not be converted
|
149
145
|
def self.numeric_mode_to_i(mode)
|
150
146
|
return mode.to_i unless mode.is_a?(String)
|
151
147
|
mode =~ /\A[0-7]{3,4}\Z/ ? mode.to_i(8) : nil
|
@@ -159,29 +155,27 @@ module Wright #:nodoc:
|
|
159
155
|
end
|
160
156
|
private_class_method :symbolic_mode?
|
161
157
|
|
162
|
-
#
|
158
|
+
# Returns a file's current mode.
|
163
159
|
#
|
164
|
-
# path
|
165
|
-
#
|
166
|
-
# Examples
|
160
|
+
# @param path [String] the file's path
|
167
161
|
#
|
162
|
+
# @example
|
168
163
|
# FileUtils.touch('foo')
|
169
164
|
# FileUtils.chmod(0644, 'foo')
|
170
165
|
# Wright::Util::File.file_mode('foo').to_s(8)
|
171
166
|
# # => "644"
|
172
167
|
#
|
173
|
-
#
|
174
|
-
# not exist
|
168
|
+
# @return [Integer] the file mode as an integer or +nil+ if the
|
169
|
+
# file does not exist
|
175
170
|
def self.file_mode(path)
|
176
171
|
::File.exist?(path) ? (::File.stat(path).mode & 07777) : nil
|
177
172
|
end
|
178
173
|
|
179
|
-
#
|
180
|
-
#
|
181
|
-
# path - The file's path.
|
174
|
+
# Returns a file's owner.
|
182
175
|
#
|
183
|
-
#
|
176
|
+
# @param path [String] the file's path
|
184
177
|
#
|
178
|
+
# @example
|
185
179
|
# FileUtils.touch('foo')
|
186
180
|
# FileUtils.chown(0, 0, 'foo')
|
187
181
|
# Wright::Util::File.file_owner('foo')
|
@@ -190,18 +184,17 @@ module Wright #:nodoc:
|
|
190
184
|
# Wright::Util::File.file_owner('nonexistent')
|
191
185
|
# # => nil
|
192
186
|
#
|
193
|
-
#
|
194
|
-
# exist
|
187
|
+
# @return [Integer] the file owner's uid or +nil+ if the file
|
188
|
+
# does not exist
|
195
189
|
def self.file_owner(path)
|
196
190
|
::File.exist?(path) ? ::File.stat(path).uid : nil
|
197
191
|
end
|
198
192
|
|
199
|
-
#
|
200
|
-
#
|
201
|
-
# path - The file's path.
|
193
|
+
# Returns a file's owner.
|
202
194
|
#
|
203
|
-
#
|
195
|
+
# @param path [String] the file's path
|
204
196
|
#
|
197
|
+
# @example
|
205
198
|
# FileUtils.touch('foo')
|
206
199
|
# FileUtils.chown(0, 0, 'foo')
|
207
200
|
# Wright::Util::File.file_group('foo')
|
@@ -210,19 +203,18 @@ module Wright #:nodoc:
|
|
210
203
|
# Wright::Util::File.file_group('nonexistent')
|
211
204
|
# # => nil
|
212
205
|
#
|
213
|
-
#
|
214
|
-
# exist.
|
206
|
+
# @return [Integer] the file owner's uid or nil if the file does
|
207
|
+
# not exist.
|
215
208
|
def self.file_group(path)
|
216
209
|
::File.exist?(path) ? ::File.stat(path).gid : nil
|
217
210
|
end
|
218
211
|
|
219
|
-
#
|
220
|
-
#
|
212
|
+
# Expands tilde symbols in file paths. Path elements other than
|
213
|
+
# the first one are left alone.
|
221
214
|
#
|
222
|
-
# path
|
223
|
-
#
|
224
|
-
# Examples
|
215
|
+
# @param path [String] the file path
|
225
216
|
#
|
217
|
+
# @example
|
226
218
|
# Wright::Util::File.expand_tilde_path('~root/foo')
|
227
219
|
# # => "/root/foo"
|
228
220
|
#
|
@@ -232,7 +224,7 @@ module Wright #:nodoc:
|
|
232
224
|
# Wright::Util::File.expand_tilde_path('../foo/bar')
|
233
225
|
# # => "../foo/bar"
|
234
226
|
#
|
235
|
-
#
|
227
|
+
# @return [String] the expanded path
|
236
228
|
def self.expand_tilde_path(path)
|
237
229
|
return path unless path.start_with?('~')
|
238
230
|
|
@@ -240,13 +232,16 @@ module Wright #:nodoc:
|
|
240
232
|
::File.join(::File.expand_path(first), rest)
|
241
233
|
end
|
242
234
|
|
243
|
-
#
|
235
|
+
# Creates symlinks without descending into directories.
|
244
236
|
#
|
245
237
|
# If the file denoted by link_name is a symlink to a directory,
|
246
|
-
# ln_sfn does not descend into it. Behaves similar to GNU
|
247
|
-
# OpenBSD ln(1) when using
|
238
|
+
# {ln_sfn} does not descend into it. Behaves similar to GNU
|
239
|
+
# ln(1) or OpenBSD ln(1) when using +ln -sfn target link_name+.
|
240
|
+
#
|
241
|
+
# @param target [String] the link target
|
242
|
+
# @param link_name [String] the link name
|
248
243
|
#
|
249
|
-
#
|
244
|
+
# @return [void]
|
250
245
|
def self.ln_sfn(target, link_name)
|
251
246
|
if ::File.symlink?(link_name) && ::File.directory?(link_name)
|
252
247
|
FileUtils.rm(link_name)
|
@@ -3,14 +3,17 @@ require 'wright/util/user'
|
|
3
3
|
|
4
4
|
module Wright
|
5
5
|
module Util
|
6
|
-
#
|
6
|
+
# Helper class to manage file permissions.
|
7
7
|
class FilePermissions
|
8
|
-
#
|
8
|
+
# Creates a FilePermissions object from a
|
9
|
+
# {Wright::Resource::File} or {Wright::Resource::Directory}.
|
9
10
|
#
|
10
|
-
#
|
11
|
-
#
|
11
|
+
# @param resource [Wright::Resource::File,
|
12
|
+
# Wright::Resource::Directory] the resource object
|
13
|
+
# @param filetype [Symbol] the file's type (+:file+ or +:directory+)
|
12
14
|
#
|
13
|
-
#
|
15
|
+
# @return [Wright::Util::FilePermissions] the FilePermissions
|
16
|
+
# object
|
14
17
|
def self.create_from_resource(resource, filetype)
|
15
18
|
filepath = ::File.expand_path(resource.name)
|
16
19
|
p = Wright::Util::FilePermissions.new(filepath, filetype)
|
@@ -20,25 +23,25 @@ module Wright
|
|
20
23
|
p
|
21
24
|
end
|
22
25
|
|
23
|
-
#
|
26
|
+
# @return [String] the target file's name
|
24
27
|
attr_accessor :filename
|
25
28
|
|
26
|
-
#
|
27
|
-
|
29
|
+
# @return [Integer] the file's target group id
|
30
|
+
attr_reader :group
|
28
31
|
|
29
|
-
#
|
30
|
-
|
32
|
+
# @return [Integer] the file's target mode
|
33
|
+
attr_reader :mode
|
31
34
|
|
32
|
-
#
|
35
|
+
# @return [Integer] the file's target owner uid
|
33
36
|
attr_reader :owner
|
34
37
|
|
35
|
-
# Internal: Supported filetypes.
|
36
38
|
VALID_FILETYPES = [:file, :directory]
|
39
|
+
private_constant :VALID_FILETYPES
|
37
40
|
|
38
|
-
#
|
41
|
+
# Initializes a FilePermissions object.
|
39
42
|
#
|
40
|
-
# filename
|
41
|
-
# filetype
|
43
|
+
# @param filename [String] the file's name
|
44
|
+
# @param filetype [Symbol] the file's type (+:file+ or +:directory+)
|
42
45
|
def initialize(filename, filetype)
|
43
46
|
unless VALID_FILETYPES.include?(filetype)
|
44
47
|
fail ArgumentError, "Invalid filetype '#{filetype}'"
|
@@ -47,17 +50,17 @@ module Wright
|
|
47
50
|
@filetype = filetype
|
48
51
|
end
|
49
52
|
|
50
|
-
#
|
53
|
+
# Sets the file's owner
|
51
54
|
def owner=(owner)
|
52
55
|
@owner = Util::User.user_to_uid(owner)
|
53
56
|
end
|
54
57
|
|
55
|
-
#
|
58
|
+
# Sets the file's group
|
56
59
|
def group=(group)
|
57
60
|
@group = Util::User.group_to_gid(group)
|
58
61
|
end
|
59
62
|
|
60
|
-
#
|
63
|
+
# Sets the file's mode
|
61
64
|
def mode=(mode)
|
62
65
|
if mode.nil?
|
63
66
|
@mode = nil
|
@@ -72,7 +75,9 @@ module Wright
|
|
72
75
|
@mode = mode_i
|
73
76
|
end
|
74
77
|
|
75
|
-
#
|
78
|
+
# Checks if the file's owner, group and mode are up-to-date
|
79
|
+
# @return [Bool] +true+ if the file is up to date, +false+
|
80
|
+
# otherwise
|
76
81
|
def uptodate?
|
77
82
|
if ::File.exist?(@filename)
|
78
83
|
owner_uptodate? && group_uptodate? && mode_uptodate?
|
@@ -81,23 +86,25 @@ module Wright
|
|
81
86
|
end
|
82
87
|
end
|
83
88
|
|
84
|
-
#
|
89
|
+
# Updates the file's owner, group and mode.
|
90
|
+
#
|
91
|
+
# @return [void]
|
85
92
|
def update
|
86
93
|
::File.chmod(@mode, @filename) if @mode
|
87
94
|
::File.chown(@owner, @group, @filename) if @owner || @group
|
88
95
|
end
|
89
96
|
|
90
|
-
#
|
97
|
+
# @return [Integer] the file's current mode
|
91
98
|
def current_mode
|
92
99
|
Wright::Util::File.file_mode(@filename)
|
93
100
|
end
|
94
101
|
|
95
|
-
#
|
102
|
+
# @return [Integer] the file's current owner's uid
|
96
103
|
def current_owner
|
97
104
|
Wright::Util::File.file_owner(@filename)
|
98
105
|
end
|
99
106
|
|
100
|
-
#
|
107
|
+
# @return [Integer] the file's current group's gid
|
101
108
|
def current_group
|
102
109
|
Wright::Util::File.file_group(@filename)
|
103
110
|
end
|
@@ -2,22 +2,20 @@ require 'wright/util'
|
|
2
2
|
|
3
3
|
module Wright
|
4
4
|
module Util
|
5
|
-
#
|
6
|
-
#
|
5
|
+
# Recursive autoloader, recursively adds autoloads for all files
|
6
|
+
# in a directory.
|
7
7
|
module RecursiveAutoloader
|
8
|
-
#
|
9
|
-
# parent class.
|
8
|
+
# Adds autoloads for all files in a directory to a parent class.
|
10
9
|
#
|
11
10
|
# Registers all files in directory to be autoloaded the
|
12
11
|
# first time ParentClass::CamelCased::FileName is accessed.
|
13
12
|
#
|
14
|
-
# directory
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
# Examples
|
13
|
+
# @param directory [String] the path of the directory containing
|
14
|
+
# the files to be autoloaded
|
15
|
+
# @param parent_class [String] the parent class to add the
|
16
|
+
# autoloads to
|
20
17
|
#
|
18
|
+
# @example
|
21
19
|
# require 'fileutils'
|
22
20
|
#
|
23
21
|
# # set up a test directory
|
@@ -42,8 +40,8 @@ module Wright
|
|
42
40
|
# Root::Foo.autoload? 'BarBaz'
|
43
41
|
# # => nil
|
44
42
|
#
|
45
|
-
#
|
46
|
-
#
|
43
|
+
# @return [void]
|
44
|
+
# @raise [ArgumentError] if the parent class cannot be resolved
|
47
45
|
def self.add_autoloads(directory, parent_class)
|
48
46
|
unless class_exists?(parent_class)
|
49
47
|
fail ArgumentError, "Can't resolve parent_class #{parent_class}"
|