Skip to content

Commit 6aab4eb

Browse files
authored
Dev/jgough/8310 consistent error code (#80)
Make run responsible for return error codes consistently Also make main responsible for actually sys exiting AB#8310
1 parent ec9701d commit 6aab4eb

14 files changed

Lines changed: 104 additions & 55 deletions

File tree

archivist_samples/c2pa/main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# pylint: disable=missing-docstring
44

55
import logging
6+
from sys import exit as sys_exit
7+
from sys import stdout as sys_stdout
68

79
from ..testing.archivist_parser import common_parser
810
from ..testing.parser import common_endpoint
@@ -27,4 +29,8 @@ def main():
2729

2830
poc = common_endpoint("document", args)
2931

30-
run(poc, args)
32+
err_code = run(poc, args)
33+
34+
if err_code != 0:
35+
parser.print_help(sys_stdout)
36+
sys_exit(err_code)

archivist_samples/c2pa/run.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import logging
77

8-
from sys import exit as sys_exit
98
from archivist import about
109

1110
from .c2pa import C2PADocument, upload_attachment
@@ -15,6 +14,9 @@
1514

1615

1716
def run(arch, args):
17+
"""
18+
runs the sample and returns the system error code.
19+
"""
1820
LOGGER.info("Using version %s of rkvst-archivist", about.__version__)
1921
LOGGER.info("Fetching use case test assets namespace %s", args.namespace)
2022

@@ -56,7 +58,7 @@ def run(arch, args):
5658
"C2PA Image",
5759
c2pa_document.asset["identity"],
5860
)
59-
sys_exit(0)
61+
return 0
6062

6163
LOGGER.info("C2PA Document Created (Identity=%s)", c2pa_document.asset["identity"])
6264

@@ -179,4 +181,4 @@ def run(arch, args):
179181
},
180182
)
181183
LOGGER.info("V1.3.ME published")
182-
sys_exit(0)
184+
return 0

archivist_samples/document/main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# pylint: disable=missing-docstring
44

55
import logging
6+
from sys import exit as sys_exit
7+
from sys import stdout as sys_stdout
68

79
from ..testing.archivist_parser import common_parser
810
from ..testing.parser import common_endpoint
@@ -27,4 +29,8 @@ def main():
2729

2830
poc = common_endpoint("document", args)
2931

30-
run(poc, args)
32+
err_code = run(poc, args)
33+
34+
if err_code != 0:
35+
parser.print_help(sys_stdout)
36+
sys_exit(err_code)

archivist_samples/document/run.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import logging
77

8-
from sys import exit as sys_exit
98
from archivist import about
109

1110
from .document import Document, upload_attachment
@@ -15,6 +14,9 @@
1514

1615

1716
def run(arch, args):
17+
"""
18+
runs the sample and returns the system error code.
19+
"""
1820
LOGGER.info("Using version %s of rkvst-archivist", about.__version__)
1921
LOGGER.info("Fetching use case test assets namespace %s", args.namespace)
2022

@@ -55,11 +57,8 @@ def run(arch, args):
5557
"Asteroid Mining Inc Invoice",
5658
document.asset["identity"],
5759
)
58-
LOGGER.info(
59-
"Public URL: %s/archivist/public%s", args.url, document.asset["identity"]
60-
)
6160

62-
sys_exit(0)
61+
return 0
6362

6463
LOGGER.info("Invoice Asset Created (Identity=%s)", document.asset["identity"])
6564

@@ -104,4 +103,4 @@ def run(arch, args):
104103
)
105104
LOGGER.info("V3 published, with discount applied")
106105

107-
sys_exit(0)
106+
return 0

archivist_samples/door_entry/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def main():
6969

7070
arch = common_endpoint("door_entry", args)
7171

72-
run(arch, args)
72+
err_code = run(arch, args)
7373

74-
parser.print_help(sys_stdout)
75-
sys_exit(1)
74+
if err_code != 0:
75+
parser.print_help(sys_stdout)
76+
sys_exit(err_code)

archivist_samples/door_entry/run.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from copy import copy
99
import logging
1010
from tempfile import TemporaryFile
11-
from sys import exit as sys_exit
1211
import uuid
1312

1413
from archivist import about
@@ -573,6 +572,9 @@ def open_door(doors, doorid, cards, cardid):
573572

574573

575574
def run(arch, args):
575+
"""
576+
runs the sample and returns the system error code.
577+
"""
576578
LOGGER.info("Using version %s of rkvst-archivist", about.__version__)
577579
LOGGER.info("Fetching use case test assets namespace %s", args.namespace)
578580

@@ -606,15 +608,15 @@ def run(arch, args):
606608
if args.wait_for_confirmation:
607609
cards.assets.wait_for_confirmed()
608610

609-
sys_exit(0)
611+
return 0
610612

611613
if number_of_doors == 0:
612614
LOGGER.error("Could not find door entry assets. Please create them first.")
613-
sys_exit(1)
615+
return 1
614616

615617
if number_of_cards == 0:
616618
LOGGER.error("Could not find card assets. Please create them first.")
617-
sys_exit(1)
619+
return 1
618620

619621
# Show info about the system ?
620622
if args.listspec:
@@ -632,10 +634,12 @@ def run(arch, args):
632634
list_usage(doors, cards, spec)
633635

634636
LOGGER.info("List %s FINISH", spec)
635-
sys_exit(0)
637+
return 0
636638

637639
# Open a door using a specified card ?
638640
if args.doorid_cardid:
639641
doorid, cardid = args.doorid_cardid
640642
open_door(doors, doorid, cards, cardid)
641-
sys_exit(0)
643+
return 0
644+
645+
return 1

