Commits

Anonymous committed 8332297

Remove old stuff.

Comments (0)

Files changed (4)

.hgignore

-syntax: glob
-*.so
-*.test
-*.swp
-build/*

setup.py

-from distutils.core import setup, Extension
-import sys
-
-def get_extension():
-    """Get the proper Extension object for the platform we're compiling"""
-    if sys.platform == "darwin":
-        # Use kqueue on Mac OS X
-        macros=[("DARWIN", None)]
-        # TODO: Like Windows, I think we might get __APPLE__ for free
-    elif sys.platform == "linux2":
-        # Use inotify on Linux
-        macros=[] 
-    elif sys.platform == "win32":
-        # Use ReadDirectoryChangesW on Win32
-        macros=[] # We get MS_WINDOWS for free
-    return Extension("watcher", sources=["watcher.c"],
-                     define_macros=macros)
-
-setup(name="watcher",
-      version="0.1",
-      description="Cross platform file system watcher",
-      url="blah",
-      keywords="blah",
-      maintainer="Brian Curtin",
-      maintainer_email="curtin@acm.org",
-      license="PSF",
-      platforms=["Win", "Mac OS X", "Linux"],
-      #py_modules=["watch"],
-      ext_modules=[get_extension()])

test_watcher.py

-import unittest
-import tempfile
-
-import watcher
-
-TESTFILE = tempfile.mkstemp()[1] # Don't need fd here
-
-class TestWatcher(unittest.TestCase):
-    def setUp(self):
-        self.watcher = watcher.Watcher(TESTFILE, self.callback)
-
-    def tearDown(self):
-        # It doesn't hurt to disable again
-        self.watcher.disable()
-
-    def callback(self):
-        pass
-
-    def test_enable_disable(self):
-        # Ensure we can safely start and stop, and restart a watcher
-        for i in range(3):
-            self.watcher.enable()
-            self.assertTrue(self.watcher.running)
-            self.watcher.disable()
-            self.assertFalse(self.watcher.running)
-
-
-if __name__ == "__main__":
-    unittest.main()

watcher.c

-#include "Python.h"
-
-#ifdef MS_WINDOWS
-#include <windows.h>
-/* ignore "conditional expression is constant" */
-#pragma warning(disable: 4100)
-/* ignore "unreferenced formal parameter" */
-#pragma warning(disable: 4127)
-
-#define MAX_BUFFER 4096
-#endif /* MS_WINDOWS */
-
-#ifdef DARWIN
-#include <pthread.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/event.h>
-#include <sys/time.h>
-
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-#endif /*DARWIN */
-
-#ifdef LINUX
-#endif
-
-static volatile int die = 0;
-
-static PyObject *watcher_error;
-
-typedef struct {
-    PyObject *callback;
-    PyObject *user_data;
-    const char *path;
-    int running;
-    int recursive;
-    unsigned int flags;
-
-#ifdef DARWIN
-    int kq;
-    int fd;
-    pthread_t thread_id;
-    struct kevent events[1];
-    struct kevent data[1];
-#elif defined(MS_WINDOWS)
-    HANDLE handle;
-    HANDLE completion;
-    HANDLE thread;
-    DWORD thread_id;
-    CHAR buffer[MAX_BUFFER];
-    DWORD buffer_len;
-    OVERLAPPED overlapped;
-#endif
-} watcher_object;
-
-#ifdef DARWIN
-void *
-kqueue_thread(void *arg)
-{
-    PyGILState_STATE state;
-    int count;
-    struct timespec timeout;
-    timeout.tv_sec = 0;
-    timeout.tv_nsec = 100000000; /* 100 milliseconds */
-
-    watcher_object *parent = (watcher_object*)arg;
-
-    while(1) {
-        if(die == 1) {
-            goto exit;
-        }
-
-        /*count = kevent(parent->kq, parent->events, 1, parent->data,
-                       1, &timeout); */
-       
-        state = PyGILState_Ensure();
-        PyObject_CallObject(parent->callback, NULL);
-        PyGILState_Release(state);
-        sleep(5);
-    }
-exit:
-    pthread_exit(NULL);
-}
-#endif /* DARWIN */
-
-#ifdef MS_WINDOWS
-void WINAPI
-handle_directory_change(DWORD completion_port)
-{
-
-}
-
-void WINAPI
-watch_thread(watcher_object *obj)
-{
-    PyGILState_STATE state;
-    HANDLE handle_thread;
-
-    ReadDirectoryChangesW(obj->handle, obj->buffer, MAX_BUFFER,
-                          obj->recursive, /* watch subdirectories */
-                          obj->flags,
-                          &obj->buffer_len,
-                          &obj->overlapped,
-                          NULL); /* completion routine */
-    
-    handle_thread = CreateThread(NULL, 0,
-                                 (LPTHREAD_START_ROUTINE)handle_directory_change,
-                                 obj->completion,
-                                 0, NULL);
-    while(1) {
-        if(die == 1) {
-           break; 
-        }
-        state = PyGILState_Ensure();
-        PyObject_CallObject(obj->callback, NULL);
-        PyGILState_Release(state);
-        Sleep(5000);
-    }
-
-    /* Kill the completion port */
-    PostQueuedCompletionStatus(obj->completion, 0, 0, NULL);
-    WaitForSingleObject(handle_thread, INFINITE);
-    CloseHandle(handle_thread);
-}
-#endif
-
-static PyObject *
-watcher_object_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
-{
-    watcher_object *obj;
-    PyObject *callback;
-    PyObject *user_data = Py_None;
-    const char *path;
-    unsigned int flags;
-    int recursive;
-
-    static char *keywords[] = {"path", "callback", "user_data", "flags",
-                               "recursive", NULL};
-    if(!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|Oii", keywords,
-                                    &path, &callback, &user_data, &flags,
-                                    &recursive))
-        return NULL;
-
-    if(!PyCallable_Check(callback)) {
-        PyErr_Format(PyExc_TypeError,
-                     "callback parameter must be callable");
-        return NULL;
-    }
-
-    obj = (watcher_object*)type->tp_alloc(type, 0);
-    if(obj == NULL)
-        return NULL;
-    
-    obj->path = path;
-    obj->callback = callback;
-    obj->flags = flags;
-    obj->recursive = recursive;
-    obj->running = 0;
-
-    if(user_data != Py_None) {
-        Py_INCREF(user_data);
-        obj->user_data = user_data;
-    }
-
-#ifdef DARWIN
-    /* Get the file descripter of a new kernel event queue */
-    Py_BEGIN_ALLOW_THREADS
-    obj->kq = kqueue();
-    Py_END_ALLOW_THREADS
-
-    if(obj->kq < 0) {
-        PyErr_Format(watcher_error, "unable to create kqueue");
-        return NULL;
-    }
-    
-    /* Open a file specifically for event notifications */
-    Py_BEGIN_ALLOW_THREADS
-    obj->fd = open(path, O_EVTONLY);
-    Py_END_ALLOW_THREADS
-
-    if(obj->fd <= 0) {
-        PyErr_Format(watcher_error, "unable to open path");
-        return NULL;
-    }
-#elif defined(MS_WINDOWS)
-    obj->handle = CreateFile(path, FILE_LIST_DIRECTORY,
-                   FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
-                   NULL, OPEN_EXISTING,
-                   FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
-                   NULL);
-    if(obj->handle == INVALID_HANDLE_VALUE) {
-        PyErr_Format(watcher_error, "unable to open path");
-        return NULL;
-    }
-#endif /* DARWIN */
-
-    return (PyObject*)obj;
-}
-
-static void
-watcher_object_dealloc(watcher_object *obj)
-{
-#ifdef DARWIN
-    if(obj->kq >=0)
-        (void)close(obj->kq);
-    if(obj->fd >=0)
-        (void)close(obj->fd);
-#endif
-
-    /* Make sure everything is shutdown */
-    Py_TYPE(obj)->tp_free((PyObject*)obj);
-}
-
-PyDoc_STRVAR(watcher_enable_doc, "TODO");
-
-static PyObject *
-watcher_enable_function(watcher_object *self, PyObject *unused)
-{
-    if(self->running == 1)
-        Py_RETURN_FALSE;
-
-#ifdef DARWIN
-    /* vnode_events = NOTE_DELETE |  NOTE_WRITE | NOTE_EXTEND |
-                   NOTE_ATTRIB | NOTE_LINK | NOTE_RENAME | NOTE_REVOKE;
-    EV_SET(&events[0], self->fd, EVFILT_VNODE, EV_ADD | EV_CLEAR,
-           vnode_events, 0, NULL); */
-    if(pthread_create(&self->thread_id, NULL, kqueue_thread,
-                      (void*)self)) {
-        PyErr_Format(watcher_error, "unable to start kqueue thread");
-        return NULL;
-    }
-    die = 0;
-#elif defined(MS_WINDOWS)
-    self->completion = CreateIoCompletionPort(self->handle, self->completion,
-                                              self, /* ignored */ 0);
-    if(self->completion == NULL) {
-        PyErr_Format(watcher_error, "unable to complete completion port");
-        return NULL;
-    }
-
-    self->thread = CreateThread(NULL, 0,
-                                (LPTHREAD_START_ROUTINE)watch_thread,
-                                self, 0, &self->thread_id);
-    if(self->thread == NULL) {
-        PyErr_Format(watcher_error, "unable to start watcher thread");
-        return NULL;
-    }
-#endif
-
-    self->running = 1;
-    Py_RETURN_NONE;
-}
-
-PyDoc_STRVAR(watcher_disable_doc, "TODO");
-
-static PyObject *
-watcher_disable_function(watcher_object *self, PyObject *unused)
-{
-    if(self->running == 0)
-        Py_RETURN_FALSE;
-
-#ifdef DARWIN
-    /*pthread_mutex_lock(&mutex);*/
-    die = 1;
-    /*pthread_mutex_unlock(&mutex);*/
-    if(pthread_join(self->thread_id, NULL)) {
-        PyErr_Format(watcher_error, "error joining kqueue thread");
-        return NULL;
-    }
-#elif defined(MS_WINDOWS)
-    die = 1;
-    WaitForSingleObject(self->thread, INFINITE);
-    CloseHandle(self->completion);
-    CloseHandle(self->thread);
-#endif
-
-    self->running = 0;
-    Py_RETURN_NONE;
-}
-
-static PyMethodDef watcher_object_methods[] = {
-    {"enable", (PyCFunction)watcher_enable_function, METH_NOARGS, 
-               watcher_enable_doc},
-    {"disable", (PyCFunction)watcher_disable_function, METH_NOARGS,
-                watcher_disable_doc},
-    {NULL, NULL}
-};
-
-static PyObject *
-watcher_running_getter(watcher_object *self, void *unused)
-{
-    if(self->running == 0)
-        Py_RETURN_FALSE;
-    else
-        Py_RETURN_TRUE;
-}
-
-PyDoc_STRVAR(watcher_running_doc, "TODO");
-
-static PyGetSetDef watcher_object_getset[] = {
-    {"running", (getter)watcher_running_getter, NULL, watcher_running_doc},
-    {NULL}
-};
-
-PyDoc_STRVAR(watcher_doc, "TODO");
-
-static PyTypeObject watcher_object_type = {
-    PyVarObject_HEAD_INIT(NULL, 0)
-    "watcher.Watch",                    /* tp_name */
-    sizeof(watcher_object),             /* tp_size */
-    0,                                  /* tp_itemsize */
-    (destructor)watcher_object_dealloc, /* tp_dealloc */
-    0,                                  /* tp_print */
-    0,                                  /* tp_getattr */
-    0,                                  /* tp_setattr */
-    0,                                  /* tp_compare */
-    0,                                  /* tp_repr */
-    0,                                  /* tp_as_number */
-    0,                                  /* tp_as_sequence */
-    0,                                  /* tp_as_mapping */
-    0,                                  /* tp_hash */
-    0,                                  /* tp_call */
-    0,                                  /* tp_str */
-    0,                                  /* tp_getattro */
-    0,                                  /* tp_setattro */
-    0,                                  /* tp_as_buffer */
-    Py_TPFLAGS_DEFAULT | 
-    Py_TPFLAGS_BASETYPE,                /* tp_flags */
-    watcher_doc,                        /* tp_doc */
-    0,                                  /* tp_traverse */
-    0,                                  /* tp_clear */
-    0,                                  /* tp_richcompare */
-    0,                                  /* tp_weaklistoffset */
-    0,                                  /* tp_iter */
-    0,                                  /* tp_iterneext */
-    watcher_object_methods,             /* tp_methods */
-    0,                                  /* tp_members */
-    watcher_object_getset,              /* tp_getset */
-    0,                                  /* tp_base */
-    0,                                  /* tp_dict */
-    0,                                  /* tp_descr_get */
-    0,                                  /* tp_descr_set */
-    0,                                  /* tp_dictoffset */
-    0,                                  /* tp_init */
-    PyType_GenericAlloc,                /* tp_alloc */
-    watcher_object_new,                 /* tp_new */
-    PyObject_Del,                       /* tp_free */
-};
-
-static PyObject *
-watch_function(PyObject *self, PyObject *args)
-{
-    Py_RETURN_NONE;
-}
-
-static PyMethodDef watcher_functions[] = {
-    {"watch", (PyCFunction)watch_function, METH_VARARGS | METH_KEYWORDS,
-              "TODO"},
-    {NULL, NULL}
-};
-
-PyDoc_STRVAR(module_doc,"TODO");
-
-static void
-setint(PyObject *dict, const char* name, long value)
-{
-    PyObject *val = PyLong_FromLong(value);
-    if(val && PyDict_SetItemString(dict, name, val) == 0)
-        Py_DECREF(val);
-}
-
-#if PY_MAJOR_VERSION == 3
-static struct PyModuleDef _watchermodule = {
-    PyModuleDef_HEAD_INIT,
-    "watcher",
-    module_doc,
-    -1,
-    watcher_functions,
-    NULL,
-    NULL,
-    NULL,
-    NULL
-};
-#endif
-
-#if PY_MAJOR_VERSION == 3
-PyMODINIT_FUNC
-PyInit_watcher(void)
-#else
-PyMODINIT_FUNC
-initwatcher(void)
-#endif
-{
-    PyObject *module, *dict;
-
-    /* Initialize and acquire the GIL before we do anything else. */
-    PyEval_InitThreads();
-
-#if PY_MAJOR_VERSION == 3
-    module = PyModule_Create(&_watchermodule);
-#else
-    module = Py_InitModule3("watcher", watcher_functions, module_doc);
-#endif
-
-    if(PyType_Ready(&watcher_object_type) < 0)
-        goto fail;
-
-    dict = PyModule_GetDict(module);
-    if(!dict)
-        goto fail;
-
-    watcher_error = PyErr_NewException("watcher.error",
-                                       PyExc_EnvironmentError, NULL);
-    if(watcher_error == NULL)
-        goto fail;
-
-    PyDict_SetItemString(dict, "error", watcher_error);
-    PyDict_SetItemString(dict, "Watcher", (PyObject*)&watcher_object_type);
-
-#ifdef DARWIN
-    setint(dict, "DELETE", NOTE_DELETE);
-    setint(dict, "UPDATE", NOTE_WRITE | NOTE_EXTEND);
-    setint(dict, "RENAME", NOTE_RENAME);
-#elif defined(MS_WINDOWS)
-    /* Notify Filters */
-    setint(dict, "FILE_NOTIFY_CHANGE_FILE_NAME",
-           FILE_NOTIFY_CHANGE_FILE_NAME);
-    setint(dict, "FILE_NOTIFY_CHANGE_DIR_NAME", 
-           FILE_NOTIFY_CHANGE_DIR_NAME);
-    setint(dict, "FILE_NOTIFY_CHANGE_ATTRIBUTES", 
-           FILE_NOTIFY_CHANGE_ATTRIBUTES);
-    setint(dict, "FILE_NOTIFY_CHANGE_SIZE",
-           FILE_NOTIFY_CHANGE_SIZE);
-    setint(dict, "FILE_NOTIFY_CHANGE_LAST_WRITE", 
-           FILE_NOTIFY_CHANGE_LAST_WRITE);
-    setint(dict, "FILE_NOTIFY_CHANGE_LAST_ACCESS",
-           FILE_NOTIFY_CHANGE_LAST_ACCESS);
-    setint(dict, "FILE_NOTIFY_CHANGE_CREATION", 
-           FILE_NOTIFY_CHANGE_CREATION);
-    setint(dict, "FILE_NOTIFY_CHANGE_SECURITY", 
-           FILE_NOTIFY_CHANGE_SECURITY);
-
-    /* FILE_NOTIFY_INFORMATION */
-    setint(dict, "FILE_ACTION_ADDED", FILE_ACTION_ADDED);
-    setint(dict, "FILE_ACTION_REMOVED", FILE_ACTION_REMOVED);
-    setint(dict, "FILE_ACTION_MODIFIED", FILE_ACTION_MODIFIED);
-    setint(dict, "FILE_ACTION_RENAMED_OLD_NAME", FILE_ACTION_RENAMED_OLD_NAME);
-    setint(dict, "FILE_ACTION_RENAMED_NEW_NAME", FILE_ACTION_RENAMED_NEW_NAME);
-#endif
-
-#if PY_MAJOR_VERSION == 3
-    return module;
-#endif
-
-fail:
-#if PY_MAJOR_VERSION == 3
-    return NULL;
-#else
-    return;
-#endif
-}
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 ProjectModifiedEvent.java.
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.