import os
import sys
import py_compile
import subprocess

def get_py_file():
    p = subprocess.Popen('find -name "*.py"', stdout=subprocess.PIPE, shell=True)
    p.wait()
    flist = p.stdout.readlines()
    return flist

def get_pyc_file():
    p = subprocess.Popen('find -name "*.pyc"', stdout=subprocess.PIPE, shell=True)
    p.wait()
    flist = p.stdout.readlines()
    return flist

def mkpyc(flist):
    for f in flist:
        print 'compile %s' % f.strip()
        py_compile.compile(f.strip())

def delete_file(flist):
    for f in flist:
        print 'delete file: %s'%f.strip()
        os.remove(f.strip())

def usage():
    print 'Usage:'
    print 'python %s <dir_name>  [pyc] [py] '%__file__
    print 'example:'
    print '     python %s ./ pyc  ------>  delete ./ pyc file   '%__file__

if __name__ == '__main__':
    if len(sys.argv) < 2:
        usage()
        exit(-1)
    
    os.chdir(sys.argv[1])

    if len(sys.argv) < 3:
        mkpyc(get_py_file())
    elif sys.argv[2] == 'pyc':
        delete_file(get_pyc_file())
    elif sys.argv[2] == 'py':
        delete_file(get_py_file())
    else:
        usage()
