import os
import datetime
import sys
import stat

crawl_start_dir = "/homes/"
crawl_check_dir = "public_html"
record_file = "result"
curr_time = datetime.date.today()
if len(sys.argv) > 1:
  time_period = sys.argv[1]
else:
  time_period = 365		# in days
old_files = {}

def verify_age (file, time_period):
  no_file_slices = len(file.split("/"))
  if file.split("/")[no_file_slices-1].startswith("."):
    return None
  if os.path.islink(file):
    return None
  if not (os.stat(file).st_mode & stat.S_IROTH):
    return None
  edit_time = os.path.getmtime(file)
  edit_time = datetime.date.fromtimestamp(edit_time)
  time_diff = (curr_time - edit_time).days
  if time_diff > time_period:
    return file
  return None 

def record_results (filename, results):
  wfile = open(filename, 'w')
  for login in results:
    if results[login]:
      wfile.write("*" * 40)
      wfile.write("\nUser %s old files:\n" % (login))
      for file in results[login]:
        wfile.write("\t%s\n" % (file))
  wfile.write("*" * 40)
  for login in results:
    wfile.write("\nUser %s checked with %s old files identified." % (login, len(results[login])))
  wfile.close();

for (path, name, files) in os.walk(crawl_start_dir):
  if crawl_check_dir in name:
    login = path.split("/")[2]
    old_files[login] = set()
    for (in_path, in_name, in_files) in os.walk(os.path.join(path, crawl_check_dir)):
      for in_file in in_files:
        filechk = verify_age(os.path.join(in_path, in_file), time_period)
        if filechk:
          old_files[login].add(filechk)
record_results(record_file, old_files)

#for (path, name, files) in os.walk(crawl_start_dir):
#  if files:
#    for file in files:
#      old_files.union(verify_age(os.path.join(path, file), time_period))

#print old_files
  
      
