15f9996aaSopenharmony_ci#!/usr/bin/env python
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright 2014 The Chromium Authors. All rights reserved.
45f9996aaSopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
55f9996aaSopenharmony_ci# found in the LICENSE file.
65f9996aaSopenharmony_ci"""Finds files in directories.
75f9996aaSopenharmony_ci"""
85f9996aaSopenharmony_ci
95f9996aaSopenharmony_ciimport fnmatch
105f9996aaSopenharmony_ciimport optparse
115f9996aaSopenharmony_ciimport os
125f9996aaSopenharmony_ciimport sys
135f9996aaSopenharmony_ci
145f9996aaSopenharmony_ci
155f9996aaSopenharmony_cidef main(argv):
165f9996aaSopenharmony_ci    parser = optparse.OptionParser()
175f9996aaSopenharmony_ci    parser.add_option('--pattern', default='*', help='File pattern to match.')
185f9996aaSopenharmony_ci    parser.add_option('--base-dir', help='base directory')
195f9996aaSopenharmony_ci    parser.add_option('--return-relpath',
205f9996aaSopenharmony_ci                      action='store_true',
215f9996aaSopenharmony_ci                      help='return relative address from base directory')
225f9996aaSopenharmony_ci    parser.add_option('--follow-symlinks',
235f9996aaSopenharmony_ci                      action='store_true',
245f9996aaSopenharmony_ci                      help='whether to follow links')
255f9996aaSopenharmony_ci    options, directories = parser.parse_args(argv)
265f9996aaSopenharmony_ci
275f9996aaSopenharmony_ci    for d in directories:
285f9996aaSopenharmony_ci        if not os.path.exists(d):
295f9996aaSopenharmony_ci            print('%s does not exist' % d)
305f9996aaSopenharmony_ci            return 1
315f9996aaSopenharmony_ci        if os.path.isfile(d):
325f9996aaSopenharmony_ci            if options.return_relpath:
335f9996aaSopenharmony_ci                if options.base_dir is not None:
345f9996aaSopenharmony_ci                    if fnmatch.filter(d, options.pattern):
355f9996aaSopenharmony_ci                        print(os.path.relpath(d, options.base_dir))
365f9996aaSopenharmony_ci                else:
375f9996aaSopenharmony_ci                    print("Please specify the relative base directory")
385f9996aaSopenharmony_ci                    return 1
395f9996aaSopenharmony_ci            else:
405f9996aaSopenharmony_ci                if fnmatch.filter(d, options.pattern):
415f9996aaSopenharmony_ci                    print(d)
425f9996aaSopenharmony_ci            return 0
435f9996aaSopenharmony_ci        elif not os.path.isdir(d):
445f9996aaSopenharmony_ci            # if input path is not a directory nor a normal file, return error.
455f9996aaSopenharmony_ci            print('%s is not a directory or a file' % d)
465f9996aaSopenharmony_ci            return 1
475f9996aaSopenharmony_ci        for root, _, files in os.walk(d, followlinks=options.follow_symlinks):
485f9996aaSopenharmony_ci            for f in fnmatch.filter(files, options.pattern):
495f9996aaSopenharmony_ci                if options.return_relpath:
505f9996aaSopenharmony_ci                    if options.base_dir is not None:
515f9996aaSopenharmony_ci                        print(
525f9996aaSopenharmony_ci                            os.path.relpath(os.path.join(root, f),
535f9996aaSopenharmony_ci                                            options.base_dir))
545f9996aaSopenharmony_ci                    else:
555f9996aaSopenharmony_ci                        print("Please specify the relative base directory")
565f9996aaSopenharmony_ci                        return 1
575f9996aaSopenharmony_ci                else:
585f9996aaSopenharmony_ci                    print(os.path.join(root, f))
595f9996aaSopenharmony_ci
605f9996aaSopenharmony_ci
615f9996aaSopenharmony_ciif __name__ == '__main__':
625f9996aaSopenharmony_ci    sys.exit(main(sys.argv[1:]))
63