#!/usr/bin/python

from os import chmod
from sys import argv
from os.path import walk
from os import access
from os import W_OK

# get the target directory from the command line
targetdir = argv[1]

def setperms(arg, dirname, names):
  writable = access(dirname, W_OK)
  if not writable:
    print "Ignoring directory "+dirname+"; not writable."
  else:
    # set the perms of the directory
    chmod(dirname, 0700)
    
    # set the perms of files in the directory
    for item in names:
      # generate the full relative path
      item = dirname+"/"+item
      # can we write the individual file?
      writable = access(item, W_OK)
      if not writable:
        print "Ignoring file "+item+"; not writable."
      else:
        chmod(item, 0600)

# walk the target directory, calling our function as we go
walk(targetdir, setperms, 0)
