@@ -20,12 +20,12 @@ def initialize_args(argslist = sys.argv[1:]):
20
20
global args
21
21
global parser
22
22
args = parser .parse_args (argslist )
23
-
23
+
24
24
return args
25
25
26
26
def do_search (provided_path , regex_pattern , name_to_match , type_ForD ):
27
27
outputFromSearch = io .StringIO ()
28
-
28
+
29
29
if regex_pattern :
30
30
try :
31
31
compiled_re = re .compile (regex_pattern )
@@ -34,52 +34,56 @@ def do_search(provided_path, regex_pattern, name_to_match, type_ForD):
34
34
exit (1 )
35
35
else :
36
36
compiled_re = None
37
-
37
+
38
38
if not os .path .isdir (provided_path ):
39
39
exit ("That is not a valid directory" )
40
-
40
+
41
41
outputFromSearch = search_recursively (provided_path , compiled_re , name_to_match , type_ForD )
42
42
return outputFromSearch
43
43
44
44
45
45
def process_asset (fileOrDir , fileType , fileTypeRequired , compiled_re , name_to_match , rootDir = "" ):
46
-
46
+
47
47
global results_found
48
48
abs_url = os .path .join (rootDir , fileOrDir )
49
49
base_name = os .path .basename (fileOrDir )
50
-
50
+
51
51
if fileTypeRequired and fileType != fileTypeRequired :
52
52
return None
53
53
if compiled_re is not None and not compiled_re .search (base_name ):
54
54
return None
55
55
if name_to_match is not None and not fnmatch .fnmatch (base_name , name_to_match ):
56
56
return None
57
-
57
+
58
58
results_found = True
59
59
return abs_url
60
60
61
61
62
62
def search_recursively (provided_path , regex_pattern , name_to_match , type_ForD ):
63
-
63
+
64
64
search_results_output = io .StringIO ()
65
-
66
- for root , dirs , files in os .walk (provided_path , topdown = True ):
65
+
66
+ for root , dirs , files in os .walk (provided_path , topdown = True ):
67
67
d_processed = process_asset (root , 'd' , type_ForD , regex_pattern , name_to_match )
68
68
if d_processed is not None :
69
69
print (d_processed , file = search_results_output )
70
-
70
+
71
71
for f in files :
72
72
f_processed = process_asset (f , 'f' , type_ForD , regex_pattern , name_to_match , root )
73
73
if f_processed is not None :
74
74
print (f_processed , file = search_results_output )
75
-
75
+
76
76
return search_results_output
77
77
78
78
79
- #execute the search
80
- initArgs = initialize_args ()
81
- results = do_search (args .path , args .regex , args .name , args .type )
82
- print (results .getvalue ())
83
- results .close ()
84
- if results_found is not True :
85
- print ("Sorry, no results were found" )
79
+ def do_script_run_through ():
80
+ #execute the search
81
+ initArgs = initialize_args ()
82
+ results = do_search (args .path , args .regex , args .name , args .type )
83
+ print (results .getvalue ())
84
+ results .close ()
85
+ if results_found is not True :
86
+ print ("Sorry, no results were found" )
87
+
88
+ if __name__ == '__main__' :
89
+ do_script_run_through ()
0 commit comments