manman 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -7,3 +7,4 @@ lib/manman.rb
7
7
  lib/manman/opts.rb
8
8
  lib/manman/runner.rb
9
9
  lib/manman/version.rb
10
+ lib/manman/worker.rb
data/README.markdown CHANGED
@@ -4,10 +4,26 @@ Manifest Manager in Ruby
4
4
 
5
5
  * [geraldb.github.com/manman](http://geraldb.github.com/manman)
6
6
 
7
+ `manman` calculates MD5 digests/hashes for files listed in manifest template.
7
8
 
8
9
  ## Usage
9
10
 
10
- TBD
11
+ manman - Manifest Manager
12
+
13
+ Usage: manman [options]
14
+ -r, --release RELEASE Release Version (e.g. 2013.08)
15
+ -e, --env ENV Environment (e.g. TEST)
16
+ -u, --valid_until VALID Valid until (e.g. 2014.01.31)
17
+ -d, --dir PATH Path to Packages (default is ./paket)
18
+ -t, --template FILE Input Template (default is paket.txt.erb)
19
+ -o, --out FILE Output File (default is paket.txt)
20
+ -v, --version Show version
21
+ --verbose Show debug trace
22
+ -h, --help Show this message
23
+
24
+ Examples:
25
+ manman -r 2013.08 -e TEST
26
+ manman -r 2013.08 -e TEST -u 2014.12.31
11
27
 
12
28
 
13
29
  ## Install
@@ -17,6 +33,54 @@ Just install the gem:
17
33
  $ gem install manman
18
34
 
19
35
 
36
+
37
+
38
+ ## MD5 Digest Samples/Notes
39
+
40
+ ### What is MD5?
41
+
42
+ MD5 is a one-way hashing algorithm for 128 bit (16 byte) digest "signatures" or checksums
43
+ (e.g. `bd2e45b8fde5af0ead14ceb80ce3256a`).
44
+
45
+ ### MD5 Digest for Strings
46
+
47
+ require 'digest/md5'
48
+
49
+ digest = Digest::MD5.hexdigest( "Hello MD5 Digest!\n" )
50
+ puts digest
51
+
52
+ # => bd2e45b8fde5af0ead14ceb80ce3256a
53
+
54
+
55
+ ### MD5 Digest for Files
56
+
57
+ require 'digest/md5'
58
+
59
+ digest = Digest::MD5.hexdigest( File.read( ARGV[0] ) )
60
+ puts digest
61
+
62
+ # => fc2f4ec029715550401c99a188b904b1
63
+
64
+ ### MD5 Digest Calculation in Steps/Chunks
65
+
66
+ require 'digest/md5'
67
+
68
+ all_digest = Digest::MD5.hexdigest( File.read( ARGV[0] ) )
69
+
70
+ inc_digest = Digest::MD5.new
71
+ file = File.open( ARGV[0], 'r' )
72
+ file.each_line do |line|
73
+ inc_digest.update( line ) # or use <<-alias e.g. inc_digest << line
74
+ end
75
+
76
+ puts inc_digest.hexdigest
77
+ puts all_digest
78
+
79
+ # => fc2f4ec029715550401c99a188b904b1
80
+ # => fc2f4ec029715550401c99a188b904b1
81
+
82
+
83
+
20
84
  ## License
21
85
 
22
86
  The `manman` scripts are dedicated to the public domain.
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ Hoe.spec 'manman' do
11
11
  self.urls = ['http://geraldb.github.com/manman']
12
12
 
13
13
  self.author = 'Gerald Bauer'
14
- self.email = 'webslideshow@googlegroups.com'
14
+ self.email = 'gerald.bauer@example.com'
15
15
 
16
16
  # switch extension to .markdown for gihub formatting
17
17
  self.readme_file = 'README.markdown'
data/lib/manman.rb CHANGED
@@ -18,6 +18,7 @@ require 'digest/md5'
18
18
  require 'manman/version'
19
19
  require 'manman/opts'
20
20
  require 'manman/runner'
21
+ require 'manman/worker'
21
22
 
22
23
  module Manman
23
24
 
@@ -25,10 +26,6 @@ module Manman
25
26
  "manman #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
26
27
  end
27
28
 
28
- def self.root
29
- "#{File.expand_path( File.dirname(File.dirname(__FILE__)) )}"
30
- end
31
-
32
29
  def self.main
33
30
  Runner.new.run(ARGV)
34
31
  end
data/lib/manman/opts.rb CHANGED
@@ -2,6 +2,45 @@ module Manman
2
2
 
3
3
  class Opts
4
4
 
5
+ # extra headers for manifest
6
+ # - headers get ignored (assumed NOT to be files that get md5 checksum/digest/hash calculated)
7
+ def headers=(value)
8
+ # NB: value is supposed to be an array of strings
9
+ @headers = value
10
+ end
11
+
12
+ ## fix/todo:
13
+ ## also make it into a command line option!!
14
+
15
+ def headers
16
+ # NB: return value is supposed to be an array of strings
17
+ @headers || []
18
+ end
19
+
20
+
21
+ ## NB: for now pass in an obj, that is, self
22
+ ## in the future pass in proc? or something else?? to make it more configurable??
23
+
24
+ def filter_callback=(value)
25
+ @filter = value
26
+ end
27
+
28
+ def filter_callback
29
+ @filter # NB: has no default; returns nil if not set
30
+ end
31
+
32
+ def encrypt_callback=(value)
33
+ @encrypt = value
34
+ end
35
+
36
+ def encrypt_callback
37
+ @encrypt # NB: has no default; returns nil if not set
38
+ end
39
+
40
+
41
+
42
+
43
+
5
44
  def base=(value)
6
45
  @base = value
7
46
  end
@@ -54,15 +93,7 @@ class Opts
54
93
  def valid
55
94
  @valid # NB: has no default; required arg
56
95
  end
57
-
58
96
 
59
- def config_path=(value)
60
- @config_path = value
61
- end
62
-
63
- def config_path
64
- @config_path || '~/.manman'
65
- end
66
97
 
67
98
  end # class Opts
68
99
 
data/lib/manman/runner.rb CHANGED
@@ -2,9 +2,13 @@ module Manman
2
2
 
3
3
  class Runner
4
4
 
5
- def initialize
6
- @logger = Logger.new(STDOUT)
7
- @logger.level = Logger::INFO
5
+ def initialize( logger=nil )
6
+ if logger.nil?
7
+ @logger = Logger.new(STDOUT)
8
+ @logger.level = Logger::INFO
9
+ else
10
+ @logger = logger
11
+ end
8
12
 
9
13
  @opts = Opts.new
10
14
  end
@@ -42,11 +46,6 @@ class Runner
42
46
  end
43
47
 
44
48
 
45
-
46
- cmd.on( '-c', '--config PATH', "Configuration Path (default is #{opts.config_path})" ) do |path|
47
- opts.config_path = path
48
- end
49
-
50
49
  cmd.on( '-v', '--version', "Show version" ) do
51
50
  puts Manman.banner
52
51
  exit
@@ -79,130 +78,24 @@ EOS
79
78
 
80
79
  # check for required args
81
80
 
82
- puts "*** Missing Argument: Release Argument Required" if opts.release.nil?
83
- puts "*** Missing Argument: Environment Argument Required" if opts.env.nil?
84
- puts "*** Missing Argument: Environment Argument Required" if opts.valid.nil?
81
+ puts "*** Missing Argument: -r/--release Release Argument Required" if opts.release.nil?
82
+ puts "*** Missing Argument: -e/--env Argument Required" if opts.env.nil?
85
83
 
86
- if opts.release.nil? || opts.env.nil? || opts.valid.nil?
84
+ if opts.release.nil? || opts.env.nil?
87
85
  puts
88
86
  puts "Use -h/--help for usage options."
89
87
  return ## exit ??
90
88
  end
91
89
 
92
- puts "Using settings:"
93
- pp opts
94
-
95
-
96
- generate_manifest()
97
-
98
- puts 'Done.'
99
-
100
- end # method run
101
-
102
-
103
-
104
- def generate_manifest
105
-
106
- puts " working folder is: #{Dir.pwd}"
107
-
108
- puts "Loading template #{opts.template}..."
109
-
110
- old_lines = File.read( opts.template )
111
-
112
- new_lines = []
113
-
114
- header_lines = []
115
-
90
+ # puts "Using settings:"
91
+ # pp opts
116
92
 
117
- ### todo/fix: move to template ???
118
- header_lines << "####################################################\n"
119
- header_lines << "# generated on #{Time.now} using #{Manman.banner}\n"
120
- header_lines << "# - version: #{opts.release}, env: #{opts.env}, valid until: #{opts.valid}\n"
121
-
122
- old_lines.each_line do |line|
123
-
124
- if line =~ /^\s*#/
125
- # skip komments and do NOT copy to result (keep comments secret!)
126
- logger.debug 'skipping comment line'
127
- next
128
- end
93
+ worker = Worker.new( logger, opts )
94
+ worker.run
129
95
 
130
- if line =~ /^\s*$/
131
- # kommentar oder leerzeile überspringen
132
- logger.debug 'skipping blank line'
133
- new_lines << line
134
- next
135
- end
136
-
137
- # split/process key value pair
138
-
139
- tokens = line.split( ':' )
140
-
141
- key = tokens[0]
142
- values = tokens[1]
143
-
144
-
145
- ## special keys (NOT files; do NOT calculate md5 digest)
146
-
147
- ## todo: make headers configurable
148
-
149
- if [ 'VERSION', 'UMGEBUNG', 'MANDANT', 'GUELTIG_BIS', 'CHECKSUM' ].include?( key )
150
- if key == 'VERSION'
151
- new_lines << "#{key}: #{opts.release}\n"
152
- elsif key == 'UMGEBUNG'
153
- new_lines << "#{key}: #{opts.env}\n"
154
- elsif key == 'GUELTIG_BIS'
155
- new_lines << "#{key}: #{opts.valid}\n"
156
- else
157
- new_lines << line ## just pass header line throug; not modified
158
- end
159
- next
160
- end
161
-
162
- ### calculate md5 digests
163
-
164
- md5 = ""
165
- fn = "#{opts.base}/#{key}" # filename
166
-
167
- if File.exists?( fn ) == false
168
- md5 = "xxxxxxx"
169
- msg = "*** ERROR: #{key} missing; using path #{fn}"
170
- puts msg
171
- header_lines << "# #{msg}\n" # add error to header in manifest
172
- else
173
- md5 = calc_digest_md5( fn )
174
- puts "OK #{key} -> #{md5}"
175
- end
176
-
177
- new_lines << "#{key}: #{md5}, #{values}"
178
-
179
- end # oldlines.each
180
-
181
-
182
- header_lines << "#####################################\n"
183
- header_lines << "\n"
184
-
185
- new_lines = header_lines + new_lines
186
-
187
- File.open( opts.output, 'w') do |f|
188
- new_lines.each do |line|
189
- f.write( line )
190
- end
191
- end
96
+ # puts 'Done.'
192
97
 
193
- end # method generate_manifest
194
-
195
-
196
- def calc_digest_md5( fn )
197
- # digest/hash is a string of 20 hexadecimal 8-bit numbers
198
- # example:
199
- # 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
200
-
201
- md5 = Digest::MD5.hexdigest( File.open( fn, 'rb') { |f| f.read } )
202
- md5
203
- end
204
-
205
-
98
+ end # method run
206
99
 
207
100
  end # class Runner
208
101
  end # module Manman
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Manman
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
@@ -0,0 +1,189 @@
1
+ module Manman
2
+
3
+ class Worker
4
+
5
+ def initialize( logger=nil, opts=nil )
6
+ if logger.nil?
7
+ @logger = Logger.new(STDOUT)
8
+ @logger.level = Logger::INFO
9
+ else
10
+ @logger = logger
11
+ end
12
+
13
+ if opts.nil?
14
+ @opts = Opts.new
15
+ else
16
+ @opts = opts
17
+ end
18
+ end
19
+
20
+ attr_reader :logger, :opts
21
+
22
+
23
+ def run
24
+ puts "Using settings:"
25
+ pp opts
26
+
27
+ generate_manifest()
28
+
29
+ puts 'Done.'
30
+ end # method run
31
+
32
+
33
+ def generate_manifest
34
+
35
+ puts " working folder is: #{Dir.pwd}"
36
+
37
+ puts "Loading template #{opts.template}..."
38
+
39
+ old_lines = File.read( opts.template )
40
+
41
+ new_lines = []
42
+
43
+ header_lines = []
44
+
45
+
46
+ ### todo/fix: move to template ???
47
+ header_lines << "####################################################\n"
48
+ header_lines << "# generated on #{Time.now} using #{Manman.banner}\n"
49
+ header_lines << "# - version: #{opts.release}, env: #{opts.env}\n"
50
+
51
+ old_lines.each_line do |line|
52
+
53
+ if line =~ /^\s*#/
54
+ # skip komments and do NOT copy to result (keep comments secret!)
55
+ logger.debug 'skipping comment line'
56
+ next
57
+ end
58
+
59
+ if line =~ /^\s*$/
60
+ # kommentar oder leerzeile überspringen
61
+ logger.debug 'skipping blank line'
62
+ new_lines << line
63
+ next
64
+ end
65
+
66
+ # split/process key value pair
67
+
68
+ tokens = line.split( ':' )
69
+
70
+ key = tokens[0]
71
+ values = tokens[1]
72
+
73
+ ## special keys (NOT files; do NOT calculate md5 digest)
74
+
75
+ headers_release = [ 'RELEASE', 'VERSION' ]
76
+ headers_env = [ 'ENV', 'UMGEBUNG' ]
77
+ headers_valid = [ 'VALID_UNTIL', 'GUELTIG_BIS' ]
78
+ headers_checksum = [ 'CHECKSUM' ]
79
+
80
+ headers = headers_release + headers_env + headers_valid + headers_checksum
81
+ headers += opts.headers # add possible user defined extra headers
82
+
83
+ if headers.include?( key )
84
+ if headers_release.include?( key )
85
+ new_lines << "#{key}: #{opts.release}\n"
86
+ elsif headers_env.include?( key )
87
+ new_lines << "#{key}: #{opts.env}\n"
88
+ elsif headers_valid.include?( key )
89
+ new_lines << "#{key}: #{opts.valid}\n"
90
+ else # assume extra headers or checksum
91
+ new_lines << line ## just pass header line throug; not modified
92
+ end
93
+ next
94
+ end
95
+
96
+ ### calculate md5 digests
97
+
98
+ md5 = ""
99
+ fn = "#{opts.base}/#{key}" # filename
100
+
101
+ if File.exists?( fn ) == false
102
+ md5 = "xxxxxxx"
103
+ msg = "*** ERROR: #{key} missing; using path #{fn}"
104
+ puts msg
105
+ header_lines << "# #{msg}\n" # add error to header in manifest
106
+ else
107
+ md5 = calc_digest_md5( fn )
108
+ puts "OK #{key} -> #{md5}"
109
+ end
110
+
111
+ new_lines << "#{key}: #{md5}, #{values}"
112
+
113
+ end # oldlines.each
114
+
115
+
116
+ header_lines << "#####################################\n"
117
+ header_lines << "\n"
118
+
119
+ new_lines = header_lines + new_lines
120
+
121
+ # lines all in one string (no array/ary of lines)
122
+ new_lines_all_in_one = ""
123
+ new_lines.each do |line|
124
+ new_lines_all_in_one << line
125
+ end
126
+
127
+ puts "[debug] new_lines_all_in_one:"
128
+ puts new_lines_all_in_one
129
+
130
+ ## allow custom filter (lets you add custom headers,checksums,etc.)
131
+ new_lines_all_in_one = filter_callback( new_lines_all_in_one )
132
+
133
+ ## last step - calculate checksum
134
+ new_lines_all_in_one = generate_checksum( new_lines_all_in_one )
135
+
136
+ File.open( opts.output, 'w') do |f|
137
+ f.write( new_lines_all_in_one )
138
+ end
139
+
140
+ end # method generate_manifest
141
+
142
+
143
+ def encrypt_callback( text )
144
+ if opts.encrypt_callback.nil?
145
+ puts "[debug] no encrypt callback configured"
146
+ text # pass through; identity; do nothing
147
+ else
148
+ opts.encrypt_callback.encrypt_callback( text )
149
+ end
150
+ end
151
+
152
+ def filter_callback( text )
153
+ if opts.filter_callback.nil?
154
+ puts "[debug] no filter callback configured"
155
+ text
156
+ else
157
+ opts.filter_callback.filter_callback( text )
158
+ end
159
+ end
160
+
161
+ def generate_checksum( text )
162
+
163
+ # NB: remove possible old checksum in checksum line w/ empty line before md5 calculation
164
+ hash = Digest::MD5.hexdigest( text.gsub( /^CHECKSUM:.*/, '' ) )
165
+ puts "Paket-Hash >#{hash}<"
166
+ encrypted_hash = encrypt_callback( hash )
167
+
168
+ ## use strip why? why not?
169
+ ## was encrypted_hash.strip
170
+
171
+ text.gsub( /^CHECKSUM:.*/, "CHECKSUM: #{encrypted_hash}" )
172
+ end
173
+
174
+ def calc_digest_md5( fn )
175
+ # digest/hash is a string of 20 hexadecimal 8-bit numbers
176
+ # example:
177
+ # 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
178
+
179
+ ## NB: use b - binary
180
+ # required for Windows-only (avoids changing of newlines \n to \n\r or similar)
181
+
182
+ md5 = Digest::MD5.hexdigest( File.open( fn, 'rb') { |f| f.read } )
183
+ md5
184
+ end
185
+
186
+
187
+
188
+ end # class Worker
189
+ end # module Manman
metadata CHANGED
@@ -1,61 +1,46 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: manman
3
- version: !ruby/object:Gem::Version
4
- hash: 27
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 0
10
- version: 0.1.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Gerald Bauer
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-10-17 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-10-25 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rdoc
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &17647704 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
18
+ requirements:
26
19
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 19
29
- segments:
30
- - 3
31
- - 10
32
- version: "3.10"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.10'
33
22
  type: :development
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: hoe
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *17647704
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ requirement: &17647416 !ruby/object:Gem::Requirement
39
28
  none: false
40
- requirements:
29
+ requirements:
41
30
  - - ~>
42
- - !ruby/object:Gem::Version
43
- hash: 7
44
- segments:
45
- - 3
46
- - 0
47
- version: "3.0"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.1'
48
33
  type: :development
49
- version_requirements: *id002
34
+ prerelease: false
35
+ version_requirements: *17647416
50
36
  description: manman - Manifest Manager
51
- email: webslideshow@googlegroups.com
52
- executables:
37
+ email: gerald.bauer@example.com
38
+ executables:
53
39
  - manman
54
40
  extensions: []
55
-
56
- extra_rdoc_files:
41
+ extra_rdoc_files:
57
42
  - Manifest.txt
58
- files:
43
+ files:
59
44
  - History.markdown
60
45
  - Manifest.txt
61
46
  - README.markdown
@@ -65,39 +50,31 @@ files:
65
50
  - lib/manman/opts.rb
66
51
  - lib/manman/runner.rb
67
52
  - lib/manman/version.rb
53
+ - lib/manman/worker.rb
68
54
  homepage: http://geraldb.github.com/manman
69
55
  licenses: []
70
-
71
56
  post_install_message:
72
- rdoc_options:
57
+ rdoc_options:
73
58
  - --main
74
59
  - README.markdown
75
- require_paths:
60
+ require_paths:
76
61
  - lib
77
- required_ruby_version: !ruby/object:Gem::Requirement
62
+ required_ruby_version: !ruby/object:Gem::Requirement
78
63
  none: false
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- hash: 3
83
- segments:
84
- - 0
85
- version: "0"
86
- required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
69
  none: false
88
- requirements:
89
- - - ">="
90
- - !ruby/object:Gem::Version
91
- hash: 3
92
- segments:
93
- - 0
94
- version: "0"
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
95
74
  requirements: []
96
-
97
75
  rubyforge_project: manman
98
- rubygems_version: 1.8.24
76
+ rubygems_version: 1.7.2
99
77
  signing_key:
100
78
  specification_version: 3
101
79
  summary: manman - Manifest Manager
102
80
  test_files: []
103
-