#!/usr/bin/python import sys, os, getopt def usage(): print '' print 'break_into_pieces' print '' print 'takes file with operations (base_file) and breaks them into several files with' print 'a specified number of operations per file (operations_per_file).' print 'The name of the base_file is used for hte output files with an index number added for distinction.' print 'Each operation can spread over multiple lines (number_of_lines_per_operation).' print '' print 'required parameters:' print '--tier (-t) : hit, digi, dst to use the default file names' print '' print 'optional parameters:' print '--help (-h) : help' print '--debug (-d) : debug statements' print '' print '--base_file_name (-b) : base_file with dcache url\'s, default _to_be_staged_srmcp.cpy' print '--operations_per_file (-f) : number of operations per file, default: 30' print '--lines_per_operation (-l) : number of lines per operation, default: 1' print '' print '--base_is_executable (-e) : base_file is a script and should be broken into scripts' print '' print '--srmcp (-s) : srmcp mode, write main script which calls breaked files as -copyjobfile for srmcp' print '--srmcp_file_name (-n) : file_name of srmcp main script, default: _stage_script.sh' print '' def main(argv) : # default tier = '' base_file_name = '' operations_per_file = 30 lines_per_operation = 1 srmcp = 0 srmcp_file_name = '' base_is_executable = 0 try: opts, args = getopt.getopt(argv, "hdt:b:f:l:sn:e", ["help", "debug", "tier=", "base_file_name=", "operations_per_file=", "lines_per_operation=", "srmcp", "srmcp_file_name=", "base_is_executable"]) except getopt.GetoptError: usage() sys.exit(2) # check command line parameter for opt, arg in opts : if opt in ("-h","--help") : usage() sys.exit() elif opt in ("-d","--debug") : global _debug _debug = 1 elif opt in ("-t","--tier") : tier = arg elif opt in ("-b","--base_file_name") : base_file_name = arg elif opt in ("-f","--operations_per_file") : operations_per_file = int(arg) elif opt in ("-l","--lines_per_operation") : lines_per_operation = int(arg) elif opt in ("-s","--srmcp") : srmcp = 1 elif opt in ("-n","--srmcp_file_name") : srmcp_file_name = arg elif opt in ("-e","--base_is_executable") : base_is_executable = 1 if base_is_executable == 1 and srmcp == 1: print '' print 'Please either activate srmcp mode or switch on base_is_executable' usage() sys.exit(2) if base_file_name == '': if tier == '' : usage() sys.exit() else : base_file_name = tier+'_to_be_staged_srmcp.cpy' if srmcp == 1 : if srmcp_file_name == '' : if tier == '' : usage() sys.exit() else : srmcp_file_name = tier+'_stage_script.sh' try: base_file = open(base_file_name) except IOError: print 'Could not open file: ',base_file_name sys.exit() if srmcp == 1 : srmcp_file = open(srmcp_file_name,'w') srmcp_file.write('#!/bin/sh\n') base_file_name_template = os.path.splitext(base_file_name)[0] counter = 0 breaker_counter = 0 base_file_line = base_file.readline() if base_is_executable : if base_file_line[0] == '#' : base_file_line = base_file.readline() while base_file_line : if breaker_counter == 0 : base_file_break_name = base_file_name_template + '_' + str(counter) if base_is_executable : base_file_break_name += '.sh' else : base_file_break_name += '.cpy' report_file_break_name = base_file_name_template + '_' + str(counter) + '.rpt' file = open(base_file_break_name,'w') if base_is_executable : file.write('#!/bin/sh\n') counter += 1 breaker_counter = 0 elif breaker_counter == operations_per_file : file.close() if base_is_executable : os.chmod(base_file_break_name,0755) if srmcp == 1 : srmcp_file.write('~/srmclient/bin/srmcp -debug=true -copyjobfile='+base_file_break_name+' -report='+report_file_break_name+'\n') base_file_break_name = base_file_name_template + '_' + str(counter) if base_is_executable : base_file_break_name += '.sh' else : base_file_break_name += '.cpy' report_file_break_name = base_file_name_template + '_' + str(counter) + '.rpt' file = open(base_file_break_name,'w') if base_is_executable : file.write('#!/bin/sh\n') counter += 1 breaker_counter = 0 for i in range(lines_per_operation) : file.write(base_file_line) base_file_line = base_file.readline() breaker_counter += 1 if srmcp == 1 : if os.path.exists(base_file_break_name) : file.close() if base_is_executable : os.chmod(base_file_break_name,0755) srmcp_file.write('~/srmclient/bin/srmcp -debug=true -copyjobfile='+base_file_break_name+' -report='+report_file_break_name+'\n') os.chmod(srmcp_file_name,0755) print 'File: ',base_file_name,' broken up into: ',counter,' files.' if __name__ == '__main__' : main(sys.argv[1:])