Last active
October 27, 2018 14:21
-
-
Save reuben/4465e39ba2bc8a80671747c8a197b5dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
from __future__ import absolute_import, division, print_function | |
import argparse | |
import itertools | |
import tempfile | |
import os | |
import shlex | |
import shutil | |
import subprocess | |
import sys | |
parser = argparse.ArgumentParser() | |
parser.add_argument('modulos', type=str, nargs='*', help='Módulos a serem compilados') | |
parser.add_argument('-m', dest='main', type=str, help='Módulo main') | |
parser.add_argument('-o', dest='out', type=str, nargs='?', help='Arquivo de saída, opcional') | |
v = parser.add_mutually_exclusive_group() | |
v.add_argument('-v', dest='verbose', action='store_const', const='v', default='s', help='Ativa modo verboso') | |
v.add_argument('-s', dest='verbose', action='store_const', const='s', default='s', help='Desativa modo verboso') | |
args = parser.parse_args() | |
# Caminhos para os programas dos TPs. Caso não tenha algum deles, basta apontar pra um arquivo não existente. | |
EMULADOR = './emulador' | |
MONTADOR = './montador' | |
EXPANSOR = './expansor' | |
LIGADOR = './ligador' | |
def run(cmd): | |
print('> {}'.format(cmd), file=sys.stderr) | |
return subprocess.call(shlex.split(cmd)) | |
def expande_arquivo(arq, tmp): | |
base = os.path.basename(arq) | |
expandido = os.path.join(tmp, '{}.expandido'.format(base)) | |
run('{} {} {}'.format(EXPANSOR, arq, expandido)) | |
return expandido | |
# Diretório temporário para arquivos intermediários | |
tmp = tempfile.mkdtemp() | |
if os.path.exists(EXPANSOR): | |
main_expandido = expande_arquivo(args.main, tmp) if args.main else None | |
modulos_expandidos = [expande_arquivo(m, tmp) for m in args.modulos] | |
else: | |
main_expandido = args.main | |
modulos_expandidos = args.modulos | |
# Caso só um arquivo tenha sido passado na entrada, ele é o main | |
if len(modulos_expandidos) == 1 and not main_expandido: | |
main_expandido = modulos_expandidos.pop() | |
if not args.out: | |
args.out = os.path.join(tmp, 'programa.saida.mv') | |
if os.path.exists(LIGADOR): | |
run('{} {} -m {} -o {}'.format(LIGADOR, ' '.join(modulos_expandidos), main_expandido, args.out)) | |
else: | |
if len(modulos_expandidos) > 0: | |
print('Múltiplos módulos mas ligador não foi encontrado.', file=sys.stderr) | |
exit(1) | |
run('{} {} {} {}'.format(MONTADOR, main_expandido, args.out, args.verbose)) | |
run('{} 0 1000 0 {} {}'.format(EMULADOR, args.verbose, args.out)) | |
shutil.rmtree(tmp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment