#!/usr/bin/python

import os, sys
import pwd

if os.geteuid():
    print >> sys.stderr, "Script must be run as root to manipulate permissions"
    sys.exit(1)

from conary import dbstore
from mint import config

apacheGid = pwd.getpwnam('apache')[3]
isogenUid = pwd.getpwnam('isogen')[2]

cfg = config.MintConfig()
cfg.read(config.RBUILDER_CONFIG)
db = dbstore.connect(cfg.dbPath, cfg.dbDriver)
cu = db.cursor()
cu.execute('SELECT filename FROM ImageFiles')
imageFiles = [x[0] for x in cu.fetchall()]

for baseDir, dirs, files in os.walk(cfg.imagesPath):
    if len(baseDir.split(os.path.sep)) == 6:
        os.chown(baseDir, isogenUid, apacheGid)
        os.chmod(baseDir, os.stat(baseDir)[0] & 0777 | 0020)
        for file in files:
            path = os.path.join(baseDir, file)
            if file not in imageFiles:
                os.unlink(path)
            else:
                os.chown(path, isogenUid, apacheGid)

@@ -39,18 +39,213 @@
 rt pwd%0A%0A
-if
+dryRun = False%0Aif len(sys.argv) %3E 1 and sys.argv%5B1%5D == %22--dry-run%22:%0A    dryRun = True%0Aelif len(sys.argv) %3E 1:%0A    print %22usage: cleanup-images.py %5B--dry-run%5D%22%0A    sys.exit(1)%0A%0Aif not dryRun and not
  os.gete
@@ -288,15 +288,11 @@
 ipt 
-must be
+not
  run
@@ -303,51 +303,46 @@
 root
- to manipulate permissions%22%0A    sys.exit(1)
+: forcing --dry-run%22%0A    dryRun = True
 %0A%0Afr
@@ -779,16 +779,202 @@
 ) == 6:%0A
+        if dryRun:%0A            print %22chown %25d.%25d %25s%22 %25 (isogenUid, apacheGid, baseDir)%0A            print %22chmod %250o %25s%22 %25 (os.stat(baseDir)%5B0%5D & 0777 %7C 0020, baseDir)%0A        else:%0A    
         
@@ -1013,16 +1013,20 @@
 cheGid)%0A
+    
         
@@ -1211,42 +1211,263 @@
     
-os.unlink(path)%0A            else:%0A
+if dryRun:%0A                    print %22rm %25s%22 %25 path%0A                else:%0A                    os.unlink(path)%0A            else:%0A                if dryRun:%0A                    print %22chown %25d.%25d %25s%22 %25 (isogenUid, apacheGid, path)%0A                else:%0A    
     

add a --dry-run mode to cleanup-images.py