# ===========================================================================
# Copyright 2017 Autodesk, Inc. All rights reserved.
# 
# Use of this software is subject to the terms of the Autodesk license
# agreement provided at the time of installation or download, or which
# otherwise accompanies this software in either electronic or hard copy form.
# ===========================================================================

# This script is used to removed files that are unlicensed to be used in a cloud.
# It must be called from the Maya directory you wish to remove the files
# ex: C:\Program Files\Autodesk\Maya2018
# To use simply `bin\mayapy.exe cleanMayaForCloud.py`
# You may need administrator privileges



import os
import shutil
import subprocess

def removeFile(func, file, excinfo):
    # Remove forcefully
    subprocess.check_call(['attrib', '-R', file])
    os.unlink(file)
    
def removeDir(directory):
    if os.path.isdir(directory) and os.path.exists(directory):
        shutil.rmtree(directory, onerror=removeFile)
    

def removeATF():
    toRemove = [
        'plug-ins\\ATF\\',
        'lib\\ATF\\',
        'modules\\ATF.mod',   
        ]
    for thing in toRemove:
        thing = os.path.abspath(thing)
        
        if not os.path.exists(thing):
            print('"%s" not found.. Already removed?' % thing)
            continue
        
        if os.path.isfile(thing):
        # Simply remove a lone file
            print('-removing file %s' % thing)
            removeFile(None, thing, None)
            
        else:
            # Print recursively the files and directories that will be removed
            # do not remove them yet
            for dirpath, dirs, files in os.walk(thing):
                print('-in directory: "%s"' %  dirpath)
                for file in files:
                    print('--attempting to remove file: "%s"' % file)
            #remove top level directory, everything under will be gone
            removeDir(thing)
            print('files in %s removed' % os.path.abspath(thing))
                
        
if __name__ == '__main__':
    os.getcwd().split('\\')[-1]
    
    
    mayaExePath = os.getcwd() + '\\bin\\maya.exe'
    if not os.path.isfile(mayaExePath) :
        print('You need to run this script in the Maya directory.')
        raise SystemExit
    
    print('Found a Maya installation in "%s". ' % os.getcwd())
    print('Removing components that are not licensed to run in the cloud')
    
    removeATF()
    
    print('Done!')
    exit(0)