diff options
author | yenatch <yenatch@gmail.com> | 2014-09-24 12:07:21 -0700 |
---|---|---|
committer | yenatch <yenatch@gmail.com> | 2014-09-24 12:07:21 -0700 |
commit | c9e7f22f8f9119ca08209fda4ee173abcb8e65e6 (patch) | |
tree | cf5905d20cd5a1b55af0c9fe8dcd31f9f5ff5918 | |
parent | a87f66964cc602ff5575efebf27c388ef03fa94d (diff) |
Rewrite scan_includes.py. Add include paths.
-rw-r--r-- | pokemontools/scan_includes.py | 73 |
1 files changed, 48 insertions, 25 deletions
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)) |