MrMurano 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3d8ab1106bc7c79174b671075339b574dd017de
4
- data.tar.gz: 53c2be7de4579527f6174ffbc0957109d3a29d51
3
+ metadata.gz: 9d80701051b1837ecfe462e1487ec2cc238e54b9
4
+ data.tar.gz: 40e4cca578b534b259092f496d161697969e5bf6
5
5
  SHA512:
6
- metadata.gz: 36c4e019e27f568af176f0544ac3137c720aab8f4a7e49f83d00faefdea09b23d9e3bc8855cf008ed4b6c3620dda5bf58b28296303166bd3567f1a86505d2611
7
- data.tar.gz: cc2e8f89109bd70c3fb6aaaa8bdf28c12caeeb6aedb47e0294445124a628d5b4d3e034259df287d92241be45a1eabaa3326ee7f4b37aecd089126711346d0ca9
6
+ metadata.gz: e24973636dfb33ec1343886be2196d03338ea377a49f7b7a6598fe456c9b426c0064a89f9dc99e8199a18c226287a63ab1fe2deb958a44434f2103b925e18a1f
7
+ data.tar.gz: 53bed541a095ba1ecf0ba52003e3f13c0a5f2c563737a9e6bcd51634c3aebee02dbc1107f9a4bbb96e0ac2f5babb619a40c2fa4c6f347d9aac4371c5e62f675f
@@ -1,6 +1,8 @@
1
1
  require 'uri'
2
2
  require 'net/http'
3
3
  require 'json'
4
+ require 'tempfile'
5
+ require 'shellwords'
4
6
  require 'pp'
5
7
 
6
8
  module MrMurano
@@ -59,6 +61,7 @@ module MrMurano
59
61
  else
60
62
  say_error "got #{response} from #{request} #{request.uri.to_s}"
61
63
  say_error ":: #{response.body}"
64
+ say_error '==='
62
65
  raise response
63
66
  end
64
67
  end
@@ -90,13 +93,35 @@ module MrMurano
90
93
 
91
94
  # …
92
95
 
96
+ ##
97
+ # Compute a remote resource name from the local path
98
+ # @param root Pathname: Root path for this resource type from config files
99
+ # @param path Pathname: Path to local item
100
+ # @return String: remote resource name
93
101
  def toremotename(root, path)
94
102
  path = Pathname.new(path) unless path.kind_of? Pathname
95
103
  root = Pathname.new(root) unless root.kind_of? Pathname
96
104
  path.relative_path_from(root).to_s
97
105
  end
106
+
107
+ ##
108
+ # Compute the local name from remote item details
109
+ # @param item Hash: listing details for the item.
110
+ # @param itemkey Symbol: Key for look up.
111
+ def tolocalname(item, itemkey)
112
+ item[itemkey]
113
+ end
114
+
115
+ ##
116
+ # Compute the local path from the listing details
117
+ #
118
+ # If there is already a matching local item, some of its details are also in
119
+ # the item hash.
120
+ #
121
+ # @param into Pathname: Root path for this resource type from config files
122
+ # @param item Hash: listing details for the item.
123
+ # @return Pathname: path to save (or merge) remote item into
98
124
  def tolocalpath(into, item)
99
- into.mkpath unless $cfg['tool.dry']
100
125
  return item[:local_path] if item.has_key? :local_path
101
126
  itemkey = @itemkey.to_sym
102
127
  name = tolocalname(item, itemkey)
@@ -111,7 +136,20 @@ module MrMurano
111
136
  end
112
137
  raise "Not a directory: #{from.to_s}" unless from.directory?
113
138
 
114
- Pathname.glob(from.to_s + '/**/*').map do |path|
139
+ from.children.map do |path|
140
+ if path.directory? then
141
+ # TODO: look for definition. ( ?.rockspec? ?mr.modules? ?mr.manifest? )
142
+ # Lacking definition, find all *.lua but not *_test.lua
143
+ # This specifically and intentionally only goes one level deep.
144
+ path.children
145
+ else
146
+ path
147
+ end
148
+ end.flatten.compact.reject do |path|
149
+ path.fnmatch('*_test.lua') or path.basename.fnmatch('.*')
150
+ end.select do |path|
151
+ path.extname == '.lua'
152
+ end.map do |path|
115
153
  name = toremotename(from, path)
116
154
  case name
117
155
  when Hash
@@ -128,133 +166,115 @@ module MrMurano
128
166
  item[key]
129
167
  end
130
168
 
