forked from BlueBrain/nmodl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
generate_references.py
configurable. (BlueBrain#1286)
Adds the ability to customize with which flags `nmodl` is run to generate the references. The default is `""` (for CoreNEURON) and `"--neuron"` (for NEURON). These choices can be overridden by adding a file `references.json` to the usecase directory. Since the logic is slightly more involved, the Bash script is reimplemented as a Python script.
- Loading branch information
Showing
3 changed files
with
69 additions
and
37 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
import glob | ||
import os | ||
import json | ||
import subprocess | ||
|
||
|
||
def substitute_line(lines, starts_with, replacement): | ||
for k, line in enumerate(lines): | ||
if line.startswith(starts_with): | ||
lines[k] = replacement | ||
break | ||
|
||
|
||
def generate_references(nmodl, usecase_dir, output_dir, nmodl_flags): | ||
mod_files = glob.glob(os.path.join(usecase_dir, "*.mod")) | ||
for mod_file in mod_files: | ||
try: | ||
subprocess.run( | ||
["nmodl", mod_file, *nmodl_flags, "-o", output_dir], check=True | ||
) | ||
except subprocess.CalledProcessError as e: | ||
print(e.output) | ||
raise e | ||
|
||
cxx_files = glob.glob(os.path.join(output_dir, "*.cpp")) | ||
|
||
for cxx_file in cxx_files: | ||
with open(cxx_file, "r") as f: | ||
cxx = f.readlines() | ||
|
||
sub_date = "Created : ", "Created : DATE\n" | ||
substitute_line(cxx, *sub_date) | ||
|
||
sub_version = "NMODL Compiler : ", "NMODL Compiler : VERSION\n" | ||
substitute_line(cxx, *sub_version) | ||
|
||
with open(cxx_file, "w") as f: | ||
f.writelines(cxx) | ||
|
||
|
||
def load_config(script_dir, usecase_dir): | ||
config = os.path.join(usecase_dir, "references.json") | ||
if os.path.exists(config): | ||
with open(config, "r") as f: | ||
return json.load(f) | ||
else: | ||
return [ | ||
{"output_reldir": "coreneuron", "nmodl_flags": []}, | ||
{"output_reldir": "neuron", "nmodl_flags": ["--neuron"]}, | ||
] | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) != 4: | ||
print(f"Usage: {sys.argv[0]} NMODL USECASE_DIR OUTPUT_DIR") | ||
sys.exit(-1) | ||
|
||
nmodl = sys.argv[1] | ||
usecase_dir = sys.argv[2] | ||
output_basedir = sys.argv[3] | ||
script_dir = os.path.dirname(sys.argv[0]) | ||
|
||
config = load_config(script_dir=script_dir, usecase_dir=usecase_dir) | ||
for c in config: | ||
output_dir = os.path.join(output_basedir, c["output_reldir"]) | ||
generate_references(nmodl, usecase_dir, output_dir, c["nmodl_flags"]) |
This file was deleted.
Oops, something went wrong.