xref: /aosp_15_r20/external/perfetto/infra/perfetto.dev/mime_util/file (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1#!/usr/bin/env python3
2
3import sys
4import os
5
6
7def main():
8  # Typically the target file argument is the last arg. gsutil invokes:
9  # `file -b --mime /path/to/file`. Rathter than depending on it, just pick
10  # the right-most argument which points to a valid file.
11  fname = None
12  for arg in reversed(sys.argv[1:]):
13    if os.path.exists(arg):
14      fname = arg
15      break
16  if fname is None:
17    raise 'File not found in ' + ' '.join(sys.argv)
18
19  ext = os.path.splitext(fname)[1].lower()
20  ext_map = {
21      '.html': 'text/html',
22      '.css': 'text/css',
23      '.js': 'text/javascript',
24      '.jpg': 'image/jpeg',
25      '.png': 'image/png',
26      '.svg': 'image/svg+xml',
27  }
28  print(ext_map.get(ext, 'text/html'))
29  return 0
30
31
32if __name__ == '__main__':
33  sys.exit(main())
34