clutil 2010.127.0 → 2010.129.0

Sign up to get free protection for your applications and to get access to all the features.
data/cl/util/smtp.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # $Id: smtp.rb,v 1.4 2008/12/09 06:58:56 chrismo Exp $
2
2
  =begin
3
3
  --------------------------------------------------------------------------
4
- Copyright (c) 2001-2005, Chris Morris
4
+ Copyright (c) 2001-2010, Chris Morris
5
5
  All rights reserved.
6
6
 
7
7
  Redistribution and use in source and binary forms, with or without modification,
@@ -32,100 +32,120 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
32
  =end
33
33
  require 'net/smtp'
34
34
  require 'time'
35
-
36
- def get_filedata(filename)
37
- data = nil
38
- File.open(filename, 'rb') do |f|
39
- data = f.read()
35
+
36
+ module ClUtil
37
+ class Attachment
38
+ def self.load_from_file(filename)
39
+ data = nil
40
+ File.open(filename, 'rb') do |f|
41
+ data = f.read()
42
+ end
43
+ data = [data].pack("m*")
44
+ Attachment.new(File.basename(filename), data)
45
+ end
46
+
47
+ attr_reader :name, :data
48
+
49
+ def initialize(name, data)
50
+ @name = name
51
+ @data = data
52
+ end
40
53
  end
41
- data = [data].pack("m*")
42
- data
43
- end
44
54
 
45
- def create_boundary()
46
- return "_____clabs_smtp_boundary______#{Time.new.to_i.to_s}___"
55
+ class Smtp
56
+ attr_reader :attachments
57
+ attr_accessor :from, :subj, :body, :extra_headers
58
+
59
+ def initialize(smtpsrv='localhost')
60
+ @smtpsrv = smtpsrv
61
+ @attachments = []
62
+ end
63
+
64
+ def to
65
+ @to
66
+ end
67
+
68
+ def to=(value)
69
+ @to = [value].flatten
70
+ end
71
+
72
+ def sendmail
73
+ msg = build_message
74
+ Net::SMTP.start(@smtpsrv) do |smtp|
75
+ smtp.sendmail(msg, @from, @to)
76
+ end
77
+ end
78
+
79
+ def build_message
80
+ msg = format_headers
81
+ boundary = create_boundary
82
+
83
+ if !@attachments.empty?
84
+ msg << [
85
+ "Content-Type: multipart/mixed; boundary=\"#{boundary}\"\n",
86
+ "\n",
87
+ "This is a multi-part message in MIME format.\n",
88
+ "\n"
89
+ ]
90
+ end
91
+
92
+ if @body
93
+ msg << [ "--#{boundary}\n" ] if !@attachments.empty?
94
+ msg << [
95
+ "Content-Type: text/plain; charset=\"iso-8859-1\"\n",
96
+ "Content-Transfer-Encoding: 8bit\n",
97
+ "\n",
98
+ "#{@body}\n",
99
+ "\n"
100
+ ]
101
+ end
102
+
103
+ @attachments.each do |attachment|
104
+ basename = attachment.name
105
+ msg << [
106
+ "--#{boundary}\n",
107
+ "Content-Type: application/octet-stream; name=\"#{basename}\"\n",
108
+ "Content-Transfer-Encoding: base64\n",
109
+ "Content-Disposition: attachment; filename=\"#{basename}\"\n",
110
+ "\n",
111
+ "#{attachment.data}", # no \n needed
112
+ "\n"
113
+ ]
114
+ end
115
+
116
+ msg << ["--#{boundary}--\n"] if !@attachments.empty?
117
+
118
+ msg.flatten!
119
+ end
120
+
121
+ def create_boundary()
122
+ return "_____clabs_smtp_boundary______#{Time.new.to_i.to_s}___"
123
+ end
124
+
125
+ def format_headers
126
+ headers = ["Subject: #{subj}", "From: #{from}", "To: #{to.join(";")}", "Date: #{Time.now.rfc2822}", "MIME-Version: 1.0" ]
127
+ headers << @extra_headers if @extra_headers
128
+ headers.flatten!
129
+ headers.collect! { |hdr| hdr.strip << "\n" }
130
+ [headers].flatten
131
+ end
132
+ end
47
133
  end
