-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind.py
More file actions
executable file
·78 lines (55 loc) · 1.46 KB
/
Copy pathfind.py
File metadata and controls
executable file
·78 lines (55 loc) · 1.46 KB
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
68
69
70
71
72
73
74
75
76
77
#! /usr/bin/env python
'''
Space Telescope Science Institute
Synopsis:
This is a simple routine to locate a specific version of a file. If
the file is in the current directory that is the file name that
will be returned. If it is in one of the subdirectories of the
current directory then the one that was modified most recently
will be returned.
Command line usage (if any):
usage: find.py filename'
Description:
Primary routines:
Notes:
History:
100906 ksl Coding begun
'''
import os
import sys
import subprocess
def find_best(filename='foo'):
'''
Find the best version of a file, defined to be the one in
the current directory or the most recent one in any of the
subdirectories.
This returns and empty string if nothing is found
'''
proc=subprocess.Popen('find . -follow -name %s -print ' % filename,shell=True,stdout=subprocess.PIPE)
# Before
lines=proc.stdout.readlines()
if len(lines)==0:
print 'Warning: find_best: No versions of %s found' % filename
return ''
tbest=0
fbest=''
for line in lines:
fname=line.strip()
if fname.count('/') <= 1:
fbest=fname
break
else:
time=os.path.getmtime(fname)
if time>tbest:
fbest=fname
tbest=time
return fbest
# Next lines permit one to run the routine from the command line
if __name__ == "__main__":
import sys
if len(sys.argv)>1:
# doit(int(sys.argv[1]))
x=find_best(sys.argv[1])
print x
else:
print 'usage: find.py filename'