ratch 1.2.0 → 2.0.0

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.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/HISTORY.rdoc +87 -0
  3. data/README.rdoc +86 -66
  4. data/bin/ludo +26 -10
  5. data/lib/ratch/batch.rb +115 -74
  6. data/lib/ratch/core_ext/facets.rb +2 -1
  7. data/lib/ratch/core_ext/to_console.rb +11 -11
  8. data/lib/ratch/file_list.rb +6 -15
  9. data/lib/ratch/script.rb +1 -1
  10. data/lib/ratch/shell.rb +356 -518
  11. data/lib/ratch/utils/config.rb +4 -4
  12. data/lib/ratch/utils/email.rb +20 -97
  13. data/lib/ratch/utils/rdoc.rb +4 -7
  14. data/lib/ratch/utils/tar.rb +94 -50
  15. data/lib/ratch/utils/xdg.rb +2 -3
  16. data/lib/ratch/utils/zlib.rb +5 -4
  17. data/lib/ratch/version.rb +3 -0
  18. data/lib/ratch.rb +8 -24
  19. metadata +134 -157
  20. data/.ruby +0 -99
  21. data/History.rdoc +0 -35
  22. data/License.txt +0 -204
  23. data/Version +0 -1
  24. data/lib/ratch/console.rb +0 -199
  25. data/lib/ratch/system.rb +0 -15
  26. data/lib/ratch/utils/ftp.rb +0 -134
  27. data/lib/ratch/utils/pom.rb +0 -22
  28. data/lib/ratch.yml +0 -99
  29. data/man/ratch.1 +0 -28
  30. data/spec/01_shell.rdoc +0 -198
  31. data/spec/02_script.rdoc +0 -34
  32. data/spec/03_batch.rdoc +0 -71
  33. data/spec/04_system.rdoc +0 -3
  34. data/spec/applique/array.rb +0 -8
  35. data/spec/applique/setup.rb +0 -20
  36. data/test/case_batch.rb +0 -63
  37. data/test/case_shell.rb +0 -46
  38. data/test/core_ext/case_pathname.rb +0 -361
  39. data/test/helper.rb +0 -4
  40. data/test/utils/case_cli.rb +0 -6
  41. data/test/utils/case_config.rb +0 -12
  42. data/test/utils/case_email.rb +0 -10
  43. data/test/utils/case_ftp.rb +0 -6
  44. data/test/utils/case_pom.rb +0 -17
  45. data/test/utils/case_rdoc.rb +0 -23
  46. data/test/utils/case_tar.rb +0 -6
  47. data/test/utils/case_zlib.rb +0 -11
  48. data/test/utils/fixtures/pom_sample/Profile +0 -4
  49. data/test/utils/fixtures/rdoc_sample/README.rdoc +0 -4
  50. data/test/utils/fixtures/rdoc_sample/lib/rdoc_sample/rdoc_sample.rb +0 -9
  51. /data/{COPYING → LICENSE.txt} +0 -0
@@ -36,14 +36,14 @@ module Ratch
36
36
  # Since they are YAML files, they can optionally end with '.yaml' or '.yml'.
37
37
  def configuration!(file)
38
38
  @configuration ||= {}
39
- patt = file + "{.yml,.yaml,}"
40
- path = Dir.glob(patt, File::FNM_CASEFOLD).find{ |f| File.file?(f) }
39
+ patt = localize(file) + "{.yml,.yaml,}"
40
+ path = Dir.glob(patt).find{ |f| File.file?(f) }
41
41
  if path
42
42
  # The || {} is in case the file is empty.
43
- data = YAML::load(File.open(path)) || {}
43
+ data = YAML.load_file(path) || {}
44
44
  @configuration[file] = data
45
45
  else
46
- raise LoadError, "Missing file -- #{path}"
46
+ raise LoadError, "Missing file -- #{file}"
47
47
  end
48
48
  end
49
49
 
@@ -72,8 +72,7 @@ module Ratch
72
72
  end
73
73
 
74
74
  def new_with_environment(options={})
75
- environment_options.merge(options.rekey)
76
- new(options)
75
+ new(environment_options.merge(options.rekey))
77
76
  end
78
77
  end
79
78
 
@@ -133,7 +132,7 @@ module Ratch
133
132
  #
134
133
 
135
134
  def email(options={})
136
- options.rekey
135
+ options = options.rekey
137
136
 
138
137
  message = options[:message] || self.message
139
138
  subject = options[:subject] || self.subject
@@ -147,32 +146,33 @@ module Ratch
147
146
  raise ArgumentError, "missing email field -- mailto" unless mailto
148
147
  raise ArgumentError, "missing email field -- subject" unless subject
149
148
 
150
- passwd ||= password("#{account} password:")
149
+ passwd = self.passwd
151
150
 
152
151
  mailto = [mailto].flatten.compact
153
152
 
154
- msg = ""
153
+ msg = String.new
155
154
  msg << "From: #{from}\n"
156
- msg << "To: #{mailto.join(';')}\n"
155
+ msg << "To: #{mailto.join(', ')}\n"
157
156
  msg << "Subject: #{subject}\n"
158
- msg << ""
159
- msg << message
157
+ msg << "\n"
158
+ msg << message.to_s
160
159
 
161
160
  #p server, port, domain, account, passwd, login, secure if verbose?
162
161
 
163
162
  begin
164
- if Net::SMTP.respond_to?(:enable_tls) && secure
165
- Net::SMTP.enable_tls
166
- Net::SMTP.start(server, port, domain, account, passwd, login, secure) do |smtp|
167
- smtp.send_message(msg, from, mailto)
168
- end
163
+ smtp = Net::SMTP.new(server, port)
164
+ if secure
165
+ port == 465 ? smtp.enable_tls : smtp.enable_starttls
166
+ else
167
+ smtp.disable_starttls
168
+ end
169
+ if passwd.to_s.empty?
170
+ smtp.start(domain){ |s| s.send_message(msg, from, mailto) }
169
171
  else
170
- Net::SMTP.start(server, port, domain, account, passwd, login) do |smtp|
171
- smtp.send_message(msg, from, mailto)
172
- end
172
+ smtp.start(domain, account, passwd, login){ |s| s.send_message(msg, from, mailto) }
173
173
  end
174
174
  return mailto
175
- rescue Exception => e
175
+ rescue StandardError => e
176
176
  return e
177
177
  end
178
178
  end
@@ -201,88 +201,11 @@ module Ratch
201
201
 
202
202
  #
203
203
  def require_smtp
204
- begin
205
- require 'facets/net/smtp_tls'
206
- rescue LoadError
207
- require 'net/smtp'
208
- end
204
+ require 'net/smtp'
205
+ rescue LoadError
206
+ raise LoadError, "the `net-smtp` gem is needed to send email"
209
207
  end
210
208
 
211
209
  end
212
210
 
213
211
  end
214
-
215
-
216
-
217
- =begin
218
- # Email function to easily send out an email.
219
- #
220
- # Settings:
221
- #
222
- # subject Subject of email message.
223
- # from Message FROM address [email].
224
- # to Email address to send announcemnt.
225
- # server Email server to route message.
226
- # port Email server's port.
227
- # domain Email server's domain name.
228
- # account Email account name if needed.
229
- # password Password for login..
230
- # login Login type: plain, cram_md5 or login [plain].
231
- # secure Uses TLS security, true or false? [false]
232
- # message Mesage to send -or-
233
- # file File that contains message.
234
- #
235
- def email(message, settings)
236
- settings ||= {}
237
- settings.rekey!
238
-
239
- server = settings[:server]
240
- account = settings[:account] || ENV['EMAIL_ACCOUNT']
241
- passwd = settings[:password] || ENV['EMAIL_PASSWORD']
242
- login = settings[:login].to_sym
243
- subject = settings[:subject]
244
- mail_to = settings[:to] || settings[:mail_to]
245
- mail_from = settings[:from] || settings[:mail_from]
246
- secure = settings[:secure]
247
- domain = settings[:domain] || server
248
-
249
- port ||= (secure ? 465 : 25)
250
- account ||= mail_from
251
- login ||= :plain
252
-
253
- #mail_to = nil if mail_to.empty?
254
-
255
- raise ArgumentError, "missing email field -- server" unless server
256
- raise ArgumentError, "missing email field -- account" unless account
257
- raise ArgumentError, "missing email field -- subject" unless subject
258
- raise ArgumentError, "missing email field -- to" unless mail_to
259
- raise ArgumentError, "missing email field -- from" unless mail_from
260
-
261
- passwd ||= password(account)
262
-
263
- mail_to = [mail_to].flatten.compact
264
-
265
- msg = ""
266
- msg << "From: #{mail_from}\n"
267
- msg << "To: #{mail_to.join(';')}\n"
268
- msg << "Subject: #{subject}\n"
269
- msg << ""
270
- msg << message
271
-
272
- begin
273
- Net::SMTP.enable_tls if Net::SMTP.respond_to?(:enable_tls) and secure
274
- Net::SMTP.start(server, port, domain, account, passwd, login) do |s|
275
- s.send_message( msg, mail_from, mail_to )
276
- end
277
- puts "Email sent successfully to #{mail_to.join(';')}."
278
- return true
279
- rescue => e
280
- if trace?
281
- raise e
282
- else
283
- abort "Email delivery failed."
284
- end
285
- end
286
- end
287
- =end
288
-
@@ -16,18 +16,15 @@ module Ratch
16
16
  require 'rdoc/rdoc'
17
17
 
18
18
  options = Hash===files.last ? files.pop : {}
19
- options.rekey!(&:to_s)
19
+ options = options.rekey(&:to_s)
20
20
 
21
- options['title'] ||= options.delete('T')
22
-
23
- options['debug'] = options['debug'] #|| debug?
24
- options['quiet'] = options['quiet'] #|| quiet?
25
- options['verbose'] = options['verbose'] #|| verbose?
21
+ title = options.delete('T')
22
+ options['title'] ||= title if title
26
23
 
27
24
  # apply pom (todo?)
28
25
  #options['title'] ||= metadata.title
29
26
 
30
- options = DEFAULT_RDOC_OPTIONS.merge(options)
27
+ options = DEFAULT_RDOC_OPTIONS.rekey(&:to_s).merge(options)
31
28
 
32
29
  locally do
33
30
  rdoc = RDoc::RDoc.new
@@ -2,87 +2,131 @@ require 'ratch/utils/zlib'
2
2
 
3
3
  module Ratch
4
4
 
5
- # Utility methods for working with tarballs archives.
6
- #--
7
- # TODO: Add compress support ?
8
- # TODO: Add bzip support ?
9
- #++
5
+ # Utility methods for working with tar archives. They use the tar
6
+ # reader and writer that come with RubyGems, so nothing else is needed.
7
+ #
8
+ # Regular files and directories are archived; links and special files
9
+ # are skipped.
10
10
  module TarUtils
11
11
 
12
- # When included include GZipUtils too.
12
+ # When included, include ZlibUtils too.
13
13
  def self.included(base)
14
- #require 'zlib'
15
- include ZLibUtils
16
- require 'archive/tar/minitar'
14
+ require 'rubygems/package'
15
+ base.__send__(:include, ZlibUtils)
17
16
  end
18
17
 
19
18
  #
20
19
  def self.extended(base)
21
- included(base)
20
+ require 'rubygems/package'
21
+ base.extend(ZlibUtils)
22
22
  end
23
23
 
24
- # Tar
24
+ # Archive a folder as FOLDER.tar, or as +file+. Returns the archive's
25
+ # path.
25
26
  def tar(folder, file=nil, options={})
27
+ options, file = file, nil if Hash === file
26
28
  noop, verbose = *util_options(options)
27
- file ||= File.basename(File.expand_path(folder)) + '.tar'
28
- cmd = "tar -cf #{file} #{folder}"
29
- puts cmd if verbose
30
- unless noop
31
- locally do
32
- gzIO = File.open(file, 'wb')
33
- Archive::Tar::Minitar.pack(folder, gzIO)
34
- end
35
- end
29
+
30
+ file ||= File.basename(localize(folder)) + '.tar'
31
+
32
+ $stderr.puts "tar -cf #{file} #{folder}" if verbose
33
+
34
+ File.open(localize(file), 'wb'){ |io| tar_pack(folder, io) } unless noop
35
+
36
36
  path(file)
