From c9e7f22f8f9119ca08209fda4ee173abcb8e65e6 Mon Sep 17 00:00:00 2001 From: yenatch Date: Wed, 24 Sep 2014 12:07:21 -0700 Subject: Rewrite scan_includes.py. Add include paths. --- pokemontools/scan_includes.py | 73 ++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 25 deletions(-) (limited to 'pokemontools/scan_includes.py') diff --git a/pokemontools/scan_includes.py b/pokemontools/scan_includes.py index 7f34e92..297e034 100644 --- a/pokemontools/scan_includes.py +++ b/pokemontools/scan_includes.py @@ -1,36 +1,59 @@ +#!/bin/python # coding: utf-8 """ -Recursively scan an asm file for rgbasm INCLUDEs and INCBINs. -Used to generate dependencies for each rgbasm object. +Recursively scan an asm file for dependencies. """ import os import sys +import argparse -import configuration -conf = configuration.Config() - -def recursive_scan(filename, includes = []): - if (filename[-4:] == '.asm' or filename[-3] == '.tx') and os.path.exists(filename): - lines = open(filename).readlines() - for line in lines: - for directive in ('INCLUDE', 'INCBIN'): - if directive in line: - line = line[:line.find(';')] - if directive in line: - include = line.split('"')[1] - if include not in includes: - includes += [include] - includes = recursive_scan(os.path.join(conf.path, include), includes) - break - return includes + +class IncludeReader: + """ + Usage: + includer = IncludeReader() + includer.read(filename) + or + includer = IncludeReader(filename='filename.asm') + includer.read() + """ + path = '' + includes = [] + directives = ['INCLUDE', 'INCBIN'] + extensions = ['.asm', '.tx'] + + def __init__(self, **kwargs): + self.__dict__.update(kwargs) + + def read(self, filename=None): + if filename is None: + if hasattr(self, 'filename'): + filename = os.path.join(self.path, self.filename) + else: + raise Exception, 'no filename given!' + if os.path.splitext(filename)[1] in self.extensions and os.path.exists(filename): + for line in open(filename).readlines(): + self.read_line(line) + + def read_line(self, line): + parts = line[:line.find(';')].split() + for directive in self.directives: + if directive in map(str.upper, parts): + include = os.path.join(self.path, parts[parts.index(directive) + 1].split('"')[1]) + if include not in self.includes: + self.includes.append(include) + self.read(include) if __name__ == '__main__': - filenames = sys.argv[1:] - dependencies = [] - for filename in filenames: - dependencies += recursive_scan(os.path.join(conf.path, filename)) - dependencies = list(set(dependencies)) - sys.stdout.write(' '.join(dependencies)) + ap = argparse.ArgumentParser() + ap.add_argument('-i', default='') + ap.add_argument('filenames', nargs='*') + args = ap.parse_args() + + includes = IncludeReader(path=args.i) + for filename in args.filenames: + includes.read(filename) + sys.stdout.write(' '.join(includes.includes)) -- cgit v1.2.3 From 31f843814cf17ef0fbcef7847837da73f7f9baf9 Mon Sep 17 00:00:00 2001 From: yenatch Date: Wed, 24 Sep 2014 12:54:44 -0700 Subject: Add docstrings for IncludeReader methods. --- pokemontools/scan_includes.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'pokemontools/scan_includes.py') diff --git a/pokemontools/scan_includes.py b/pokemontools/scan_includes.py index 297e034..e8255ac 100644 --- a/pokemontools/scan_includes.py +++ b/pokemontools/scan_includes.py @@ -28,6 +28,9 @@ class IncludeReader: self.__dict__.update(kwargs) def read(self, filename=None): + """ + Recursively look for includes in and add them to self.includes. + """ if filename is None: if hasattr(self, 'filename'): filename = os.path.join(self.path, self.filename) @@ -38,6 +41,9 @@ class IncludeReader: self.read_line(line) def read_line(self, line): + """ + Add any includes in to self.includes, and look for includes in those. + """ parts = line[:line.find(';')].split() for directive in self.directives: if directive in map(str.upper, parts): -- cgit v1.2.3