hold information about one of the .f90/.F90 files
| 48 | |
| 49 | |
| 50 | class SourceFile(object): |
| 51 | """ hold information about one of the .f90/.F90 files """ |
| 52 | |
| 53 | def __init__(self, filename): |
| 54 | |
| 55 | self.name = filename |
| 56 | |
| 57 | # do we need to be preprocessed? We'll use the convention |
| 58 | # that .F90 = yes, .f90 = no |
| 59 | self.ext = os.path.splitext(filename)[1] |
| 60 | |
| 61 | self.preprocess = bool(self.ext in [".F90", ".F95", ".F03"]) |
| 62 | |
| 63 | # when we preprocess, the output file has a different name |
| 64 | self.cpp_name = None |
| 65 | |
| 66 | |
| 67 | def search_name(self): |
| 68 | """return the file name we use for searching -- this is the |
| 69 | preprocessed file if it exists""" |
| 70 | |
| 71 | if self.cpp_name is not None: |
| 72 | search_file = self.cpp_name |
| 73 | else: |
| 74 | search_file = self.name |
| 75 | |
| 76 | return search_file |
| 77 | |
| 78 | |
| 79 | def obj(self): |
| 80 | """ the name of the object file we expect to be produced -- this |
| 81 | will always be based on the original name -- we do not compile the |
| 82 | preprocessed files """ |
| 83 | return self.name.replace(self.ext, ".o") |
| 84 | |
| 85 | |
| 86 | def defined_modules(self): |
| 87 | """determine what modules this file provides -- we work off of the |
| 88 | preprocessed file if it exists.""" |
| 89 | |
| 90 | defines = [] |
| 91 | |
| 92 | with io.open(self.search_name(), "r", encoding="latin-1") as f: |
| 93 | |
| 94 | for line in f: |
| 95 | |
| 96 | # strip off the comments |
| 97 | idx = line.find("!") |
| 98 | line = line[:idx] |
| 99 | |
| 100 | # we want a module definition itself, not a 'module procedure' |
| 101 | # also, Fortran is case-insensitive |
| 102 | rebreak = module_re.search(line) |
| 103 | rebreak2 = module_proc_re.search(line) |
| 104 | if rebreak and not rebreak2: |
| 105 | defines.append(rebreak.group(4).lower()) |
| 106 | |
| 107 | return defines |