#Packages
import os
import subprocess
import shutil
import time
import re
#----------------------------------------------------------------------------------------------------
start_time = time.time()
#----------------------------------------------------------------------------------------------------
#Functions
#Force Close Chrome
print os.popen("taskkill /f /im chrome.exe 2> nul").read()
def remove(path):
    #Remove the file or directory
    if os.path.isdir(path):
        try:
            shutil.rmtree(path)
        except OSError:
            print "Unable to remove folder: %s" % path
    else:
        try:
            if os.path.exists(path):
                os.remove(path)
        except OSError:
            print "Unable to remove file: %s" % path


def chrome_cleanup(dirpath,folder_to_exclude):
    for root, dirs, files in os.walk(dirpath, topdown=True):
        for file_ in files:
            full_path = os.path.join(root, file_)
            if folder_to_exclude[0] not in full_path and folder_to_exclude[1] not in full_path and folder_to_exclude[2] not in full_path:
                print 'removing -> ' + full_path
                remove(full_path)
        for folder in dirs:
            full_path = os.path.join(root, folder)
            if folder_to_exclude[0] not in full_path and folder_to_exclude[1] not in full_path and folder_to_exclude[2] not in full_path:
                remove(full_path)

                
def permissions(dirpath):
    mode=0o777
    if os.path.isdir(dirpath):
        try:
            for root,dirs,files in os.walk(dirpath,topdown=False):
                for dircs in [os.path.join(root,d) for d in dirs]:
                    os.chmod(dircs,mode)
                for s_file in [os.path.join(root,f) for f in files]:
                    os.chmod(s_file,mode)
        except Exception as E:
            print E

#path_creation
def path_creation(fil_users):
    chr_ls=[]
    l=[]
    #chrome
    for i in fil_users:
        chr_path='C:\Users\%s\AppData\Local\Google\Chrome\User Data\Default'%i
        if os.path.exists(chr_path):
            chr_ls.append(chr_path)
        else:
            pass
    return [l,chr_ls]
#-----------------------------------------------------------------------------------------------------
#codes
users=os.popen("net user").read().split()[5:-5]
filtered_users=[i for i in users if i!='Guest']
#print filtered_users

paths=path_creation(filtered_users)
#print paths


chrome_paths=[]
for i in paths[1]:
    chrome_paths.append([i+"\Bookmarks",i+"\Bookmarks.bak",i+"\Login Data"])
tot_chrome=zip(paths[1],chrome_paths)
#print tot_chrome

try:
    #chrome
    for i,j in tot_chrome:
        if os.path.exists(i):
            print("Operation Executing on Google Chrome...\n")
            print("Chrome Permission fixing...")
            permissions(i)
            print("Cleaning Up Chrome....\n")
            chrome_cleanup(i,j)
            print("Chrome Completed!!!\n")
            print os.popen("start chrome").read()
except Exception as E:
    print E
finally:
    print "Code Executed"
    print("--- %s seconds ---" % (time.time() - start_time))