#!/usr/bin/env python
# -*- mode: python -*-
import sys
import os
import glob
import shutil
from ometa.runtime import ParseError
from ometa.grammar import OMeta, TreeTransformerGrammar
from ometa.builder import writePython


def main():
    bin_dir = os.path.dirname(os.path.realpath(__file__))
    parsley_dir = os.path.split(bin_dir)[0]
    stage_dir = os.path.join(parsley_dir, 'stage')
    sys.path.append(parsley_dir)

    if os.path.exists(stage_dir):
        print "Removing stage dir"
        shutil.rmtree(stage_dir)

    os.mkdir(stage_dir)
    stage_pkg(parsley_dir, stage_dir, 'ometa')
    stage_pkg(parsley_dir, stage_dir, 'terml')


def stage_pkg(parsley_dir, stage_dir, pkg):
    print "\nCopying", pkg
    stage_pkg_dir = os.path.join(stage_dir, pkg)
    stage_gen_dir = os.path.join(stage_pkg_dir, '_generated')
    pkg_dir = os.path.join(parsley_dir, pkg)

    shutil.copytree(pkg_dir, stage_pkg_dir,
                    ignore=lambda src, names: [n for n in names if n.endswith('pyc')])
    for filename in glob.glob(pkg_dir + "/*.parsley"):
        grammar = open(filename).read()
        grammarname = os.path.split(filename)[1].split('.')[0]
        pyfn = os.path.join(stage_gen_dir, grammarname + '.py')
        print "{src:38} => {dst}".format(src=os.path.relpath(filename, parsley_dir),
                                         dst=os.path.relpath(pyfn, parsley_dir))
        if grammar.startswith('#TreeTransformer'):
            g = TreeTransformerGrammar(grammar)
        else:
            g = OMeta(grammar)
        tree = g.parseGrammar(grammarname)
        source = writePython(tree, grammar)
        pythonFile = open(pyfn, 'w')
        pythonFile.write(source)


if __name__ == '__main__':
    main()











