#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 humanbytes(B):
    B = float(B)
    KB = float(1024)
    MB = float(KB ** 2) # 1,048,576

    if MB <= B :
        return '{0:.2f}'.format(B/MB)
    else:
        return 0
        
    
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):
            size = 0
            for path, dirs, files in os.walk(i):
                for f in files:
                    fp = os.path.join(path, f)
                    size += os.path.getsize(fp)
            si=float(humanbytes(size))
            if si>200:    
                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()
            else:
                print("Size is less than 200 MB for %s"%i)
except Exception as E:
    print E
finally:
    print "Code Executed"
    print("--- %s seconds ---" % (time.time() - start_time))