diff --git a/basic_examples/python_regex.py b/basic_examples/python_regex.py new file mode 100755 index 0000000..158a4a4 --- /dev/null +++ b/basic_examples/python_regex.py @@ -0,0 +1,26 @@ +#!/usr/bin/python +# Filename : python_regex.py +import re + +if None == re.match("c", "abcdef"): # No match + print "Odd but true: didn't match \"c\" in \"abcdef\"" +else: + print "matched c in abcdef" + +if re.search("c", "abcdef"): + print "As expected: found \"c\" in search of \"abcdef\"" +else: + print "did not find c in search of abcdef" + +if re.search("g", "abcdef"): + print "found g in search of abcdef" +else: + print "As expected: did not find \"g\" in search of \"abcdef\"" + +# Word Boundaries are not \< and \> +# Thou shalt useth raw strings to saveth thineself from backslash hell (i.e \\\\b) +re_abcs = re.compile(r'\babc\b') # this would be /\/ in other languages +if re_abcs.search("I know my abc's"): + print "Not PERL alert: As found \"r'\\babc\\b'\" (i.e. \"/\\/\") in compiled search of \"I know my abc's\"" +else: + print "did not find '\' in compiled search of \"I know my abc's\"" diff --git a/inotify/README.md b/inotify/README.md new file mode 100644 index 0000000..264fe57 --- /dev/null +++ b/inotify/README.md @@ -0,0 +1,26 @@ +Rudd-O's python-inotify +==== + +Apparently there are a number (about 3 or so it seems) of inotify modules. + + * python-inotify + * python-inotifyx + * pyinotify + +This example is for [Rudd-O's `python-inotify`](http://rudd-o.com/projects/python-inotify/) v0.1.0 + +Documentation +==== + + import inotify + help(inotify) + +requires `python-pydoc` and `python-pkgutil` + +BUGS +==== + +Doesn't seem to work more than once or twice (at least not on ARM). +After that a message appears and the process freezes + +> No handlers could be found for logger "inotify" diff --git a/inotify/help.py b/inotify/help.py new file mode 100755 index 0000000..37b507e --- /dev/null +++ b/inotify/help.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python +import inotify +help(inotify) diff --git a/inotify/watch_all.py b/inotify/watch_all.py new file mode 100755 index 0000000..068be92 --- /dev/null +++ b/inotify/watch_all.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +from inotify import Watcher +import inotify +import sys + +w = Watcher() +paths = sys.argv[1:] or ['/tmp'] + +for path in paths: + try: + # Watch all paths recursively, and all events on them. + w.watch(path, inotify.ALL_EVENTS) + except OSError, err: + print >> sys.stderr, '%s: %s' % (err.filename, err.strerror) + +# If we have nothing to watch, don't go into the read loop, or we'll +# sit there forever. + +if not len(w.get_watching()): + sys.exit(1) + +while True: + event = w.get_next_event() + print event.event_mask + print event.filename + print event.watched_filename + print event + #print repr(evt.fullpath), ' | '.join(inotify.decode_mask(evt.mask)) diff --git a/inotify/watch_close.py b/inotify/watch_close.py new file mode 100755 index 0000000..90f744a --- /dev/null +++ b/inotify/watch_close.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +from inotify import Watcher +import inotify +import sys + +w = Watcher() +paths = sys.argv[1:] or ['/tmp'] + +for path in paths: + try: + # Watch all paths recursively, and all events on them. + w.watch(path, inotify.ALL_EVENTS) + except OSError, err: + print >> sys.stderr, '%s: %s' % (err.filename, err.strerror) + +# If we have nothing to watch, don't go into the read loop, or we'll +# sit there forever. + +if not len(w.get_watching()): + sys.exit(1) + +while True: + event = w.get_next_event() + if not inotify.CLOSE_WRITE & event.event_mask: + continue + print event.event_mask + print event.filename + print event.watched_filename + print event + #print repr(evt.fullpath), ' | '.join(inotify.decode_mask(evt.mask)) diff --git a/inotify/watch_pattern_close.py b/inotify/watch_pattern_close.py new file mode 100755 index 0000000..b9a6a9d --- /dev/null +++ b/inotify/watch_pattern_close.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +from inotify import Watcher +import inotify +import sys +import re + +# to test run this script and then run +# rm /tmp/foo /tmp/bar /tmp/baz /tmp/nomatch -rf +# touch /tmp/foo /tmp/bar /tmp/baz /tmp/nomatch + +w = Watcher() +paths = sys.argv[1:] or ['/tmp'] +patterns = [re.compile(r'^foo'), re.compile(r'^ba'), re.compile(r'az$')] + + +for path in paths: + try: + # Watch all paths recursively; only on CLOSE_WRITE + # this seems to throw the error 'No handlers could be found for logger "inotify"' + # when trying to watch only CLOSE_WRITE + w.watch(path, inotify.ALL_EVENTS) + except OSError, err: + print >> sys.stderr, '%s: %s' % (err.filename, err.strerror) + +# Quit now if we have nothing to watch +if not len(w.get_watching()): + sys.exit(1) + +# Check each event to see if any of the patterns prescribed matche +while True: + event = w.get_next_event() + if not inotify.CLOSE_WRITE & event.event_mask: + continue + dirlen = len(event.watched_filename) + for pattern in patterns: + # Just the filename without the leading directory + filename = event.filename[dirlen+1:] + if pattern.search(filename): + print filename, "matches", pattern.pattern +