import repository from arizona
[raven.git] / 2.0 / python / storktrackusage.py
1 #! /usr/bin/env python
2
3 """
4 Stork Project (http://www.cs.arizona.edu/stork/)
5 Module: storktrackusage
6 Description:  Tracks usage of files
7 """
8
9 # Use arizonaconfig
10 #           [option, long option,       variable,        action,  data,     default, metavar,    description]
11 """arizonaconfig
12    options=[["",     "--tempusagefilename", "tempUsageFileName", "store", "string", "/usr/local/stork/var/filesopened.temp",      None, "track usage of stork files (this session)"],
13             ["",     "--permusagefilename", "permUsageFileName", "store", "string", "/usr/local/stork/var/filesopened.perm",      None, "track usage of stork files (last session)"]]
14    includes=[]
15 """
16
17 import arizonaconfig
18 import arizonacrypt
19 import arizonareport
20 import sys
21 import os
22
23 file_list = []
24
25 def reset():
26    tempUsageFileName = arizonaconfig.get_option("tempUsageFileName")
27    if os.path.exists(tempUsageFileName):
28       os.remove(tempUsageFileName)
29
30 def add_file(filename):
31    if filename in file_list:
32        return
33
34    # get the hash of the file
35    if os.path.exists(filename):
36       hash = arizonacrypt.get_fn_hash(filename)
37    else:
38       hash = "doesnotexist"
39
40    file_list.append(filename)
41
42    try:
43        tempUsageFileName = arizonaconfig.get_option("tempUsageFileName")
44        file = open(tempUsageFileName, "a")
45        if file:
46            file.write(filename + " " + hash + "\n")
47            file.close()
48    except IOError:
49        arizonareport.send_error(0, "failed to write to usage file ")
50    except OSError:
51        arizonareport.send_error(0, "failed to write to usage file ")
52
53 def commit():
54    permUsageFileName = arizonaconfig.get_option("permUsageFileName")
55    tempUsageFileName = arizonaconfig.get_option("tempUsageFileName")
56    if os.path.exists(permUsageFileName):
57       os.remove(permUsageFileName)
58    if os.path.exists(tempUsageFileName):
59       os.rename(tempUsageFileName, permUsageFileName)
60
61 def verify():
62    result = True
63
64    permUsageFileName = arizonaconfig.get_option("permUsageFileName")
65
66    # open and read the file
67    file = open(permUsageFileName, "r")
68    lines = file.readlines()
69
70    # there could be duplicate lines because multiple copies of stork may have
71    # been invoked from one run of pacman
72    lines = uniq(lines)
73
74    for line in lines:
75        splitline = line.split(" ")
76        (filename, last_hash) = (splitline[0], splitline[1])
77
78        # get the hash of the file
79        if os.path.exists(filename):
80           hash = arizonacrypt.get_fn_hash(filename)
81        else:
82           hash = "doesnotexist"
83
84        if (hash != last_hash):
85           arizonareport.send_out("hash mismatch file " + filename + " last=" + last_hash + " cur =" + hash)
86           result = False
87
88    return result
89
90
91
92
93