Home | History | Annotate | Download | only in tools
      1 #!/usr/bin/env python
      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
      4 # found in the LICENSE file.
      5 
      6 # This scripts generates a manifest file for the MountHttp file system.
      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.
      9 #
     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.
     12 #
     13 
     14 import glob
     15 import optparse
     16 import os
     17 import sys
     18 
     19 
     20 def main(argv):
     21   parser = optparse.OptionParser(
     22       usage='Usage: %prog [options] filename ...')
     23   parser.add_option('-C', '--srcdir',
     24                     help='Change directory.', dest='srcdir', default=None)
     25   parser.add_option('-o', '--output',
     26                     help='Output file name.', dest='output', default=None)
     27   parser.add_option('-v', '--verbose',
     28                     help='Verbose output.',  dest='verbose',
     29                     action='store_true')
     30   parser.add_option('-r', '--recursive',
     31                     help='Recursive search.', action='store_true')
     32   options, args = parser.parse_args(argv)
     33 
     34   if options.output:
     35     outfile = open(options.output, 'w')
     36   else:
     37     outfile = sys.stdout
     38 
     39   if options.srcdir:
     40     os.chdir(options.srcdir)
     41 
     42   # Generate a set of unique file names bases on the input globs
     43   fileset = set()
     44   for fileglob in args:
     45     filelist = glob.glob(fileglob)
     46     if not filelist:
     47       raise RuntimeError('Could not find match for "%s".\n' % fileglob)
     48     for filename in filelist:
     49       if os.path.isfile(filename):
     50         fileset |= set([filename])
     51         continue
     52       if os.path.isdir(filename) and options.recursive:
     53         for root, _, files in os.walk(filename):
     54           fileset |= set([os.path.join(root, name) for name in files])
     55         continue
     56       raise RuntimeError('Can not handle path "%s".\n' % filename)
     57 
     58   cwd = os.path.abspath(os.getcwd())
     59   cwdlen = len(cwd)
     60   for filename in sorted(fileset):
     61     relname = os.path.abspath(filename)
     62     if cwd not in relname:
     63       raise RuntimeError('%s is not relative to CWD %s.\n' % filename, cwd)
     64     relname = relname[cwdlen:]
     65     stat = os.stat(filename)
     66     mode = '-r--'
     67     outfile.write('%s %d %s\n' % (mode, stat.st_size, relname))
     68 
     69   return 0
     70 
     71 
     72 if __name__ == '__main__':
     73   try:
     74     sys.exit(main(sys.argv[1:]))
     75   except OSError, e:
     76     sys.stderr.write('%s: %s\n' % (os.path.basename(__file__), e))
     77     sys.exit(1)
     78