Skip to content

Instantly share code, notes, and snippets.

@GoldsteinE
Created December 12, 2017 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GoldsteinE/660b49d785059cf8241e4eef54a8873f to your computer and use it in GitHub Desktop.
Save GoldsteinE/660b49d785059cf8241e4eef54a8873f to your computer and use it in GitHub Desktop.
main.py
import structs
from structs import MakefileError
import sys
import os
def main(script, args):
if len(args) != 1:
print('Usage:\n{} <filename>'.format(script), file=sys.stderr)
sys.exit(1)
filename = args[0]
if not (os.path.isfile(filename) and os.access(filename, os.R_OK)):
print('File is not exists or access denied.', file=sys.stderr)
sys.exit(1)
result = structs.Makefile()
with open(filename) as file:
curr_goal = None
for num, line in enumerate(file, 1):
line = line.rstrip('\n')
if line == '':
try:
result.add_goal(curr_goal)
except MakefileError as e:
print('Line #{}: Error while finishing goal block: {}'.format(num, e))
sys.exit(1)
curr_goal = None
continue
if line[0] == '#':
continue
if line[0].isspace():
if curr_goal is None:
print('Line #{}: Commands outside of goal block.'.format(num), file=sys.stderr)
sys.exit(1)
else:
curr_goal.add_command(line)
else:
if curr_goal is not None:
print('Line #{}: Beginning new goal while previous isn\'t finished.'.format(num), file=sys.stderr)
sys.exit(1)
else:
try:
curr_goal = structs.Goal.parse_header(line)
except MakefileError as e:
print('Line #{}: Error while parsing goal header: {}'.format(num, e))
sys.exit(1)
if curr_goal is not None:
try:
result.add_goal(curr_goal)
except MakefileError as e:
print('Error while finishing last goal block: {}'.format(e))
sys.exit(1)
try:
result.check()
except MakefileError as e:
print('Integrity check error: {}'.format(e))
sys.exit(1)
print('Makefile OK')
if __name__ == '__main__':
main(sys.argv[0], sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment