#!/usr/bin/env python
########################################################################################
# Utility to clean up a Free Electrons training lab directory
#
# Just run it in the main labs directory:
# ./cleanlabs
#
# Copyright 2008, Free Electrons http://free-electrons.com
# License: Public Domain
########################################################################################

import glob, os, os.path, shutil, sys, fnmatch, re

########################################################################################
# Remove all the files in a list of directories, except the specified ones
# Don't complain if the specified files no longer exist (manual cleanup by the user)
########################################################################################

def find_file_pattern (top, filepat):
    for path, dirlist, filelist in os.walk(top):
        for name in fnmatch.filter(filelist,filepat):
            yield os.path.join(path,name)

def find_dir_pattern (top, dirpat):
    for path, dirlist, filelist in os.walk(top):
        for name in fnmatch.filter(dirlist,dirpat):
            yield os.path.join(path,name)

def clean_dirs(remove_patterns, keep_patterns):

	for pattern in remove_patterns:
		for dir in find_dir_pattern('.', pattern):
			clean_dir(dir, keep_patterns)

def clean_files(remove_patterns):
	for pattern in remove_patterns:
		for file in find_file_pattern('.', pattern):
			if os.path.exists(file) and not(os.path.islink(file)):
				# We ignore links... this way the user
				# sees that he used a link
				os.remove(file)

def clean_dir(dir, keep_patterns):

        curdir = os.getcwd()
	os.chdir(dir)
	kept = []
	
	for pattern in keep_patterns:
		for match in glob.glob(pattern):
			kept.append(match)
	
	newdir = os.path.join(curdir, 'cleanuplab.tmp-' + os.path.basename(dir))
	os.makedirs(newdir)
		
	for file in kept:
		filedir = os.path.dirname(file)

		newsubdir = os.path.join(newdir, filedir)

		if filedir != '' and not(os.path.exists(newsubdir)):
			os.makedirs(newsubdir)

		shutil.move(file, os.path.join(newdir, file))

	os.chdir(curdir)
	shutil.rmtree(dir, True)
	shutil.move(newdir, dir)

########################################################################################
# Main commands
########################################################################################
		
# Remove Linux source directories
# except the most important user defined or generated files
# Some people have already put their linux sources in the root lab directory,
# or in the data/ subdirectory.

clean_dirs(['linux-2.6*', 'linux-3.*'], ['Makefile', 'arch/*/boot/*zImage', '.config', '*.config'])

# Remove BusyBox source directories
# except the most important user defined or generated files

clean_dirs(['busybox-*'], ['Makefile', '.config', '*.config'])

# Remove source archives and patches
# Not exactly sure where the user left them

clean_files(['*.bz2', '*.gz', '*.zip', '*.lzma', '*.7z', '*.tar', '*.xz'])

# Remove misc files

clean_files(['core', 'cscope.out'])

# Clean misc lab directories

clean_dirs(['crosstool-ng-*/targets'], [])
clean_dirs(['buildroot-*'], ['.config', 'binaries'])
clean_dirs(['u-boot-*'], [])
clean_dirs(['DirectFB-*'], [])
clean_dirs(['freetype-*'], [])
clean_dirs(['jpeg-*'], [])
clean_dirs(['libpng-*'], [])
clean_dirs(['zlib-*'], [])