131
- def syncup(from, options={})
132
- there = list()
133
- here = locallist(from)
134
- itemkey = @itemkey.to_sym
135
-
136
- # split into three lists.
137
- # - Items here and not there. (toadd)
138
- # - Items there and not here. (todel)
139
- # - Items here and there. (tomod)
140
- therebox = {}
141
- there.each do |item|
142
- item = Hash.transform_keys_to_symbols(item)
143
- therebox[ synckey(item) ] = item
144
- end
145
- herebox = {}
146
- here.each do |item|
147
- item = Hash.transform_keys_to_symbols(item)
148
- herebox[ synckey(item) ] = item
169
+ def download(local, item)
170
+ local.dirname.mkpath
171
+ id = item[@itemkey.to_sym]
172
+ local.open('wb') do |io|
173
+ fetch(id) do |chunk|
174
+ io.write chunk
175
+ end
149
176
  end
150
- toadd = herebox.keys - therebox.keys
151
- todel = therebox.keys - herebox.keys
152
- tomod = herebox.keys & therebox.keys
177
+ end
178
+
179
+ def removelocal(dest, item)
180
+ dest.unlink
181
+ end
182
+
183
+ def syncup(from, options=Commander::Command::Options.new)
184
+ itemkey = @itemkey.to_sym
185
+ options.asdown=false
186
+ dt = status(from, options)
187
+ toadd = dt[:toadd]
188
+ todel = dt[:todel]
189
+ tomod = dt[:tomod]
153
190
 
154
191
  if options.delete then
155
- todel.each do |key|
156
- verbose "Removing item #{key}"
192
+ todel.each do |item|
193
+ verbose "Removing item #{item[:synckey]}"
157
194
  unless $cfg['tool.dry'] then
158
- item = therebox[key]
159
195
  remove(item[itemkey])
160
196
  end
161
197
  end
162
198
  end
163
199
  if options.create then
164
- toadd.each do |key|
165
- verbose "Adding item #{key}"
200
+ toadd.each do |item|
201
+ verbose "Adding item #{item[:synckey]}"
166
202
  unless $cfg['tool.dry'] then
167
- item = herebox[key]
168
203
  upload(item[:local_path], item.reject{|k,v| k==:local_path})
169
204
  end
170
205
  end
171
206
  end
172
207
  if options.update then
173
- tomod.each do |key|
174
- verbose "Updating item #{key}"
208
+ tomod.each do |item|
209
+ verbose "Updating item #{item[:synckey]}"
175
210
  unless $cfg['tool.dry'] then
176
- #item = therebox[key].merge herebox[key] # need to be consistent with key types for this to work
177
- id = therebox[key][itemkey]
178
- item = herebox[key].dup
179
- item[itemkey] = id
180
211
  upload(item[:local_path], item.reject{|k,v| k==:local_path})
181
212
  end
182
213
  end
183
214
  end
184
215
  end
185
216
 
186
- def syncdown(into, options={})
187
- there = list()
188
- into = Pathname.new(into) unless into.kind_of? Pathname
189
- here = locallist(into)
190
- itemkey = @itemkey.to_sym
191
-
192
- # split into three lists.
193
- # - Items here and not there. (todel)
194
- # - Items there and not here. (toadd)
195
- # - Items here and there. (tomod)
196
- therebox = {}
197
- there.each do |item|
198
- item = Hash.transform_keys_to_symbols(item)
199
- therebox[ synckey(item) ] = item
200
- end
201
- herebox = {}
202
- here.each do |item|
203
- item = Hash.transform_keys_to_symbols(item)
204
- herebox[ synckey(item) ] = item
205
- end
206
- todel = herebox.keys - therebox.keys
207
- toadd = therebox.keys - herebox.keys
208
- tomod = herebox.keys & therebox.keys
217
+ def syncdown(into, options=Commander::Command::Options.new)
218
+ options.asdown = true
219
+ dt = status(into, options)
220
+ toadd = dt[:toadd]
221
+ todel = dt[:todel]
222
+ tomod = dt[:tomod]
209
223
 
210
224
  if options.delete then
211
- todel.each do |key|
212
- verbose "Removing item #{key}"
225
+ todel.each do |item|
226
+ verbose "Removing item #{item[:synckey]}"
213
227
  unless $cfg['tool.dry'] then
214
- item = herebox[key]
215
228
  dest = tolocalpath(into, item)
216
229
  removelocal(dest, item)
217
230
  end
218
231
  end
219
232
  end
220
233
  if options.create then
221
- toadd.each do |key|
222
- verbose "Adding item #{key}"
234
+ toadd.each do |item|
235
+ verbose "Adding item #{item[:synckey]}"
223
236
  unless $cfg['tool.dry'] then
224
- item = therebox[key]
225
237
  dest = tolocalpath(into, item)
226
-
227
238
  download(dest, item)
228
239
  end
229
240
  end
230
241
  end
231
242
  if options.update then
