Browse Source

update: Automatically generate update filename if not specified on command line.

Keir Fraser 4 years ago
parent
commit
44a632de5d
1 changed files with 18 additions and 6 deletions
  1. 18 6
      scripts/greaseweazle/tools/update.py

+ 18 - 6
scripts/greaseweazle/tools/update.py

@@ -7,18 +7,29 @@
 # This is free and unencumbered software released into the public domain.
 # See the file COPYING for more details, or visit <http://unlicense.org>.
 
-import sys, argparse, serial, struct
+import sys, argparse, serial, struct, os
 import crcmod.predefined
 
 from greaseweazle.tools import util
+from greaseweazle import version
 from greaseweazle import usb as USB
 
 # update_firmware:
 # Updates the Greaseweazle firmware using the specified Update File.
 def update_firmware(usb, args):
 
+    filename = args.file
+    if filename == "auto":
+        # Get the absolute path to the root Greaseweazle folder.
+        path = os.path.dirname(os.path.abspath(__file__))
+        for _ in range(3):
+            path = os.path.join(path, os.pardir)
+        path = os.path.normpath(path)
+        filename = os.path.join(path, "Greaseweazle-v%d.%d.upd"
+                                % (version.major, version.minor))
+    
     # Read the entire update catalogue.
-    with open(args.file, "rb") as f:
+    with open(filename, "rb") as f:
         dat = f.read()
 
     # Search the catalogue for a match on our Weazle's hardware type.
@@ -32,18 +43,18 @@ def update_firmware(usb, args):
         dat = dat[upd_len+4:]
 
     if not dat:
-        print("%s: No match for hardware type %x" % (args.file, usb.hw_type))
+        print("%s: No match for hardware type %x" % (filename, usb.hw_type))
         return
 
     # Check the matching update file's footer.
     sig, maj, min, hw_type = struct.unpack("<2s2BH", dat[-8:-2])
     if len(dat) & 3 != 0 or sig != b'GW' or hw_type != usb.hw_type:
-        print("%s: Bad update file" % (args.file))
+        print("%s: Bad update file" % (filename))
         return
     crc16 = crcmod.predefined.Crc('crc-ccitt-false')
     crc16.update(dat)
     if crc16.crcValue != 0:
-        print("%s: Bad CRC" % (args.file))
+        print("%s: Bad CRC" % (filename))
         return
 
     # Perform the update.
@@ -64,7 +75,8 @@ def main(argv):
 
     parser = argparse.ArgumentParser(
         formatter_class=argparse.ArgumentDefaultsHelpFormatter)
-    parser.add_argument("file", help="update filename")
+    parser.add_argument("file", nargs="?", default="auto",
+                        help="update filename")
     parser.add_argument("device", nargs="?", default="auto",
                         help="serial device")
     parser.prog += ' ' + argv[1]