hmstools 0.0.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.
Files changed (2) hide show
  1. data/lib/hmstools.rb +216 -0
  2. metadata +65 -0
data/lib/hmstools.rb ADDED
@@ -0,0 +1,216 @@
1
+ ###
2
+ # Copyright (c) 2011 Jeff Kreitner hitmyserver.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ ###
23
+
24
+ #### HMSTools
25
+ #
26
+ # Classes
27
+ #
28
+ # LOCReader - Reads Lines of Code in a given directory
29
+ # XFDL - Decompresses xfdl files and recompresses XML files into XFDL
30
+ # requires: base64, zlib, stringio
31
+ ####
32
+
33
+ module HMSTools
34
+
35
+ VERSION = 0.1
36
+
37
+
38
+ # Basic Lines Of Code reader
39
+ # Searches through all subfolders
40
+ # You must set the extensions to search. Takes a string, comma seperated
41
+
42
+ class LOCReader
43
+
44
+ @ext = []
45
+
46
+ @blank_lines = 0
47
+ @lines = 0
48
+ @num_files = 0
49
+ @total = 0
50
+
51
+ def initialize
52
+ @lines = @lines.to_i
53
+ @blank_lines = @blank_lines.to_i
54
+ @num_files = @lines.to_i
55
+ @total = @total.to_i
56
+ end
57
+
58
+ def run(dir, ext = "")
59
+ unless ext == ""
60
+ @ext = ext.split(',')
61
+ end
62
+
63
+ get_files dir
64
+ @total = @lines - @blank_lines
65
+ end
66
+
67
+ def get_files(dir)
68
+
69
+ files = Dir.glob(dir)
70
+ dir_path = dir.chomp("*")
71
+
72
+ for file in files
73
+ unless File.directory?(file)
74
+ if @ext.include?(File.extname(file))
75
+ read_lines file
76
+ @num_files = @num_files+1
77
+ end
78
+
79
+ else
80
+ get_files file+"/*"
81
+ end
82
+
83
+ end
84
+ end
85
+
86
+ def read_lines(file)
87
+ begin
88
+ ofile = File.new(file, "r")
89
+ while (line = ofile.gets)
90
+ @lines += 1
91
+ if line.strip == ""
92
+ @blank_lines += 1
93
+ end
94
+ end
95
+ ofile.close
96
+ rescue => err
97
+ puts "Could not access #{err}"
98
+ end
99
+ end
100
+
101
+ def print_array
102
+ [@num_files, @total, @blank_lines, @lines]
103
+ end
104
+
105
+ def print_hash
106
+ {"files" => @num_files, "loc" => @total, "bloc" => @blank_lines, "lines" => @lines}
107
+ end
108
+
109
+ def print_output
110
+ print "Files: #@num_files \n"
111
+ print "Lines of Code: #@total \n"
112
+ print "Blank Lines: #@blank_lines \n"
113
+ print "Total Lines: #@lines"
114
+ end
115
+
116
+ end
117
+
118
+ class XFDL
119
+
120
+ def b64decode_it(contents = nil)
121
+ x = Base64.decode64(contents)
122
+ return x
123
+ end
124
+
125
+ def b64encode_it(contents = nil)
126
+ unless contents.nil?
127
+ x = Base64.encode64(contents)
128
+ return x
129
+ end
130
+ end
131
+
132
+ def build_and_write_xfdl_from_directory(xmldirectory, xfdldirectory)
133
+ @files = Dir.glob(xmldirectory)
134
+ numfiles = 0
135
+ numxmlfiles = 0
136
+
137
+ for file in @files
138
+ if File.extname(file) == ".xml"
139
+ print "Packaging "+ File.basename(file) +" to " + File.basename(file, ".xml") +".xfdl"
140
+ build_and_write_xfdl_from_xml(file, xfdldirectory+"/"+File.basename(file, ".xml")+".xfdl")
141
+ print " - Complete\n"
142
+ numxmlfiles += 1
143
+ end
144
+
145
+ numfiles += 1
146
+ end
147
+
148
+ print "\n" + numxmlfiles.to_s + " out of " + numfiles.to_s + " were XML files and processed to the XFDL format!"
149
+ end
150
+
151
+ def build_and_write_xfdl_from_xml(xmlfile, xfdlfile)
152
+ # 1. Gzip
153
+ # 2. Base64
154
+ # 3. Write string with mime stuff on the fist line
155
+ # 4. Save as XFDL
156
+
157
+ xml_file = ''
158
+ b64_string = ''
159
+ new_contents = ''
160
+
161
+ if File.exists?(xmlfile)
162
+ xml_file = IO.read(xmlfile).to_s
163
+ end
164
+
165
+ # Tell Zlib to use StringIO instead of a file.. Suck it Zlib!
166
+ a = StringIO.new ""
167
+
168
+ gz = Zlib::GzipWriter.new(a)
169
+ gz.write xml_file
170
+ gz.close
171
+
172
+ b64_string = b64encode_it(a.string)
173
+ new_contents = "application/vnd.xfdl;content-encoding=\"base64-gzip\"\n" + b64_string
174
+
175
+ File.open(xfdlfile, "w") { |f| f.write(new_contents) }
176
+
177
+ end
178
+
179
+ def deconstruct_and_write_xml(old_filename, new_filename)
180
+
181
+ if File.exists?(old_filename)
182
+ enc_file = ''
183
+ enc_file = IO.read(old_filename).to_s
184
+ content = get_xml enc_file
185
+ File.open(new_filename, "w") { |f| f.write(content) }
186
+ else
187
+ puts "File does not exist!"
188
+ return nil
189
+ end
190
+
191
+ end
192
+
193
+ def initialize
194
+ require 'base64'
195
+ require 'zlib'
196
+ require 'stringio'
197
+ end
198
+
199
+
200
+ def get_xml(contents)
201
+ b64_decoded = ''
202
+ xml_contents = ''
203
+
204
+ b64_decoded = b64decode_it(contents.lines.to_a[1..-1].join)
205
+
206
+ # Tell Zlib to use StringIO instead of a file.. Suck it Zlib!
207
+ a = StringIO.new b64_decoded
208
+
209
+ gz = Zlib::GzipReader.new(a)
210
+ xml_contents = gz.read
211
+ gz.close
212
+
213
+ return xml_contents
214
+ end
215
+ end
216
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hmstools
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jeff Kreitner
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-02 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: HMSTools provides a Line of Code reader and XFDL compilation/decompilation. These are tools used by HitMyServer in various projects
22
+ email: codeshare@hitmyserver.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/hmstools.rb
31
+ homepage: http://hitmyserver.com/hmstools
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Line of Code reader and XFDL compilation/decompilation!
64
+ test_files: []
65
+