| 1 | from optparse import OptionParser |
|---|
| 2 | from textwrap import dedent |
|---|
| 3 | |
|---|
| 4 | from programs import get_executable, Script |
|---|
| 5 | from datastore import FileSystemDataStore |
|---|
| 6 | from projects import SimProject, load_simulation_project |
|---|
| 7 | from launch import SerialLaunchMode |
|---|
| 8 | from parameters import build_parameters |
|---|
| 9 | |
|---|
| 10 | def setup(argv): |
|---|
| 11 | """Set up a new simulation project in the current directory.""" |
|---|
| 12 | usage = "%prog setup [options] NAME MAINFILE" |
|---|
| 13 | description = dedent("""\ |
|---|
| 14 | Set up a new simulation project in the current directory. |
|---|
| 15 | |
|---|
| 16 | NAME is the project name. |
|---|
| 17 | |
|---|
| 18 | MAINFILE is the name of the simulator script that would be supplied on the |
|---|
| 19 | command line if running the simulator normally, e.g. init.hoc. |
|---|
| 20 | """) |
|---|
| 21 | parser = OptionParser(usage=usage, |
|---|
| 22 | description=description) |
|---|
| 23 | parser.add_option('-d', '--datapath', metavar='PATH', default='./Data', help="set the path to the directory in which smt will search for datafiles generated by the simulation. Defaults to %default") |
|---|
| 24 | parser.add_option('-s', '--simpath', metavar='PATH', help="set the path to the simulator executable. If this is not set, smt will try to infer the executable from MAINFILE, and will try to find the executable from the PATH environment variable, then by searching various likely locations on the filesystem.") |
|---|
| 25 | parser.add_option('-r', '--repository', help="the URL of a Subversion or Mercurial repository containing the simulation code. This will be checked out/cloned into the current directory.") |
|---|
| 26 | parser.add_option('-D', '--debug', action='store_true', help="print debugging information") |
|---|
| 27 | (options, args) = parser.parse_args(argv) |
|---|
| 28 | if len(args) != 2: |
|---|
| 29 | parser.error('Both NAME and MAINFILE must be specified.') |
|---|
| 30 | project_name, main_file = args |
|---|
| 31 | |
|---|
| 32 | global _debug |
|---|
| 33 | _debug = options.debug |
|---|
| 34 | |
|---|
| 35 | script_code = Script(main_file=main_file, repository_url=options.repository) |
|---|
| 36 | script_code.checkout() # worth doing now, to find any errors early |
|---|
| 37 | executable = get_executable(path=options.simpath, script_file=main_file) |
|---|
| 38 | |
|---|
| 39 | project = SimProject(name=project_name, |
|---|
| 40 | default_executable=executable, |
|---|
| 41 | default_script=script_code, |
|---|
| 42 | default_launch_mode=SerialLaunchMode(), |
|---|
| 43 | data_store=FileSystemDataStore(options.datapath)) |
|---|
| 44 | |
|---|
| 45 | def info(argv): |
|---|
| 46 | """Print information about the current simulation project.""" |
|---|
| 47 | usage = "%prog info" |
|---|
| 48 | description = __doc__ |
|---|
| 49 | parser = OptionParser(usage=usage, |
|---|
| 50 | description=description) |
|---|
| 51 | (options, args) = parser.parse_args(argv) |
|---|
| 52 | if len(args) != 0: |
|---|
| 53 | parser.error('info does not take any arguments') |
|---|
| 54 | project = load_simulation_project() |
|---|
| 55 | print project.info() |
|---|
| 56 | |
|---|
| 57 | def run(argv): |
|---|
| 58 | """Run a simulation.""" |
|---|
| 59 | usage = "%prog run [options] PARAMFILE [param=value, ...]" |
|---|
| 60 | description = dedent("""\ |
|---|
| 61 | PARAMFILE is the name of the parameter file to be used for this simulation. |
|---|
| 62 | For convenience, it is possible to specify a file with default parameters |
|---|
| 63 | and then specify those parameters that are different from the default values |
|---|
| 64 | on the command line with any number of param=value pairs (note no space |
|---|
| 65 | around the equals sign). The parameter file should also consist of |
|---|
| 66 | param=value pairs, one per line, although here spaces are allowed around the |
|---|
| 67 | equals sign. Comments may be included using #.""") |
|---|
| 68 | parser = OptionParser(usage=usage, |
|---|
| 69 | description=description) |
|---|
| 70 | parser.add_option('-v', '--version', metavar='REV', |
|---|
| 71 | help="use version REV of the simulation code (if this is not the same as the working copy, it will be checked out of the repository). If this option is not specified, the most recent version in the repository will be used. If there are changes in the working copy, the user will be prompted to commit them first") |
|---|
| 72 | parser.add_option('-l', '--label', help="specify a label for the simulation. This label will have a time stamp appended, and will then be used to label the archive of data files that is generated by this run. If no label is specified, the label will be based on PARAMFILE.") |
|---|
| 73 | parser.add_option('-r', '--reason', help="explain the reason for running this simulation.") |
|---|
| 74 | |
|---|
| 75 | (options, args) = parser.parse_args(argv) |
|---|
| 76 | if len(args) < 1: |
|---|
| 77 | parser.error('A parameter file must be specified.') |
|---|
| 78 | |
|---|
| 79 | parameter_file = args[0] |
|---|
| 80 | cmdline_parameters = args[1:] |
|---|
| 81 | parameters = build_parameters(parameter_file, cmdline_parameters) |
|---|
| 82 | print parameters.pretty(expand_urls=True) |
|---|
| 83 | |
|---|
| 84 | project = load_simulation_project() |
|---|
| 85 | label = options.label or parameter_file |
|---|
| 86 | project.launch_simulation(parameters, label=label, reason=options.reason) |
|---|
| 87 | |
|---|