Skip to content

Instantly share code, notes, and snippets.

@bryanheinz
Created May 12, 2020 00:14
Show Gist options
  • Save bryanheinz/ef2aa4b6baade474654a0bf4adfa6136 to your computer and use it in GitHub Desktop.
Save bryanheinz/ef2aa4b6baade474654a0bf4adfa6136 to your computer and use it in GitHub Desktop.
patches Munki's makecatalogs
#!/usr/bin/python3
# this script patches Munki's makecatalogslib.py file to skip over any files or
# folders containing @eaDir. if you're hosting your munki repo on a Synology,
# then you've likely seen the ton of noisy error output makecatalogs generates
# due to these @eaDir files. this patch skips those files thus silencing those errors.
import os
import shutil
import getpass
def alert_and_exit():
print("Couldn't find icon_ref and pkginfo_ref.")
print("Please review the following link and code:")
print("https://github.com/munki/munki/pull/944/commits/c9095fbc6550dd55d950d2df076cdf7120b9a1d1")
exit()
index = 0
icon_ref = None
pkginfo_ref = None
mc_path = '/usr/local/munki/munkilib/admin/makecatalogslib.py'
icon_ref_patch = """ # Skipping Synology index files
if '@eaDir' in icon_ref:
continue
"""
pkginfo_ref_patch = """ # Skipping Synology index files
if '@eaDir' in pkginfo_ref:
continue
"""
if not os.path.exists(mc_path):
print(f"Munki's {mc_path} file not found, exiting...")
exit(0)
if getpass.getuser() != 'root':
print("Please run as root, exiting...")
exit(0)
# create temp backup file
shutil.copy2(mc_path, '/private/tmp/makecatalogslib.py')
# read the original file
with open(mc_path, 'r') as file:
mc = file.readlines()
# loop through, find the icon_ref index
for line in mc:
if 'for icon_ref in icon_list' in line:
icon_ref = index
if '@eaDir' in line:
print("is makecatalogslib.py already patched?")
print(f"please review {mc_path}")
print(f"line {index} for the eadir patch. Exiting...")
exit()
index += 1
# validate icon_ref exists
if not icon_ref:
alert_and_exit()
# validate the next line is the expected output
if mc[icon_ref+1] != ' if output_fn:\n':
print("icon_ref")
alert_and_exit()
# patch
patch_index = icon_ref + 1
mc.insert(patch_index, icon_ref_patch)
# recheck index for the pkginfo_ref patch
index = 0
for line in mc:
if 'for pkginfo_ref in pkgsinfo_list' in line:
pkginfo_ref = index
break
index += 1
# validate pkginfo_ref exists
if not pkginfo_ref:
alert_and_exit()
# validate the next line is the expected output
if mc[pkginfo_ref+1] != ' # Try to read the pkginfo file\n':
print('pkginfo_ref')
alert_and_exit()
# patch
patch_index = pkginfo_ref + 1
mc.insert(patch_index, pkginfo_ref_patch)
# join the patched files
mc_patched = ''.join(mc)
# write the newly patched file
with open(mc_path, 'w') as file:
file.write(mc_patched)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment