Skip to content

Commit 199cab7

Browse files
author
John Messenger
committed
Add option handling. Improve error messages. Tidy.
1 parent d8473df commit 199cab7

File tree

1 file changed

+55
-21
lines changed

1 file changed

+55
-21
lines changed

maint-uploader.rb

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# limitations under the License.
1616
####
1717

18+
require 'rubygems'
19+
require 'slop'
1820
require 'open-uri'
1921
require 'date'
2022
require 'json'
@@ -65,28 +67,56 @@ def find_request(api, item)
6567
def add_request_to_item(api, cookie, item, newreq)
6668
itemid = item['id']
6769
option_hash = { content_type: :json, accept: :json, cookies: cookie }
68-
res = api["items/#{itemid}/requests"].post newreq.to_json, option_hash
69-
#twit = 11
70+
begin
71+
res = api["items/#{itemid}/requests"].post newreq.to_json, option_hash
72+
rescue => e
73+
$logger.error "add_request_to_item => exception #{e.class.name} : #{e.message}"
74+
if (ej = JSON.parse(e.response)) && (eje = ej['errors'])
75+
eje.each do |k, v|
76+
$logger.error "#{k}: #{v.first}"
77+
end
78+
exit(1)
79+
end
80+
end
7081
end
7182

7283
####
7384
# Delete a request which is attached to an item
7485
####
7586
def delete_request(api, cookie, item, request)
7687
option_hash = { accept: :json, cookies: cookie }
77-
res = api["items/#{item['id']}/requests/#{request['id']}"].delete option_hash
78-
twit = 13
88+
begin
89+
res = api["items/#{item['id']}/requests/#{request['id']}"].delete option_hash
90+
rescue => e
91+
$logger.error "delete_request => exception #{e.class.name} : #{e.message}"
92+
if (ej = JSON.parse(e.response)) && (eje = ej['errors'])
93+
eje.each do |k, v|
94+
$logger.error "#{k}: #{v.first}"
95+
end
96+
exit(1)
97+
end
98+
end
7999
end
80100

81101
####
82102
# Create a new item and add a request to it
83103
####
84104
def add_new_item(api, cookie, number, subject, newreq)
85105
item = nil
86-
option_hash = { content_type: :json, accept: :json, cookies: cookie }
87-
newitem = { number: number, clause: newreq['clauseno'], date: newreq['date'], standard: newreq['standard'],
88-
subject: subject }
89-
res = api["items"].post newitem.to_json, option_hash
106+
option_hash = {content_type: :json, accept: :json, cookies: cookie}
107+
newitem = {number: number, clause: newreq['clauseno'], date: newreq['date'], standard: newreq['standard'],
108+
subject: subject}
109+
begin
110+
res = api["items"].post newitem.to_json, option_hash
111+
rescue => e
112+
$logger.error "add_new_item => exception #{e.class.name} : #{e.message}"
113+
if (ej = JSON.parse(e.response)) && (eje = ej['errors'])
114+
eje.each do |k, v|
115+
$logger.error "#{k}: #{v.first}"
116+
end
117+
exit(1)
118+
end
119+
end
90120
if res.code == 201
91121
item = JSON.parse(res.body)
92122
reqres = add_request_to_item(api, cookie, item, newreq)
@@ -196,18 +226,25 @@ def parse_request(url, creds)
196226
end
197227

198228
begin
199-
config = YAML.load(File.read("secrets.yml"))
229+
opts = Slop.parse do |o|
230+
o.string '-s', '--secrets', 'secrets YAML file name', default: 'secrets.yml'
231+
o.bool '-d', '--debug', 'debug mode'
232+
o.bool '-a', '--all', 'don\'t stop at first already-existing item'
233+
o.on '--help' do
234+
STDERR.puts o
235+
exit
236+
end
237+
end
238+
config = YAML.load(File.read(opts[:secrets]))
239+
240+
# Set up logging
241+
$DEBUG = opts.debug?
242+
$logger = Logger.new(STDERR)
243+
$logger.level = Logger::INFO
244+
$logger.level = Logger::DEBUG if $DEBUG
200245
#
201246
# Log in to the 802.1 Maintenance Database
202247
#
203-
#$DEBUG = false
204-
# If $PROCESS_ALL_ITEMS is false, the script will only process new items, stopping when it finds the
205-
# first item that already exists in the database.
206-
$PROCESS_ALL_ITEMS = false
207-
$logger = Logger.new(STDOUT)
208-
$logger.level = Logger::INFO
209-
$logger.level = Logger::DEBUG if $DEBUG
210-
211248
if $DEBUG
212249
RestClient.proxy = "http://localhost:8888"
213250
$logger.debug("Using HTTP proxy #{RestClient.proxy}")
@@ -218,7 +255,6 @@ def parse_request(url, creds)
218255
# Save the session cookie
219256
maint_cookie = {}
220257
res.cookies.each { |ck| maint_cookie[ck[0]] = ck[1] if /_session/.match(ck[0]) }
221-
#twit=10
222258

223259
#
224260
# Parse each index page of the 802.1 Maintenance Reflector Archive
@@ -269,7 +305,7 @@ def parse_request(url, creds)
269305
item = find_item(maint, number)
270306
request = {}
271307
if item
272-
unless $PROCESS_ALL_ITEMS
308+
unless opts[:all]
273309
$logger.info 'Stopping at first already-existing item'
274310
throw :done
275311
end
@@ -303,8 +339,6 @@ def parse_request(url, creds)
303339
end
304340
end
305341
end
306-
307-
twit = 8
308342
end
309343
nextpage = pagedoc.search('tr')[1].children.search('td').children[4].attributes['href']
310344
em_arch_url = nextpage ? config['email_archive'] + '/' + nextpage.value : nil

0 commit comments

Comments
 (0)