Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(857)

Side by Side Diff: tools/usb_gadget/package.py

Issue 412243002: [usb_gadget p09] Software updates over HTTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/usb_gadget/__main__.py ('k') | tools/usb_gadget/server.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright 2014 The Chromium Authors. All rights reserved. 2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Utility to package the USB gadget framework. 6 """Utility to package and upload the USB gadget framework.
7 """ 7 """
8 8
9 import argparse 9 import argparse
10 import hashlib 10 import hashlib
11 import os 11 import os
12 import StringIO 12 import StringIO
13 import urllib2
13 import zipfile 14 import zipfile
14 15
15 16
16 def MakeZip(directory=None, files=None): 17 def MakeZip(directory=None, files=None):
17 """Construct a zip file. 18 """Construct a zip file.
18 19
19 Args: 20 Args:
20 directory: Include Python source files from this directory 21 directory: Include Python source files from this directory
21 files: Include these files 22 files: Include these files
22 23
23 Returns: 24 Returns:
24 A tuple of the buffer containing the zip file and its MD5 hash. 25 A tuple of the buffer containing the zip file and its MD5 hash.
25 """ 26 """
26 buf = StringIO.StringIO() 27 buf = StringIO.StringIO()
27 archive = zipfile.PyZipFile(buf, 'w') 28 archive = zipfile.PyZipFile(buf, 'w')
28 if directory is not None: 29 if directory is not None:
29 archive.writepy(directory) 30 archive.writepy(directory)
30 if files is not None: 31 if files is not None:
31 for f in files: 32 for f in files:
32 archive.write(f, os.path.basename(f)) 33 archive.write(f, os.path.basename(f))
33 archive.close() 34 archive.close()
34 content = buf.getvalue() 35 content = buf.getvalue()
35 buf.close() 36 buf.close()
36 md5 = hashlib.md5(content).hexdigest() 37 md5 = hashlib.md5(content).hexdigest()
37 return content, md5 38 return content, md5
38 39
39 40
41 def EncodeBody(filename, buf):
42 return '\r\n'.join([
43 '--foo',
44 'Content-Disposition: form-data; name="file"; filename="{}"'
45 .format(filename),
46 'Content-Type: application/octet-stream',
47 '',
48 buf,
49 '--foo--',
50 ''
51 ])
52
53
54 def UploadZip(content, md5, host):
55 filename = 'usb_gadget-{}.zip'.format(md5)
56 req = urllib2.Request(url='http://{}/update'.format(host),
57 data=EncodeBody(filename, content))
58 req.add_header('Content-Type', 'multipart/form-data; boundary=foo')
59 urllib2.urlopen(req)
60
61
40 def main(): 62 def main():
41 parser = argparse.ArgumentParser( 63 parser = argparse.ArgumentParser(
42 description='Package (and upload) the USB gadget framework.') 64 description='Package (and upload) the USB gadget framework.')
43 parser.add_argument( 65 parser.add_argument(
44 '--dir', type=str, metavar='DIR', 66 '--dir', type=str, metavar='DIR',
45 help='package all Python files from DIR') 67 help='package all Python files from DIR')
46 parser.add_argument( 68 parser.add_argument(
47 '--zip-file', type=str, metavar='FILE', 69 '--zip-file', type=str, metavar='FILE',
48 help='save package as FILE') 70 help='save package as FILE')
49 parser.add_argument( 71 parser.add_argument(
50 '--hash-file', type=str, metavar='FILE', 72 '--hash-file', type=str, metavar='FILE',
51 help='save package hash as FILE') 73 help='save package hash as FILE')
52 parser.add_argument( 74 parser.add_argument(
75 '--upload', type=str, metavar='HOST[:PORT]',
76 help='upload package to target system')
77 parser.add_argument(
53 'files', metavar='FILE', type=str, nargs='*', 78 'files', metavar='FILE', type=str, nargs='*',
54 help='source files') 79 help='source files')
55 80
56 args = parser.parse_args() 81 args = parser.parse_args()
57 82
58 content, md5 = MakeZip(directory=args.dir, files=args.files) 83 content, md5 = MakeZip(directory=args.dir, files=args.files)
59 if args.zip_file: 84 if args.zip_file:
60 with open(args.zip_file, 'w') as zip_file: 85 with open(args.zip_file, 'w') as zip_file:
61 zip_file.write(content) 86 zip_file.write(content)
62 if args.hash_file: 87 if args.hash_file:
63 with open(args.hash_file, 'w') as hash_file: 88 with open(args.hash_file, 'w') as hash_file:
64 hash_file.write(md5) 89 hash_file.write(md5)
90 if args.upload:
91 UploadZip(content, md5, args.upload)
65 92
66 93
67 if __name__ == '__main__': 94 if __name__ == '__main__':
68 main() 95 main()
OLDNEW
« no previous file with comments | « tools/usb_gadget/__main__.py ('k') | tools/usb_gadget/server.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698