| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 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 # This scripts generates a manifest file for the MountHttp file system. | 6 """This script generates a manifest file for nacl_io's HTTP file-system. |
| 7 # Files and directory paths are specified on the command-line. The names | 7 Files and directory paths are specified on the command-line. The names |
| 8 # with glob and directories are recursed to form a list of files. | 8 with glob and directories are recursed to form a list of files. |
| 9 # | 9 |
| 10 # For each file, the mode bits, size and path relative to the CWD are written | 10 For each file, the mode bits, size and path relative to the CWD are written |
| 11 # to the output file which is stdout by default. | 11 to the output file which is stdout by default. |
| 12 # | 12 """ |
| 13 | 13 |
| 14 import glob | 14 import glob |
| 15 import optparse | 15 import optparse |
| 16 import os | 16 import os |
| 17 import sys | 17 import sys |
| 18 import urllib |
| 19 |
| 20 class Error(Exception): |
| 21 pass |
| 18 | 22 |
| 19 | 23 |
| 20 def main(argv): | 24 def main(argv): |
| 21 parser = optparse.OptionParser( | 25 parser = optparse.OptionParser(description=__doc__, |
| 22 usage='Usage: %prog [options] filename ...') | 26 usage='Usage: %prog [options] <filename>...') |
| 23 parser.add_option('-C', '--srcdir', | 27 parser.add_option('-C', '--srcdir', |
| 24 help='Change directory.', dest='srcdir', default=None) | 28 help='Change directory.', dest='srcdir', default=None) |
| 25 parser.add_option('-o', '--output', | 29 parser.add_option('-o', '--output', |
| 26 help='Output file name.', dest='output', default=None) | 30 help='Output file name.', dest='output', default=None) |
| 27 parser.add_option('-v', '--verbose', | 31 parser.add_option('-v', '--verbose', |
| 28 help='Verbose output.', dest='verbose', | 32 help='Verbose output.', dest='verbose', |
| 29 action='store_true') | 33 action='store_true') |
| 30 parser.add_option('-r', '--recursive', | 34 parser.add_option('-r', '--recursive', |
| 31 help='Recursive search.', action='store_true') | 35 help='Recursive search.', action='store_true') |
| 32 options, args = parser.parse_args(argv) | 36 options, args = parser.parse_args(argv) |
| 33 | 37 |
| 34 if options.output: | 38 if options.output: |
| 35 outfile = open(options.output, 'w') | 39 outfile = open(options.output, 'w') |
| 36 else: | 40 else: |
| 37 outfile = sys.stdout | 41 outfile = sys.stdout |
| 38 | 42 |
| 39 if options.srcdir: | 43 if options.srcdir: |
| 40 os.chdir(options.srcdir) | 44 os.chdir(options.srcdir) |
| 41 | 45 |
| 46 if not args: |
| 47 parser.error("One or more pathnames must be specified. See --help.") |
| 48 |
| 42 # Generate a set of unique file names bases on the input globs | 49 # Generate a set of unique file names bases on the input globs |
| 43 fileset = set() | 50 fileset = set() |
| 44 for fileglob in args: | 51 for fileglob in args: |
| 45 filelist = glob.glob(fileglob) | 52 filelist = glob.glob(fileglob) |
| 46 if not filelist: | 53 if not filelist: |
| 47 raise RuntimeError('Could not find match for "%s".\n' % fileglob) | 54 raise Error('Could not find match for "%s".\n' % fileglob) |
| 48 for filename in filelist: | 55 for filename in filelist: |
| 49 if os.path.isfile(filename): | 56 if os.path.isfile(filename): |
| 50 fileset |= set([filename]) | 57 fileset |= set([filename]) |
| 51 continue | 58 continue |
| 52 if os.path.isdir(filename) and options.recursive: | 59 if os.path.isdir(filename) and options.recursive: |
| 53 for root, _, files in os.walk(filename): | 60 for root, _, files in os.walk(filename): |
| 54 fileset |= set([os.path.join(root, name) for name in files]) | 61 fileset |= set([os.path.join(root, name) for name in files]) |
| 55 continue | 62 continue |
| 56 raise RuntimeError('Can not handle path "%s".\n' % filename) | 63 raise Error('Can not handle path "%s".\n' % filename) |
| 57 | 64 |
| 58 cwd = os.path.abspath(os.getcwd()) | 65 cwd = os.path.abspath(os.getcwd()) |
| 59 cwdlen = len(cwd) | 66 cwdlen = len(cwd) |
| 60 for filename in sorted(fileset): | 67 for filename in sorted(fileset): |
| 61 relname = os.path.abspath(filename) | 68 relname = os.path.abspath(filename) |
| 62 if cwd not in relname: | 69 if cwd not in relname: |
| 63 raise RuntimeError('%s is not relative to CWD %s.\n' % filename, cwd) | 70 raise Error('%s is not relative to CWD %s.\n' % filename, cwd) |
| 64 relname = relname[cwdlen:] | 71 relname = relname[cwdlen:] |
| 65 stat = os.stat(filename) | 72 stat = os.stat(filename) |
| 66 mode = '-r--' | 73 mode = '-r--' |
| 67 outfile.write('%s %d %s\n' % (mode, stat.st_size, relname)) | 74 name = urllib.quote(relname) |
| 75 outfile.write('%s %d %s\n' % (mode, stat.st_size, name)) |
| 68 | 76 |
| 69 return 0 | 77 return 0 |
| 70 | 78 |
| 71 | 79 |
| 72 if __name__ == '__main__': | 80 if __name__ == '__main__': |
| 73 try: | 81 try: |
| 74 sys.exit(main(sys.argv[1:])) | 82 sys.exit(main(sys.argv[1:])) |
| 75 except OSError, e: | 83 except Error, e: |
| 76 sys.stderr.write('%s: %s\n' % (os.path.basename(__file__), e)) | 84 sys.stderr.write('%s: %s\n' % (os.path.basename(__file__), e)) |
| 77 sys.exit(1) | 85 sys.exit(1) |
| OLD | NEW |