37
37
  end
38
38
 
39
- # Untar
39
+ # Unpack a tar archive into the working directory. Returns the paths
40
+ # unpacked.
40
41
  def untar(file, options={})
41
42
  noop, verbose = *util_options(options)
42
- #file ||= File.basename(File.expand_path(folder)) + '.tar'
43
- cmd = "untar #{file}"
44
- puts cmd if verbose
45
- unless noop
46
- locally do
47
- gzIO = File.open(file, 'wb')
48
- Archive::Tar::Minitar.unpack(gzIO)
49
- end
50
- end
51
- path(file)
43
+
44
+ $stderr.puts "tar -xf #{file}" if verbose
45
+
46
+ return [] if noop
47
+
48
+ File.open(localize(file), 'rb'){ |io| tar_unpack(io) }
52
49
  end
53
50
 
54
- # Tar Gzip
51
+ # Archive a folder as FOLDER.tar.gz, or as +file+. Returns the
52
+ # archive's path.
55
53
  def tar_gzip(folder, file=nil, options={})
54
+ options, file = file, nil if Hash === file
56
55
  noop, verbose = *util_options(options)
57
- file ||= File.basename(File.expand_path(folder)) + '.tar.gz' # '.tgz' which ?
58
- cmd = "tar --gzip -czf #{file} #{folder}"
59
- puts cmd if verbose
60
- unless noop
61
- locally do #folder, file = localize(folder), localize(file)
62
- gzIO = Zlib::GzipWriter.new(File.open(file, 'wb'))
63
- Archive::Tar::Minitar.pack(folder, gzIO)
64
- end
65
- end
56
+
57
+ file ||= File.basename(localize(folder)) + '.tar.gz'
58
+
59
+ $stderr.puts "tar -czf #{file} #{folder}" if verbose
60
+
61
+ Zlib::GzipWriter.open(localize(file)){ |gz| tar_pack(folder, gz) } unless noop
62
+
66
63
  path(file)
67
64
  end
68
65
 
69
66
  alias_method :tar_z, :tar_gzip
70
67
 
71
- # Untar Gzip
72
- #
73
- # FIXME: Write unified untar_gzip function.
68
+ # Unpack a gzipped tar archive into the working directory. Returns
69
+ # the paths unpacked.
74
70
  def untar_gzip(file, options={})
75
- untar(ungzip(file, options), options)
71
+ noop, verbose = *util_options(options)
72
+
73
+ $stderr.puts "tar -xzf #{file}" if verbose
74
+
75
+ return [] if noop
76
+
77
+ Zlib::GzipReader.open(localize(file)){ |gz| tar_unpack(gz) }
76
78
  end
77
79
 
78
80
  alias_method :untar_z, :untar_gzip
79
81
 
80
- #def tgz(folder, file=nil, options={})
81
- # file ||= File.basename(File.expand_path(folder)) + '.tgz'
82
- # tar_gzip(folder, file, options)
83
- #end
82
+ private
83
+
84
+ # Write the folder and all in it to +io+ as a tar archive, with paths
85
+ # as given, relative to the working directory.
86
+ def tar_pack(folder, io)
87
+ folder = folder.to_s.chomp('/')
88
+ Gem::Package::TarWriter.new(io) do |tar|
89
+ ([folder] + glob(File.join(folder, '**/*'), File::FNM_DOTMATCH)).uniq.each do |entry|
90
+ next if entry =~ /(\A|\/)\.\.?\z/
91
+ real = localize(entry)
92
+ stat = File.lstat(real)
93
+ if stat.directory?
94
+ tar.mkdir(entry, stat.mode & 0777)
95
+ elsif stat.file?
96
+ tar.add_file_simple(entry, stat.mode & 0777, stat.size) do |out|
97
+ File.open(real, 'rb'){ |f| IO.copy_stream(f, out) }
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ # Unpack the tar archive read from +io+ into the working directory.
105
+ # An entry that would land outside it is refused.
106
+ def tar_unpack(io)
107
+ paths = []
108
+ Gem::Package::TarReader.new(io) do |tar|
109
+ tar.each do |entry|
110
+ name = entry.full_name
111
+ dest = File.expand_path(File.join(work.to_s, name))
112
+ unless dest == work.to_s || dest.start_with?(work.to_s + '/')
113
+ raise ArgumentError, "tar entry outside of the directory -- #{name}"
114
+ end
115
+ if entry.directory?
116
+ ::FileUtils.mkdir_p(dest)
117
+ elsif entry.file?
118
+ ::FileUtils.mkdir_p(File.dirname(dest))
119
+ File.open(dest, 'wb'){ |f| IO.copy_stream(entry, f) }
120
+ File.chmod(entry.header.mode & 0777, dest)
121
+ else
122
+ next
123
+ end
124
+ paths << name
125
+ end
126
+ end
127
+ paths
128
+ end
84
129
 