48
134
 
135
+ # deprecated - use ClUtil::Smtp instead. The attachments argument is designed
136
+ # to be either a single filename or an array of filenames.
49
137
  def sendmail(to, from, subj, body, smtpsrv=nil, attachments=nil, extra_headers=nil)
50
138
  smtpsrv = 'localhost' if !smtpsrv
139
+ smtp = ClUtil::Smtp.new(smtpsrv)
140
+ smtp.to = to
141
+ smtp.from = from
142
+ smtp.subj = subj
143
+ smtp.body = body
144
+ smtp.extra_headers = extra_headers
51
145
  attachments = [attachments].flatten
52
146
  attachments.compact!
53
- attachments = nil if attachments.empty?
54
- to = [to].flatten
55
-
56
- headers = ["Subject: #{subj}", "From: #{from}", "To: #{to.join(";")}", "Date: #{Time.now.rfc2822}", "MIME-Version: 1.0" ]
57
- headers << extra_headers if !extra_headers.nil?
58
- headers.flatten!
59
- headers.collect! { |hdr| hdr.strip << "\n" }
60
- msg = [ headers ].flatten
61
-
62
- boundary = create_boundary
63
-
64
- if attachments
65
- msg << [
66
- "Content-Type: multipart/mixed; boundary=\"#{boundary}\"\n",
67
- "\n",
68
- "This is a multi-part message in MIME format.\n",
69
- "\n"
70
- ]
147
+ attachments.each do |attachment_fn|
148
+ smtp.attachments << ClUtil::Attachment.load_from_file(attachment_fn)
71
149
  end
72
-
73
- if body
74
- msg << [ "--#{boundary}\n" ] if attachments
75
- msg << [
76
- "Content-Type: text/plain; charset=\"iso-8859-1\"\n",
77
- "Content-Transfer-Encoding: 8bit\n",
78
- "\n",
79
- "#{body}\n",
80
- "\n"
81
- ]
82
- end
83
-
84
- attachments.each do |filename|
85
- basename = File.basename(filename)
86
- filedata = get_filedata(filename)
87
- msg << [
88
- "--#{boundary}\n",
89
- "Content-Type: application/octet-stream; name=\"#{basename}\"\n",
90
- "Content-Transfer-Encoding: base64\n",
91
- "Content-Disposition: attachment; filename=\"#{basename}\"\n",
92
- "\n",
93
- "#{filedata}", # no \n needed
94
- "\n"
95
- ]
96
- end if attachments
97
-
98
- msg << ["--#{boundary}--\n"] if attachments
99
-
100
- msg.flatten!
101
-
102
- Net::SMTP.start(smtpsrv) do |smtp|
103
- smtp.sendmail(msg, from, to)
104
- end
150
+ smtp.sendmail
105
151
  end
106
-
107
- if __FILE__ == $0
108
- require 'test/unit'
109
-
110
- class TestSendMail < Test::Unit::TestCase
111
- def testsendmail
112
- addr = 'chrismo@clabs.org'
113
- sendmail(addr, addr, 'unit test', 'this is a unit test')
114
- sendmail([addr, addr], addr, 'unit test', 'this is a unit test')
115
- end
116
-
117
- def testattachment
118
- File.open('test.txt', 'w+') do |f| f.print "test attachment\n" end
119
- addr = 'chrismo@clabs.org'
120
- sendmail(addr, addr, 'unit test', 'this is a unit test', nil, 'test.txt')
121
- sendmail(addr, addr, 'unit test', 'this is a unit test', nil, ['test.txt', 'smtp.rb'])
122
- end
123
-
124
- def test_thread
125
- addr = 'chrismo@clabs.org'
126
- headers = ["In-Reply-To: <foo@clabs.org>"]
127
- sendmail(addr, addr, 'smtp.rb thread test', 'body', nil, nil, headers)
128
- sendmail(addr, addr, 'smtp.rb thread test', 'body', nil, nil, headers)
129
- end
130
- end
131
- end
data/cl/util/test.rb CHANGED
@@ -41,7 +41,7 @@ class TempDirTest < Test::Unit::TestCase
41
41
  @tempDir = '/tmp/tests'
