Welcome, guest | Sign In | My Account | Store | Cart

You run this script with directory as parameter (defaults to current directory) and scripts watch this directory for changes in files - creating new file, deleting some file and modification to any file. All these changes are periodically commited to subversion repository using external commands.

Not especially good for coding but great for tracking changes in your text files f.e. during essay writing etc.

Python, 48 lines
 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
# -*- coding: utf8 -*-
# author: Jiri Zahradil, jiri.zahradil@gmail.com

import os, time, sys, os.path

def svn(cmdline):
    SVNBINARY = r"svn" # your path to svn binary
    print "run: svn "+cmdline
    os.system(SVNBINARY +" "+cmdline)
    
def mylistdir(path):
    return (f for f in os.listdir(path) if (f and not f.startswith(".") and not os.path.isdir(f)))
    
path_to_watch = "." if (len(sys.argv)<=1) else sys.argv[1]
start_dir = os.getcwd()

try:
    os.chdir(path_to_watch)
    path_to_watch = "."
    print "Watching",os.getcwd()    
    before = dict ([(f, os.path.getmtime(f)) for f in mylistdir(path_to_watch)])
    svn("add -q "+" ".join(before))
    svn("ci -q . -m \"watching start\"")
    msg = " -m \"auto-commit\""    
    while 1: 
        time.sleep (5) 
        after = dict ([(f, os.path.getmtime(f)) for f in mylistdir(path_to_watch)])
        
        modified = [f for f,ts in after.iteritems() if ts>before.get(f,ts)]
        added = [f for f in after if not f in before] 
        removed = [f for f in before if not f in after]
        if added:
            svn("add -q "+" ".join(added))
            print "Added: ", ", ".join (added)            
        if removed:
            svn("rm -q "+ " ".join(removed))
            print "Removed: ", ", ".join (removed)            
        if modified:
            print "Modified: ", ", ".join (modified)
            
        if added or removed or modified:
            svn("ci -q ."+msg)
    
        before = after      
    pass

finally:
    os.chdir(start_dir)