Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

# This file is part of ctrl_mpexec. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

 

"""Module defining CmdLineParser class and related methods. 

""" 

 

__all__ = ["makeParser"] 

 

# ------------------------------- 

# Imports of standard modules -- 

# ------------------------------- 

from argparse import Action, ArgumentParser, RawDescriptionHelpFormatter 

import ast 

import collections 

import re 

import textwrap 

 

# ----------------------------- 

# Imports for other modules -- 

# ----------------------------- 

 

# ---------------------------------- 

# Local non-exported definitions -- 

# ---------------------------------- 

 

# Class which determines an action that needs to be performed 

# when building pipeline, its attributes are: 

# action: the name of the action, e.g. "new_task", "delete_task" 

# label: task label, can be None if action does not require label 

# value: argument value excluding task label. 

_PipelineAction = collections.namedtuple("_PipelineAction", "action,label,value") 

 

 

class _PipelineActionType: 

"""Class defining a callable type which converts strings into 

_PipelineAction instances. 

 

Parameters 

---------- 

action : str 

Name of the action, will become `action` attribute of instance. 

regex : str 

Regular expression for argument value, it can define groups 'label' 

and 'value' which will become corresponding attributes of a 

returned instance. 

""" 

 

def __init__(self, action, regex='.*', valueType=str): 

self.action = action 

self.regex = re.compile(regex) 

self.valueType = valueType 

 

def __call__(self, value): 

match = self.regex.match(value) 

if not match: 

raise TypeError("Unrecognized option syntax: " + value) 

# get "label" group or use None as label 

try: 

label = match.group("label") 

except IndexError: 

label = None 

# if "value" group is not defined use whole string 

try: 

value = match.group("value") 

except IndexError: 

pass 

value = self.valueType(value) 

return _PipelineAction(self.action, label, value) 

 

def __repr__(self): 

"""String representation of this class. 

 

argparse can use this for some error messages, default implementation 

makes those messages incomprehensible. 

""" 

return f"_PipelineActionType(action={self.action})" 

 

 

def _nameTemplatesType(value): 

"""Convert name_templates option value to a dictionary. 

""" 

# try to parse value as python literal expression 

parsedNamesDict = ast.literal_eval(value) 

# expecting dict 

if not isinstance(parsedNamesDict, dict): 

raise TypeError(f"Unable parse --dataset-name-substitution {value} " 

"into a valid dict") 

# check that all keys and values are strings 

for key, val in parsedNamesDict.items(): 

for x in [key, val]: 

if not isinstance(x, str): 

raise TypeError(f"--dataset-name-substitution option {value} " 

"contains non-string key or value") 

return parsedNamesDict 

 

 

_ACTION_ADD_TASK = _PipelineActionType("new_task", "(?P<value>[^:]+)(:(?P<label>.+))?") 

_ACTION_DELETE_TASK = _PipelineActionType("delete_task", "(?P<value>)(?P<label>.+)") 

_ACTION_MOVE_TASK = _PipelineActionType("move_task", r"(?P<label>.+):(?P<value>-?\d+)", int) 

_ACTION_LABEL_TASK = _PipelineActionType("relabel", "(?P<label>.+):(?P<value>.+)") 

_ACTION_CONFIG = _PipelineActionType("config", "(?P<label>.+):(?P<value>.+=.+)") 

_ACTION_CONFIG_FILE = _PipelineActionType("configfile", "(?P<label>.+):(?P<value>.+)") 

_ACTION_NAME_TEMPLATES = _PipelineActionType("name_templates", "(?P<label>[^:]+):(?P<value>.+)", 

_nameTemplatesType) 

 

 

class _LogLevelAction(Action): 

"""Action class which collects logging levels. 

 

This action class collects arguments in the form "LEVEL" or 

"COMPONENT=LEVEL" where LEVEL is the name of the logging level (case- 

insensitive). It converts the series of arguments into the list of 

tuples (COMPONENT, LEVEL). If component name is missing then first 

item in tuple is set to `None`. Second item in tuple is converted to 

upper case. 

""" 

 

permittedLevels = set(['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']) 

 

def __call__(self, parser, namespace, values, option_string=None): 

"""Re-implementation of the base class method. 

 

See `argparse.Action` documentation for parameter description. 

""" 

dest = getattr(namespace, self.dest) 

if dest is None: 

dest = [] 

setattr(namespace, self.dest, dest) 

 

component, _, levelStr = values.partition("=") 

