This file is indexed.

/usr/share/pyshared/smart/mirror.py is in python-smartpm 1.4-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#
# Copyright (c) 2004 Conectiva, Inc.
#
# Written by Gustavo Niemeyer <niemeyer@conectiva.com>
#
# This file is part of Smart Package Manager.
#
# Smart Package Manager 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.
#
# Smart Package Manager 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Smart Package Manager; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
from smart import *
import random
import time

HISTORYPERMIRROR = 20
HISTORYCUTDELAY = 60
GRANULARITY = 100

class MirrorSystem(object):

    def __init__(self):
        self._mirrors = {}
        self._history = []
        self._penality = {}
        self._changed = False
        self._historychanged = False
        self._lastcuttime = 0

    def getMirrors(self):
        return self._mirrors

    def setMirrors(self, mirrors):
        self._changed = True
        self._mirrors = mirrors

    def getHistory(self):
        return self._history

    def setHistory(self, history):
        self._changed = True
        self._history = history
        self._historychanged = False

    def getHistoryChanged(self):
        return self._historychanged

    def addInfo(self, mirror, **info):
        if mirror:
            self._changed = True
            self._history.insert(0, (mirror, info))
            self._historychanged = True
            now = time.time()
            if now-self._lastcuttime > HISTORYCUTDELAY:
                self._lastcuttime = now
                count = 0
                for origin in self._mirrors:
                    count += 1+len(self._mirrors[origin])
                del self._history[count*HISTORYPERMIRROR:]

    def get(self, url): 
        elements = {}
        for origin in self._mirrors:
            if url.startswith(origin):
                elements[origin] = MirrorElement(self, origin, origin)
                for mirror in self._mirrors[origin]:
                    elements[mirror] = MirrorElement(self, origin, mirror)
        if elements:
            elements = elements.values()
        else:
            elements = [MirrorElement(self, "", "")]
        return MirrorItem(self, url, elements)

    def getPenalities(self):
        self.updatePenality()
        return self._penality

    def updatePenality(self):
        if not self._changed:
            return
        self._changed = False
        self._penality.clear()
        data = {}
        for mirror, info in self._history:
            if mirror not in data:
                mirrordata = data.setdefault(mirror, {"size": 0, "time": 0,
                                                      "failed": 0})
            else:
                mirrordata = data[mirror]
            mirrordata["size"] += info.get("size", 0)
            mirrordata["time"] += info.get("time", 0)
            mirrordata["failed"] += info.get("failed", 0)
        maxpenality = 1
        justerrors = []
        for mirror in data:
            mirrordata = data[mirror]
            if mirrordata["size"]:
                penality = (mirrordata["time"]*1000000)/mirrordata["size"]
                penality += mirrordata["failed"]*(penality*0.1)
                # Integer division by granularity ensures that mirrors
                # which are close enough will be considered equal to
                # distribute load.
                penality /= GRANULARITY
                self._penality[mirror] = penality
                if penality > maxpenality:
                    maxpenality = penality
            elif mirrordata["failed"]:
                justerrors.append(mirror)
        if justerrors:
            for mirror in justerrors:
                self._penality[mirror] = maxpenality

class MirrorElement(object):

    def __init__(self, system, origin, mirror):
        self._system = system
        self.origin = origin
        self.mirror = mirror

        if origin and mirror and origin[-1] == "/" and mirror[-1] != "/":
            self.mirror += "/"

    def __cmp__(self, other):
        # Give priority to local files.
        rc = -cmp(self.mirror.startswith("file://"),
                  other.mirror.startswith("file://"))
        if rc == 0:
            # Otherwise, check penality.
            pen = self._system._penality
            rc = cmp(pen.get(self.mirror, 0), pen.get(other.mirror, 0))
        return rc

class MirrorItem(object):

    def __init__(self, system, url, elements):
        self._system = system
        self._url = url
        self._elements = elements
        self._current = None

    def addInfo(self, **info):
        if self._current and hasattr(self._current, 'mirror'):
            self._system.addInfo(self._current.mirror, **info)

    def getNext(self):
        if self._elements:
            self._system.updatePenality()
            random.shuffle(self._elements)
            self._elements.sort()
            #for i, item in enumerate(self._elements):
            #    penality = self._system.getPenalities().get(item.mirror, 0)
            #    print "%d. %s (%d)" % (i, item.mirror, penality)
            self._current = elem = self._elements.pop(0)
            return elem.mirror+self._url[len(elem.origin):]
        else:
            self._current = None
            return None

# vim:ts=4:sw=4:et