#!/usr/bin/env python
#=====================================================================================
# Display list of files (files only) in a directory (and sub-directory) recursively
# Directory name comes from input argument  
# Sort files by modification date
# Output list of files
#=====================================================================================
import sys                                  #for command line arguments
import os
import cgi                                  #cgi 
import cgitb                                #cgi with traceback error handling
import time                                 #to convert modification date/time
import pwd                                  #to get the user/owner of the file                                  
import grp                                  #to get the user/owner group name                                  
 
cgitb.enable()
 
filesList = []                              #array that will contain list of all the files

print("Content-Type: text/html \n")         #required http response header (w/ extra line)

if len(sys.argv) <= 1:                      #name of this script is always provided as argv[0]
    dir = '.'                               #use current directory
else:
    dir = sys.argv[1]                       #use directory provided
    
dir = os.path.abspath(dir)

print("Recursive Listing of all files for " + dir + "\n\n") 


#======================================================================================
# listDirRecursive: recursively list all files of a directory
#======================================================================================
def listDirRecursive(dir):

    global filesList

    files = os.listdir(dir)                         #get list of files in directory (.. not included)

    for file in files:
        fileInfo = []                               #array that will contain a single file info

        fullpath = dir +"/"+ file                                     #create a full path
                      
        if (os.path.isfile(fullpath)):                                #regular file
            fileInfo.append(fullpath)                                 #add the name
                                
            filesize = os.path.getsize(fullpath)                      #get file size         
            fileInfo.append(filesize)
        
            lastModified  = os.path.getmtime(fullpath)                #number of millisec since 1970  
            fileInfo.append(lastModified)
            lastModified2 = time.ctime(lastModified)                  #readable date/time
            fileInfo.append(lastModified2)
        
            lastAccessed  = os.path.getatime(fullpath)                #number of millisec since 1970  
            fileInfo.append(lastAccessed)
            lastAccessed2 = time.ctime(lastAccessed)                  #readable date/time
            fileInfo.append(lastAccessed2)

            stats = os.stat(fullpath)                                 #an object of file properties
            owner_id     = stats.st_uid                               #get the uid property (user id)
            owner_struct = pwd.getpwuid(owner_id)                     #get info struct for that user id 
            owner_name   = owner_struct.pw_name                       #get the pw_name property (user name)
            fileInfo.append(owner_name)

            group_id     = stats.st_gid                               #get the gid property (group id)
            group_struct = grp.getgrgid(group_id)                     #get info struct for that group id
            group_name   = group_struct.gr_name                       #get the gr_name property (group name)
            fileInfo.append(group_name)
        
            permissions = stats.st_mode                               #get the st_mode property 
            permissions = oct(permissions)                            #convert to octal
            permissions = str(permissions)[-3:]                       #take last 3 digits  
            fileInfo.append(permissions)
  
            filesList.append(fileInfo)

        elif (os.path.isdir(fullpath)):                               #regular directory
            listDirRecursive(fullpath)                                #recursive call

   
#======================================================================================

listDirRecursive(dir)                         #call the above function

filesList.sort(key=lambda elem : elem[4])     #by: 0-name, 1-size, 2-modDate, 4-accessDate, 6-owner, 7-group, 8-permissions  

for file in filesList:
    print(str(file) + '<br>')