parameters: (in_path, out_path, desired_size)
docstring: Copies |in_path| to |out_path| such that it is approximately
    |desired_size| bytes large. This is accomplished by dropping the oldest
    events first. The final file size may not be exactly |desired_size| as only
    complete event lines are skipped.
docstring_summary: Copies |in_path| to |out_path| such that it is approximately
    |desired_size| bytes large. This is accomplished by dropping the oldest
    events first. The final file size may not be exactly |desired_size| as only
    complete event lines are skipped.
docstring_tokens: Copies |in_path| to |out_path| such that it is approximately |desired_size| bytes large . This is accomplished by dropping the oldest events first . The final file size may not be exactly |desired_size| as only complete event lines are skipped .
function: def truncate_log_file(in_path, out_path, desired_size):
    '''Copies |in_path| to |out_path| such that it is approximately
    |desired_size| bytes large. This is accomplished by dropping the oldest
    events first. The final file size may not be exactly |desired_size| as only
    complete event lines are skipped.'''
    orig_size = get_file_size(in_path)
    bytes_to_truncate = orig_size - desired_size
  
    # This variable is True if the current line being processed is an Event line.
    inside_events = False
    with open(out_path, 'w') as out_file:
      with open(in_path, 'r') as in_file:
        for line in in_file:
          # The final line before polledData closes the events array, and hence
          # ends in "],". The check for polledData is more for documentation
          # sake.
          if inside_events and (line.startswith('"polledData": {' or
                                line.endswith('],\n'))):
            inside_events = False
  
          # If this is an event line and need to drop more bytes, go ahead and
          # skip the line. Otherwise copy it to the output file.
          if inside_events and bytes_to_truncate > 0:
            bytes_to_truncate -= len(line)
          else:
            out_file.write(line)
  
          # All lines after this are events (up until the closing square
          # bracket).
          if line.startswith('"events": ['):
            inside_events = True
  
    sys.stdout.write(
        'Truncated file from %d to %d bytes\n' % (orig_size,
                                                  get_file_size(out_path)))
function_tokens: def truncate_log_file ( in_path , out_path , desired_size ) : orig_size = get_file_size ( in_path ) bytes_to_truncate = orig_size - desired_size # This variable is True if the current line being processed is an Event line. inside_events = False with open ( out_path , 'w' ) as out_file : with open ( in_path , 'r' ) as in_file : for line in in_file : # The final line before polledData closes the events array, and hence # ends in "],". The check for polledData is more for documentation # sake. if inside_events and ( line . startswith ( '"polledData": {' or line . endswith ( '],\n' ) ) ) : inside_events = False # If this is an event line and need to drop more bytes, go ahead and # skip the line. Otherwise copy it to the output file. if inside_events and bytes_to_truncate > 0 : bytes_to_truncate -= len ( line ) else : out_file . write ( line ) # All lines after this are events (up until the closing square # bracket). if line . startswith ( '"events": [' ) : inside_events = True sys . stdout . write ( 'Truncated file from %d to %d bytes\n' % ( orig_size , get_file_size ( out_path ) ) )