Changeset 365


Ignore:
Timestamp:
04/19/18 15:36:50 (6 years ago)
Author:
martijk
Message:

removed rewritten print function

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/examples/new_rack_pbsmon.py.in

    r363 r365  
    109109    TERMINAL_COLOR=False
    110110
    111 ####
    112 ## Rewriting the print function, so it will work with all versions of Python
    113 def _print(*args, **kwargs):
    114     '''A wrapper function to make the functionality for the print function the same for Python2.4 and higher'''
    115     ## First try if we are running in Python3 and higher
    116     try:
    117         Print = eval('print')
    118         Print(*args, **kwargs)
    119     except SyntaxError:
    120         ## Then Python2.6 and Python2.7
    121         try:
    122             D = dict()
    123             exec('from __future__ import print_function\np=print', D)
    124             D['p'](*args, **kwargs)
    125             del D
    126         ## Finally Python2.5 or lower
    127         except SyntaxError:
    128             del D
    129             fout    = kwargs.get('file', sys.stdout)
    130             write   = fout.write
    131             if args:
    132                 write(str(args[0]))
    133                 sep = kwargs.get('sep', ' ')
    134                 for arg in args[1:]:
    135                     write(sep)
    136                     write(str(a))
    137                 write(kwargs.get('end', '\n'))
    138 
    139111class color:
    140112    PURPLE = '\033[95m'
     
    152124    return not any(x not in list_b for x in list_a)
    153125
    154 ## Import argparse here, as I need the _print function
     126## Import argparse here, as I need the print function
    155127try:
    156128    import argparse
    157129except ImportError:
    158     _print('Cannot find argparse module', file=sys.stderr)
     130    print('Cannot find argparse module', file=sys.stderr)
    159131    sys.exit(1)
    160132
     
    301273            p = PBSQuery( OPT_SERVERNAME )
    302274    except PBSError, reason:
    303         _print('Error: %s' % reason)
     275        print('Error: %s' % reason)
    304276        sys.exit( -1 )
    305277
     
    311283        nodes = p.getnodes( attr )
    312284    except PBSError, reason:
    313         _print('Error: %s' % reason)
     285        print('Error: %s' % reason)
    314286        sys.exit( -1 )
    315287
     
    435407    save_column = None
    436408   
    437     _print()   
    438     _print('  ', end=' ')
     409    print()   
     410    print('  ', end=' ')
    439411    for rack in xrange( START_RACK, racknr + 1 ):
    440412       
     
    450422                    char = save_column
    451423                    save_column = None
    452                 _print(char, end=' ')
    453         else:
    454             _print(char, end=' ')
    455     _print()   
    456 
    457     _print('  ', end=' ')
     424                print(char, end=' ')
     425        else:
     426            print(char, end=' ')
     427    print()   
     428
     429    print('  ', end=' ')
    458430    for rack in xrange( START_RACK, racknr + 1 ):
    459431       
     
    461433        if OPT_SKIP_EMPTY_RACKS:
    462434            if nodes.has_key( rack ):
    463                 _print(char, end=' ')
    464         else:
    465             _print(char, end=' ')
    466     _print()
     435                print(char, end=' ')
     436        else:
     437            print(char, end=' ')
     438    print()
    467439
    468440    for node in xrange( 1, nodenr + 1 ):
    469         _print('%2d' % node, end=' ')
     441        print('%2d' % node, end=' ')
    470442
    471443        for rack in xrange( START_RACK, racknr + 1 ):
     
    486458                if prop_color or job_color:
    487459                    if TERMINAL_COLOR and prop_color and job_color:
    488                         _print(color.GREEN + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
     460                        print(color.GREEN + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
    489461                    elif TERMINAL_COLOR and  prop_color:
    490                         _print(color.BLUE + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
     462                        print(color.BLUE + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
    491463                    elif TERMINAL_COLOR and  job_color:
    492                         _print(color.YELLOW + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
     464                        print(color.YELLOW + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
    493465                    else:
    494                         _print('M', end=' ')
     466                        print('M', end=' ')
    495467                else:
    496                     _print(nodes[ rack ][ node ][ 'state_char' ], end=' ')
     468                    print(nodes[ rack ][ node ][ 'state_char' ], end=' ')
    497469            except KeyError:
    498                 _print(' ', end=' ')
    499         _print()
    500     _print()
     470                print(' ', end=' ')
     471        print()
     472    print()
    501473
    502474def print_table_summary():
     
    510482            p = PBSQuery( OPT_SERVERNAME )
    511483    except PBSError, reason:
    512         _print('error: %s' % reason)
     484        print('error: %s' % reason)
    513485        sys.exit(-1)
    514486
     
    518490        nodes = p.getnodes(attr)
    519491    except PBSError, reason:
    520         _print('error: %s' % reason)
     492        print('error: %s' % reason)
    521493        sys.exit(-1)
    522494
     
    542514        if node.is_free():                          # can happen for single CPU jobs
    543515            if node.has_job():
    544 #               _print('TD: %s' % nodename, node)
     516#               print('TD: %s' % nodename, node)
    545517                state_char = PBS_STATES[pbs_ND_single]
    546518                count_states[pbs.ND_free] -=  1
     
    566538    n = 0
    567539    for state in legend:
    568         _print('  %s  %-13s : %-5d' % (PBS_STATES[state], state, count_states[state]), end=' ')
     540        print('  %s  %-13s : %-5d' % (PBS_STATES[state], state, count_states[state]), end=' ')
    569541
    570542        n = n + 1
    571543        if not (n & 1):
    572             _print()
     544            print()
    573545 
    574546    if TERMINAL_COLOR:
    575         _print()
    576         _print('Colors has been enabled for your terminal:')
    577         _print(' - ' + color.YELLOW + 'Matched jobs' + color.END)
    578         _print(' - ' + color.BLUE + 'Matched properties' + color.END)
    579         _print(' - ' + color.GREEN + 'Matched jobs and properties' + color.END)
     547        print()
     548        print('Colors has been enabled for your terminal:')
     549        print(' - ' + color.YELLOW + 'Matched jobs' + color.END)
     550        print(' - ' + color.BLUE + 'Matched properties' + color.END)
     551        print(' - ' + color.GREEN + 'Matched jobs and properties' + color.END)
    580552
    581553def print_extended(hosts=None, properties=None, jobs=None):
     
    606578        rows_str.append( row_str )
    607579
    608     _print()
    609     _print(row_header)
    610     _print(EXTENDED_PATTERNS[ 'line' ] % ( EXTENDED_PATTERNS[ 'line_char' ] * LENGTH_ROW ))
    611     _print('\n'.join( rows_str ))
    612     _print()
     580    print()
     581    print(row_header)
     582    print(EXTENDED_PATTERNS[ 'line' ] % ( EXTENDED_PATTERNS[ 'line_char' ] * LENGTH_ROW ))
     583    print('\n'.join( rows_str ))
     584    print()
    613585
    614586if __name__ == '__main__':
     
    661633        print_table(args.properties, args.jobs)
    662634    else:
    663         _print('Something is wrong, bye!', file=sys.stderr)
     635        print('Something is wrong, bye!', file=sys.stderr)
    664636        sys.exit( -1 )
    665637
  • trunk/examples/sara_nodes.py.in

    r363 r365  
    3333ARGS_DRYRUN     = False
    3434
    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
    6536try:
    6637    import argparse
    6738except ImportError:
    68     _print('Cannot find argparse module', file=sys.stderr)
     39    print('Cannot find argparse module', file=sys.stderr)
    6940    sys.exit(1)
    7041
     
    196167    '''This function retrieves the information from your batch environment'''
    197168    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)
    199170
    200171    ## there are 2 possible filters, by hostname, or by state
     
    204175
    205176    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)
    207178    ## We ask from the batch all nodes, and with the properties state and note
    208179    for host, properties in pbsq.getnodes(['state', 'note']).items():
     
    226197   
    227198    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)
    229200    return split_1, split_2
    230201
     
    232203    '''This function processes the data from the batch system and make it for all hosts the same layout'''
    233204    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)
    239210
    240211    ## Generate a list containing a dictionary, so we can use the stringformatting functionality
     
    275246            add_dict['date'] = add_dict['date_add'] = add_dict['date_edit'] = add_dict['username'] = add_dict['ticket'] = add_dict['note'] = ''
    276247
    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_print
     248        lineprint.append(add_dict)
     249
     250    if ARGS_VERBOSE:
     251        print('func:print_process_dict returning values', file=sys.stderr)
     252    return lineprint
    282253
    283254def print_create_list(values):
     
    290261    '''Print the default overview'''
    291262    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)
    293264
    294265    ## Determine some default values for the column width
     
    339310            show_fields.append([w_note,'Note'])
    340311
    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 ]))
    343314
    344315    ## Show the information to the user
     
    358329            show_line_fields.append([w_note,line['note']])
    359330
    360         _print(' %s' % ' | '.join(print_create_list(show_line_fields)))
     331        print(' %s' % ' | '.join(print_create_list(show_line_fields)))
    361332
    362333def print_overview_format(hosts=None, format=None):
     
    369340
    370341    for line in print_list:
    371         _print(format % line)
     342        print(format % line)
    372343## END functions for printing
    373344####
     
    381352        '''A function to retrieve the current message'''
    382353        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)
    384355
    385356        pbsq = PBSQuery.PBSQuery()
     
    395366        '''Returns the current time'''
    396367        if ARGS_VERBOSE:
    397             _print('class:SaraNodes func:_get_curdate', file=sys.stderr)
     368            print('class:SaraNodes func:_get_curdate', file=sys.stderr)
    398369        return time.strftime('%d-%m %H:%M', time.localtime())
    399370
     
    401372        '''Get the username'''
    402373        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)
    404375        cur_uid = getpass.getuser()
    405376        if prev_uid and cur_uid == 'root':
     
    410381        '''Check if we already have a ticket number'''
    411382        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)
    413384        cur_ticket = '%s' % self.ticket
    414385        if prev_ticket and cur_ticket == prev_ticket:
     
    425396        '''Generates the node in a specific format'''
    426397        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)
    428399
    429400        ## First step, is to get the current info of a host
     
    457428
    458429        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)
    460431        attributes          = pbs.new_attropl(2)
    461432        attributes[0].name  = pbs.ATTR_NODE_state
     
    478449
    479450        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)
    481452        attributes          = pbs.new_attropl(2)
    482453        attributes[0].name  = pbs.ATTR_NODE_state
     
    499470
    500471        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)
    502473        attributes          = pbs.new_attropl(1)
    503474        attributes[0].name  = pbs.ATTR_NODE_note
     
    517488
    518489        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)
    520491        attributes          = pbs.new_attropl(1)
    521492        attributes[0].name  = pbs.ATTR_NODE_note
     
    535506
    536507        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)
    538509
    539510        ## Always get the pbs_server name, even in dry-run mode
    540511        pbs_server = pbs.pbs_default()
    541512        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)
    543514            sys.exit(1)
    544515
    545516        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)
    547518
    548519        ## If dry-run is not specified create a connection
     
    557528                if rcode > 0:
    558529                    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)
    560531            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])))
    562533
    563534        ## Close the connection with the batch system
     
    596567
    597568    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)
    599570
    600571    if args.nodes and args.jobs:
     
    606577    if args.offline or args.modify or args.clear or args.clear_note or args.ticket:
    607578        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)
    609580            sys.exit(1)
    610581
     
    615586        if args.offline:
    616587            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)
    618589            sn.do_offline(args.nodes, args.offline)
    619590        elif args.modify:
    620591            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)
    622593            sn.do_modify(args.nodes, args.modify)
    623594        elif args.clear:
    624595            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)
    626597            sn.do_clear(args.nodes)
    627598        elif args.clear_note:
    628599            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)
    630601            sn.do_clear_note(args.nodes)
    631602        elif args.ticket:
    632603            if ARGS_VERBOSE:
    633                 _print('func:__main__ call sn.do_modify')
     604                print('func:__main__ call sn.do_modify')
    634605            sn.do_offline(args.nodes, '')
    635606    else:
    636607        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)
    638609
    639610        if args.format:
    640611            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)
    642613            print_overview_format(args.nodes, args.format)
    643614        else:
    644615            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)
    646617            print_overview_normal(args.nodes)
    647618   
    648619    if ARGS_VERBOSE:
    649         _print('func:__main__ exit', file=sys.stderr)
     620        print('func:__main__ exit', file=sys.stderr)
  • trunk/release.json

    r348 r365  
    11{
    2   "version": "4.6.1",
    3   "changes": "Support added for torque 5.X (rm interface does not work), PBSQuery had been update to new cpuid range for torque 5 and  new_rack_pbsmon.py has an new option -j/--job. It will show the nodes that are used by the specified job ",
     2  "version": "4.6.4",
     3  "changes": "Use system Python instead of environment Python",
    44  "state": "stable",
    55  "scope": "bug fixes",
    6   "download": "ftp://ftp.surfsara.nl/pub/outgoing/pbs_python-4.6.1.tar.gz"
     6  "download": "ftp://ftp.surfsara.nl/pub/outgoing/pbs_python-4.6.4.tar.gz"
    77}
Note: See TracChangeset for help on using the changeset viewer.