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.
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.
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.
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',
30 parser
.add_option('-r', '--recursive',
31 help='Recursive search.', action
='store_true')
32 options
, args
= parser
.parse_args(argv
)
35 outfile
= open(options
.output
, 'w')
40 os
.chdir(options
.srcdir
)
42 # Generate a set of unique file names bases on the input globs
45 filelist
= glob
.glob(fileglob
)
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
])
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
])
56 raise RuntimeError('Can not handle path "%s".\n' % filename
)
58 cwd
= os
.path
.abspath(os
.getcwd())
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
)
67 outfile
.write('%s %d %s\n' % (mode
, stat
.st_size
, relname
))
72 if __name__
== '__main__':
74 sys
.exit(main(sys
.argv
[1:]))
76 sys
.stderr
.write('%s: %s\n' % (os
.path
.basename(__file__
), e
))