42
42
  end
43
43
 
44
- def set_up
44
+ def setup
45
45
  @fileNameInc = 0
46
46
  setTempDir
47
47
  File.makedirs(@tempDir) if !FileTest.directory?(@tempDir)
@@ -51,9 +51,6 @@ class TempDirTest < Test::Unit::TestCase
51
51
  ClUtilFile.delTree(@tempDir)
52
52
  end
53
53
 
54
- alias setup set_up
55
- alias teardown tear_down
56
-
57
54
  # to ward off the new Test::Unit detection of classes with no test
58
55
  # methods
59
56
  def default_test
@@ -48,91 +48,91 @@ TestData = Struct.new("TestData",
48
48
  )
49
49
 
50
50
  class TestDirSize < TempDirTest
51
- def set_up
51
+ def setup
52
52
  super
53
- @testData = TestData.new
53
+ @test_data = TestData.new
54
54
  end
55
55
 
56
- def testLargerSingleton
56
+ def test_larger_singleton
57
57
  makeSampleTextFile('', 5000)
58
- @testData.clusterSize = 4096
59
- @testData.expectedFileSize = 5000
60
- @testData.expectedDiskSpace = (4096 * 2)
61
- @testData.expectedFileSizeIncludeSubs = 5000
62
- @testData.expectedDiskSpaceIncludeSubs = (4096 * 2)
63
- @testData.expectedFileCount = 1
64
- @testData.expectedAvgFileSize = 5000
65
- @testData.expectedFileCountIncludeSubs = 1
66
- @testData.expectedAvgFileSizeIncludeSubs = 5000
67
- doTestDirSize(@testData)
58
+ @test_data.clusterSize = 4096
59
+ @test_data.expectedFileSize = 5000
60
+ @test_data.expectedDiskSpace = (4096 * 2)
61
+ @test_data.expectedFileSizeIncludeSubs = 5000
62
+ @test_data.expectedDiskSpaceIncludeSubs = (4096 * 2)
63
+ @test_data.expectedFileCount = 1
64
+ @test_data.expectedAvgFileSize = 5000
65
+ @test_data.expectedFileCountIncludeSubs = 1
66
+ @test_data.expectedAvgFileSizeIncludeSubs = 5000
67
+ do_test_dir_size(@test_data)
68
68
  end
69
69
 
