Skip to content

Commit c9f5c54

Browse files
committedJun 18, 2012
init commit
This is progress.
0 parents  commit c9f5c54

32 files changed

+240
-0
lines changed
 

‎Tree/A/dos/1.txt

Whitespace-only changes.

‎Tree/A/dos/2.txt

Whitespace-only changes.

‎Tree/A/dos/3.txt

Whitespace-only changes.

‎Tree/A/tres/4.txt

Whitespace-only changes.

‎Tree/A/tres/5.txt

Whitespace-only changes.

‎Tree/A/tres/6.txt

Whitespace-only changes.

‎Tree/A/uno/7.txt

Whitespace-only changes.

‎Tree/A/uno/8.txt

Whitespace-only changes.

‎Tree/A/uno/9.txt

Whitespace-only changes.

‎Tree/B/dos/1.txt

Whitespace-only changes.

‎Tree/B/dos/2.txt

Whitespace-only changes.

‎Tree/B/dos/3.txt

Whitespace-only changes.

‎Tree/B/tres/1234.txt

Whitespace-only changes.

‎Tree/B/tres/4.txt

Whitespace-only changes.

‎Tree/B/tres/5.txt

Whitespace-only changes.

‎Tree/B/tres/6.txt

Whitespace-only changes.

‎Tree/B/uno/7.txt

Whitespace-only changes.

‎Tree/B/uno/8.txt

Whitespace-only changes.

‎Tree/B/uno/9.txt

Whitespace-only changes.

‎Tree/B/uno/B.txt

Whitespace-only changes.

‎Tree/B/uno/a.txt

Whitespace-only changes.

‎Tree/C/dos/1.txt

Whitespace-only changes.

‎Tree/C/dos/2.txt

Whitespace-only changes.

‎Tree/C/dos/3.txt

Whitespace-only changes.

‎Tree/C/tres/4.txt

Whitespace-only changes.

‎Tree/C/tres/5.txt

Whitespace-only changes.

‎Tree/C/tres/6.txt

Whitespace-only changes.

‎Tree/C/uno/7.txt

Whitespace-only changes.

‎Tree/C/uno/8.txt

Whitespace-only changes.

‎Tree/C/uno/9.txt

Whitespace-only changes.

‎find.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import os
2+
import os.path
3+
import argparse
4+
import re
5+
import fnmatch
6+
import io
7+
8+
9+
parser = argparse.ArgumentParser()
10+
11+
parser.add_argument("path", help="path to search in", nargs='?', default="./")
12+
parser.add_argument("-regex", help="use regular expression matching")
13+
parser.add_argument("-name", help="name of file")
14+
parser.add_argument("-type", help="type: be it a file or dir", choices='df')
15+
16+
args = parser.parse_args()
17+
18+
global results_found
19+
results_found = False
20+
21+
def do_search(provided_path, regex_pattern, name_to_match, type_ForD):
22+
outputFromSearch = io.StringIO()
23+
24+
if regex_pattern:
25+
try:
26+
compiled_re = re.compile(regex_pattern)
27+
except re.error as ex:
28+
print("Illegal regex '%s': %s" % (regex_pattern, ex))
29+
exit(1)
30+
else:
31+
compiled_re = None
32+
33+
if not os.path.isdir(provided_path):
34+
exit("That is not a valid directory")
35+
36+
outputFromSearch = search_recursively(provided_path, compiled_re, name_to_match, type_ForD)
37+
return outputFromSearch
38+
39+
40+
def process_asset(fileOrDir, fileType, fileTypeRequired, compiled_re, name_to_match, rootDir=""):
41+
42+
global results_found
43+
abs_url = os.path.join(rootDir, fileOrDir)
44+
base_name = os.path.basename(fileOrDir)
45+
46+
if fileTypeRequired and fileType != fileTypeRequired:
47+
return None
48+
if compiled_re is not None and not compiled_re.search(base_name):
49+
return None
50+
if name_to_match is not None and not fnmatch.fnmatch(base_name, name_to_match):
51+
return None
52+
53+
results_found = True
54+
return abs_url
55+
56+
57+
def search_recursively(provided_path, regex_pattern, name_to_match, type_ForD):
58+
59+
search_results_output = io.StringIO()
60+
61+
for root, dirs, files in os.walk(provided_path, topdown=True):
62+
d_processed = process_asset(root, 'd', type_ForD, regex_pattern, name_to_match)
63+
if d_processed is not None:
64+
print(d_processed, file=search_results_output)
65+
66+
for f in files:
67+
f_processed = process_asset(f, 'f', type_ForD, regex_pattern, name_to_match, root)
68+
if f_processed is not None:
69+
print(f_processed, file=search_results_output)
70+
71+
return search_results_output
72+
73+
74+
#execute the search
75+
results = do_search(args.path, args.regex, args.name, args.type)
76+
print(results.getvalue())
77+
results.close()
78+
if results_found is not True:
79+
print("Sorry, no results were found")

