Changeset 365 for trunk/examples/sara_nodes.py.in
- Timestamp:
- 04/19/18 15:36:50 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/examples/sara_nodes.py.in
r363 r365 33 33 ARGS_DRYRUN = False 34 34 35 #### 36 ## Rewriting the print function, so it will work with all versions of Python 37 def _print(*args, **kwargs): 38 '''A wrapper function to make the functionality for the print function the same for Python2.4 and higher''' 39 40 ## First try if we are running in Python3 and higher 41 try: 42 Print = eval('print') 43 Print(*args, **kwargs) 44 except SyntaxError: 45 ## Then Python2.6 and Python2.7 46 try: 47 D = dict() 48 exec('from __future__ import print_function\np=print', D) 49 D['p'](*args, **kwargs) 50 del D 51 ## Finally Python2.5 or lower 52 except SyntaxError: 53 del D 54 fout = kwargs.get('file', sys.stdout) 55 write = fout.write 56 if args: 57 write(str(args[0])) 58 sep = kwargs.get('sep', ' ') 59 for arg in args[1:]: 60 write(sep) 61 write(str(a)) 62 write(kwargs.get('end', '\n')) 63 64 ## Import argparse here, as I need the _print function 35 ## Import argparse here, as I need the print function 65 36 try: 66 37 import argparse 67 38 except ImportError: 68 _print('Cannot find argparse module', file=sys.stderr)39 print('Cannot find argparse module', file=sys.stderr) 69 40 sys.exit(1) 70 41 … … 196 167 '''This function retrieves the information from your batch environment''' 197 168 if ARGS_VERBOSE: 198 _print('func:print_get_nodes input:%s' % str(hosts), file=sys.stderr)169 print('func:print_get_nodes input:%s' % str(hosts), file=sys.stderr) 199 170 200 171 ## there are 2 possible filters, by hostname, or by state … … 204 175 205 176 if ARGS_VERBOSE: 206 _print('func:print_get_nodes fetching node information', file=sys.stderr)177 print('func:print_get_nodes fetching node information', file=sys.stderr) 207 178 ## We ask from the batch all nodes, and with the properties state and note 208 179 for host, properties in pbsq.getnodes(['state', 'note']).items(): … … 226 197 227 198 if ARGS_VERBOSE: 228 _print('func:print_get_nodes returning values', file=sys.stderr)199 print('func:print_get_nodes returning values', file=sys.stderr) 229 200 return split_1, split_2 230 201 … … 232 203 '''This function processes the data from the batch system and make it for all hosts the same layout''' 233 204 if ARGS_VERBOSE: 234 _print('func:print_process_dict input:%s' % str(dictin), file=sys.stderr)235 236 line _print = list()237 if ARGS_VERBOSE: 238 _print('func:print_process_dict processing data', file=sys.stderr)205 print('func:print_process_dict input:%s' % str(dictin), file=sys.stderr) 206 207 lineprint = list() 208 if ARGS_VERBOSE: 209 print('func:print_process_dict processing data', file=sys.stderr) 239 210 240 211 ## Generate a list containing a dictionary, so we can use the stringformatting functionality … … 275 246 add_dict['date'] = add_dict['date_add'] = add_dict['date_edit'] = add_dict['username'] = add_dict['ticket'] = add_dict['note'] = '' 276 247 277 line _print.append(add_dict)278 279 if ARGS_VERBOSE: 280 _print('func:print_process_dict returning values', file=sys.stderr)281 return line _print248 lineprint.append(add_dict) 249 250 if ARGS_VERBOSE: 251 print('func:print_process_dict returning values', file=sys.stderr) 252 return lineprint 282 253 283 254 def print_create_list(values): … … 290 261 '''Print the default overview''' 291 262 if ARGS_VERBOSE: 292 _print('func:print_overview_normal input:%s' % str(hosts), file=sys.stderr)263 print('func:print_overview_normal input:%s' % str(hosts), file=sys.stderr) 293 264 294 265 ## Determine some default values for the column width … … 339 310 show_fields.append([w_note,'Note']) 340 311 341 _print(' %s' % ' | '.join(print_create_list(show_fields)))342 _print('+'.join([ '-' * (show_field[0]+2) for show_field in show_fields ]))312 print(' %s' % ' | '.join(print_create_list(show_fields))) 313 print('+'.join([ '-' * (show_field[0]+2) for show_field in show_fields ])) 343 314 344 315 ## Show the information to the user … … 358 329 show_line_fields.append([w_note,line['note']]) 359 330 360 _print(' %s' % ' | '.join(print_create_list(show_line_fields)))331 print(' %s' % ' | '.join(print_create_list(show_line_fields))) 361 332 362 333 def print_overview_format(hosts=None, format=None): … … 369 340 370 341 for line in print_list: 371 _print(format % line)342 print(format % line) 372 343 ## END functions for printing 373 344 #### … … 381 352 '''A function to retrieve the current message''' 382 353 if ARGS_VERBOSE: 383 _print('class:SaraNodes func:_get_current_notes input:%s' % str(nodes), file=sys.stderr)354 print('class:SaraNodes func:_get_current_notes input:%s' % str(nodes), file=sys.stderr) 384 355 385 356 pbsq = PBSQuery.PBSQuery() … … 395 366 '''Returns the current time''' 396 367 if ARGS_VERBOSE: 397 _print('class:SaraNodes func:_get_curdate', file=sys.stderr)368 print('class:SaraNodes func:_get_curdate', file=sys.stderr) 398 369 return time.strftime('%d-%m %H:%M', time.localtime()) 399 370 … … 401 372 '''Get the username''' 402 373 if ARGS_VERBOSE: 403 _print('class:SaraNodes func:_get_uid input:%s' % prev_uid, file=sys.stderr)374 print('class:SaraNodes func:_get_uid input:%s' % prev_uid, file=sys.stderr) 404 375 cur_uid = getpass.getuser() 405 376 if prev_uid and cur_uid == 'root': … … 410 381 '''Check if we already have a ticket number''' 411 382 if ARGS_VERBOSE: 412 _print('class:SaraNodes func:_get_ticket input:%s' % prev_ticket, file=sys.stderr)383 print('class:SaraNodes func:_get_ticket input:%s' % prev_ticket, file=sys.stderr) 413 384 cur_ticket = '%s' % self.ticket 414 385 if prev_ticket and cur_ticket == prev_ticket: … … 425 396 '''Generates the node in a specific format''' 426 397 if ARGS_VERBOSE: 427 _print('class:SaraNodes func:_generate_note input:%s,%s,%s' % (str(nodes), note, str(append)), file=sys.stderr)398 print('class:SaraNodes func:_generate_note input:%s,%s,%s' % (str(nodes), note, str(append)), file=sys.stderr) 428 399 429 400 ## First step, is to get the current info of a host … … 457 428 458 429 if ARGS_VERBOSE: 459 _print('class:SaraNodes func:do_offline input:%s,%s' % (str(nodes), note), file=sys.stderr)430 print('class:SaraNodes func:do_offline input:%s,%s' % (str(nodes), note), file=sys.stderr) 460 431 attributes = pbs.new_attropl(2) 461 432 attributes[0].name = pbs.ATTR_NODE_state … … 478 449 479 450 if ARGS_VERBOSE: 480 _print('class:SaraNodes func:do_clear input:%s' % str(nodes), file=sys.stderr)451 print('class:SaraNodes func:do_clear input:%s' % str(nodes), file=sys.stderr) 481 452 attributes = pbs.new_attropl(2) 482 453 attributes[0].name = pbs.ATTR_NODE_state … … 499 470 500 471 if ARGS_VERBOSE: 501 _print('class:SaraNodes func:do_modify input:%s,%s' % (str(nodes), note), file=sys.stderr)472 print('class:SaraNodes func:do_modify input:%s,%s' % (str(nodes), note), file=sys.stderr) 502 473 attributes = pbs.new_attropl(1) 503 474 attributes[0].name = pbs.ATTR_NODE_note … … 517 488 518 489 if ARGS_VERBOSE: 519 _print('class:SaraNodes func:do_clear_note input:%s' % str(nodes), file=sys.stderr)490 print('class:SaraNodes func:do_clear_note input:%s' % str(nodes), file=sys.stderr) 520 491 attributes = pbs.new_attropl(1) 521 492 attributes[0].name = pbs.ATTR_NODE_note … … 535 506 536 507 if ARGS_VERBOSE: 537 _print('class:SaraNodes func:_process input:%s' % str(batch_list), file=sys.stderr)508 print('class:SaraNodes func:_process input:%s' % str(batch_list), file=sys.stderr) 538 509 539 510 ## Always get the pbs_server name, even in dry-run mode 540 511 pbs_server = pbs.pbs_default() 541 512 if not pbs_server: 542 _print('Could not locate a pbs server', file=sys.stderr)513 print('Could not locate a pbs server', file=sys.stderr) 543 514 sys.exit(1) 544 515 545 516 if ARGS_VERBOSE: 546 _print('class:SaraNodes func:_process pbs_server:%s' % pbs_server, file=sys.stderr)517 print('class:SaraNodes func:_process pbs_server:%s' % pbs_server, file=sys.stderr) 547 518 548 519 ## If dry-run is not specified create a connection … … 557 528 if rcode > 0: 558 529 errno, text = pbs.error() 559 _print('PBS error for node \'%s\': %s (%s)' % (node[0], text, errno), file=sys.stderr)530 print('PBS error for node \'%s\': %s (%s)' % (node[0], text, errno), file=sys.stderr) 560 531 else: 561 _print("pbs.pbs_manager(pbs_connection, pbs.MGR_CMD_SET, pbs.MGR_OBJ_NODE, %s, %s, 'NULL')" % (node[0], str(node[1])))532 print("pbs.pbs_manager(pbs_connection, pbs.MGR_CMD_SET, pbs.MGR_OBJ_NODE, %s, %s, 'NULL')" % (node[0], str(node[1]))) 562 533 563 534 ## Close the connection with the batch system … … 596 567 597 568 if ARGS_VERBOSE: 598 _print('func:__main__ checking type of operation', file=sys.stderr)569 print('func:__main__ checking type of operation', file=sys.stderr) 599 570 600 571 if args.nodes and args.jobs: … … 606 577 if args.offline or args.modify or args.clear or args.clear_note or args.ticket: 607 578 if not args.nodes: 608 _print('You did not specify any nodes, see --help', file=sys.stderr)579 print('You did not specify any nodes, see --help', file=sys.stderr) 609 580 sys.exit(1) 610 581 … … 615 586 if args.offline: 616 587 if ARGS_VERBOSE: 617 _print('func:__main__ call sn.do_offline', file=sys.stderr)588 print('func:__main__ call sn.do_offline', file=sys.stderr) 618 589 sn.do_offline(args.nodes, args.offline) 619 590 elif args.modify: 620 591 if ARGS_VERBOSE: 621 _print('func:__main__ call sn.do_modify', file=sys.stderr)592 print('func:__main__ call sn.do_modify', file=sys.stderr) 622 593 sn.do_modify(args.nodes, args.modify) 623 594 elif args.clear: 624 595 if ARGS_VERBOSE: 625 _print('func:__main__ call sn.do_clear', file=sys.stderr)596 print('func:__main__ call sn.do_clear', file=sys.stderr) 626 597 sn.do_clear(args.nodes) 627 598 elif args.clear_note: 628 599 if ARGS_VERBOSE: 629 _print('func:__main__ call sn.do_clear_note', file=sys.stderr)600 print('func:__main__ call sn.do_clear_note', file=sys.stderr) 630 601 sn.do_clear_note(args.nodes) 631 602 elif args.ticket: 632 603 if ARGS_VERBOSE: 633 _print('func:__main__ call sn.do_modify')604 print('func:__main__ call sn.do_modify') 634 605 sn.do_offline(args.nodes, '') 635 606 else: 636 607 if ARGS_DRYRUN: 637 _print('Dry-run is not available when we use PBSQuery', file=sys.stderr)608 print('Dry-run is not available when we use PBSQuery', file=sys.stderr) 638 609 639 610 if args.format: 640 611 if ARGS_VERBOSE: 641 _print('func:__main__ call print_overview_format', file=sys.stderr)612 print('func:__main__ call print_overview_format', file=sys.stderr) 642 613 print_overview_format(args.nodes, args.format) 643 614 else: 644 615 if ARGS_VERBOSE: 645 _print('func:__main__ call print_overview_normal', file=sys.stderr)616 print('func:__main__ call print_overview_normal', file=sys.stderr) 646 617 print_overview_normal(args.nodes) 647 618 648 619 if ARGS_VERBOSE: 649 _print('func:__main__ exit', file=sys.stderr)620 print('func:__main__ exit', file=sys.stderr)
Note: See TracChangeset
for help on using the changeset viewer.