Skip to content

Instantly share code, notes, and snippets.

@fiechr
Created November 20, 2017 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiechr/8218ae9c9f6abfedbfc283ac3b35a3d8 to your computer and use it in GitHub Desktop.
Save fiechr/8218ae9c9f6abfedbfc283ac3b35a3d8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import hashlib
import os
import re
import shutil
import sys
from os.path import abspath, basename, exists, isdir, isfile, join
def sha256sum(filepath):
sha256 = hashlib.sha256()
with open(filepath, 'rb') as f:
for chunk in iter(lambda: f.read(128 * sha256.block_size), b''):
sha256.update(chunk)
return sha256.hexdigest()
def main():
checkonly = False
filescopied = 0
fontstotal = 0
fontdir = "C:\\Windows\\Fonts" # Default, if everything else fails.
if sys.platform.startswith("win32"):
fontdir = join(os.environ['WINDIR'], "Fonts") # Get default font dir path (usually C:\Windows\Fonts)
else:
print("\nWarning: This script is supposed to be used on Windows. Otherwise you need to provide the font dir path manually (i.e. /mnt/mywindisk/Windows/Fonts).\n")
targetdir = ""
pkgbuild_path = join(os.getcwd(), "PKGBUILD") # If no other path given, assume PKGBUILD in current directory
pkgbuild_content = ""
parser = argparse.ArgumentParser(description="Gets font filenames from a PKGBUILD, searches for them in the Windows font directory and tests them using the existing filehashes in the PKGBUILD. If successful, the file will be copied to the provided TARGETDIR.")
parser.add_argument("targetdir", metavar='TARGETDIR', help="Target directory to copy all listed and found font files into.")
parser.add_argument("-p", "--pbfile", metavar="PKGBUILD", help="Path to the PKGBUILD file to use (if not in the current directory).")
parser.add_argument("-f", "--fontdir", metavar='FONTDIR', help="Provide a different Windows font directory (default is to use $WINDIR/Fonts).")
parser.add_argument("-c", "--checkonly", help="Search for and check the fonts, but don't copy them.", action='store_true')
args = parser.parse_args()
if args.targetdir:
if exists(args.targetdir):
targetdir = args.targetdir
else:
print("Target directory '{}' not found!".format(args.targetdir))
sys.exit(1)
if args.pbfile:
if exists(args.pbfile):
pkgbuild_path = args.pbfile
else:
print("PKGBUILD file '{}' not found!".format(args.pbfile))
sys.exit(1)
elif not exists(pkgbuild_path): # If there is no PKGBUILD in the current directory and none was given.
print("No PKGBUILD file found! Can't proceed without.")
sys.exit(1)
if args.fontdir:
if exists(args.fontdir):
fontdir = args.fontdir
else:
print("Windows font directory '{}' not found!".format(args.fontdir))
sys.exit(1)
elif not exists(fontdir): # If default font dir not found an none was given, complain.
print("Please provide a path to the Windows font directory (i.e. D:\\Win10\\Fonts).")
sys.exit(1)
if args.checkonly:
checkonly = True
with open(pkgbuild_path, 'r') as f:
pkgbuild_content = f.read()
# Not very robust, but it works...
font_filenames = re.findall(r"\w+\.tt[cf]", pkgbuild_content, re.IGNORECASE | re.MULTILINE)
font_filehashes = re.findall(r"[0-9a-f]{64}", pkgbuild_content, re.IGNORECASE | re.MULTILINE)
fontstotal = len(font_filenames) # How many font files are there in total?
# Since files and hashes are in the same order, simply put them together.
fonts = dict(zip(font_filenames, font_filehashes))
for filename, filehash in fonts.items():
filepath = join(fontdir, filename)
if exists(filepath) and isfile(filepath):
if sha256sum(filepath) == filehash:
if checkonly:
print("Found '{}', checksum OK.".format(filename))
else:
shutil.copy2(filepath, targetdir)
filescopied += 1
print("Copied '{}'.".format(filename, targetdir))
else:
print("Found '{}', but checksum does not match!".format(filename))
else:
print("'{}' not found in '{}'!".format(filename, fontdir))
print("Copied {} of {} files to '{}'.".format(filescopied, fontstotal, targetdir))
if __name__ == "__main__":
main()
@cswl
Copy link

cswl commented Nov 21, 2017

Somehow my windows install fonts are encrypted..?

'arial.ttf' not found in '/run/media/cswl/windrv/Windows/Fonts'!

ls -l /run/media/cswl/windrv/Windows/Fonts
ls: cannot access '/run/media/cswl/windrv/Windows/Fonts/arial.ttf': Input/output error

I will try it from windows I guess.

@cswl
Copy link

cswl commented Nov 21, 2017

Okay.. it's not encrypted but it has hardlinks to Windows.old directory..
Which I cant delete.. I've Reset this PC quite a lot

fsutil hardlink list C:\Windows\Fonts\arial.ttf

\Windows\WinSxS\amd64_microsoft-windows-font-truetype-arial_31bf3856ad364e35_10.0.15063.0_none_83974968e629cd54\arial.ttf
\Windows.old\Fonts\arial.ttf
\Windows.old\WinSxS\amd64_microsoft-windows-font-truetype-arial_31bf3856ad364e35_10.0.15063.0_none_83974968e629cd54\arial.ttf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment