#!/usr/bin/env python 
################################################################################### 
# Display dir/file properties
################################################################################### 
import sys
import cgi
import os.path
from datetime import datetime

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

elements = cgi.FieldStorage()               #obtain the http parameters

path = ''
if len(sys.argv) > 1:                       #if arg is entered
    path = sys.argv[1]                      #file or directory name

#======================================================================================
# listProperties: list all properties for a file or directory
#======================================================================================
def listProp(path):
    if path=='': path = __file__        	#if no file, use current file 

    print()

    print("File/Directory Properties -<br>")
    print(path + "<br>")
    print("<br>")

    print("abspath  \t", os.path.abspath(path) )              #full/absolute path and file name
    print("basename \t", os.path.basename(path))              #file name only
    print("dirname  \t", os.path.dirname(path) )              #relative directory name
    print("exists?  \t", os.path.exists(path)  )              #does the file exist?
    print("size     \t", os.path.getsize(path), "Bytes")      #size in bytes

    accessed = os.path.getatime(path)                                     #access time - num seconds since EPOCH
    accDate  = datetime.fromtimestamp(accessed)                           #create date obj from seconds
    print("last accessed \t", accessed, 'seconds, which is', accDate.strftime('%c'))

    modified = os.path.getmtime(path)                                     #modification time - num seconds since EPOCH
    modDate  = datetime.fromtimestamp(modified)                           #create date obj from seconds
    print("last modified \t", modified, 'seconds, which is', modDate.strftime('%c'))

    print("is absolute? \t",           os.path.isabs(path) )   #is it absolute path?
    print("is it a file? \t",          os.path.isfile(path))   #is it a file?
    print("is it a directory? \t",     os.path.isdir(path) )   #is iot a directory?                        
    print("is it a symbolic link? \t", os.path.islink(path))   #is it a symbolic link?
    print("realpath (if link) \t",     os.path.realpath(path)) #return the actual path (for symbolic link)
    print("split path/filename \t",    os.path.split(path) )   #split the path and filename

    print("access previliges \t", \
          "  Read=" , os.access(path,os.R_OK), \
          "  Write=", os.access(path,os.W_OK), \
          "  Exec=" , os.access(path,os.X_OK) )

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

listProp(path)                               #call he above function
print()



#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================