aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/data/scripts/per-site-settings.py
diff options
context:
space:
mode:
authorGravatar Ben Boeckel <MathStuf@gmail.com>2010-10-03 23:54:04 -0400
committerGravatar Ben Boeckel <MathStuf@gmail.com>2010-10-03 23:54:04 -0400
commit75002a7e7a648fcf01827186ef5e53122be998ab (patch)
tree2ae95d49b7687399808c7efa3945f0d66ac54ea6 /examples/data/scripts/per-site-settings.py
parent915fe7215d0f2202acec0258562c62d1bf4215f9 (diff)
Use the output if the file is executable
This way time-of-day dependent commands can be done (block reddit during work or browsing Wikipedia late into the morning on a weeknight). Other use cases possible.
Diffstat (limited to 'examples/data/scripts/per-site-settings.py')
-rwxr-xr-xexamples/data/scripts/per-site-settings.py28
1 files changed, 20 insertions, 8 deletions
diff --git a/examples/data/scripts/per-site-settings.py b/examples/data/scripts/per-site-settings.py
index c698592..ca95ab2 100755
--- a/examples/data/scripts/per-site-settings.py
+++ b/examples/data/scripts/per-site-settings.py
@@ -21,17 +21,19 @@
import os
import re
import socket
+import stat
+import subprocess
+import tempfile
import urlparse
import sys
-def grep_url(url, path, filepath):
+def grep_url(url, path, fin):
entries = []
- with open(filepath, 'r') as f:
- for line in f:
- parts = line.split('\t', 2)
- if (url.endswith(parts[0]) or re.match(parts[0], url)) and \
- (path.startswith(parts[1]) or re.match(parts[1], path)):
- entries.append(parts[2])
+ for line in fin:
+ parts = line.split('\t', 2)
+ if (url.endswith(parts[0]) or re.match(parts[0], url)) and \
+ (path.startswith(parts[1]) or re.match(parts[1], path)):
+ entries.append(parts[2])
return entries
def write_to_socket(commands, sockpath):
@@ -46,8 +48,18 @@ if __name__ == '__main__':
url = urlparse.urlparse(sys.argv[6])
filepath = sys.argv[8]
+ mode = os.stat(filepath)[stat.ST_MODE]
+
+ if mode & stat.S_IEXEC:
+ fin = tempfile.TemporaryFile()
+ subprocess.Popen([filepath], stdout=fin).wait()
+ else
+ fin = open(filepath, 'r')
+
host, path = (url.hostname, url.path)
- commands = grep_url(host, path, filepath)
+ commands = grep_url(host, path, fin)
+
+ find.close()
write_to_socket(commands, sockpath)