rupat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7ae691281669321109b1670d88d059e938e726dc
4
+ data.tar.gz: 13a51789ca036f7adb3a2b69b68156bfe2cb45a1
5
+ SHA512:
6
+ metadata.gz: 40df100069bb7fd748c86a28b898a4e5350edf68cba53134735a1c67387cd1791887070db8f4d15167de028afc0d25792a807a565e7afe63b8ef8cad03067bb6
7
+ data.tar.gz: c7c3dcadeeb77bde6d926eda098c7c94974e1e4f5101c3a77afd0e9277a0e03d6a1a385b665421ca20b5849489bcc7afe11069a57f7e1a7af517cb6b2374ee4a
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Version history
2
+
3
+ [0.0.1] Initial version.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 tero.isannainen@gmail.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = Rupat
2
+
3
+
4
+ == Description
5
+
6
+ {Rupat} is Ruby Patching library. {Rupat} is mainly intended to patch
7
+ text files. It can also be used to just extract information from
8
+ files. {Rupat} comes with a library for general use and simple
9
+ executable for patching files.
10
+
11
+
12
+ == Documentation
13
+
14
+ Main documentation is generated from source (See: {Rupat}).
15
+
16
+ Test files in "test" directory includes multiple ways of how to use
17
+ {Rupat}.
data/bin/rupat ADDED
@@ -0,0 +1,183 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'como'
4
+ include Como
5
+ require 'readline'
6
+ require 'rupat'
7
+
8
+ Spec.command( "rupat", "Tero Isannainen", "2014",
9
+ [
10
+ [ :opt_multi, "cmdfile", "-c", "Command file(s)." ],
11
+ [ :opt_single, "cmdline", "-i", "Setup from command line." ],
12
+ [ :opt_multi, "patch", "-p", "Create Rupat patch file from file1 vs file2." ],
13
+ [ :opt_single, "patch_out", "-o", "Patch file name (default: STDOUT)." ],
14
+ [ :switch, "batch", "-b", "Batch mode (no interaction)." ],
15
+ ] )
16
+
17
+
18
+ # Rupat "global" scope.
19
+ rupatScope = binding
20
+
21
+ if Opt['cmdline'].given
22
+ # Perform command line setup.
23
+ eval Opt['cmdline'].value, rupatScope
24
+ end
25
+
26
+
27
+ if Opt['patch'].given
28
+
29
+ Opt.main.checkAlso( 'patch', '<patch> must have 2 args' ) do
30
+ value.length == 2
31
+ end
32
+
33
+ diff1 = Opt['patch'].value[0]
34
+ diff2 = Opt['patch'].value[1]
35
+
36
+ # Create diff lines.
37
+ f = IO.popen( "diff #{diff1} #{diff2}" )
38
+ lines = f.readlines
39
+
40
+
41
+ # Set output stream.
42
+ if Opt['patch_out'].given
43
+ fh = File.open( Opt['patch_out'].value, "w" )
44
+ else
45
+ fh = STDOUT
46
+ end
47
+
48
+ fh.puts "#!/usr/bin/env ruby
49
+
50
+ require 'rupat'
51
+
52
+ r = Rupat.edit( \"#{diff1}\" )
53
+ "
54
+
55
+ i = 0
56
+ offset = 0
57
+
58
+ while i < lines.length
59
+
60
+ line = lines[i]
61
+
62
+ case line
63
+
64
+ when /^([0-9]+)?,?([0-9]+)?d([0-9]+),?([0-9]+)?/
65
+
66
+ # Delete lines.
67
+ m = /^([0-9]+),?([0-9]+)?d([0-9]+),?([0-9]+)?/.match(line)
68
+ if m[2] == nil
69
+ range = [ m[1].to_i, 1 ]
70
+ else
71
+ range = [ m[1].to_i, m[2].to_i-m[1].to_i+1 ]
72
+ end
73
+ fh.puts "r.goto1 #{range[0]+offset}"
74
+ fh.puts "r.deleteMany #{range[1]}"
75
+ offset -= range[1]
76
+ i += 1
77
+
78
+ # Roll over the file1 lines.
79
+ while /<.*/.match( lines[i] )
80
+ i += 1
81
+ end
82
+
83
+ when /^([0-9]+),?([0-9]+)?a([0-9]+),?([0-9]+)?/
84
+
85
+ # Add lines.
86
+ m = /^([0-9]+),?([0-9]+)?a([0-9]+),?([0-9]+)?/.match(line)
87
+ if m[4] == nil
88
+ range = [ m[1].to_i+1, 1 ]
89
+ else
90
+ range = [ m[1].to_i+1, m[4].to_i-m[3].to_i+1 ]
91
+ end
92
+ fh.puts "r.goto1 #{range[0]+offset-1}"
93
+ i += 1
94
+
95
+ str = ""
96
+ while />.*/.match( lines[i] )
97
+ str += lines[i][2..-1]
98
+ i += 1
99
+ end
100
+
101
+ fh.puts "r.appendMany %q{#{str}}"
102
+ offset += range[1]
103
+
104
+
105
+ when /^([0-9]+),?([0-9]+)?c([0-9]+),?([0-9]+)?/
106
+
107
+ # Change lines.
108
+
109
+ # First delete...
110
+ m = /^([0-9]+),?([0-9]+)?c([0-9]+),?([0-9]+)?/.match(line)
111
+ if m[2] == nil
112
+ range = [ m[1].to_i, 1 ]
113
+ else
114
+ range = [ m[1].to_i, m[2].to_i-m[1].to_i+1 ]
115
+ end
116
+ fh.puts "r.goto1 #{range[0]+offset}"
117
+ fh.puts "r.deleteMany #{range[1]}"
118
+
119
+ # ... then add.
120
+ if m[4] == nil
121
+ range = [ m[1].to_i, 1 ]
122
+ else
123
+ range = [ m[1].to_i, m[4].to_i-m[3].to_i+1 ]
124
+ end
125
+ fh.puts "r.goto1 #{range[0]+offset-1}"
126
+
127
+ i += 1
128
+ # Roll over the file1 lines.
129
+ while /<.*/.match( lines[i] )
130
+ str += lines[i][2..-1]
131
+ i += 1
132
+ end
133
+ i += 1
134
+
135
+ str = ""
136
+ while />.*/.match( lines[i] )
137
+ str += lines[i][2..-1]
138
+ i += 1
139
+ end
140
+ fh.puts "r.appendMany %q{#{str}}"
141
+ offset += range[1]
142
+
143
+ end
144
+
145
+ end
146
+
147
+ fh.puts "r.print"
148
+
149
+ fh.close
150
+
151
+ # Make file executable.
152
+ if fh != STDOUT
153
+ file = Opt['patch_out'].value
154
+ File.chmod( File.stat( file ).mode | 0100, file )
155
+ end
156
+
157
+ else
158
+
159
+ begin
160
+
161
+ if !Opt['cmdfile'].given && !Opt['batch'].given
162
+
163
+ hist = []
164
+
165
+ while true
166
+ cmd = Readline.readline( "rupat> ", hist )
167
+ break if cmd == nil
168
+ eval cmd, rupatScope
169
+ end
170
+
171
+ else
172
+
173
+ Opt['cmdfile'].value.each do |c|
174
+ load c
175
+ end
176
+
177
+ end
178
+
179
+ rescue
180
+ puts "RUPAT: Command file errors... No actions performed!"
181
+ end
182
+
183
+ end