‎test_find.py

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import find
2+
import io
3+
import unittest
4+
import os
5+
import os.path
6+
7+
8+
tmp_dir_previously_existed = False
9+
a_dir_p_e = False
10+
b_dir_p_e = False
11+
abc_dir_p_e = False
12+
abc123_file_p_e = False
13+
a_1_file_p_e = False
14+
b_2_file_p_e = False
15+
b_3_file_p_e = False
16+
17+
tmp_dir = os.path.join('/', 'tmp')
18+
19+
20+
class FindTestCase(unittest.TestCase):
21+
22+
def setUp(self):
23+
global tmp_dir_previously_existed
24+
global a_dir_p_e
25+
global b_dir_p_e
26+
global abc_dir_p_e
27+
global abc123_file_p_e
28+
global a_1_file_p_e
29+
global b_2_file_p_e
30+
global b_3_file_p_e
31+
32+
#create a /tmp file structure so you can have something to work with
33+
if os.path.isdir('/tmp'):
34+
tmp_dir_previously_existed = True
35+
36+
if os.path.isdir('/tmp/a'):
37+
a_dir_p_e = True
38+
else:
39+
os.mkdir('/tmp/a')
40+
41+
if os.path.isdir('/tmp/b'):
42+
b_dir_p_e = True
43+
else:
44+
os.mkdir('/tmp/b')
45+
46+
if os.path.isdir('/tmp/abc'):
47+
abc_dir_p_e = True
48+
else:
49+
os.mkdir('/tmp/abc')
50+
51+
if os.path.isfile('/tmp/abc123.txt'):
52+
abc123_file_p_e = True
53+
else:
54+
tmp_file_abc123 = open('/tmp/abc123.txt','w')
55+
tmp_file_abc123.close()
56+
57+
if os.path.isfile('/tmp/a/1.txt'):
58+
a_1_file_p_e = True
59+
else:
60+
tmp_file_a_1 = open('/tmp/a/1.txt','w')
61+
tmp_file_a_1.close()
62+
63+
if os.path.isfile('/tmp/b/2.txt'):
64+
b_2_file_p_e = True
65+
else:
66+
tmp_file_b_2 = open('/tmp/b/2.txt','w')
67+
tmp_file_b_2.close()
68+
69+
if os.path.isfile('/tmp/b/3.txt'):
70+
b_3_file_p_e = True
71+
else:
72+
tmp_file_b_3 = open('/tmp/b/3.txt','w')
73+
tmp_file_b_3.close()
74+
75+
else:
76+
os.mkdir('/tmp')
77+
os.mkdir('/tmp/a')
78+
os.mkdir('/tmp/b')
79+
os.mkdir('/tmp/abc')
80+
81+
tmp_file_abc123 = open('/tmp/abc123.txt','w')
82+
tmp_file_abc123.close()
83+
84+
tmp_file_a_1 = open('/tmp/a/1.txt','w')
85+
tmp_file_a_1.close()
86+
87+
tmp_file_b_2 = open('/tmp/b/2.txt','w')
88+
tmp_file_b_2.close()
89+
90+
tmp_file_b_3 = open('/tmp/b/3.txt','w')
91+
tmp_file_b_3.close()
92+
93+
94+
def test_find_dirs_files_with_exact_name(self):
95+
global tmp_dir
96+
actual_output = find.do_search(tmp_dir, None, 'a', None)
97+
98+
theoretical_output = io.StringIO()
99+
print(os.path.join('/tmp', 'a'), file=theoretical_output)
100+
101+
self.assertEqual(actual_output.getvalue(), theoretical_output.getvalue())
102+
103+
def test_find_dirs_file_with_globbing(self):
104+
global tmp_dir
105+
actual_output = find.do_search(tmp_dir, None, 'a*c*', None)
106+
107+
theoretical_output = io.StringIO()
108+
print(os.path.join('/tmp', 'abc123.txt'), file=theoretical_output)
109+
print(os.path.join('/tmp', 'abc'), file=theoretical_output)
110+
111+
self.assertEqual(actual_output.getvalue(), theoretical_output.getvalue())
112+
113+
def test_find_dirs_files_with_regex(self):
114+
global tmp_dir
115+
actual_output = find.do_search(tmp_dir, '1', None, None)
116+
117+
theoretical_output = io.StringIO()
118+
print(os.path.join(tmp_dir, 'abc123.txt'), file=theoretical_output)
119+
print(os.path.join(os.path.join(tmp_dir, 'a'), '1.txt'), file=theoretical_output)
120+
121+
self.assertEqual(actual_output.getvalue(), theoretical_output.getvalue())
122+
123+
def test_type_works_with_name_specified(self):
124+
global tmp_dir
125+
actual_output = find.do_search(tmp_dir, 'a', None, 'f')
126+
127+
theorectical_output = io.StringIO()
128+
print(os.path.join(tmp_dir, 'abc123.txt'), file=theoretical_output)
129+
130+
self.assertEqual(actual_output.getvalue(), theoretical_output.getvalue())
131+
132+
133+
def tearDown(self):
134+
global tmp_dir_previously_existed
135+
global a_dir_p_e
136+
global b_dir_p_e
137+
global abc123_file_p_e
138+
global a_1_file_p_e
139+
global b_2_file_p_e
140+
global b_3_file_p_e
141+
142+
if not abc123_file_p_e:
143+
os.remove('/tmp/abc123.txt')
144+
if not a_1_file_p_e:
145+
os.remove('/tmp/a/1.txt')
146+
if not b_2_file_p_e:
147+
os.remove('/tmp/b/2.txt')
148+
if not b_3_file_p_e:
149+
os.remove('/tmp/b/3.txt')
150+
151+
if not a_dir_p_e:
152+
os.rmdir('/tmp/a')
153+
if not b_dir_p_e:
154+
os.rmdir('/tmp/b')
155+
if not abc_dir_p_e:
156+
os.rmdir('/tmp/abc')
157+
if not tmp_dir_previously_existed:
158+
os.rmdir('/tmp') #Note: the dir must be empty before it can be del
159+
160+
if __name__ == '__main__':
161+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.