Skip to content

Instantly share code, notes, and snippets.

@gmr
Last active December 19, 2015 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmr/6031454 to your computer and use it in GitHub Desktop.
Save gmr/6031454 to your computer and use it in GitHub Desktop.
This script removes erroneous entries in the newrelic_plugin_agent installation manifest file that if unsanitized, will unintentionally remove everything under / (and /var).
import os
from os import path
import site
# Check to see if the previous version was installed and clean up
# installed-files.txt
prune = ['var/', 'var/run/', 'var/log/']
package_directories = site.PREFIXES
if site.USER_SITE:
package_directories.append(site.USER_SITE)
try:
import newrelic_plugin_agent
except ImportError:
newrelic_plugin_agent = None
if newrelic_plugin_agent:
package_directories.append(path.abspath(path.dirname(newrelic_plugin_agent.__file__) + '/..'))
for package_dir in package_directories:
print 'Checking %s for newrelic_plugin_agent installation manifest' % package_dir
fixed = False
for dir_path, dir_names, file_names in os.walk(package_dir):
for dir_name in dir_names:
if dir_name[:21] == 'newrelic_plugin_agent' and \
dir_name[-8:] == 'egg-info':
filename = '%s/%s/installed-files.txt' % (package_dir, dir_name)
with open(filename, 'r') as handle:
output = []
for line in handle:
safe = True
for dir_path in prune:
if line[-(len(dir_path) + 1):].strip() == dir_path:
safe = False
fixed = True
break
if safe:
output.append(line.strip())
if fixed:
with open(filename, 'w') as handle:
handle.write('\n'.join(output))
break
break
if fixed:
print 'Fixed a serious uninstallation problem in previous version'
else:
print 'Did not find the installed-files.txt manifest uninstallation issue'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment