Reformat code with pre-commit

This commit is contained in:
2022-07-21 13:39:41 +01:00
parent 61f6715b20
commit 7c855e09c0
60 changed files with 547 additions and 278 deletions

View File

@@ -6,11 +6,12 @@ from math import ceil
import modules.provision
from twisted.internet.threads import deferToThread
def allRelaysActive(net):
relayNum = len(main.network[net].relays.keys())
existNum = 0
for i in main.network[net].relays.keys():
name = net+str(i)
name = net + str(i)
if name in main.IRCPool.keys():
if main.IRCPool[name].authenticated:
existNum += 1
@@ -18,20 +19,22 @@ def allRelaysActive(net):
return True
return False
def getChanFree(net, new):
chanfree = {}
chanlimits = set()
for i in main.network[net].relays.keys():
if i in new:
continue
name = net+str(i)
chanfree[i] = main.IRCPool[name].chanlimit-len(main.IRCPool[name].channels)
name = net + str(i)
chanfree[i] = main.IRCPool[name].chanlimit - len(main.IRCPool[name].channels)
chanlimits.add(main.IRCPool[name].chanlimit)
if not len(chanlimits) == 1:
error("Network %s has servers with different CHANLIMIT values" % net)
return False
return (chanfree, chanlimits.pop())
def emptyChanAllocate(net, flist, relay, new):
chanfree = getChanFree(net, new)
if not chanfree:
@@ -41,10 +44,10 @@ def emptyChanAllocate(net, flist, relay, new):
allocated = {}
toalloc = len(flist)
if toalloc > sum(chanfree[0].values()):
correction = round(toalloc-sum(chanfree[0].values()) / chanfree[1])
#print("correction", correction)
correction = round(toalloc - sum(chanfree[0].values()) / chanfree[1])
# print("correction", correction)
warn("Ran out of channel spaces, provisioning additional %i relays for %s" % (correction, net))
#newNums = modules.provision.provisionMultipleRelays(net, correction)
# newNums = modules.provision.provisionMultipleRelays(net, correction)
return False
for i in chanfree[0].keys():
for x in range(chanfree[0][i]):
@@ -56,8 +59,9 @@ def emptyChanAllocate(net, flist, relay, new):
allocated[i] = [flist.pop()]
return allocated
def populateChans(net, clist, relay, new):
#divided = array_split(clist, relay)
# divided = array_split(clist, relay)
allocated = emptyChanAllocate(net, clist, relay, new)
if not allocated:
return
@@ -67,18 +71,20 @@ def populateChans(net, clist, relay, new):
else:
main.TempChan[net] = {i: allocated[i]}
def notifyJoin(net):
for i in main.network[net].relays.keys():
name = net+str(i)
name = net + str(i)
if name in main.IRCPool.keys():
main.IRCPool[name].checkChannels()
def minifyChans(net, listinfo):
if not allRelaysActive(net):
error("All relays for %s are not active, cannot minify list" % net)
return False
for i in main.network[net].relays.keys():
name = net+str(i)
name = net + str(i)
for x in main.IRCPool[name].channels:
for y in listinfo:
if y[0] == x:
@@ -88,29 +94,31 @@ def minifyChans(net, listinfo):
return False
return listinfo
def keepChannels(net, listinfo, mean, sigrelay, relay):
listinfo = minifyChans(net, listinfo)
if not listinfo:
return
if relay <= main.config["ChanKeep"]["SigSwitch"]: # we can cover all of the channels
if relay <= main.config["ChanKeep"]["SigSwitch"]: # we can cover all of the channels
coverAll = True
elif relay > main.config["ChanKeep"]["SigSwitch"]: # we cannot cover all of the channels
elif relay > main.config["ChanKeep"]["SigSwitch"]: # we cannot cover all of the channels
coverAll = False
if not sigrelay <= main.config["ChanKeep"]["MaxRelay"]:
error("Network %s is too big to cover: %i relays required" % (net, sigrelay))
return
if coverAll:
needed = relay-len(main.network[net].relays.keys())
needed = relay - len(main.network[net].relays.keys())
newNums = modules.provision.provisionMultipleRelays(net, needed)
flist = [i[0] for i in listinfo]
populateChans(net, flist, relay, newNums)
else:
needed = sigrelay-len(main.network[net].relays.keys())
needed = sigrelay - len(main.network[net].relays.keys())
newNums = modules.provision.provisionMultipleRelays(net, needed)
siglist = [i[0] for i in listinfo if int(i[1]) > mean]
populateChans(net, siglist, sigrelay, newNums)
notifyJoin(net)
def joinSingle(net, channel):
if allRelaysActive(net):
chanfree = getChanFree(net, [])
@@ -122,14 +130,16 @@ def joinSingle(net, channel):
error("All relays for %s are not active" % net)
return False
def nukeNetwork(net):
#purgeRecords(net)
#p = main.g.pipeline()
main.g.delete("analytics.list."+net)
#p.delete("list."+net)
#p.execute()
#def nukeNetwork(net):
def nukeNetwork(net):
# purgeRecords(net)
# p = main.g.pipeline()
main.g.delete("analytics.list." + net)
# p.delete("list."+net)
# p.execute()
# def nukeNetwork(net):
# deferToThread(_nukeNetwork, net)
@@ -141,7 +151,7 @@ def _initialList(net, num, listinfo, chanlimit):
except TypeError:
warn("Bad LIST data received from %s - %i" % (net, num))
return
mean = round(cumul/listlength, 2)
mean = round(cumul / listlength, 2)
siglength = 0
insiglength = 0
sigcumul = 0
@@ -154,8 +164,8 @@ def _initialList(net, num, listinfo, chanlimit):
insiglength += 1
insigcumul += int(i[1])
sigrelay = ceil(siglength/chanlimit)
relay = ceil(listlength/chanlimit)
sigrelay = ceil(siglength / chanlimit)
relay = ceil(listlength / chanlimit)
netbase = "list.%s" % net
abase = "analytics.list.%s" % net
p = main.g.pipeline()
@@ -163,18 +173,18 @@ def _initialList(net, num, listinfo, chanlimit):
p.hset(abase, "total", listlength)
p.hset(abase, "sigtotal", siglength)
p.hset(abase, "insigtotal", insiglength)
p.hset(abase, "sigperc", round(siglength/listlength*100, 2))
p.hset(abase, "insigperc", round(insiglength/listlength*100, 2))
p.hset(abase, "sigperc", round(siglength / listlength * 100, 2))
p.hset(abase, "insigperc", round(insiglength / listlength * 100, 2))
p.hset(abase, "cumul", cumul)
p.hset(abase, "sigcumul", sigcumul)
p.hset(abase, "insigcumul", insigcumul)
p.hset(abase, "relay", relay)
p.hset(abase, "sigrelay", sigrelay)
p.hset(abase, "insigrelay", ceil(insiglength/chanlimit))
p.hset(abase, "insigrelay", ceil(insiglength / chanlimit))
# Purge existing records before writing
#purgeRecords(net)
#for i in listinfo:
# purgeRecords(net)
# for i in listinfo:
# p.rpush(netbase+"."+i[0], i[1])
# p.rpush(netbase+"."+i[0], i[2])
# p.sadd(netbase, i[0])
@@ -183,6 +193,6 @@ def _initialList(net, num, listinfo, chanlimit):
debug("List parsing completed on %s" % net)
keepChannels(net, listinfo, mean, sigrelay, relay)
def initialList(net, num, listinfo, chanlimit):
deferToThread(_initialList, net, num, deepcopy(listinfo), chanlimit)