Skip to content

Instantly share code, notes, and snippets.

@pudquick
pudquick / timemachine.py
Last active March 9, 2023 20:28
A lot of fun TimeMachine details can be reached with pyobjc on OS X
# Backup destination details are stored in: /Library/Preferences/com.apple.TimeMachine.plist
# 10.11
from Foundation import NSBundle
TMMenu = NSBundle.bundleWithPath_('/System/Library/CoreServices/Menu Extras/TimeMachine.menu')
AppleTMSettings = TMMenu.classNamed_('AppleTMSettings')
settings = AppleTMSettings.sharedSettings()
# @interface AppleTMSettings : NSObject
@pudquick
pudquick / marketing.py
Last active December 5, 2019 22:52
Using PrivateFrameworks to get marketing model information for Apple Macs without hitting their internet API (where possible) via python and pyobjc
# Tested on 10.11
# Note:
# The marketing information embedded in the ServerInformation.framework is not the same as what
# About This Mac displays - there are differences.
#
# For example:
# ServerInformation: 15" MacBook Pro with Retina display (Mid 2015)
# About This Mac: MacBook Pro (Retina, 15-inch, Mid 2015)
#
@pudquick
pudquick / receipts.py
Created August 18, 2016 17:14
Programmatically access package receipt information using the OS X PrivateFramework PackageKit (same one pkgutil uses) with python and pyobjc
import objc
packagekit_bundle = objc.loadBundle('PackageKit', module_globals=globals(), bundle_path='/System/Library/PrivateFrameworks/PackageKit.framework', scan_classes=False)
PKReceipt = objc.lookUpClass('PKReceipt')
receipts = PKReceipt.receiptsOnVolumeAtPath_('/')
first_receipt = receipts[0]
# Things you can look up:
# installPrefixPath
@pudquick
pudquick / macvm_detect.py
Created August 4, 2016 21:44
Programmatically determine if an OS X device is a VM in python by available CPU features
from ctypes import CDLL, c_uint, byref, create_string_buffer
libc = CDLL('/usr/lib/libc.dylib')
def sysctl(name):
size = c_uint(0)
libc.sysctlbyname(name, None, byref(size), None, 0)
buf = create_string_buffer(size.value)
libc.sysctlbyname(name, buf, byref(size), None, 0)
return buf.value
@pudquick
pudquick / archiveorg_mame.py
Last active August 2, 2016 12:41
Download individual MAME ROMs from archive.org MAME reference sets via python over HTTP/S without downloading the entire set
import posixpath, urlparse, urllib2, re, ast, struct, cmd, os, os.path
import xml.etree.ElementTree as ET
def discover_roms(rom_url):
# step 1: based on the URL, download the _files.xml for the details
# I could do this a lot easier, but this is more fun
url_parts = urlparse.urlsplit(rom_url)
base_path, page_path = posixpath.split(url_parts.path)
files_path = posixpath.join(url_parts.path, page_path + '_files.xml')
new_parts = url_parts._replace(path=files_path)
@pudquick
pudquick / diskman.py
Last active December 14, 2022 17:03
Light pyobjc wrapper around the PrivateFramework DiskManagement.framework for direct access to disk devices and information about them
import objc
from Foundation import NSBundle
# Predefine some opaque types
DASessionRef = objc.createOpaquePointerType('DASessionRef', b'^{__DASession=}', None)
DADiskRef = objc.createOpaquePointerType('DADiskRef', b'^{__DADisk=}', None)
# Load DiskManagement framework classes
DiskManagment = objc.loadBundle('DiskManagment', globals(), bundle_path='/System/Library/PrivateFrameworks/DiskManagement.framework')
@pudquick
pudquick / ctypes_printers.py
Created July 7, 2016 00:30
Various PrintCore functions via ctypes on OS X
from ctypes import CDLL, Structure, POINTER, c_void_p, byref
from ctypes.util import find_library
import objc
PrintCore = CDLL('/System/Library/Frameworks/ApplicationServices.framework/Frameworks/PrintCore.framework/PrintCore')
CFoundation = CDLL(find_library('CoreFoundation'))
class OpaqueType(Structure):
pass
@pudquick
pudquick / nibbler.py
Last active February 16, 2023 06:07
nibbler - Use .nib files with python to quickly make UIs
# OFFICIAL REPO: https://github.com/pudquick/nibbler
# (this gist will be preserved for historical purposes)
# demo video here:
# https://www.dropbox.com/s/k0bpekd13muknmz/nibbler%20-%20by%20frogor.mp4?dl=0
# Example script using nibbler:
# from nibbler import *
#
@pudquick
pudquick / yvrbackup.py
Last active July 17, 2016 00:45
Backup a presentation from MacDevOpsYVR 2015
#!/usr/bin/python
import argparse, sys, os.path, subprocess, shlex, re, os
# MacDevOpsYVR 2015 presentation saver
# NOTE:
# The backup edits the main html page to preserve only HTML5 presentation mode (no Silverlight, etc.
# components are downloaded by this backup script).
#
# IMPORTANT!
@pudquick
pudquick / measure.py
Last active February 8, 2023 16:05
Get a sorted descriptive json output of hashes for a directory, suitable for diffing
#!/usr/bin/env python3
import os, os.path, json, hashlib, subprocess, argparse, sys, stat
BUFFER_SIZE = 65536
def checksum_directory(source_path, progress=False):
filesystem_details = dict()
# rough guess of how many files are in a directory
# horrible hack with a little fudge
if not os.path.isdir(source_path):