archivist_samples/estate_info/main.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323

2424
def run(poc, args):
25-
"""logic goes here"""
25+
"""
26+
runs the sample and returns the system error code.
27+
"""
2628
LOGGER.info("Using version %s of rkvst-archivist", about.__version__)
2729
if args.quick_count:
2830
LOGGER.info("Number of events is %d", poc.events.count())
2931
LOGGER.info("Number of assets is %d", poc.assets.count())
3032
LOGGER.info("Number of locations is %d", poc.locations.count())
31-
sys_exit(0)
33+
return 0
3234

3335
if args.double_check:
3436
# for around 550 events and 250 assets this can take about a 90s...
@@ -72,7 +74,9 @@ def run(poc, args):
7274
)
7375

7476
LOGGER.info("Performing double-check... FINISH")
75-
sys_exit(0)
77+
return 0
78+
79+
return 1
7680

7781

7882
def main():
@@ -101,7 +105,8 @@ def main():
101105

102106
poc = common_endpoint("estate_info", args)
103107

104-
run(poc, args)
108+
err_code = run(poc, args)
105109

106-
parser.print_help(sys_stdout)
107-
sys_exit(1)
110+
if err_code != 0:
111+
parser.print_help(sys_stdout)
112+
sys_exit(err_code)

archivist_samples/sbom_document/main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# pylint: disable=missing-docstring
44

55
import logging
6+
from sys import exit as sys_exit
7+
from sys import stdout as sys_stdout
68

79
from ..testing.archivist_parser import common_parser
810
from ..testing.parser import common_endpoint
@@ -29,4 +31,8 @@ def main():
2931

3032
arch = common_endpoint("sbom", args)
3133

32-
run(arch, args)
34+
err_code = run(arch, args)
35+
36+
if err_code != 0:
37+
parser.print_help(sys_stdout)
38+
sys_exit(err_code)

archivist_samples/sbom_document/run.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# pylint: disable=missing-docstring
44

55
import logging
6-
from sys import exit as sys_exit
76

87
from archivist import about
98

@@ -14,6 +13,9 @@
1413

1514

1615
def run(arch, args):
16+
"""
17+
runs the sample and returns the system error code.
18+
"""
1719
LOGGER.info("Using version %s of rkvst-archivist", about.__version__)
1820
LOGGER.info("Fetching use case test assets namespace %s", args.namespace)
1921

@@ -44,7 +46,7 @@ def run(arch, args):
4446

4547
if package.existed:
4648
LOGGER.info("Software Package already Created: %s", package_name)
47-
sys_exit(0)
49+
return 0
4850

4951
LOGGER.info("Software Package Created (Identity=%s)", package.asset["identity"])
5052

@@ -127,4 +129,4 @@ def run(arch, args):
127129
},
128130
)
129131
LOGGER.info("Release registered.")
130-
sys_exit(0)
132+
return 0

archivist_samples/signed_records/main.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,13 @@ def print_history(archivist, asset_name):
307307

308308
# Main app loop
309309
###############
310+
311+
312+
# pylint: disable=too-many-return-statements
310313
def run(arch, args):
314+
"""
315+
runs the sample and returns the system error code.
316+
"""
311317
LOGGER.info("Using version %s of rkvst-archivist", about.__version__)
312318
if args.namespace:
313319
asset_name = "-".join(["signed-records", args.namespace])
@@ -328,11 +334,11 @@ def run(arch, args):
328334
" Please choose a different name to create.",
329335
asset_name,
330336
)
331-
sys_exit(1)
337+
return 1
332338

333339
LOGGER.info("Generate crypto asset '%s'", asset_name)
334340
generate_crypto_asset(arch, asset_name)
335-
sys_exit(0)
341+
return 0
336342

337343
if args.sign_message:
338344
if not asset_exists(arch, asset_name):
@@ -341,11 +347,11 @@ def run(arch, args):
341347
" Please choose a different name to create.",
342348
asset_name,
343349
)
344-
sys_exit(1)
350+
return 1
345351

346352
LOGGER.info("Submit signed evidence '%s'", asset_name)
347353
submit_signed_evidence(arch, asset_name, args.sign_message, False)
348-
sys_exit(0)
354+
return 0
349355

350356
if args.bad_sign_message:
351357
if not asset_exists(arch, asset_name):
@@ -354,11 +360,11 @@ def run(arch, args):
354360
"Please choose the correct name or create it first.",
355361
asset_name,
356362
)
357-
sys_exit(1)
363+
return 1
358364

359365
LOGGER.info("Submit badly signed evidence %s", asset_name)
360366
submit_signed_evidence(arch, asset_name, args.bad_sign_message, True)
361-
sys_exit(0)
367+
return 0
362368

363369
if args.check_sigs:
364370
if not asset_exists(arch, asset_name):
@@ -367,11 +373,11 @@ def run(arch, args):
367373
"Please choose the correct name or create it first.",
368374
asset_name,
369375
)
370-
sys_exit(1)
376+
return 1
371377

372378
LOGGER.info("Check %s", asset_name)
373379
print_history(arch, asset_name)
374-
sys_exit(0)
380+
return 0
375381

376382

377383
def main():
@@ -427,7 +433,8 @@ def main():
427433

428434
arch = common_endpoint("signed_records", args)
429435

430-
run(arch, args)
436+
err_code = run(arch, args)
431437

432-
parser.print_help(sys_stdout)
433-
sys_exit(1)
438+
if err_code != 0:
439+
parser.print_help(sys_stdout)
440+
sys_exit(err_code)

0 commit comments

Comments
 (0)