update_payload: use argparse

optparse is deprecated (or going to be). Change paycheck.py and
blockdiff.py to use argparse instead. Both of these files are being used
manually and it would be a good time to fix these before major changes
in update_payload.

paycheck.sh -h:

usage: paycheck.py [-h] [-c] [-D] [-r FILE] [-t {full,delta}] [-z NUM] [-u]
                   [-d] [-k FILE] [-m FILE] [-p NUM] [-P NUM] [-x]
                   [--bspatch-path FILE] [--puffpatch-path FILE]
                   [--dst_kern FILE] [--dst_root FILE] [--src_kern FILE]
                   [--src_root FILE] [-b BLOCK] [-B BLOCK] [-s NUM]
                   PAYLOAD

Applies a Chrome OS update PAYLOAD to src_kern and src_root emitting dst_kern and dst_root, respectively. src_kern and src_root are only needed for delta payloads. When no partitions are provided, verifies the payload integrity.

positional arguments:
  PAYLOAD               the payload file

optional arguments:
  -h, --help            show this help message and exit

Checking payload integrity:
  -c, --check           force payload integrity check (e.g. before applying)
  -D, --describe        Print a friendly description of the payload.
  -r FILE, --report FILE
                        dump payload report (`-' for stdout)
  -t {full,delta}, --type {full,delta}
                        assert the payload type
  -z NUM, --block-size NUM
                        assert a non-default (4096) payload block size
  -u, --allow-unhashed  allow unhashed operations
  -d , --disabled_tests
                        space separated list of tests to disable. allowed
                        options include: dst-pseudo-extents, move-same-src-
                        dst-block, payload-sig
  -k FILE, --key FILE   override standard key used for signature validation
  -m FILE, --meta-sig FILE
                        verify metadata against its signature
  -p NUM, --root-part-size NUM
                        override rootfs partition size auto-inference
  -P NUM, --kern-part-size NUM
                        override kernel partition size auto-inference

Applying payload:
  -x, --extract-bsdiff  use temp input/output files with BSDIFF operations
                        (not in-place)
  --bspatch-path FILE   use the specified bspatch binary
  --puffpatch-path FILE
                        use the specified puffpatch binary
  --dst_kern FILE       destination kernel partition file
  --dst_root FILE       destination root partition file
  --src_kern FILE       source kernel partition file
  --src_root FILE       source root partition file

Block tracing:
  -b BLOCK, --root-block BLOCK
                        trace the origin for a rootfs block
  -B BLOCK, --kern-block BLOCK
                        trace the origin for a kernel block
  -s NUM, --skip NUM    skip first NUM occurrences of traced block

Note: a payload may verify correctly but fail to apply, and vice versa; this is by design and can be thought of as static vs dynamic correctness. A payload that both verifies and applies correctly should be safe for use by the Chrome OS Update Engine. Use --check to verify a payload prior to applying it.

BUG=chromium:796338
TEST=unitests
TEST=test_paycheck.sh
TEST=blockdiff.py

Change-Id: I794b5f61e6ba6f92939947c97c432f9fea0b6b3c
Reviewed-on: https://chromium-review.googlesource.com/834876
Commit-Ready: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Ben Chan <benchan@chromium.org>
Reviewed-by: Sen Jiang <senj@chromium.org>
diff --git a/scripts/blockdiff.py b/scripts/blockdiff.py
index 1dc60a6..1f580c9 100755
--- a/scripts/blockdiff.py
+++ b/scripts/blockdiff.py
@@ -8,7 +8,8 @@
 
 from __future__ import print_function
 
-import optparse
+# pylint: disable=import-error
+import argparse
 import sys
 
 
@@ -71,28 +72,25 @@
 
 def main(argv):
   # Parse command-line arguments.
-  parser = optparse.OptionParser(
-      usage='Usage: %prog FILE1 FILE2',
-      description='Compare FILE1 and FILE2 by blocks.')
+  parser = argparse.ArgumentParser(
+      description='Compare FILE1 and FILE2 by blocks.',
+      formatter_class=argparse.ArgumentDefaultsHelpFormatter)
 
-  parser.add_option('-b', '--block-size', metavar='NUM', type=int, default=4096,
-                    help='the block size to use (default: %default)')
-  parser.add_option('-m', '--max-length', metavar='NUM', type=int, default=-1,
-                    help='maximum number of bytes to compared')
+  parser.add_argument('-b', '--block-size', metavar='NUM', type=int,
+                      default=4096, help='the block size to use')
+  parser.add_argument('-m', '--max-length', metavar='NUM', type=int, default=-1,
+                      help='maximum number of bytes to compare')
+  parser.add_argument('file1', metavar='FILE1')
+  parser.add_argument('file2', metavar='FILE2')
 
-  opts, args = parser.parse_args(argv[1:])
-
-  try:
-    name1, name2 = args
-  except ValueError:
-    parser.error('unexpected number of arguments')
+  args = parser.parse_args(argv[1:])
 
   # Perform the block diff.
   try:
-    with open(name1) as file1:
-      with open(name2) as file2:
-        diff_list = BlockDiff(opts.block_size, file1, file2, name1, name2,
-                              opts.max_length)
+    with open(args.file1) as file1:
+      with open(args.file2) as file2:
+        diff_list = BlockDiff(args.block_size, file1, file2,
+                              args.file1, args.file2, args.max_length)
   except BlockDiffError as e:
     print('Error: ' % e, file=sys.stderr)
     return 2