#!/usr/bin/python -OO

# This file is part of Archivematica.
#
# Copyright 2010-2013 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Archivematica.  If not, see <http://www.gnu.org/licenses/>.

# @package Archivematica
# @subpackage DevCleanup
# @author Joseph Perry <joseph@artefactual.com>
import os
import sys
sys.path.append("/usr/lib/archivematica/archivematicaCommon")
import databaseInterface
from databaseFunctions import insertIntoEvents

alsoRemove = ["/var/archivematica/sharedDirectory/watchedDirectories/SIPCreation/completedTransfers/", \
              "/var/archivematica/sharedDirectory/failed/", \
              "/var/archivematica/sharedDirectory/currentlyProcessing/", \
              "/var/archivematica/sharedDirectory/rejected/"]

def removeEverythingInDirectory(directory):
    if directory[-1] != "/":
        directory = "%s/" % (directory)
    execute = "sudo rm -rf \"%s\"*" % (directory)
    print "executing: ", execute
    os.system(execute)

def cleanWatchedDirectories():
    sql = """SELECT watchedDirectoryPath FROM WatchedDirectories;"""
    c, sqlLock = databaseInterface.querySQL(sql)
    row = c.fetchone()
    while row != None:
        try:
            directory = row[0].replace("%watchDirectoryPath%", "/var/archivematica/sharedDirectory/watchedDirectories/", 1)
            removeEverythingInDirectory(directory)
        except Exception as inst:
            print "debug except 2"
            print type(inst)     # the exception instance
            print inst.args      # arguments stored in .args
        row = c.fetchone()
    sqlLock.release()

if __name__ == '__main__':
    if True:
        import getpass
        user = getpass.getuser()
        print "user: ", user
        if user != "root":
            print "Please run as root (with sudo)"
            exit (1)
    cleanWatchedDirectories()
    for directory in alsoRemove:
        removeEverythingInDirectory(directory)

@@ -985,359 +985,8 @@
 ace%0A
-from databaseFunctions import insertIntoEvents%0A%0AalsoRemove = %5B%22/var/archivematica/sharedDirectory/watchedDirectories/SIPCreation/completedTransfers/%22, %5C%0A              %22/var/archivematica/sharedDirectory/failed/%22, %5C%0A              %22/var/archivematica/sharedDirectory/currentlyProcessing/%22, %5C%0A              %22/var/archivematica/sharedDirectory/rejected/%22%5D
 %0A%0Ade
@@ -1855,25 +1855,8 @@
 _':%0A
-    if True:%0A    
     
@@ -1874,20 +1874,16 @@
 ass%0A    
-   
- 
 user = g
@@ -1899,20 +1899,16 @@
 tuser()%0A
-    
     prin
@@ -1924,20 +1924,16 @@
 %22, user%0A
-    
     if u
@@ -1951,28 +1951,24 @@
 t%22:%0A        
- 
-   
 print %22Pleas
@@ -2002,20 +2002,16 @@
         
-    
 exit (1)
@@ -2041,16 +2041,285 @@
 ories()%0A
+    alsoRemove = %5B%0A        %22/var/archivematica/sharedDirectory/failed/%22,%0A        %22/var/archivematica/sharedDirectory/currentlyProcessing/%22,%0A        %22/var/archivematica/sharedDirectory/rejected/%22,%0A        %22/var/archivematica/sharedDirectory/completed/transfers/%22,%0A    %5D%0A
     for 

Clean up, #4757