traffic learner: avoid truncated output files on error
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Fri, 19 Oct 2018 03:39:36 +0000 (16:39 +1300)
committerDouglas Bagnall <dbagnall@samba.org>
Tue, 8 Jan 2019 22:55:32 +0000 (23:55 +0100)
add_argument(type=argparse.FileType('w'), ...) will open the file
and leave it empty if the script fails.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
script/traffic_learner

index b46521f70cdcc4ef2bdbda86c9ca774cad2c09b5..cad1a64a1331ce079960daa235d5eb971d117ed8 100755 (executable)
@@ -31,7 +31,7 @@ info = logger.info
 def main():
     parser = argparse.ArgumentParser(description=__doc__,
                         formatter_class=argparse.RawDescriptionHelpFormatter)
-    parser.add_argument('-o', '--out', type=argparse.FileType('w'),
+    parser.add_argument('-o', '--out',
                         help="write model here")
     parser.add_argument('--dns-mode', choices=['inline', 'count'],
                         help='how to deal with DNS', default='count')
@@ -45,6 +45,13 @@ def main():
         error("Please specify a filename using the --out option.")
         return 1
 
+    try:
+        outfile = open(args.out, 'w')
+    except IOError as e:
+        error("could not open %s" % args.out)
+        error(e)
+        return 1
+
     if args.SUMMARY_FILE is sys.stdin:
         info("reading from STDIN...")