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 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
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.
20 class Error(Exception):
25 parser
= argparse
.ArgumentParser(description
=__doc__
)
26 parser
.add_argument('-C', '--srcdir',
27 help='Change directory.', dest
='srcdir', default
=None)
28 parser
.add_argument('-o', '--output',
29 help='Output file name.', dest
='output', default
=None)
30 parser
.add_argument('-v', '--verbose',
31 help='Verbose output.', dest
='verbose',
33 parser
.add_argument('-r', '--recursive',
34 help='Recursive search.', action
='store_true')
35 parser
.add_argument('paths', nargs
='+')
36 options
= parser
.parse_args(args
)
39 outfile
= open(options
.output
, 'w')
44 os
.chdir(options
.srcdir
)
46 # Generate a set of unique file names bases on the input globs
48 for fileglob
in options
.paths
:
49 filelist
= glob
.glob(fileglob
)
51 raise Error('Could not find match for "%s".\n' % fileglob
)
52 for filename
in filelist
:
53 if os
.path
.isfile(filename
):
54 fileset |
= set([filename
])
56 if os
.path
.isdir(filename
) and options
.recursive
:
57 for root
, _
, files
in os
.walk(filename
):
58 fileset |
= set([os
.path
.join(root
, name
) for name
in files
])
60 raise Error('Can not handle path "%s".\n' % filename
)
62 cwd
= os
.path
.abspath(os
.getcwd())
64 for filename
in sorted(fileset
):
65 relname
= os
.path
.abspath(filename
)
66 if cwd
not in relname
:
67 raise Error('%s is not relative to CWD %s.\n' % filename
, cwd
)
68 relname
= relname
[cwdlen
:]
69 stat
= os
.stat(filename
)
71 name
= urllib
.quote(relname
)
72 outfile
.write('%s %d %s\n' % (mode
, stat
.st_size
, name
))
77 if __name__
== '__main__':
79 sys
.exit(main(sys
.argv
[1:]))
81 sys
.stderr
.write('%s: %s\n' % (os
.path
.basename(__file__
), e
))