1 | #!/usr/bin/env python |
---|
2 | #****************************************************************************** |
---|
3 | # $Id: gdalident.py 18194 2009-12-06 20:07:45Z rouault $ |
---|
4 | # |
---|
5 | # Project: GDAL |
---|
6 | # Purpose: Application to identify files by format. |
---|
7 | # Author: Frank Warmerdam, warmerdam@pobox.com |
---|
8 | # |
---|
9 | #****************************************************************************** |
---|
10 | # Copyright (c) 2007, Frank Warmerdam <warmerdam@pobox.com> |
---|
11 | # |
---|
12 | # Permission is hereby granted, free of charge, to any person obtaining a |
---|
13 | # copy of this software and associated documentation files (the "Software"), |
---|
14 | # to deal in the Software without restriction, including without limitation |
---|
15 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
16 | # and/or sell copies of the Software, and to permit persons to whom the |
---|
17 | # Software is furnished to do so, subject to the following conditions: |
---|
18 | # |
---|
19 | # The above copyright notice and this permission notice shall be included |
---|
20 | # in all copies or substantial portions of the Software. |
---|
21 | # |
---|
22 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
23 | # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
24 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
---|
25 | # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
26 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
27 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
28 | # DEALINGS IN THE SOFTWARE. |
---|
29 | #****************************************************************************** |
---|
30 | # |
---|
31 | try: |
---|
32 | from osgeo import gdal |
---|
33 | except ImportError: |
---|
34 | import gdal |
---|
35 | |
---|
36 | import sys |
---|
37 | import stat |
---|
38 | import os |
---|
39 | import glob |
---|
40 | |
---|
41 | # ============================================================================= |
---|
42 | # Usage() |
---|
43 | # ============================================================================= |
---|
44 | def Usage(): |
---|
45 | print('Usage: gdalident.py [-r] file(s)') |
---|
46 | sys.exit(1) |
---|
47 | |
---|
48 | # ============================================================================= |
---|
49 | # ProcessTarget() |
---|
50 | # ============================================================================= |
---|
51 | |
---|
52 | def ProcessTarget( target, recursive, report_failure, filelist = None ): |
---|
53 | |
---|
54 | if filelist is not None: |
---|
55 | driver = gdal.IdentifyDriver( target, filelist ) |
---|
56 | else: |
---|
57 | driver = gdal.IdentifyDriver( target ) |
---|
58 | |
---|
59 | if driver is not None: |
---|
60 | print('%s: %s' % (target, driver.ShortName)) |
---|
61 | elif report_failure: |
---|
62 | print('%s: unrecognised' % target) |
---|
63 | |
---|
64 | if recursive and driver is None: |
---|
65 | try: |
---|
66 | mode = os.stat(target)[stat.ST_MODE] |
---|
67 | except: |
---|
68 | mode = 0 |
---|
69 | |
---|
70 | if stat.S_ISDIR(mode): |
---|
71 | subfilelist = os.listdir(target) |
---|
72 | for item in subfilelist: |
---|
73 | subtarget = os.path.join(target,item) |
---|
74 | ProcessTarget( subtarget, 1, report_failure, subfilelist ) |
---|
75 | |
---|
76 | # ============================================================================= |
---|
77 | # Mainline |
---|
78 | # ============================================================================= |
---|
79 | |
---|
80 | recursive = 0 |
---|
81 | report_failure = 0 |
---|
82 | files = [] |
---|
83 | |
---|
84 | gdal.AllRegister() |
---|
85 | argv = gdal.GeneralCmdLineProcessor( sys.argv ) |
---|
86 | if argv is None: |
---|
87 | sys.exit( 0 ) |
---|
88 | |
---|
89 | # Parse command line arguments. |
---|
90 | i = 1 |
---|
91 | while i < len(argv): |
---|
92 | arg = argv[i] |
---|
93 | |
---|
94 | if arg == '-r': |
---|
95 | recursive = 1 |
---|
96 | |
---|
97 | elif arg == '-f': |
---|
98 | report_failure = 1 |
---|
99 | |
---|
100 | else: |
---|
101 | # Expand any possible wildcards from command line arguments |
---|
102 | f = glob.glob( arg ) |
---|
103 | if len(f) == 0: |
---|
104 | print('File not found: "%s"' % ( str( arg ) )) |
---|
105 | files += f # append 1 or more files |
---|
106 | |
---|
107 | i = i + 1 |
---|
108 | |
---|
109 | if len(files) == 0: |
---|
110 | Usage() |
---|
111 | |
---|
112 | for file in files: |
---|
113 | ProcessTarget( file, recursive, report_failure ) |
---|