OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Utility to package the USB gadget framework. |
| 7 """ |
| 8 |
| 9 import argparse |
| 10 import hashlib |
| 11 import os |
| 12 import StringIO |
| 13 import zipfile |
| 14 |
| 15 |
| 16 def MakeZip(directory=None, files=None): |
| 17 """Construct a zip file. |
| 18 |
| 19 Args: |
| 20 directory: Include Python source files from this directory |
| 21 files: Include these files |
| 22 |
| 23 Returns: |
| 24 A tuple of the buffer containing the zip file and its MD5 hash. |
| 25 """ |
| 26 buf = StringIO.StringIO() |
| 27 archive = zipfile.PyZipFile(buf, 'w') |
| 28 if directory is not None: |
| 29 archive.writepy(directory) |
| 30 if files is not None: |
| 31 for f in files: |
| 32 archive.write(f, os.path.basename(f)) |
| 33 archive.close() |
| 34 content = buf.getvalue() |
| 35 buf.close() |
| 36 md5 = hashlib.md5(content).hexdigest() |
| 37 return content, md5 |
| 38 |
| 39 |
| 40 def main(): |
| 41 parser = argparse.ArgumentParser( |
| 42 description='Package (and upload) the USB gadget framework.') |
| 43 parser.add_argument( |
| 44 '--dir', type=str, metavar='DIR', |
| 45 help='package all Python files from DIR') |
| 46 parser.add_argument( |
| 47 '--zip-file', type=str, metavar='FILE', |
| 48 help='save package as FILE') |
| 49 parser.add_argument( |
| 50 '--hash-file', type=str, metavar='FILE', |
| 51 help='save package hash as FILE') |
| 52 parser.add_argument( |
| 53 'files', metavar='FILE', type=str, nargs='*', |
| 54 help='source files') |
| 55 |
| 56 args = parser.parse_args() |
| 57 |
| 58 content, md5 = MakeZip(directory=args.dir, files=args.files) |
| 59 if args.zip_file: |
| 60 with open(args.zip_file, 'w') as zip_file: |
| 61 zip_file.write(content) |
| 62 if args.hash_file: |
| 63 with open(args.hash_file, 'w') as hash_file: |
| 64 hash_file.write(md5) |
| 65 |
| 66 |
| 67 if __name__ == '__main__': |
| 68 main() |
OLD | NEW |