Merge lp:~patrickas/ensoft-sextant/sextant1_addprogram_exceptions into lp:ensoft-sextant

Proposed by Patrick Stevens
Status: Merged
Approved by: Patrick Stevens
Approved revision: 7
Merged at revision: 6
Proposed branch: lp:~patrickas/ensoft-sextant/sextant1_addprogram_exceptions
Merge into: lp:ensoft-sextant
Diff against target: 139 lines (+31/-28)
4 files modified
src/sextant/__main__.py (+12/-4)
src/sextant/objdump_parser.py (+3/-2)
src/sextant/query.py (+2/-2)
src/sextant/update_db.py (+14/-20)
To merge this branch: bzr merge lp:~patrickas/ensoft-sextant/sextant1_addprogram_exceptions
Reviewer Review Type Date Requested Status
Patrick Stevens Approve
James Approve
Review via email: mp+231174@code.launchpad.net

Commit message

Make update_db.upload_program use exceptions rather than returning error codes, and get __main__.py to handle these. Incidentally fix a bug where file-not-found was not handled neatly on program upload.

Description of the change

Make update_db.upload_program use exceptions rather than returning error codes, and get __main__.py to handle these. Incidentally fix a bug where file-not-found was not handled neatly on program upload.

To post a comment you must log in.
5. By Patrick Stevens <email address hidden>

Update docstring to match Python 2 change

6. By Patrick Stevens <email address hidden>

Merge Sextant 1.0.1 changes

7. By Patrick Stevens <email address hidden>

Merge minor_bugfixes branch

Revision history for this message
James (jamesh-f) wrote :

