#!/usr/bin/env python

import os, sys, glob, commands

from CleanupSettings import *


usage = """bzip2ProjectFrameFiles.py PROJNUM EXTENSION

    REQUIRES:

    PROJNUM    - the project number you want to remove files  from
    EXTENSION   - should either be 'edr' or 'log' (or others, but ONLY if you know what you're doing!)

    This script will bzip2 all the frame*.edr (or *.log) files from the project's
    RUN*/CLONE* directory, renaming each to frame*.edr.bz2   
"""


def run(cmd):
    print '>>', cmd
    output = commands.getoutput(cmd)
    print output.strip()

def bzip2ProjectFrameFiles(projnum=None, extension='edr'):
    """This script will bzip2 all the frame*.edr (or *.log) files from the project's
    RUN*/CLONE* directory, renaming each to frame*.edr.bz2   
    """

    projectDir = os.path.join(projectDataDir, 'PROJ%d'%projnum)
    print 'projectDir', projectDir

    clonedirs = glob.glob( os.path.join(projectDir,'RUN*/CLONE*') )

    for clonedir in clonedirs:
        extfiles = glob.glob( os.path.join(clonedir,'*.%s'%extension) )
        for f in extfiles:
           run( 'bzip2 %s'%f )

