Last active
December 14, 2015 22:39
-
-
Save dgrant/5159810 to your computer and use it in GitHub Desktop.
For debian systems, this finds all files that do not belong to a package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Search for orphan files (files that do not belong to any package) | |
""" | |
import fnmatch | |
import os | |
import subprocess | |
import itertools | |
INCLUDES = ('/usr', '/etc', '/opt', '/lib32', '/lib64', '/sbin') | |
EXCLUDES = ('/var/www', '/var/tmp', '/var/log', '*.pyc') | |
def get_files(root): | |
""" | |
Generate all files under the given root | |
""" | |
root = os.path.abspath(root) | |
for root, _, files in os.walk(root): | |
for afile in files: | |
full_path = os.path.join(root, afile) | |
for exclude in EXCLUDES: | |
if exclude[0] == '/': | |
if full_path.lower().startswith(exclude.lower()): | |
break | |
elif fnmatch.fnmatch(full_path.lower(), exclude): | |
break | |
else: | |
yield full_path | |
def get_files_in_packages(): | |
""" | |
Generate all files in all .deb packages | |
""" | |
output = subprocess.check_output( | |
'dpkg --get-selections', shell=True, universal_newlines=True) | |
lines = output.split('\n') | |
packages = [line.split()[0] for line in lines if line != ''] | |
for package in packages: | |
lines = subprocess.check_output( | |
'dpkg -L {0}'.format(package), | |
shell=True, | |
universal_newlines=True).split('\n') | |
for line in lines: | |
yield line | |
def main(): | |
""" Main script """ | |
os_files = list( | |
itertools.chain.from_iterable( | |
(get_files(x) for x in(INCLUDES)))) | |
package_files = list(get_files_in_packages()) | |
orphans = list(set(os_files) - set(package_files)) | |
orphans.sort() | |
for orphan in orphans: | |
print(orphan) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment