Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / tools / genhttpfs.py
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 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.
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 import urllib
19
20 class Error(Exception):
21   pass
22
23
24 def main(argv):
25   parser = optparse.OptionParser(description=__doc__,
26       usage='Usage: %prog [options] <filename>...')
27   parser.add_option('-C', '--srcdir',
28                     help='Change directory.', dest='srcdir', default=None)
29   parser.add_option('-o', '--output',
30                     help='Output file name.', dest='output', default=None)
31   parser.add_option('-v', '--verbose',
32                     help='Verbose output.',  dest='verbose',
33                     action='store_true')
34   parser.add_option('-r', '--recursive',
35                     help='Recursive search.', action='store_true')
36   options, args = parser.parse_args(argv)
37
38   if options.output:
39     outfile = open(options.output, 'w')
40   else:
41     outfile = sys.stdout
42
43   if options.srcdir:
44     os.chdir(options.srcdir)
45
46   if not args:
47     parser.error("One or more pathnames must be specified. See --help.")
48
49   # Generate a set of unique file names bases on the input globs
50   fileset = set()
51   for fileglob in args:
52     filelist = glob.glob(fileglob)
53     if not filelist:
54       raise Error('Could not find match for "%s".\n' % fileglob)
55     for filename in filelist:
56       if os.path.isfile(filename):
57         fileset |= set([filename])
58         continue
59       if os.path.isdir(filename) and options.recursive:
60         for root, _, files in os.walk(filename):
61           fileset |= set([os.path.join(root, name) for name in files])
62         continue
63       raise Error('Can not handle path "%s".\n' % filename)
64
65   cwd = os.path.abspath(os.getcwd())
66   cwdlen = len(cwd)
67   for filename in sorted(fileset):
68     relname = os.path.abspath(filename)
69     if cwd not in relname:
70       raise Error('%s is not relative to CWD %s.\n' % filename, cwd)
71     relname = relname[cwdlen:]
72     stat = os.stat(filename)
73     mode = '-r--'
74     name = urllib.quote(relname)
75     outfile.write('%s %d %s\n' % (mode, stat.st_size, name))
76
77   return 0
78
79
80 if __name__ == '__main__':
81   try:
82     sys.exit(main(sys.argv[1:]))
83   except Error, e:
84     sys.stderr.write('%s: %s\n' % (os.path.basename(__file__), e))
85     sys.exit(1)