Skip to content

Instantly share code, notes, and snippets.

@Tantalus13A98B5F
Tantalus13A98B5F / backup.py
Last active January 8, 2020 08:37
A simple backup script for Windows, based on `backup.json`, generating a tarball file. Useful if you want to pack up your `AppData\Roaming` files.
#!/usr/bin/env python3
from contextlib import contextmanager
from fnmatch import fnmatch
from glob import glob
import subprocess as subp
import datetime as dt
import logging
import tarfile
import json
import sys
@Tantalus13A98B5F
Tantalus13A98B5F / ntfs_mounter.py
Created January 6, 2020 04:15
An NTFS mounter for macOS, switching disks between the internal `mount_ntfs` and the additional `ntfs-3g`.
#!/usr/bin/env python3
import subprocess
import plistlib
import os
def execute_and_grab_output(args):
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
return proc.stdout.read()
@Tantalus13A98B5F
Tantalus13A98B5F / rsync.py
Last active January 8, 2020 07:59
My `rsync` wrapper, filtering output and filling exclude list for NTFS drives.
#!/usr/bin/env python3
import subprocess as subp
import sys
import os
exclude_list = [
'System Volume Information',
'$RECYCLE.BIN',
]
@Tantalus13A98B5F
Tantalus13A98B5F / start-ssh-agent.sh
Created January 6, 2020 04:21
My Git Bash agent starting script. Setting system variables so useful to work with graphical git tools.
#!/bin/bash
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
if [ "$agent_run_state" = 2 ]; then
rm -rf /tmp/ssh-*
eval $(ssh-agent -s)
winpty setx SSH_AUTH_SOCK $SSH_AUTH_SOCK
winpty setx SSH_AGENT_PID $SSH_AGENT_PID
fi
@Tantalus13A98B5F
Tantalus13A98B5F / office2pdf.py
Created January 6, 2020 04:23
A Python script to convert MSOffice files to PDF. Requires `pywin32` and MSOffice installation.
import win32com.client
msoTrue, msoFalse = -1, 0
def word2pdf(src, dst):
wdExportFormatPDF = 17
app = win32com.client.DispatchEx('Word.Application')
doc = app.Documents.Open(src)
@Tantalus13A98B5F
Tantalus13A98B5F / parsepdf.py
Last active January 13, 2020 10:08
Simple PDF parser
'''My Simple PDF Parser
Can inspect the size of a PDF file.
'''
from contextlib import suppress, redirect_stdout
from collections import deque, OrderedDict
from weakref import ref
import re
class PDFRef:
@Tantalus13A98B5F
Tantalus13A98B5F / memview.py
Created January 6, 2020 04:33
A simple script to visualize the memory usage on Windows by accumulation.
import subprocess as subp
import pandas as pd
from matplotlib import pyplot as plt
p = subp.Popen(['tasklist', '/fo', 'csv'], stdout=subp.PIPE)
df = pd.read_csv(p.stdout, encoding='gbk')
df['mem'] = df['内存使用 '].transform(lambda x: int(x[:-2].replace(',', '')))
df['proc'] = df['映像名称']
df = df.groupby('proc')['mem'] \
.agg(['sum', 'count']) \
@Tantalus13A98B5F
Tantalus13A98B5F / get_folder_size.py
Last active August 14, 2020 06:14
A simple GUI tool to inspect disk usage.
from tkinter.filedialog import askdirectory
from tkinter import *
from tkinter.ttk import *
import subprocess
import weakref
import sys
import os
def human_size(cntbytes):
@Tantalus13A98B5F
Tantalus13A98B5F / fzf-pipe
Created April 15, 2020 16:19
fzf-pipe: `fzf` wrapper for Git Bash
#!/usr/bin/env bash
TEMPDIR=$(mktemp -d)
cat >$TEMPDIR/input.txt
FZFOPTS="$(echo "$FZF_DEFAULT_OPTS" | sed 's/--height\s*\S*\s*//')"
for arg; do
FZFARGS="$FZFARGS \"$arg\""
done
cat >$TEMPDIR/script.sh <<EOF
@Tantalus13A98B5F
Tantalus13A98B5F / logmata.py
Last active March 3, 2021 10:44
A utility for parsing structured logs (NDPDA)
#!/usr/bin/env python3
'''Parse logs in structures with the help of a pushdown automata.
Grammar for the sample log file:
>This line is used for exact match.
$This line is used for regexp match.
This line is ignored.
The following lines marked a repeated region.
!repeat