summaryrefslogtreecommitdiffstats
path: root/shared/prestoLog.py
blob: 3c0c1e696024a5faec76c7cfb715f22d30fbbda8 (plain)
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
# author: Jonathan Dieter <jdieter@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright 2005 Duke University

def log(conduit, LOG_FILE, rpm_size, drpm_size):
    # Open log file for reading
    try:
        log_file = open(LOG_FILE, "r")
        log_exists = True
    except:
        conduit.info(5, "Info: %s doesn't exist. Will create." % LOG_FILE)
        log_exists = False
        
    # Log file doesn't exist, create
    if not log_exists:
        try:
            log_file = open(LOG_FILE, "w")
            log_file.write("Download Size (without DRPM),Download Size (with DRPM),Percentage Savings,Total Percentage Savings\n")
            log_file.close()
            log_exists = True
        except:
            conduit.info(2, "Warning: Unable to write to %s" % LOG_FILE)
        if log_exists:
            try:
                log_file = open(LOG_FILE, "r")
            except:
                conduit.info(2, "Warning: Unable to open %s for reading." % LOG_FILE)
                log_exists = False
                
    # Cycle through items already in log so we can come up with total savings
    if log_exists:
        total_rpm_size = 0
        total_drpm_size = 0
        
        # Get rid of header line
        log_file.readline()
        
        data = log_file.readline()
        while data != "":
            fc = data.find(",")
            sc = data.find(",", fc + 1)
            total_rpm_size += int(data[:fc])
            total_drpm_size += int(data[fc + 1:sc])
            data = log_file.readline()
        log_file.close()
        total_rpm_size += rpm_size
        total_drpm_size += drpm_size
        
        try:
            log_file = open(LOG_FILE, "a")
        except:
            conduit.info(2, "Warning: Unable to open %s for writing." % LOG_FILE)
            log_exists = False

        # Write data to log
        if log_exists:
            log_file.write("%i,%i,%i,%i\n" % (rpm_size, drpm_size, 100 - ((drpm_size * 100) / rpm_size), 100 - ((total_drpm_size * 100) / total_rpm_size)))
            log_file.close()