232
- tomod.each do |key|
233
- verbose "Updating item #{key}"
243
+ tomod.each do |item|
244
+ verbose "Updating item #{item[:synckey]}"
234
245
  unless $cfg['tool.dry'] then
235
- item = therebox[key]
236
- dest = tolocalpath(into, herebox[key].merge(item) )
237
-
246
+ dest = tolocalpath(into, item)
238
247
  download(dest, item)
239
248
  end
240
249
  end
241
250
  end
242
251
  end
243
252
 
244
- def download(local, item)
245
- id = item[@itemkey.to_sym]
246
- local.open('wb') do |io|
247
- fetch(id) do |chunk|
248
- io.write chunk
249
- end
250
- end
253
+ ##
254
+ # True if itemA and itemB are different
255
+ def docmp(itemA, itemB)
256
+ true
251
257
  end
252
258
 
253
- def removelocal(dest, item)
254
- dest.unlink
259
+ def dodiff(item)
260
+ tfp = Tempfile.new([tolocalname(item, @itemkey), '.lua'])
261
+ df = ""
262
+ begin
263
+ download(Pathname.new(tfp.path), item)
264
+
265
+ cmd = $cfg['diff.cmd'].shellsplit
266
+ cmd << tfp.path
267
+ cmd << item[:local_path].to_s
268
+
269
+ IO.popen(cmd) {|io| df = io.read }
270
+ ensure
271
+ tfp.close
272
+ tfp.unlink
273
+ end
274
+ df
255
275
  end
256
276
 
257
- def status(from, options={})
277
+ def status(from, options=Commander::Command::Options.new)
258
278
  there = list()
259
279
  here = locallist(from)
260
280
  itemkey = @itemkey.to_sym
@@ -271,26 +291,29 @@ module MrMurano
271
291
  item[:synckey] = synckey(item)
272
292
  herebox[ item[:synckey] ] = item
273
293
  end
294
+ toadd = []
295
+ todel = []
296
+ tomod = []
297
+ unchg = []
274
298
  if options.asdown then
275
- todel = herebox.keys - therebox.keys
276
- toadd = therebox.keys - herebox.keys
277
- tomod = herebox.keys & therebox.keys
278
- {
279
- :toadd=> toadd.map{|key| therebox[key] },
280
- :todel=> todel.map{|key| herebox[key] },
281
- # FIXME what if therebox[key] is nil?
282
- :tomod=> tomod.map{|key| therebox[key].merge(herebox[key]) }
283
- }
299
+ todel = (herebox.keys - therebox.keys).map{|key| therebox[key] }
300
+ toadd = (therebox.keys - herebox.keys).map{|key| herebox[key] }
284
301
  else
285
- toadd = herebox.keys - therebox.keys
286
- todel = therebox.keys - herebox.keys
287
- tomod = herebox.keys & therebox.keys
288
- {
289
- :toadd=> toadd.map{|key| herebox[key] },
290
- :todel=> todel.map{|key| therebox[key] },
291
- :tomod=> tomod.map{|key| therebox[key].merge(herebox[key]) }
292
- }
302
+ toadd = (herebox.keys - therebox.keys).map{|key| herebox[key] }
303
+ todel = (therebox.keys - herebox.keys).map{|key| therebox[key] }
304
+ end
305
+ (herebox.keys & therebox.keys).each do |key|
306
+ # Want here to override there except for itemkey.
307
+ mrg = herebox[key].reject{|k,v| k==itemkey}
308
+ mrg = therebox[key].merge(mrg)
309
+ if docmp(herebox[key], therebox[key]) then
310
+ mrg[:diff] = dodiff(mrg) if options.diff
311
+ tomod << mrg
312
+ else
313
+ unchg << mrg
314
+ end
293
315
  end
316
+ { :toadd=>toadd, :todel=>todel, :tomod=>tomod, :unchg=>unchg }
294
317
  end
295
318
  end
296
319
 
@@ -313,6 +336,21 @@ module MrMurano
313
336
 
314
337
  end
315
338
 
339
+ # …/serviceconfig
340
+ class ServiceConfig < SolutionBase
341
+ def initialize
342
+ super
343
+ @uriparts << 'serviceconfig'
344
+ end
345
+
346
+ def list
347
+ get()['items']
348
+ end
349
+ def fetch(id)
350
+ get('/' + id.to_s)
351
+ end
352
+ end
353
+
316
354
  end
317
355
 
318
356
  # vim: set ai et sw=2 ts=2 :
@@ -1,4 +1,4 @@
1
1
  module MrMurano
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.0.2'.freeze
3
3
  end
4
4
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: MrMurano
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Conrad Tadpol Tilstra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-01 00:00:00.000000000 Z
11
+ date: 2016-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: commander