85
130
  end
86
131
 
87
132
  end
88
-
@@ -15,9 +15,8 @@ module Ratch
15
15
  def self.included(base)
16
16
  begin
17
17
  require 'xdg'
18
- rescue
19
- $stderr << "The `xdg` gem is needed to use the XDGUtils module."
20
- exit -1
18
+ rescue LoadError
19
+ raise LoadError, "the `xdg` gem is needed to use the XDGUtils module"
21
20
  end
22
21
  end
23
22
 
@@ -14,11 +14,12 @@ module Ratch
14
14
 
15
15
  # Create a gzip file.
16
16
  def gzip(file, tofile=nil, options={})
17
+ options, tofile = tofile, nil if Hash === tofile
17
18
  noop, verbose = *util_options(options)
18
19
 
19
- tofile ||= File.basename(file) + '.gz'
20
+ tofile ||= file.to_s + '.gz'
20
21
 
21
- puts "gzip #{file}" if verbose
22
+ $stderr.puts "gzip #{file}" if verbose
22
23
 
23
24
  file = localize(file)
24
25
  tofile = localize(tofile)
@@ -34,9 +35,9 @@ module Ratch
34
35
  def ungzip(file, options={})
35
36
  noop, verbose = *util_options(options)
36
37
 
37
- fname = File.basename(file).chomp(File.extname(file))
38
+ fname = file.to_s.chomp(File.extname(file.to_s))
38
39
 
39
- puts "ungzip #{file}" if verbose
40
+ $stderr.puts "ungzip #{file}" if verbose
40
41
 
41
42
  fname = localize(fname)
42
43
  file = localize(file)
@@ -0,0 +1,3 @@
1
+ module Ratch
2
+ VERSION = '2.0.0'
3
+ end
data/lib/ratch.rb CHANGED
@@ -1,28 +1,12 @@
1
- module Ratch
2
- # Access to project metadata.
3
- def self.metadata
4
- @metadata ||= (
5
- require 'yaml'
6
- YAML.load(File.dirname(__FILE__) + '/ratch.yml')
7
- )
8
- end
9
-
10
- # Access to project metadata via constants.
11
- def self.const_missing(name)
12
- metadata[name.to_s.downcase] || super(name)
13
- end
14
-
15
- # TODO: Only here b/c of issue with Ruby 1.8.x.
16
- VERSION = metadata['version']
17
- end
18
-
1
+ require 'ratch/version'
19
2
  require 'ratch/script'
20
3
 
21
- # Load utility extension modules.
4
+ # Utility extension modules, to include in a Shell subclass or extend a
5
+ # shell with. Each loads what it depends on only when it is used.
22
6
  require 'ratch/utils/cli'
23
- require 'ratch/utils/pom'
7
+ require 'ratch/utils/config'
8
+ require 'ratch/utils/zlib'
9
+ require 'ratch/utils/tar'
24
10
  require 'ratch/utils/rdoc'
25
- #require 'ratch/utils/email'
26
- #require 'ratch/utils/tar'
27
- #require 'ratch/utils/zlib'
28
-
11
+ require 'ratch/utils/email'
12
+ require 'ratch/utils/xdg'