Anonymous avatar Anonymous committed dc8aed8

Restructured main, use getopt, use regular expression search instead
of prefix match

Comments (0)

Files changed (1)


 # findlinksto
-# find symbolic links to a given path
+# find symbolic links to a path matching a regular expression
-import os, sys
+import os
+import sys
+import regex
+import getopt
-def visit(pattern, dirname, names):
+def main():
+	try:
+		opts, args = getopt.getopt(sys.argv[1:], '')
+		if len(args) < 2:
+			raise getopt.error, 'not enough arguments'
+	except getopt.error, msg:
+		sys.stdout = sys.stderr
+		print msg
+		print 'usage: findlinksto pattern directory ...'
+		sys.exit(2)
+	pat, dirs = args[0], args[1:]
+	prog = regex.compile(pat)
+	for dirname in dirs:
+		os.path.walk(dirname, visit, prog)
+def visit(prog, dirname, names):
 	if os.path.islink(dirname):
 		names[:] = []
 	if os.path.ismount(dirname):
 		print 'descend into', dirname
-	n = len(pattern)
 	for name in names:
 		name = os.path.join(dirname, name)
 			linkto = os.readlink(name)
-			if linkto[:n] == pattern:
+			if >= 0:
 				print name, '->', linkto
 		except os.error:
-def main(pattern, args):
-	for dirname in args:
-		os.path.walk(dirname, visit, pattern)
-main(sys.argv[1], sys.argv[2:])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.