clutil 2014.304.0 → 2014.304.1

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.
@@ -0,0 +1,84 @@
1
+ require 'rbconfig'
2
+ require 'find'
3
+ require 'fileutils'
4
+ require "#{File.dirname(__FILE__)}/console"
5
+
6
+ module CLabs
7
+ class Install
8
+ include RbConfig
9
+
10
+ attr_reader :version, :libdir, :bindir, :sitedir, :siteverdir, :archdir
11
+
12
+ def initialize
13
+ init
14
+ end
15
+
16
+ # altered copy of rbconfig.rb -> Config::expand
17
+ def custom_expand(val, conf)
18
+ val.gsub!(/\$\(([^()]+)\)/) do |var|
19
+ key = $1
20
+ if CONFIG.key? key
21
+ custom_expand(conf[key], conf)
22
+ else
23
+ var
24
+ end
25
+ end
26
+ val
27
+ end
28
+
29
+ def get_conf(customizations)
30
+ conf = {}
31
+ MAKEFILE_CONFIG.each { |k, v| conf[k] = v.dup }
32
+ customizations.each do |k, v|
33
+ conf[k] = v
34
+ end
35
+ conf.each_value do |val|
36
+ custom_expand(val, conf)
37
+ end
38
+ conf
39
+ end
40
+
41
+ def init
42
+ prefixdir = get_switch('-p')
43
+ customizations = {}
44
+ customizations['prefix'] = prefixdir if prefixdir
45
+ conf = get_conf(customizations)
46
+
47
+ @version = conf["MAJOR"]+"."+conf["MINOR"]
48
+ @libdir = File.join(conf["libdir"], "ruby", @version)
49
+
50
+ @bindir = conf["bindir"]
51
+ @sitedir = conf["sitedir"] || File.join(@libdir, "site_ruby")
52
+ @siteverdir = File.join(@sitedir, @version)
53
+ @archdir = File.join(@libdir, "i586-mswin32")
54
+ end
55
+
56
+ def install_executables(files)
57
+ tmpfn = "cl_tmp"
58
+ files.each do |aFile, dest|
59
+ File.open(aFile) do |ip|
60
+ File.open(tmpfn, "w") do |op|
61
+ ruby = File.join($bindir, "ruby")
62
+ op.puts "#!#{ruby}"
63
+ op.write ip.read
64
+ end
65
+ end
66
+
67
+ File::makedirs(dest)
68
+ # 0755 I assume means read/write/execute, not needed for Windows
69
+ File::chmod(0755, dest, true)
70
+ File::install(tmpfn, File.join(dest, File.basename(aFile)), 0755, true)
71
+ File::unlink(tmpfn)
72
+ end
73
+ end
74
+
75
+ def install_lib(files)
76
+ files.each do |aFile, dest|
77
+ File::makedirs(dest)
78
+ File::install(aFile, File.join(dest, File.basename(aFile)), 0644, true)
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+
@@ -0,0 +1,34 @@
1
+ require "#{File.dirname(__FILE__)}/install"
2
+ init
3
+
4
+ $cldir = File.join($siteverdir, "cl")
5
+ $clutildir = File.join($cldir, "util")
6
+ $clutiltestdir = File.join($clutildir, "test")
7
+
8
+ $libfiles = { 'util.rb' => $cldir ,
9
+
10
+ "file.rb" => $clutildir ,
11
+ "win.rb" => $clutildir ,
12
+ "dirsize.rb" => $clutildir ,
13
+ "test.rb" => $clutildir ,
14
+ "time.rb" => $clutildir ,
15
+ "console.rb" => $clutildir ,
16
+ "progress.rb" => $clutildir ,
17
+ "install.rb" => $clutildir ,
18
+ "smtp.rb" => $clutildir ,
19
+ "string.rb" => $clutildir ,
20
+ "version.rb" => $clutildir ,
21
+ "clqsend.rb" => $bindir ,
22
+
23
+ "./test/dirsizetest.rb" => $clutiltestdir,
24
+ "./test/filetest.rb" => $clutiltestdir ,
25
+ "./test/installtest.rb" => $clutiltestdir ,
26
+ "./test/progresstest.rb" => $clutiltestdir,
27
+ "./test/stringtest.rb" => $clutiltestdir,
28
+ "./test/versiontest.rb" => $clutiltestdir,
29
+ "./test/wintest.rb" => $clutiltestdir
30
+ }
31
+
32
+ if __FILE__ == $0
33
+ install_lib($libfiles)
34
+ end
data/cl/util/net.rb ADDED
@@ -0,0 +1,35 @@
1
+ module ClUtilNet
2
+ def ping(host)
3
+ (`ping -n 1 #{host}` =~ "Request timed out") == nil
4
+ end
5
+ end
6
+
7
+ class NetUse
8
+ def initialize(unc, user='', password='')
9
+ @unc = unc
10
+ @user = user
11
+ @password = password
12
+ end
13
+
14
+ def attached?
15
+
16
+ end
17
+
18
+ def attach(drive='')
19
+
20
+ end
21
+
22
+ def detach
23
+
24
+ end
25
+ end
26
+
27
+ if __FILE__ == $0
28
+ include ClUtilNet
29
+
30
+ puts "Pinging 66.169.210.208"
31
+ puts ping("66.169.210.208")
32
+
33
+ puts "Pinging localhost"
34
+ puts ping("localhost")
35
+ end
@@ -0,0 +1,71 @@
1
+ class Progress
2
+ def initialize(total)
3
+ @total = total
4
+ @current = 0
5
+ end
6
+
7
+ def start
8
+ @start = Time.now
9
+ @in_place_called_already = false
10
+ end
11
+
12
+ def step
13
+ @current += 1
14
+ end
15
+
16
+ def elapsed
17
+ (Time.now - @start)
18
+ end
19
+
20
+ def format_seconds(seconds)
21
+ minutes, seconds = seconds.divmod 60
22
+ hours, minutes = minutes.divmod 60
23
+ sprintf("%02d:%02d:%02d", hours, minutes, seconds)
24
+ end
25
+
26
+ def elapsedf
27
+ format_seconds(elapsed)
28
+ end
29
+
30
+ def done
31
+ @current
32
+ end
33
+
34
+ def remaining
35
+ @total - @current
36
+ end
37
+
38
+ def est_remaining_time
39
+ ((elapsed / done) * remaining)
40
+ end
41
+
42
+ def est_remaining_timef
43
+ format_seconds(est_remaining_time)
44
+ end
45
+
46
+ def progress(doStep=false)
47
+ step if doStep
48
+ "#{@current.to_s}/#{@total.to_s} done. Elapsed: #{elapsedf} Est Remain: #{est_remaining_timef}"
49
+ end
50
+
51
+
52
+ def pct_done
53
+ if @total > 0
54
+ pct = ((done.to_f / @total.to_f) * 100).to_i
55
+ else
56
+ pct = 0
57
+ end
58
+
59
+ pct.to_s.rjust(3) + '%'
60
+ end
61
+
62
+ def in_place_pct(doStep=false)
63
+ step if doStep
64
+ rubout = 8.chr
65
+ out = ''
66
+ out << (rubout * 4) if @in_place_called_already
67
+ out << pct_done
68
+ @in_place_called_already = true
69
+ out
70
+ end
71
+ end
data/cl/util/smtp.rb ADDED
@@ -0,0 +1,138 @@
1
+ require 'net/smtp'
2
+ require 'time'
3
+
4
+ module ClUtil
5
+ class Attachment
6
+ def self.load_from_file(filename)
7
+ data = nil
8
+ File.open(filename, 'rb') do |f|
9
+ data = f.read()
10
+ end
11
+ data = [data].pack("m*")
12
+ Attachment.new(File.basename(filename), data)
13
+ end
14
+
15
+ attr_reader :name, :data
16
+
17
+ def initialize(name, data)
18
+ @name = name
19
+ @data = data
20
+ end
21
+ end
22
+
23
+ class Smtp
24
+ attr_reader :attachments
25
+ attr_accessor :from, :subj, :body, :extra_headers, :username, :password, :auth_type, :enable_starttls, :port
26
+
27
+ def initialize(smtpsrv='localhost', port=25)
28
+ @smtpsrv = smtpsrv
29
+ @port = port
30
+ @attachments = []
31
+ @username = nil
32
+ @password = nil
33
+ @auth_type = :login
34
+ @enable_starttls = false
35
+ end
36
+
37
+ def to
38
+ @to
39
+ end
40
+
41
+ def to=(value)
42
+ @to = [value].flatten
43
+ end
44
+
45
+ def sendmail
46
+ msg = build_message
47
+ @smtp = Net::SMTP.new(@smtpsrv, @port)
48
+ start_tls_if_needed
49
+ @smtp.start('localhost.localdomain', @username, @password, @auth_type) do |smtp|
50
+ smtp.sendmail(msg, @from, @to)
51
+ end
52
+ end
53
+
54
+ def start_tls_if_needed
55
+ return if !@enable_starttls
56
+ # require 'rubygems'
57
+ # gem 'smtp_tls'
58
+ # require 'smtp_tls'
59
+ @smtp.enable_starttls
60
+ end
61
+
62
+ def content_type
63
+ @body =~ /<html>/ ? "text/html" : "text/plain"
64
+ end
65
+
66
+ def build_message
67
+ msg = format_headers
68
+ boundary = create_boundary
69
+
70
+ if !@attachments.empty?
71
+ msg << [
72
+ "Content-Type: multipart/mixed; boundary=\"#{boundary}\"\n",
73
+ "\n",
74
+ "This is a multi-part message in MIME format.\n",
75
+ "\n"
76
+ ]
77
+ end
78
+
79
+ if @body
80
+ msg << [ "--#{boundary}\n" ] if !@attachments.empty?
81
+ msg << [
82
+ "Content-Type: #{content_type}; charset=\"iso-8859-1\"\n",
83
+ "Content-Transfer-Encoding: 8bit\n",
84
+ "\n",
85
+ "#{@body}\n",
86
+ "\n"
87
+ ]
88
+ end
89
+
90
+ @attachments.each do |attachment|
91
+ basename = attachment.name
92
+ msg << [
93
+ "--#{boundary}\n",
94
+ "Content-Type: application/octet-stream; name=\"#{basename}\"\n",
95
+ "Content-Transfer-Encoding: base64\n",
96
+ "Content-Disposition: attachment; filename=\"#{basename}\"\n",
97
+ "\n",
98
+ "#{attachment.data}", # no \n needed
99
+ "\n"
100
+ ]
101
+ end
102
+
103
+ msg << ["--#{boundary}--\n"] if !@attachments.empty?
104
+
105
+ msg.flatten!
106
+ end
107
+
108
+ def create_boundary()
109
+ return "_____clabs_smtp_boundary______#{Time.new.to_i.to_s}___"
110
+ end
111
+
112
+ def format_headers
113
+ headers = ["Subject: #{subj}", "From: #{from}", "To: #{to.join(";")}", "Date: #{Time.now.rfc2822}", "MIME-Version: 1.0" ]
114
+ headers << @extra_headers if @extra_headers
115
+ headers.flatten!
116
+ headers.collect! { |hdr| hdr.strip << "\n" }
117
+ [headers].flatten
118
+ end
119
+ end
120
+ end
121
+
122
+ # deprecated - use ClUtil::Smtp instead. The attachments argument is designed
123
+ # to be either a single filename or an array of filenames.
124
+ def sendmail(to, from, subj, body, smtpsrv=nil, attachments=nil, extra_headers=nil)
125
+ smtpsrv = 'localhost' if !smtpsrv
126
+ smtp = ClUtil::Smtp.new(smtpsrv)
127
+ smtp.to = to
128
+ smtp.from = from
129
+ smtp.subj = subj
130
+ smtp.body = body
131
+ smtp.extra_headers = extra_headers
132
+ attachments = [attachments].flatten
133
+ attachments.compact!
134
+ attachments.each do |attachment_fn|
135
+ smtp.attachments << ClUtil::Attachment.load_from_file(attachment_fn)
136
+ end
137
+ smtp.sendmail
138
+ end
data/cl/util/string.rb ADDED
@@ -0,0 +1,38 @@
1
+ class String
2
+ def get_indent
3
+ scan(/^(\s*)/).flatten[0].to_s
4
+ end
5
+
6
+ def rbpath
7
+ self.gsub(/\\/, '/')
8
+ end
9
+
10
+ def winpath
11
+ self.gsub(/\//, "\\")
12
+ end
13
+ end
14
+
15
+ def indent(s, amt)
16
+ a = s.split("\n", -1)
17
+ if amt >= 0
18
+ a.collect! do |ln|
19
+ if !ln.empty?
20
+ (' ' * amt) + ln
21
+ else
22
+ ln
23
+ end
24
+ end
25
+ else
26
+ a.collect! do |ln|
27
+ (1..amt.abs).each do ln = ln[1..-1] if ln[0..0] == ' ' end
28
+ ln
29
+ end
30
+ end
31
+ a.join("\n")
32
+ end
33
+
34
+ def here_ltrim(s, add_indent_amt=0)
35
+ a = s.split("\n", -1)
36
+ trim_indent_amt = a[0].get_indent.length
37
+ indent(s, add_indent_amt - trim_indent_amt)
38
+ end
data/cl/util/test.rb ADDED
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/file'
2
+ require 'fileutils'
3
+ require 'tmpdir'
4
+ require 'test/unit'
5
+
6
+ class TempDirTest < Test::Unit::TestCase
7
+ def set_temp_dir
8
+ @temp_dir = Dir.mktmpdir
9
+ end
10
+
11
+ def setup
12
+ @file_name_inc = 0
13
+ set_temp_dir
14
+ FileUtils::makedirs(@temp_dir) if !FileTest.directory?(@temp_dir)
15
+ end
16
+
17
+ def teardown
18
+ ClUtilFile.delTree(@temp_dir)
19
+ end
20
+
21
+ # to ward off the new Test::Unit detection of classes with no test
22
+ # methods
23
+ def default_test
24
+ super unless(self.class == TempDirTest)
25
+ end
26
+
27
+ def make_sub_dir(dirname)
28
+ newdirname = File.join(@temp_dir, dirname)
29
+ FileUtils::makedirs(newdirname) if !FileTest.directory?(newdirname)
30
+ newdirname
31
+ end
32
+
33
+ def make_sample_text_file(dirname='', size=0)
34
+ crlf_length = 2
35
+
36
+ if size == 0
37
+ content = 'this is a sample file'
38
+ else
39
+ content = ''
40
+ (size - crlf_length).times do content << 'x' end
41
+ end
42
+
43
+ if dirname.empty?
44
+ sample_file_dir = @temp_dir
45
+ else
46
+ sample_file_dir = File.join(@temp_dir, dirname)
47
+ end
48
+
49
+ @file_name_inc += 1
50
+ filename = File.join(sample_file_dir, 'sample' + @file_name_inc.to_s + '.txt')
51
+ File.open(filename, 'w+') do |f|
52
+ f.puts content
53
+ end
54
+ filename
55
+ end
56
+ end