Commit ee8d3b00 authored by reillyg@chromium.org's avatar reillyg@chromium.org

[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
parent 49c167f3
# 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.
{
'variables': {
'usb_gadget_files': [
'usb_gadget/__init__.py',
'usb_gadget/__main__.py',
'usb_gadget/default_gadget.py',
'usb_gadget/gadget.py',
'usb_gadget/hid_constants.py',
'usb_gadget/hid_descriptors.py',
'usb_gadget/hid_gadget.py',
'usb_gadget/keyboard_gadget.py',
'usb_gadget/linux_gadgetfs.py',
'usb_gadget/mouse_gadget.py',
'usb_gadget/server.py',
'usb_gadget/usb_constants.py',
'usb_gadget/usb_descriptors.py',
],
'usb_gadget_package': '<(PRODUCT_DIR)/usb_gadget.zip',
'usb_gadget_package_hash': '<(PRODUCT_DIR)/usb_gadget.zip.md5',
},
'targets': [
{
'target_name': 'usb_gadget',
'type': 'none',
'actions': [
{
'action_name': 'Building USB Gadget ZIP bundle',
'inputs': [
'usb_gadget/package.py',
'<@(usb_gadget_files)',
],
'outputs': [
'<(usb_gadget_package)',
'<(usb_gadget_package_hash)',
],
'action': [
'python', 'usb_gadget/package.py',
'--zip-file', '<(usb_gadget_package)',
'--hash-file', '<(usb_gadget_package_hash)',
'<@(usb_gadget_files)',
]
}
]
}
]
}
#!/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()
......@@ -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),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment