diff options
author | reillyg@chromium.org <reillyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-25 07:04:26 +0000 |
---|---|---|
committer | reillyg@chromium.org <reillyg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-25 07:04:26 +0000 |
commit | ee8d3b000414b0b3529f0117a706e0f6f6cbd2d2 (patch) | |
tree | c700656618b8c6340a2b09cd84e0f1f7ffae0a05 /tools/usb_gadget | |
parent | 49c167f3b58f8899e5380cef56d6a350b8fce264 (diff) | |
download | chromium_src-ee8d3b000414b0b3529f0117a706e0f6f6cbd2d2.zip chromium_src-ee8d3b000414b0b3529f0117a706e0f6f6cbd2d2.tar.gz chromium_src-ee8d3b000414b0b3529f0117a706e0f6f6cbd2d2.tar.bz2 |
[usb_gadget p08] Package the USB gadget framework for easy distribution.
This software package will need to be uploaded to the test hardware.
Python can easily run code out of a zip file. The package hash is saved
to verify integrity and as a versioning mechanism to allow tests to
ensure that they are running against a device with the expected software
version.
BUG=396682
R=rockot@chromium.org,rpaquay@chromium.org,kalman@chromium.org
Review URL: https://codereview.chromium.org/414833004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285513 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/usb_gadget')
-rwxr-xr-x | tools/usb_gadget/package.py | 68 | ||||
-rw-r--r-- | tools/usb_gadget/server.py | 19 |
2 files changed, 87 insertions, 0 deletions
diff --git a/tools/usb_gadget/package.py b/tools/usb_gadget/package.py new file mode 100755 index 0000000..d1a2f2d --- /dev/null +++ b/tools/usb_gadget/package.py @@ -0,0 +1,68 @@ +#!/usr/bin/python +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Utility to package the USB gadget framework. +""" + +import argparse +import hashlib +import os +import StringIO +import zipfile + + +def MakeZip(directory=None, files=None): + """Construct a zip file. + + Args: + directory: Include Python source files from this directory + files: Include these files + + Returns: + A tuple of the buffer containing the zip file and its MD5 hash. + """ + buf = StringIO.StringIO() + archive = zipfile.PyZipFile(buf, 'w') + if directory is not None: + archive.writepy(directory) + if files is not None: + for f in files: + archive.write(f, os.path.basename(f)) + archive.close() + content = buf.getvalue() + buf.close() + md5 = hashlib.md5(content).hexdigest() + return content, md5 + + +def main(): + parser = argparse.ArgumentParser( + description='Package (and upload) the USB gadget framework.') + parser.add_argument( + '--dir', type=str, metavar='DIR', + help='package all Python files from DIR') + parser.add_argument( + '--zip-file', type=str, metavar='FILE', + help='save package as FILE') + parser.add_argument( + '--hash-file', type=str, metavar='FILE', + help='save package hash as FILE') + parser.add_argument( + 'files', metavar='FILE', type=str, nargs='*', + help='source files') + + args = parser.parse_args() + + content, md5 = MakeZip(directory=args.dir, files=args.files) + if args.zip_file: + with open(args.zip_file, 'w') as zip_file: + zip_file.write(content) + if args.hash_file: + with open(args.hash_file, 'w') as hash_file: + hash_file.write(md5) + + +if __name__ == '__main__': + main() diff --git a/tools/usb_gadget/server.py b/tools/usb_gadget/server.py index f9ccd9b..9f23726 100644 --- a/tools/usb_gadget/server.py +++ b/tools/usb_gadget/server.py @@ -5,11 +5,16 @@ """WSGI application to manage a USB gadget. """ +import re +import sys + from tornado import httpserver from tornado import web import default_gadget +VERSION_PATTERN = re.compile(r'.*usb_gadget-([a-z0-9]{32})\.zip') + address = None chip = None claimed_by = None @@ -27,6 +32,19 @@ def SwitchGadget(new_gadget): chip.Create(gadget) +class VersionHandler(web.RequestHandler): + + def get(self): + version = 'unpackaged' + for path in sys.path: + match = VERSION_PATTERN.match(path) + if match: + version = match.group(1) + break + + self.write(version) + + class ClaimHandler(web.RequestHandler): def post(self): @@ -69,6 +87,7 @@ class ReconnectHandler(web.RequestHandler): app = web.Application([ + (r'/version', VersionHandler), (r'/claim', ClaimHandler), (r'/unclaim', UnclaimHandler), (r'/unconfigure', UnconfigureHandler), |