Skip to content

Instantly share code, notes, and snippets.

@kaecy
Created March 3, 2024 18:03
Show Gist options
  • Save kaecy/27858b1fdde13765d052f567b84ee56b to your computer and use it in GitHub Desktop.
Save kaecy/27858b1fdde13765d052f567b84ee56b to your computer and use it in GitHub Desktop.
CRC32 Scan Thingy

Readme

Wanna try it out? Point main.py to a directory.

Required:

  • wxpython (python 3.11.4 works well with wxpython 4.2.1)
import os, sys, re
import binascii as checksum
def get_crc32(filename):
crc32 = 0
binaryfile = open(filename, "rb")
while True:
chunk = binaryfile.read(1000000)
if chunk != b"":
crc32 = checksum.crc32(chunk, crc32)
else: break
return "%08X" % crc32
def scan(dir):
dirlist = os.listdir(dir)
result = []
checksum_pattern = "\[[A-F0-9]{8}\]"
for file in dirlist:
match = re.search(checksum_pattern, file)
if match:
crc32 = get_crc32(dir + "\\" + file)
if match[0][1:-1] == crc32:
status = True
else:
status = False
result.append({
"good": status,
"sum": crc32,
"file": file
})
return result
if __name__ == "__main__":
if len(sys.argv)-1 == 1:
print("CRC32", get_crc32(sys.argv[1]))
import os, sys
import wx
import crc32
class CRC32ScanWindow(wx.Frame):
def __init__(self, parent, title, filepath):
super(CRC32ScanWindow, self).__init__(parent, title=title, size=(500, 300))
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
self.list_ctrl = wx.ListCtrl(panel, style=wx.LC_REPORT)
self.list_ctrl.InsertColumn(0, 'File Name', width=300)
self.list_ctrl.InsertColumn(1, 'CRC32', width=100)
self.list_ctrl.InsertColumn(2, 'Status', width=100)
vbox.Add(self.list_ctrl, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
panel.SetSizer(vbox)
self.list_ctrl.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
self.Center()
busy = wx.BusyInfo("Please wait, scanning...")
wx.Yield()
results = crc32.scan(filepath)
for result in results:
index = self.list_ctrl.InsertItem(self.list_ctrl.GetItemCount(), result['file'])
self.list_ctrl.SetItem(index, column=1, label=result['sum'])
self.list_ctrl.SetItem(index, column=2, label="Good" if result['good'] else "Bad")
busy = None
self.Show(True)
def OnDoubleClick(self, event):
pos = event.GetPosition()
index, _ = self.list_ctrl.HitTest(pos)
column = self.GetColumnFromPosition(pos)
if index != wx.NOT_FOUND and column != wx.NOT_FOUND:
value = self.list_ctrl.GetItem(index, column).GetText()
clipboard = wx.Clipboard.Get()
clipboard.Open()
clipboard.SetData(wx.TextDataObject(value))
clipboard.Close()
wx.MessageBox(f"Value copied: {value}", "Copied", wx.OK | wx.ICON_INFORMATION)
def GetColumnFromPosition(self, pos):
x, _ = pos
width_accumulator = 0
for col in range(self.list_ctrl.GetColumnCount()):
width = self.list_ctrl.GetColumnWidth(col)
if x >= width_accumulator and x < width_accumulator + width:
return col
width_accumulator += width
return None
def show_crc32_scan_window(filepath):
app = wx.App()
frame = CRC32ScanWindow(None, 'CRC32 Scan', filepath)
app.MainLoop()
if __name__ == "__main__":
if len(sys.argv)-1 == 1:
show_crc32_scan_window(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment