Makefile: add pickrand
[cmccabe-bin] / pickrand.py
index 8253e74..688460a 100755 (executable)
@@ -3,15 +3,29 @@
 import os
 import random
 import sys
+import time
 
-allfiles = []
+print_to_stderr = False
+random.seed(os.getpid() + int(time.time()))
 
-for root, dirs, files in os.walk(".", followlinks=True):
-    for f in files:
-        allfiles.append(os.path.join(root, f))
-if (len(allfiles) == 0):
-   sys.exit(1)
-random.seed(None)
-r = random.randint(0,len(allfiles)) 
-print(allfiles[r])
+file_name = None
+if (len(sys.argv) == 1):
+    allfiles = []
+    for root, dirs, files in os.walk("."):
+        for f in files:
+            allfiles.append(os.path.join(root, f))
+    if (len(allfiles) == 0):
+       sys.exit(1)
+    r = random.randint(0,len(allfiles) - 1)
+    file_name = allfiles[r]
+elif (len(sys.argv) == 2) and (sys.argv[1] == "-S"):
+    print_to_stderr = True
+else:
+    allfiles = sys.argv[1:]
+    r = random.randint(0,len(allfiles) - 1)
+    file_name = allfiles[r]
+
+print(file_name)
+if (print_to_stderr):
+    print >>sys.stderr, file_name
 sys.exit(0)