forked from adnanaziz/EPIJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory_path_normalization.py
34 lines (26 loc) · 1.09 KB
/
directory_path_normalization.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
from test_framework import generic_test
def shortest_equivalent_path(path):
if not path:
raise ValueError('Empty string is not a valid path.')
path_names = [] # Uses list as a stack.
# Special case: starts with '/', which is an absolute path.
if path[0] == '/':
path_names.append('/')
for token in (token for token in path.split('/')
if token not in ['.', '']):
if token == '..':
if not path_names or path_names[-1] == '..':
path_names.append(token)
else:
if path_names[-1] == '/':
raise ValueError('Path error')
path_names.pop()
else: # Must be a name.
path_names.append(token)
result = '/'.join(path_names)
return result[result.startswith('//'):] # Avoid starting '//'.
if __name__ == '__main__':
exit(
generic_test.generic_test_main("directory_path_normalization.py",
'directory_path_normalization.tsv',
shortest_equivalent_path))