if not levelStr: 

levelStr, component = component, None 

logLevelUpr = levelStr.upper() 

if logLevelUpr not in self.permittedLevels: 

parser.error("loglevel=%s not one of %s" % (levelStr, tuple(self.permittedLevels))) 

dest.append((component, logLevelUpr)) 

 

 

def _inputCollectionType(value): 

"""Special argument type for input collections. 

 

Accepts string vlues in format: 

 

value :== collection[,collection[...]] 

collection :== [dataset_type:]collection_name 

 

and converts value into a dictionary whose keys a dataset type names 

(or empty string when dataset type name is missing) and values are 

ordered lists of collection names. 

 

Parameters 

---------- 

value : `str` 

Value of the command line option 

 

Returns 

------- 

`dict` 

""" 

res = {} 

for collstr in value.split(","): 

dsType, sep, coll = collstr.partition(':') 

if not sep: 

dsType, coll = "", dsType 

res.setdefault(dsType, []).append(coll) 

return res 

 

 

def _outputCollectionType(value): 

"""Special argument type for input collections. 

 

Accepts string vlues in format: 

 

value :== collection[,collection[...]] 

collection :== [dataset_type:]collection_name 

 

and converts value into a dictionary whose keys a dataset type names 

(or empty string when dataset type name is missing) and values are 

collection names. 

 

Parameters 

---------- 

value : `str` 

Value of the command line option 

 

Returns 

------- 

`dict` 

 

Raises 

------ 

`ValueError` if there is more than one collection per dataset type. 

""" 

res = {} 

for collstr in value.split(","): 

dsType, sep, coll = collstr.partition(':') 

if not sep: 

dsType, coll = "", dsType 

if dsType in res: 

raise ValueError("multiple collection names: " + value) 

res[dsType] = coll 

return res 

 

 

_EPILOG = """\ 

Notes: 

* many options can appear multiple times; all values are used, in order 

left to right 

* @file reads command-line options from the specified file: 

* data may be distributed among multiple lines (e.g. one option per line) 

* data after # is treated as a comment and ignored 

* blank lines and lines starting with # are ignored 

""" 

 

# ------------------------ 

# Exported definitions -- 

# ------------------------ 

 

 

def makeParser(fromfile_prefix_chars='@', parser_class=ArgumentParser, **kwargs): 

"""Make instance of command line parser for `CmdLineFwk`. 

 

Creates instance of parser populated with all options that are supported 

by command line activator. There is no additional logic in this class, 

all semantics is handled by the activator class. 

 

Parameters 

---------- 

fromfile_prefix_chars : `str`, optional 

Prefix for arguments to be used as options files (default: `@`) 

parser_class : `type`, optional 

Specifies the class of the argument parser, by default 

`ArgumentParser` is used. 

kwargs : extra keyword arguments 

Passed directly to `parser_class` constructor 

 

Returns 

------- 

instance of `parser_class` 

""" 

 

parser = parser_class(usage="%(prog)s [global-options] subcommand [command-options]", 

fromfile_prefix_chars=fromfile_prefix_chars, 

epilog=_EPILOG, 

formatter_class=RawDescriptionHelpFormatter, 

**kwargs) 

 

# global options which come before sub-command 

 

group = parser.add_argument_group("Task search options") 

group.add_argument("-p", "--package", action="append", dest="packages", default=[], 

metavar="NAME1.NAME2.NAME3", 

help=("Package to search for task classes. Package name is specified as " 

"dot-separated names found in $PYTHON PATH (e.g. lsst.pipe.tasks). " 

"It should not include module name. This option overrides default " 

"built-in list of modules. It can be used multiple times.")) 

 

# butler options 

group = parser.add_argument_group("Data repository and selection options") 

group.add_argument("-b", "--butler-config", dest="butler_config", default=None, metavar="PATH", 

help="Location of the gen3 butler/registry config file.") 

group.add_argument("-i", "--input", dest="input", type=_inputCollectionType, 

metavar="COLL,DSTYPE:COLL", default={}, 

help=("Comma-separated names of the data butler collection. " 

"If collection includes dataset type name separated by colon " 

"then collection is only used for that specific dataset type. " 

"Pre-flight uses these collections to search for input datasets. " 

"Task execution stage only uses first global collection name " 

"to override collection specified in Butler configuration file.")) 

group.add_argument("-o", "--output", dest="output", type=_outputCollectionType, 

metavar="COLL,DSTYPE:COLL", default={}, 

help=("Comma-separated names of the data butler collection. " 

"See description of --input option. This option only allows " 

"single collection (per-dataset type or global).")) 

group.add_argument("-d", "--data-query", dest="data_query", default="", metavar="QUERY", 

help="User data selection expression.") 

 

# output options 

group = parser.add_argument_group("Meta-information output options") 

group.add_argument("--clobber-config", action="store_true", dest="clobberConfig", default=False, 

help=("backup and then overwrite existing config files instead of checking them " 

"(safe with -j, but not all other forms of parallel execution)")) 

group.add_argument("--no-backup-config", action="store_true", dest="noBackupConfig", default=False, 

help="Don't copy config to file~N backup.") 

group.add_argument("--clobber-versions", action="store_true", dest="clobberVersions", default=False, 

help=("backup and then overwrite existing package versions instead of checking" 

"them (safe with -j, but not all other forms of parallel execution)")) 

group.add_argument("--no-versions", action="store_true", dest="noVersions", default=False, 

help="don't check package versions; useful for development") 

 

# logging/debug options 

group = parser.add_argument_group("Execution and logging options") 

group.add_argument("-L", "--loglevel", action=_LogLevelAction, default=[], 

help="logging level; supported levels are [trace|debug|info|warn|error|fatal]", 

metavar="LEVEL|COMPONENT=LEVEL") 

group.add_argument("--longlog", action="store_true", help="use a more verbose format for the logging") 

group.add_argument("--debug", action="store_true", help="enable debugging output?") 

group.add_argument("--doraise", action="store_true", 

help="raise an exception on error (else log a message and continue)?") 

group.add_argument("--profile", metavar="PATH", help="Dump cProfile statistics to filename") 

 

# parallelism options 

group.add_argument("-j", "--processes", type=int, default=1, help="Number of processes to use") 

group.add_argument("-t", "--timeout", type=float, 

help="Timeout for multiprocessing; maximum wall time (sec)") 

 

# define sub-commands 

subparsers = parser.add_subparsers(dest="subcommand", 

title="commands", 

description=("Valid commands, use `<command> --help' to get " 

"more info about each command:"), 

prog=parser.prog) 

# Python3 workaround, see http://bugs.python.org/issue9253#msg186387 

# The issue was fixed in Python 3.6, workaround is not need starting with that version 

subparsers.required = True 

 

# list sub-command 

subparser = subparsers.add_parser("list", 

usage="%(prog)s [options]", 

description="Display information about tasks and where they are " 

"found. If none of the options are specified then --super-tasks " 

"is used by default") 

subparser.set_defaults(subparser=subparser) 

subparser.add_argument("-p", "--packages", dest="show", action="append_const", const="packages", 

help="Shows list of the packages to search for tasks") 

subparser.add_argument("-m", "--modules", dest="show", action="append_const", const='modules', 

help="Shows list of all modules existing in current list of packages") 

subparser.add_argument("-t", "--tasks", dest="show", action="append_const", const="tasks", 

help="Shows list of all tasks (any sub-class of Task) existing" 

" in current list of packages") 

subparser.add_argument("-s", "--super-tasks", dest="show", action="append_const", const="super-tasks", 

help="(default) Shows list of all super-tasks existing in current set of packages") 

subparser.add_argument("--no-headers", dest="show_headers", action="store_false", default=True, 

help="Do not display any headers on output") 

 

for subcommand in ("build", "qgraph", "run"): 

# show/run sub-commands, they are all identical except for the 

# command itself and description 

 

if subcommand == "build": 

description = textwrap.dedent("""\ 

Build and optionally save pipeline definition. 

This does not require input data to be specified.""") 

elif subcommand == "qgraph": 

description = textwrap.dedent("""\ 

Build and optionally save pipeline and quantum graph.""") 

else: 

description = textwrap.dedent("""\ 

Build and execute pipeline and quantum graph.""") 

 

subparser = subparsers.add_parser(subcommand, 

description=description, 

epilog=_EPILOG, 

formatter_class=RawDescriptionHelpFormatter) 

subparser.set_defaults(subparser=subparser, 

pipeline_actions=[]) 

if subcommand in ("qgraph", "run"): 

subparser.add_argument("-g", "--qgraph", dest="qgraph", 

help="Location for a serialized quantum graph definition " 

"(pickle file). If this option is given then all input data " 

"options and pipeline-building options cannot be used.", 

metavar="PATH") 

if subcommand == "run": 

subparser.add_argument("--skip-init-writes", dest="skip_init_writes", default=False, 

action="store_true", 

help="Do not write collection-wide 'init output' datasets (e.g. schemas).") 

subparser.add_argument("--init-only", dest="init_only", default=False, 

action="store_true", 

help=("Do not actually run; just register dataset types and/or save " 

"init outputs.")) 

subparser.add_argument("--register-dataset-types", dest="register_dataset_types", default=False, 

action="store_true", 

help="Register DatasetTypes that do not already exist in the Registry.") 

subparser.add_argument("-p", "--pipeline", dest="pipeline", 

help="Location of a serialized pipeline definition (pickle file).", 

metavar="PATH") 

subparser.add_argument("-t", "--task", metavar="TASK[:LABEL]", 

dest="pipeline_actions", action='append', type=_ACTION_ADD_TASK, 

help="Task name to add to pipeline, can be either full name " 

"with dots including package and module name, or a simple name " 

"to find the class in one of the modules in pre-defined packages " 

"(see --packages option). Task name can be followed by colon and " 

"label name, if label is not given than task base name (class name) " 

"is used as label.") 

subparser.add_argument("-d", "--delete", metavar="LABEL", 

dest="pipeline_actions", action='append', type=_ACTION_DELETE_TASK, 

help="Delete task with given label from pipeline.") 

subparser.add_argument("-m", "--move", metavar="LABEL:NUMBER", 

dest="pipeline_actions", action='append', type=_ACTION_MOVE_TASK, 

help="Move given task to a different position in a pipeline.") 

subparser.add_argument("-l", "--label", metavar="LABEL:NEW_LABEL", 

dest="pipeline_actions", action='append', type=_ACTION_LABEL_TASK, 

help="Change label of a given task.") 

subparser.add_argument("-c", "--config", metavar="LABEL:NAME=VALUE", 

dest="pipeline_actions", action='append', type=_ACTION_CONFIG, 

help="Configuration override(s) for a task with specified label, " 

"e.g. -c task:foo=newfoo -c task:bar.baz=3.") 

subparser.add_argument("-C", "--configfile", metavar="LABEL:PATH", 

dest="pipeline_actions", action='append', type=_ACTION_CONFIG_FILE, 

help="Configuration override file(s), applies to a task with a given label.") 

subparser.add_argument("--dataset-name-substitution", metavar="LABEL:VALUE", 

dest='pipeline_actions', action='append', type=_ACTION_NAME_TEMPLATES, 

help='Configuration name template values, applies to a task with a given ' 

'label.') 

subparser.add_argument("-o", "--order-pipeline", dest="order_pipeline", 

default=False, action="store_true", 

help="Order tasks in pipeline based on their data dependencies, " 

"ordering is performed as last step before saving or executing " 

"pipeline.") 

if subcommand in ("qgraph", "run"): 

subparser.add_argument("--skip-existing", dest="skip_existing", 

default=False, action="store_true", 

help="If all Quantum outputs already exist in output collection " 

"then Qauntum will be excluded from QuantumGraph.") 

subparser.add_argument("-s", "--save-pipeline", dest="save_pipeline", 

help="Location for storing a serialized pipeline definition (pickle file).", 

metavar="PATH") 

if subcommand in ("qgraph", "run"): 

subparser.add_argument("-q", "--save-qgraph", dest="save_qgraph", 

help="Location for storing a serialized quantum graph definition " 

"(pickle file).", 

metavar="PATH") 

subparser.add_argument("--pipeline-dot", dest="pipeline_dot", 

help="Location for storing GraphViz DOT representation of a pipeline.", 

metavar="PATH") 

if subcommand in ("qgraph", "run"): 

subparser.add_argument("--qgraph-dot", dest="qgraph_dot", 

help="Location for storing GraphViz DOT representation of a " 

"quantum graph.", 

metavar="PATH") 

subparser.add_argument("--show", metavar="ITEM|ITEM=VALUE", action="append", default=[], 

help="Dump various info to standard output. Possible items are: " 

"`config', `config=[Task/]<PATTERN>' or " 

"`config=[Task::]<PATTERN>:NOIGNORECASE' to dump configuration " 

"possibly matching given pattern; `history=<FIELD>' to dump " 

"configuration history for a field, field name is specified as " 

"[Task::][SubTask.]Field; `pipeline' to show pipeline composition; " 

"`graph' to show information about quanta; " 

"`tasks' to show task composition.") 

 

return parser