File: monitor-directory

package info (click to toggle)
pytagsfs 0.9.2-6
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 984 kB
  • ctags: 1,858
  • sloc: python: 10,656; xml: 543; makefile: 47
file content (124 lines) | stat: -rwxr-xr-x 3,270 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python

# Copyright (c) 2007-2008 Forest Bond.
# This file is part of the pytagsfs software package.
#
# pytagsfs is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# A copy of the license has been included in the COPYING file.

import sys, os, stat, time, optparse, signal

from sclapp import main_function

from pytagsfs.sourcetreemon import (
  get_source_tree_monitor,
  SOURCE_TREE_MONITORS,
)

from pytagsfs.exceptions import MissingDependency

def add_source_dir(stm, path):
    stm.add_source_dir(path)

    try:
        names = os.listdir(path)
    except (OSError, IOError):
        return

    for name in names:
        sub_path = os.path.join(path, name)
        stm.add_cb(sub_path)

def make_add_cb(stm):
    def add_cb(path, is_dir = None):
        print 'ADD', path, is_dir

        if is_dir is None:
            add_source_dir(stm, path)
            stm.add_source_file(path)
        elif is_dir:
            add_source_dir(stm, path)
        else:
            stm.add_source_file(path)
    return add_cb

def make_remove_cb(stm):
    def remove_cb(path, is_dir = None):
        print 'REMOVE', path, is_dir

        if is_dir is None:
            stm.remove_source_dir(path)
            stm.remove_source_file(path)
        elif is_dir:
            stm.remove_source_dir(path)
        else:
            stm.remove_source_file(path)
    return remove_cb

def make_update_cb(stm):
    def update_cb(path, is_dir = None):
        print 'UPDATE', path, is_dir
    return update_cb

def choose_source_tree_monitor(stm_dotted_name = None):
    if stm_dotted_name is not None:
        candidates = [stm_dotted_name]
    else:
        candidates = SOURCE_TREE_MONITORS

    for candidate in candidates:
        try:
            stm = get_source_tree_monitor(candidate)
        except MissingDependency, e:
            print >>sys.stderr, (
              'source tree monitor %s unsupported '
              'due to missing dependency %s'
            ) % (candidate, str(e))
        else:
            print >>sys.stderr, 'using source tree monitor %s' % candidate
            return stm

    raise AssertionError('unable to find a usable source tree monitor')

def handle_argv(argv):
    parser = optparse.OptionParser(usage = '%prog [OPTIONS] {directory}')
    parser.add_option(
      '--source-tree-monitor',
      default = None,
      help = 'use SOURCE_TREE_MONITOR as source tree monitor class',
    )
    opts, args = parser.parse_args(argv[1:])

    stm_dotted_name = opts.source_tree_monitor
    stm = choose_source_tree_monitor(stm_dotted_name)

    dir_name = os.path.abspath(args[0])

    return stm, dir_name

@main_function(
  exit_signals = [signal.SIGINT, signal.SIGTERM, signal.SIGHUP, signal.SIGPIPE],
)
def main(argv):
    stm, dir_name = handle_argv(argv)

    stm.set_add_cb(make_add_cb(stm))
    stm.set_remove_cb(make_remove_cb(stm))
    stm.set_update_cb(make_update_cb(stm))

    stm.start()

    add_source_dir(stm, dir_name)

    try:
        while True:
            stm.process_events()
            time.sleep(0.1)
    finally:
        stm.stop()

if __name__ == '__main__':
    sys.exit(main())