@@ -1,157 +1,168 | |||
|
1 | 1 | import click |
|
2 | 2 | import schainpy |
|
3 | 3 | import subprocess |
|
4 | 4 | import os |
|
5 | 5 | import sys |
|
6 | 6 | import glob |
|
7 | 7 | save_stdout = sys.stdout |
|
8 | 8 | sys.stdout = open('trash', 'w') |
|
9 | 9 | from multiprocessing import cpu_count |
|
10 | 10 | from schaincli import templates |
|
11 | 11 | from schainpy.controller import Project |
|
12 | 12 | from schainpy.model import Operation, ProcessingUnit |
|
13 | 13 | from schainpy.utils import log |
|
14 | 14 | from importlib import import_module |
|
15 | 15 | from pydoc import locate |
|
16 | 16 | from fuzzywuzzy import process |
|
17 | 17 | from schainpy.utils import paramsFinder |
|
18 | 18 | sys.stdout = save_stdout |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | def print_version(ctx, param, value): |
|
22 | 22 | if not value or ctx.resilient_parsing: |
|
23 | 23 | return |
|
24 | 24 | click.echo(schainpy.__version__) |
|
25 | 25 | ctx.exit() |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | cliLogger = log.makelogger('schain cli') |
|
29 | 29 | PREFIX = 'experiment' |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | @click.command() |
|
33 | 33 | @click.option('--version', '-v', is_flag=True, callback=print_version, help='SChain version', type=str) |
|
34 | @click.option('--xml', '-x', default=None, help='run an XML file', type=click.Path(exists=True, resolve_path=True)) | |
|
35 | 34 | @click.argument('command', default='run', required=True) |
|
36 | 35 | @click.argument('nextcommand', default=None, required=False, type=str) |
|
37 |
def main(command, nextcommand, version |
|
|
36 | def main(command, nextcommand, version): | |
|
38 | 37 | """COMMAND LINE INTERFACE FOR SIGNAL CHAIN - JICAMARCA RADIO OBSERVATORY \n |
|
39 | 38 | Available commands.\n |
|
40 | 39 | --xml: runs a schain XML generated file\n |
|
41 | 40 | run: runs any python script starting 'experiment_'\n |
|
42 | 41 | generate: generates a template schain script\n |
|
43 | 42 | search: return avilable operations, procs or arguments of the give operation/proc\n""" |
|
44 | if xml is not None: | |
|
45 |
runFromXML( |
|
|
43 | if command == 'xml': | |
|
44 | runFromXML(nextcommand) | |
|
46 | 45 | elif command == 'generate': |
|
47 | 46 | generate() |
|
48 | 47 | elif command == 'test': |
|
49 | 48 | test() |
|
50 | 49 | elif command == 'run': |
|
51 | 50 | runschain(nextcommand) |
|
52 | 51 | elif command == 'search': |
|
53 | 52 | search(nextcommand) |
|
54 | 53 | else: |
|
55 | 54 | log.error('Command {} is not defined'.format(command)) |
|
56 | 55 | |
|
56 | ||
|
57 | 57 | def check_module(possible, instance): |
|
58 | 58 | def check(x): |
|
59 | 59 | try: |
|
60 | 60 | instancia = locate('schainpy.model.{}'.format(x)) |
|
61 | 61 | return isinstance(instancia(), instance) |
|
62 | 62 | except Exception as e: |
|
63 | 63 | return False |
|
64 | 64 | clean = clean_modules(possible) |
|
65 | 65 | return [x for x in clean if check(x)] |
|
66 | 66 | |
|
67 | 67 | |
|
68 | 68 | def clean_modules(module): |
|
69 | 69 | noEndsUnder = [x for x in module if not x.endswith('__')] |
|
70 | 70 | noStartUnder = [x for x in noEndsUnder if not x.startswith('__')] |
|
71 | 71 | noFullUpper = [x for x in noStartUnder if not x.isupper()] |
|
72 | 72 | return noFullUpper |
|
73 | 73 | |
|
74 | 74 | |
|
75 | 75 | def search(nextcommand): |
|
76 | 76 | if nextcommand is None: |
|
77 | 77 | log.error('There is no Operation/ProcessingUnit to search') |
|
78 | 78 | elif nextcommand == 'procs': |
|
79 | 79 | procs = paramsFinder.getProcs() |
|
80 | log.success('Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs))) | |
|
80 | log.success( | |
|
81 | 'Current ProcessingUnits are:\n\033[1m{}\033[0m'.format('\n'.join(procs))) | |
|
81 | 82 | |
|
82 | 83 | elif nextcommand == 'operations': |
|
83 | 84 | operations = paramsFinder.getOperations() |
|
84 |
log.success('Current Operations are:\n\033[1m{}\033[0m'.format( |
|
|
85 | log.success('Current Operations are:\n\033[1m{}\033[0m'.format( | |
|
86 | '\n'.join(operations))) | |
|
85 | 87 | else: |
|
86 | 88 | try: |
|
87 | 89 | args = paramsFinder.getArgs(nextcommand) |
|
88 | log.warning('Use this feature with caution. It may not return all the allowed arguments') | |
|
90 | log.warning( | |
|
91 | 'Use this feature with caution. It may not return all the allowed arguments') | |
|
89 | 92 | if len(args) == 0: |
|
90 | 93 | log.success('{} has no arguments'.format(nextcommand)) |
|
91 | 94 | else: |
|
92 |
log.success('Showing arguments |
|
|
95 | log.success('Showing {} arguments:\n\033[1m{}\033[0m'.format( | |
|
96 | nextcommand, '\n'.join(args))) | |
|
93 | 97 | except Exception as e: |
|
94 | 98 | log.error('Module {} does not exists'.format(nextcommand)) |
|
95 | 99 | allModules = paramsFinder.getAll() |
|
96 | 100 | similar = process.extractOne(nextcommand, allModules)[0] |
|
97 | 101 | log.success('Showing {} instead'.format(similar)) |
|
98 | 102 | search(similar) |
|
99 | 103 | |
|
100 | 104 | |
|
101 | 105 | def runschain(nextcommand): |
|
102 | 106 | if nextcommand is None: |
|
103 | 107 | currentfiles = glob.glob('./{}_*.py'.format(PREFIX)) |
|
104 | 108 | numberfiles = len(currentfiles) |
|
105 | 109 | if numberfiles > 1: |
|
106 | 110 | log.error('There is more than one file to run') |
|
107 | 111 | elif numberfiles == 1: |
|
108 | 112 | subprocess.call(['python ' + currentfiles[0]], shell=True) |
|
109 | 113 | else: |
|
110 | 114 | log.error('There is no file to run') |
|
111 | 115 | else: |
|
112 | 116 | try: |
|
113 | 117 | subprocess.call(['python ' + nextcommand], shell=True) |
|
114 | 118 | except Exception as e: |
|
115 | 119 | log.error("I cannot run the file. Does it exists?") |
|
116 | 120 | |
|
117 | 121 | |
|
118 | 122 | def basicInputs(): |
|
119 | 123 | inputs = {} |
|
120 | inputs['desc'] = click.prompt('Enter a description', default="A schain project", type=str) | |
|
121 | inputs['name'] = click.prompt('Name of the project', default="project", type=str) | |
|
122 | inputs['path'] = click.prompt('Data path', default=os.getcwd(), type=click.Path(exists=True, resolve_path=True)) | |
|
123 | inputs['startDate'] = click.prompt('Start date', default='1970/01/01', type=str) | |
|
124 |
inputs[' |
|
|
125 | inputs['startHour'] = click.prompt('Start hour', default='00:00:00', type=str) | |
|
124 | inputs['desc'] = click.prompt( | |
|
125 | 'Enter a description', default="A schain project", type=str) | |
|
126 | inputs['name'] = click.prompt( | |
|
127 | 'Name of the project', default="project", type=str) | |
|
128 | inputs['path'] = click.prompt('Data path', default=os.getcwd( | |
|
129 | ), type=click.Path(exists=True, resolve_path=True)) | |
|
130 | inputs['startDate'] = click.prompt( | |
|
131 | 'Start date', default='1970/01/01', type=str) | |
|
132 | inputs['endDate'] = click.prompt( | |
|
133 | 'End date', default='2017/12/31', type=str) | |
|
134 | inputs['startHour'] = click.prompt( | |
|
135 | 'Start hour', default='00:00:00', type=str) | |
|
126 | 136 | inputs['endHour'] = click.prompt('End hour', default='23:59:59', type=str) |
|
127 | 137 | inputs['figpath'] = inputs['path'] + '/figs' |
|
128 | 138 | return inputs |
|
129 | 139 | |
|
130 | 140 | |
|
131 | 141 | def generate(): |
|
132 | 142 | inputs = basicInputs() |
|
133 | 143 | inputs['multiprocess'] = click.confirm('Is this a multiprocess script?') |
|
134 | 144 | if inputs['multiprocess']: |
|
135 |
inputs['nProcess'] = click.prompt( |
|
|
145 | inputs['nProcess'] = click.prompt( | |
|
146 | 'How many process?', default=cpu_count(), type=int) | |
|
136 | 147 | current = templates.multiprocess.format(**inputs) |
|
137 | 148 | else: |
|
138 | 149 | current = templates.basic.format(**inputs) |
|
139 | 150 | scriptname = '{}_{}.py'.format(PREFIX, inputs['name']) |
|
140 | 151 | script = open(scriptname, 'w') |
|
141 | 152 | try: |
|
142 | 153 | script.write(current) |
|
143 | 154 | log.success('Script {} generated'.format(scriptname)) |
|
144 | 155 | except Exception as e: |
|
145 | 156 | log.error('I cannot create the file. Do you have writing permissions?') |
|
146 | 157 | |
|
147 | 158 | |
|
148 | 159 | def test(): |
|
149 | 160 | log.warning('testing') |
|
150 | 161 | |
|
151 | 162 | |
|
152 | 163 | def runFromXML(filename): |
|
153 | 164 | controller = Project() |
|
154 | 165 | if not controller.readXml(filename): |
|
155 | 166 | return |
|
156 | 167 | controller.start() |
|
157 | 168 | return |
General Comments 0
You need to be logged in to leave comments.
Login now