Last active
August 16, 2017 07:04
-
-
Save mwtoews/50ed3ddabc8c6e9677a3a11dd5ba6dab 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
# -*- coding: utf-8 -*- | |
import os | |
import re | |
re_fn = re.compile(r'(.+?\.)(f|for|f90|fpp)$') | |
# sdir = '/home/mwtoews/src/GWM-2005/src' | |
sdir = '.' | |
fn_lines = {} | |
for root, dirs, files in os.walk(sdir): | |
path = root.split(os.sep) | |
for fn in files: | |
if re_fn.findall(fn): | |
if fn in fn_lines: | |
raise KeyError('content from {0!r} already read'.format(fn)) | |
with open(os.path.join(root, fn), 'r') as fp: | |
fn_lines[fn] = fp.readlines() | |
re_mod = re.compile(r'^\s*MODULE\s+([a-z0-9\_]+)', re.I) | |
re_use = re.compile(r'^\s*USE\s+([a-z0-9\_]+)', re.I) | |
modules = {} # key module name, value file | |
use = {} # key module name, value list of files | |
for fn in fn_lines.keys(): | |
lines = fn_lines[fn] | |
for line in lines: | |
mod = re_mod.findall(line) | |
if mod: | |
mod_name = mod[0].lower() | |
if mod_name in modules: | |
print('{0!r} already used by {1!r}, so ignoring from {2!r}' | |
.format(mod_name, modules[mod_name], fn)) | |
else: | |
modules[mod_name] = fn | |
continue | |
mod = re_use.findall(line) | |
if mod: | |
mod_name = mod[0].lower() | |
if mod_name not in use: | |
use[mod_name] = set([fn]) | |
else: | |
use[mod_name].add(fn) | |
modules_s = set(modules.keys()) | |
use_s = set(use.keys()) | |
mods_not_used = modules_s.difference(use_s) | |
if mods_not_used: | |
one = len(mods_not_used) == 1 | |
print('There {0} {1} module{2} that {0} not used:' | |
.format('is' if one else 'are', | |
len(mods_not_used), | |
'' if one else 's')) | |
for i, mod in enumerate(sorted(mods_not_used), 1): | |
print(" {0}: {1!r} : {2!r}".format(i, mod, modules[mod])) | |
mods_not_defined = use_s.difference(modules_s) | |
if mods_not_defined: | |
one = len(mods_not_defined) == 1 | |
print('There {0} {1} module{2} that {0} not defined:' | |
.format('is' if one else 'are', | |
len(mods_not_defined), | |
'' if one else 's')) | |
for i, mod in enumerate(sorted(mods_not_defined), 1): | |
print(" {0}: {1!r} : {2!r}".format(i, mod, sorted(use[mod]))) | |
mods_used = modules_s.intersection(use_s) | |
mods_from = {} | |
prereqs = {} | |
for mod in sorted(mods_used): | |
fn = modules[mod] | |
# find dependencies other than itself | |
targs = frozenset(use[mod].difference([fn])) | |
if targs: | |
if targs not in mods_from: | |
mods_from[targs] = [] | |
prereqs[targs] = set() | |
mods_from[targs].append((mod, fn)) | |
prereqs[targs].add(fn) | |
lines = [] | |
for targs in sorted(prereqs.keys()): | |
for mod, fn in sorted(mods_from[targs]): | |
lines.append('# {0}.mod from {1}\n'.format(mod, fn)) | |
line = '' | |
for fn in sorted(targs): | |
o = re_fn.findall(fn)[0][0] + 'o ' | |
if len(line) + len(o) + 2 >= 80: | |
lines.append(line + '\\\n') | |
line = ' ' | |
line += o | |
if len(line) + len(o) + 3 >= 80: | |
lines.append(line + '\\\n') | |
line = ' ' | |
line += ': ' | |
for fn in sorted(prereqs[targs]): | |
o = re_fn.findall(fn)[0][0] + 'o ' | |
if len(line) + len(o) + 2 >= 80: | |
lines.append(line + '\\\n') | |
line = ' ' | |
line += o | |
lines.append(line + '\n') | |
lines.append('\n') | |
print(''.join(lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment