-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawldirs.py
67 lines (56 loc) · 2.37 KB
/
crawldirs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: UTF-8 -*-
#==============================================================================
# Copyright (C) 2011 Milo Casagrande <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#==============================================================================
import os
import re
from parsers import parsefile
hidden_files_pattern = re.compile("^\.\w+")
def _crawl_dir(args, cmd_options, excluded_dirs, cwd=None):
'''
Traverse the directories and perform some checks on dir and file names
'''
for arg in args:
if len(excluded_dirs) > 0 and os.path.basename(arg) in excluded_dirs:
continue
if hidden_files_pattern.match(os.path.basename(arg)) != None:
continue
if cwd != None:
arg = os.path.join(cwd, arg)
if os.path.exists(arg):
if os.path.isfile(arg):
parsefile.parse(arg, cmd_options)
elif os.path.isdir(arg):
_crawl_dir(os.listdir(arg), cmd_options, excluded_dirs, arg)
else:
print "File or directory '%(arg)s' does not exist. It will not be considered." % {'arg': arg}
def _recurse_file(included_files, cmd_options):
'''
Parse the file passed in 'included_files', and add the license
'''
for f in included_files:
if os.path.exists(f) and os.path.isfile(f):
parsefile.parse(f, cmd_options)
else:
print "File or directory '%(arg)s' does not exist. It will not be considered." % {'arg': f}
def crawl(args, cmd_options, excluded_dirs, included_files):
'''
Traverse the directories in 'args', and add the license to the files in
them.
'''
_crawl_dir(args, cmd_options, excluded_dirs)
if len(included_files) > 0:
_recurse_file(included_files, cmd_options)