#!BPY
# -*- coding: iso8859-1 -*- 
"""
Name: 'Nettoyer les chemins python utilisateur'
Blender: 240
Group: 'Help'
Tooltip: "Enlève tous les chemins de l'utilisateur de sys.path"
"""

__author__ = "VVPix"
__url__ = ("www.vvpix.com/bld_Index.htm")
__version__ = "1.00"

__bpydoc__ = """\
Ce script permet de visualiser les chemins Python.
"""

# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****

# V 1.00 : première version

import Blender
from Blender import Draw
import sys, os


def nettoyer():
	print "----------------------------------------"
	# Affichage de l'état actuel
	print "Chemin Python pour Blender avant traitement :"
	sys.path.sort()
	for strChemin in sys.path:
		print strChemin
	print "Nb chemins : %d\n"%( len(sys.path) )
	
	# Init
	nNbSuppressions = 0
	nNbDoublons = 0
	
	# Récupération du chemin des scripts utilisateurs
	strCheminUser = Blender.Get('uscriptsdir')
	# Suppression du dernier séparateur de fichiers
	if strCheminUser[-1:] == os.sep:
		strCheminUser = strCheminUser[0:-1]
	
	# Suppression de tous les chemins utilisateur
	bSupprimer = True
	while bSupprimer:
		bSupprimer = False
		for strChemin in sys.path:
			if strChemin.find( strCheminUser ) == 0 and abs( len(strChemin) - len( strCheminUser) ) > 2:
				print "Suppression de ", strChemin
				sys.path.remove( strChemin )
				nNbSuppressions += 1
				bSupprimer = True
	
	# Suppression des doublons
	bSupprimer = True
	while bSupprimer:
		bSupprimer = False
		strCheminPrec = ""
		for strChemin in sys.path:
			if strCheminPrec == strChemin:
				sys.path.remove( strChemin )
				nNbDoublons += 1
				bSupprimer = True
			else:
				strCheminPrec = strChemin
	
	# Fin
	print "Chemin Python pour Blender apres traitement :"
	for strChemin in sys.path:
		print strChemin
	print "Nb chemins : %d\n"%( len(sys.path) )
	
	print "----------------------------------------"
	strM = "Information%t|"
	strM += ("%d doublons supprimés, %d chemin(s) supprimés (voir le détail dans la console)"%( nNbDoublons, nNbSuppressions ))
	Draw.PupMenu( strM )


if __name__ == "__main__":
	nettoyer()