70
- def doTestDirSize(testData)
71
- dirSize = DirSize.new
72
- dirSize.directory = @tempDir
73
- dirSize.clusterSize = testData.clusterSize
74
- dirSize.getSize
75
- assert_equal(testData.expectedFileSize, dirSize.fileSize(false))
76
- assert_equal(testData.expectedDiskSpace, dirSize.diskSpace(false))
77
- assert_equal(testData.expectedFileSizeIncludeSubs, dirSize.fileSize(true))
78
- assert_equal(testData.expectedDiskSpaceIncludeSubs,
79
- dirSize.diskSpace(true))
80
- assert_equal(testData.expectedDiskSpace - testData.expectedFileSize,
81
- dirSize.unusedDiskSpace(false))
70
+ def do_test_dir_size(test_data)
71
+ dir_size = DirSize.new
72
+ dir_size.directory = @tempDir
73
+ dir_size.clusterSize = test_data.clusterSize
74
+ dir_size.getSize
75
+ assert_equal(test_data.expectedFileSize, dir_size.fileSize(false))
76
+ assert_equal(test_data.expectedDiskSpace, dir_size.diskSpace(false))
77
+ assert_equal(test_data.expectedFileSizeIncludeSubs, dir_size.fileSize(true))
78
+ assert_equal(test_data.expectedDiskSpaceIncludeSubs,
79
+ dir_size.diskSpace(true))
80
+ assert_equal(test_data.expectedDiskSpace - test_data.expectedFileSize,
81
+ dir_size.unusedDiskSpace(false))
82
82
  assert_equal(
83
- (testData.expectedDiskSpaceIncludeSubs -
84
- testData.expectedFileSizeIncludeSubs), dirSize.unusedDiskSpace(true))
85
- assert_equal(testData.expectedFileCount, dirSize.fileCount(false))
86
- assert_equal(testData.expectedAvgFileSize, dirSize.avgFileSize(false))
87
- assert_equal(testData.expectedFileCountIncludeSubs,
88
- dirSize.fileCount(true))
89
- assert_equal(testData.expectedAvgFileSizeIncludeSubs,
90
- dirSize.avgFileSize(true))
83
+ (test_data.expectedDiskSpaceIncludeSubs -
84
+ test_data.expectedFileSizeIncludeSubs), dir_size.unusedDiskSpace(true))
85
+ assert_equal(test_data.expectedFileCount, dir_size.fileCount(false))
86
+ assert_equal(test_data.expectedAvgFileSize, dir_size.avgFileSize(false))
87
+ assert_equal(test_data.expectedFileCountIncludeSubs,
88
+ dir_size.fileCount(true))
89
+ assert_equal(test_data.expectedAvgFileSizeIncludeSubs,
90
+ dir_size.avgFileSize(true))
91
91
  end
92
92
 
93
- def testSmallSingleton
93
+ def test_small_singleton
94
94
  makeSampleTextFile('', 1000)
95
- @testData.clusterSize = 4096
96
- @testData.expectedFileSize = 1000
97
- @testData.expectedDiskSpace = (4096 * 1)
98
- @testData.expectedFileSizeIncludeSubs = 1000
99
- @testData.expectedDiskSpaceIncludeSubs = (4096 * 1)
100
- @testData.expectedFileCount = 1
101
- @testData.expectedAvgFileSize = 1000
102
- @testData.expectedFileCountIncludeSubs = 1
103
- @testData.expectedAvgFileSizeIncludeSubs = 1000
104
- doTestDirSize(@testData)
95
+ @test_data.clusterSize = 4096
96
+ @test_data.expectedFileSize = 1000
97
+ @test_data.expectedDiskSpace = (4096 * 1)
98
+ @test_data.expectedFileSizeIncludeSubs = 1000
99
+ @test_data.expectedDiskSpaceIncludeSubs = (4096 * 1)
100
+ @test_data.expectedFileCount = 1
101
+ @test_data.expectedAvgFileSize = 1000
102
+ @test_data.expectedFileCountIncludeSubs = 1
103
+ @test_data.expectedAvgFileSizeIncludeSubs = 1000
104
+ do_test_dir_size(@test_data)
105
105
  end
106
106
 
107
- def testSubDir
107
+ def test_sub_dir
108
108
  makeSubDir('suba')
109
109
  makeSubDir("suba\\suba1")
110
110
  makeSampleTextFile('', 1000)
111
111
  makeSampleTextFile('suba', 1000)
112
112
  makeSampleTextFile("suba\\suba1", 1000)
113
113
  makeSampleTextFile("suba\\suba1", 2000)
114
- @testData.clusterSize = 4096
115
- @testData.expectedFileSize = 1000
116
- @testData.expectedDiskSpace = (4096 * 1)
117
- @testData.expectedFileSizeIncludeSubs = 5000
118
- @testData.expectedDiskSpaceIncludeSubs = (4096 * 4)
119
- @testData.expectedFileCount = 1
120
- @testData.expectedAvgFileSize = 1000
121
- @testData.expectedFileCountIncludeSubs = 4
122
- @testData.expectedAvgFileSizeIncludeSubs = 1250
123
- doTestDirSize(@testData)
114
+ @test_data.clusterSize = 4096
115
+ @test_data.expectedFileSize = 1000
116
+ @test_data.expectedDiskSpace = (4096 * 1)
117
+ @test_data.expectedFileSizeIncludeSubs = 5000
118
+ @test_data.expectedDiskSpaceIncludeSubs = (4096 * 4)
119
+ @test_data.expectedFileCount = 1
120
+ @test_data.expectedAvgFileSize = 1000
121
+ @test_data.expectedFileCountIncludeSubs = 4
122
+ @test_data.expectedAvgFileSizeIncludeSubs = 1250
123
+ do_test_dir_size(@test_data)
124
124
  end
125
125
 
126
- def testEmptyDir
127
- @testData.clusterSize = 4096
128
- @testData.expectedFileSize = 0
129
- @testData.expectedDiskSpace = 0
130
- @testData.expectedFileCount = 0
131
- @testData.expectedAvgFileSize = 0
132
- @testData.expectedFileCountIncludeSubs = 0
133
- @testData.expectedAvgFileSizeIncludeSubs = 0
134
- @testData.expectedFileSizeIncludeSubs = 0
135
- @testData.expectedDiskSpaceIncludeSubs = 0
136
- doTestDirSize(@testData)
126
+ def test_empty_dir
127
+ @test_data.clusterSize = 4096
128
+ @test_data.expectedFileSize = 0
129
+ @test_data.expectedDiskSpace = 0
130
+ @test_data.expectedFileCount = 0
131
+ @test_data.expectedAvgFileSize = 0
132
+ @test_data.expectedFileCountIncludeSubs = 0
133
+ @test_data.expectedAvgFileSizeIncludeSubs = 0
134
+ @test_data.expectedFileSizeIncludeSubs = 0
135
+ @test_data.expectedDiskSpaceIncludeSubs = 0
136
+ do_test_dir_size(@test_data)
137
137
  end
138
138
  end
@@ -51,13 +51,13 @@ class TestUtilFile < Test::Unit::TestCase
51
51
  File.chmod(attr, fileName)
52
52
  end
53
53
 
54
- def set_up
54
+ def setup
55
55
  @files = []
56
56
  end
57
57
 
58
58
  def teardown
59
59
  @files.each { | filename | File.delete_all(filename) if File.exists?(filename) }
60
- @dirs.reverse_each { | dirname | Dir.delete(dirname) if File.exists?(dirname) }
60
+ @dirs.reverse_each { | dirname | Dir.delete(dirname) if File.exists?(dirname) } if @dirs
61
61
  end
62
62
 
63
63
  def doTestDelTree(attr)
data/cl/util/util.rb CHANGED
@@ -3,8 +3,8 @@ require 'cl/util/version'
3
3
  # VERSION auto generated section
4
4
  begin
5
5
  require 'cl/util/version'
6
- VERSION = CLabs::Version.new('2010', '127', '0')
6
+ VERSION = CLabs::Version.new('2010', '129', '0')
7
7
  rescue LoadError
8
- VERSION = '2010.127.0'
8
+ VERSION = '2010.129.0'
9
9
  end
10
10
  # END_VERSION
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clutil
3
3
  version: !ruby/object:Gem::Version
4
- version: 2010.127.0
4
+ prerelease: false
5
+ segments:
6
+ - 2010
7
+ - 129
8
+ - 0
9
+ version: 2010.129.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - chrismo
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-05-07 00:00:00 -05:00
17
+ date: 2010-05-09 00:00:00 -05:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -62,18 +67,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
67
  requirements:
63
68
  - - ">="
64
69
  - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
65
72
  version: "0"
66
- version:
67
73
  required_rubygems_version: !ruby/object:Gem::Requirement
68
74
  requirements:
69
75
  - - ">="
70
76
  - !ruby/object:Gem::Version
77
+ segments:
78
+ - 1
79
+ - 3
80
+ - 6
71
81
  version: 1.3.6
72
- version:
73
82
  requirements: []
74
83
 
75
84
  rubyforge_project: scrapware
76
- rubygems_version: 1.3.5
85
+ rubygems_version: 1.3.6
77
86
  signing_key:
78
87
  specification_version: 3
79
88
  summary: cLabs Ruby Utilities