Code: Select all
local pyscript = [=[
def CalcHash(filename):
import hashlib
from os.path import getsize
import lua
f_src = open(filename, "rb")
if not f_src : ErrorExit("Cannot open file:", filename)
hlist = hashlib.algorithms_guaranteed
# hlist = hashlib.algorithms_available
hh = [hashlib.new(name) for name in hlist]
display_progress = lua.eval("python._display_progress")
CHUNK = 16 * 1024
nSize = getsize(filename)
nLeft = nSize
nDisplayCount = 0
while nLeft > 0:
buf = f_src.read((CHUNK < nLeft) and CHUNK or nLeft)
nRead = len(buf)
if nRead == 0:
break
nLeft = nLeft - nRead
nDisplayCount += 1
for h in hh: h.update(buf)
if nSize > 4*1024*1024 and nDisplayCount%201==1:
if display_progress(filename, nSize-nLeft+0.0, nSize+0.0, 1.0-nLeft/nSize):
f_src.close()
return
f_src.close()
res = []
for name,h in zip(hlist,hh):
res.append(name)
res.append(h.hexdigest())
return res
]=]
local uchar = unicode.utf8.char
local function set_progress (LEN, ratio)
local uchar1, uchar2 = uchar(9608), uchar(9617)
local len = math.floor(ratio*LEN + 0.5)
return uchar1:rep(len) .. uchar2:rep(LEN-len) .. (" %3d%%"):format(ratio*100)
end
local function FormatNum(num)
local s = tostring(num)
local len = #s
s = s:gsub("()", function(pos) return pos>1 and pos<len and (len-pos)%3==2 and "," end)
return s
end
local function display_progress (fullname, cntFound, cntTotal, ratio)
if win.ExtractKey()=="ESCAPE" then
far.AdvControl("ACTL_REDRAWALL")
return true
end
local WID, W1 = 60, 3
local W2 = WID - W1 - 3
local len = fullname:len()
local s = len<=WID and fullname..(" "):rep(WID-len) or
fullname:sub(1,W1).. "..." .. fullname:sub(-W2)
far.Message(
(s.."\n") .. (set_progress(W2, ratio).."\n") .. ("Processed: "..FormatNum(cntFound).."/"..FormatNum(cntTotal)),
"Working...", "")
end
Macro {
description="Lunatic Python - HASHES";
area="Shell"; key="CtrlShiftF1";
condition=function(key) return not (APanel.Plugin or APanel.Folder) end;
action=function()
require "python"
python._display_progress = display_progress
local pb = python.builtins()
python.execute(pyscript)
local CalcHash = python.eval("CalcHash")
local all = CalcHash(APanel.UNCPath.."\\"..APanel.Current)
if all then
local names, hashes, nums = {}, {}, {}
local maxlen = 0
for k=1,pb.len(all),2 do
nums[#nums+1] = #nums+1
names [#nums] = all[k-1]
hashes[#nums] = all[k]
maxlen = math.max(maxlen, names[#nums]:len())
end
table.sort(nums, function(a,b) return names[a]:lower()<names[b]:lower() end)
local items = { {"DI_DOUBLEBOX",3,1,Far.Width-11,#nums+4,0,0,0,0,APanel.Current} }
for i,v in ipairs(nums) do
items[#items+1] = {"DI_TEXT",5,i+1,0,0,0,0,0,0,names[v]}
items[#items+1] = {"DI_EDIT",6+maxlen,i+1,Far.Width-12,0,0,0,0,{DIF_READONLY=1,DIF_SELECTONENTRY=1},hashes[v]}
end
items[#items+1] = {"DI_TEXT",-1, #nums+2,0,0,0,0,0,"DIF_SEPARATOR",""}
items[#items+1] = {"DI_BUTTON",0,#nums+3,0,0,0,0,0,{DIF_DEFAULTBUTTON=1,DIF_CENTERGROUP=1},"OK"}
local guid = win.Uuid("3417EE88-021E-4BEC-9F46-E3EF8FE8CBE9")
far.Dialog (guid,-1,-1,Far.Width-7,#nums+6,nil,items)
end
end;
}