-
Notifications
You must be signed in to change notification settings - Fork 22
/
PatchMagicWin.py
30 lines (23 loc) · 1.05 KB
/
PatchMagicWin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# This file patches the magic library on Windows so that it works properly after being compiled.
import re
from pathlib import Path
magicLibPath = Path.cwd() / "buildEnv" / "Lib" / "site-packages" / "magic" / "loader.py"
with open(magicLibPath, 'r') as magicLibFile:
magicLines = magicLibFile.readlines()
# Find out what level of indentation is used, in case it changes in the future.
lineIndent = next(
(
len(re.split(r'\S', line)[0])
for line in magicLines
if line.startswith(' ')
),
None,
)
magicLines.insert(0, 'from pathlib import Path\n')
magicLines.insert(0, 'import os\n')
funcIndex = magicLines.index(" elif sys.platform in ('win32', 'cygwin'):\n")
# This patch is only done on Windows, so we can safely 'hardcode' the path to the dll, since we're the ones
# providing it.
magicLines.insert(funcIndex + 1, ' ' * lineIndent * 2 + "yield find_library(str(Path(os.environ['ProgramFiles']) / 'LinkScope' / 'magic' / 'libmagic-1.dll'))\n")
with open(magicLibPath, 'w') as magicLibFile:
magicLibFile.writelines(magicLines)