
'''
Script:  First Script-Attempt 3
Author:  Steve Dubina
Date:    October 31 2021- modified November 6 2021
Version: 1.e
Purpose: script Description
'''

notready = "WK-2 Attempt: Steve K Dubina - Version 1.e"
print(notready)

''' IMPORT STANDARD LIBRARIES '''
import os       # File System Methods
import sys      # System Methods
import time     # Time Conversion Methods
                       


''' IMPORT 3RD PARTY LIBRARIES '''
# NONE

''' DEFINE PSEUDO CONSTANTS '''
# NONE

''' LOCAL FUNCTIONS '''

def GetFileMetaData(fileName):
    ''' 
        obtain filesystem metadata
        from the specified file
        specifically, fileSize and MAC Times
        
        return True, None, fileSize and MacTimeList
    '''
    try:
        
        metaData         = os.stat(fileName)        # Use the stat method to obtain meta data
        fileSize         = metaData.st_size         # Extract fileSize and MAC Times
        timeLastAccess   = metaData.st_atime
        timeLastModified = metaData.st_mtime
        timeCreated      = metaData.st_ctime
        
        macTimeList = [timeLastModified, timeLastAcess, timeCreated] # Output MAC Times in a List
        return True, None, fileSize, macTimeList
    
    except Exception as err:
        return False, str(err), None, None
              
        
''' LOCAL CLASSES '''
# NONE

''' MAIN ENTRY POINT '''

if __name__ == '__main__':
    
    print("\nWeek 2 Assignment: Steve Dubina Version 1.e\n")
            
    targetDIR = input('Enter a Directory Path i.e. c:/ >>> ')
    print()
    
    try:
        fileList = os.listdir(targetDIR)
        for eachEntry in fileList:
            print("="*40)
            print(eachEntry)
            fullPath = os.path.join(targetDIR, eachEntry)
            
            success, errInfo, fileSize, macList = GetFileMetaData(fullPath)        
                        
            if success:
                print("Success:   ", fullPath)
                print("File Size: ", "{:,}".format(fileSize))            
                
                modEPOCH = macList[0]
                accEPOCH = macList[1]
                creEPOCH = macList[2]
                
                modTime = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(modEPOCH))
                accTime = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(accEPOCH))                
                creTime = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(creEPOCH))
                
                print("Modified-Time: ", modTime)
                print("Access-Time:   ", accTime)
                print("Created-Time:  ", creTime)
                
            else:
                print("Fail:       ", path, "Exception =    ", errInfo)
                
    except Exception as err:
         print("\n\nScript Aborted     ", "Exception =     ", err)    
            
                
        
                                        
             