I like the exception handling in __main__ for the neo4j connection.
All looks fine to me (although initially I didn't think you were returning a connection error from uploading a program.)

Revision history for this message
James (jamesh-f) wrote :

Approve

review: Approve
Revision history for this message
Patrick Stevens (patrickas) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/sextant/__main__.py'
2--- src/sextant/__main__.py 2014-08-15 10:49:08 +0000
3+++ src/sextant/__main__.py 2014-08-18 16:11:12 +0000
4@@ -15,6 +15,7 @@
5 import logging
6 import logging.config
7 import os
8+import requests
9 import sys
10
11 from . import update_db
12@@ -169,10 +170,17 @@
13 not_object_file = namespace.not_object_file
14 # the default is "yes, this is an object file" if not-object-file was
15 # unsupplied
16-
17- update_db.upload_program(namespace.input_file[0],
18- namespace.remote_neo4j,
19- alternative_name, not_object_file)
20+ try:
21+ update_db.upload_program(namespace.input_file[0],
22+ namespace.remote_neo4j,
23+ alternative_name, not_object_file)
24+ except IOError: # in case of Python 2, where FileNotFoundError is undefined
25+ logging.error('Input file {} was not found.'.format(namespace.input_file[0]))
26+ except requests.exceptions.ConnectionError as e:
27+ logging.error('Connection error to server {}: {}'.format(namespace.remote_neo4j,
28+ e))
29+ except ValueError as e:
30+ logging.error(e)
31
32
33 def delete_file(namespace):
34
35=== modified file 'src/sextant/objdump_parser.py'
36--- src/sextant/objdump_parser.py 2014-08-14 14:41:19 +0000
37+++ src/sextant/objdump_parser.py 2014-08-18 16:11:12 +0000
38@@ -214,8 +214,9 @@
39
40 # first, check whether the given file exists
41 if not os.path.isfile(filepath):
42- logging.error('Input file does not exist')
43- return False
44+ # we'd like to use FileNotFoundError, but we might be running under
45+ # Python 2, which doesn't have it.
46+ raise IOError(filepath + 'is not found.')
47
48 #now the file should exist
49 if not not_object_file: #if it is something we need to run through objdump first
50
51=== modified file 'src/sextant/query.py'
52--- src/sextant/query.py 2014-08-11 15:14:05 +0000
53+++ src/sextant/query.py 2014-08-18 16:11:12 +0000
54@@ -18,8 +18,8 @@
55 try:
56 db = db_api.SextantConnection(remote_neo4j)
57 except requests.exceptions.ConnectionError as err:
58- logging.exception("Could not connect to Neo4J server {}. Are you sure it is running?".format(remote_neo4j))
59- logging.exception(str(err))
60+ logging.error("Could not connect to Neo4J server {}. Are you sure it is running?".format(remote_neo4j))
61+ logging.error(str(err))
62 return 2
63 #Not supported in python 2
64 #except (urllib.exceptions.MaxRetryError):
65
66=== modified file 'src/sextant/update_db.py'
67--- src/sextant/update_db.py 2014-08-11 15:14:05 +0000
68+++ src/sextant/update_db.py 2014-08-18 16:11:12 +0000
69@@ -11,57 +11,51 @@
70 from .objdump_parser import get_parsed_objects
71
72 import logging
73-import requests
74
75
76 def upload_program(file_path, db_url, alternative_name=None, not_object_file=False):
77 """
78 Uploads a program to the remote database.
79+ Raises requests.exceptions.ConnectionError if the server didn't exist.
80+ Raises IOError if file_path doesn't correspond to a file.
81+ Raises ValueError if the desired alternative_name (or the default, if no
82+ alternative_name was specified) already exists in the database.
83 :param file_path: the path to the local file we wish to upload
84 :param db_url: the URL of the database (eg. http://localhost:7474)
85 :param alternative_name: a name to give the program to override the default
86 :param object_file: bool(the file is an objdump text output file, rather than a compiled binary)
87- :return: 1 if the program already exists in database, 2 if there was a connection error
88 """
89- try:
90- connection = SextantConnection(db_url)
91- except requests.exceptions.ConnectionError as err:
92- logging.exception("Could not connect to Neo4J server {}. Are you sure it is running?".format(db_url))
93- return 2
94- #except urllib.exceptions.MaxRetryError:
95- # logging.error("Connection was refused to {}. Are you sure the server is running?".format(db_url))
96- # return 2
97+
98+ connection = SextantConnection(db_url)
99
100 program_names = connection.get_program_names()
101 if alternative_name is None:
102 if Validator.sanitise(file_path) in program_names:
103- logging.error("There is already a program under this name; please delete the previous one with the same name "
104- "and retry, or rename the input file.")
105- return 1
106+ raise ValueError("There is already a program under this name; "
107+ "please delete the previous one with the same name"
108+ " and retry, or rename the input file.")
109 else:
110 if Validator.sanitise(alternative_name) in program_names:
111- logging.error("There is already a program under this name; please delete the previous one with the same name "
112- "and retry, or rename the input file.")
113- return 1
114+ raise ValueError("There is already a program under this name; "
115+ "please delete the previous one with the same name"
116+ " and retry, or rename the input file.")
117
118 parsed_objects = get_parsed_objects(filepath=file_path, sections_to_view=['.text'],
119 not_object_file=not_object_file, ignore_function_pointers=False)
120
121- logging.warning('Objdump has parsed!')
122+ logging.info('Objdump has parsed!')
123
124 if alternative_name is None:
125 program_representation = connection.new_program(Validator.sanitise(file_path))
126 else:
127 program_representation = connection.new_program(Validator.sanitise(alternative_name))
128
129-
130-
131 for obj in parsed_objects:
132 for called in obj.what_do_i_call:
133 if not program_representation.add_function_call(obj.name, called[-1]): # called is a tuple (address, name)
134 logging.error('Validation error: {} calling {}'.format(obj.name, called[-1]))
135
136- logging.warning('Sending {} named objects to server {}...'.format(len(parsed_objects), db_url))
137+ logging.info('Sending {} named objects to server {}...'.format(len(parsed_objects), db_url))
138 program_representation.commit()
139 logging.info('Sending complete! Exiting.')
140

Subscribers

People subscribed via source and target branches