()
Import multiple users' mbox files to Gmail.
Import multiple users' mbox files to Gmail.
Import multiple users mbox files to Gmail .
def main():
  """Import multiple users' mbox files to Gmail.

  """
  httplib2.debuglevel = args.httplib2debuglevel
  # Use args.logging_level if defined.
  try:
    logging_level = args.logging_level
  except AttributeError:
    logging_level = 'INFO'

  # Default logging to standard output
  logging.basicConfig(
      level=logging_level,
      format='%(asctime)s %(levelname)s %(funcName)s@%(filename)s %(message)s',
      datefmt='%H:%M:%S')

  # More detailed logging to file
  file_handler = logging.handlers.RotatingFileHandler(args.log,
                                                      maxBytes=1024 * 1024 * 32,
                                                      backupCount=8)
  file_formatter = logging.Formatter(
      '%(asctime)s %(process)d %(levelname)s %(funcName)s '
      '(%(filename)s:%(lineno)d) %(message)s')
  file_formatter.datefmt = '%Y-%m-%dT%H:%M:%S (%z)'
  file_handler.setFormatter(file_formatter)
  logging.getLogger().addHandler(file_handler)

  logging.info('*** Starting %s %s on Python %s ***',
               APPLICATION_NAME,
               APPLICATION_VERSION,
               sys.version)
  logging.info('Arguments:')
  for arg, value in sorted(vars(args).items()):
    logging.info('\t%s: %r', arg, value)

  number_of_labels_imported_without_error = 0
  number_of_labels_imported_with_some_errors = 0
  number_of_labels_failed = 0
  number_of_messages_imported_without_error = 0
  number_of_messages_failed = 0
  number_of_users_imported_without_error = 0
  number_of_users_imported_with_some_errors = 0
  number_of_users_failed = 0

  for username in next(os.walk(unicode(args.dir)))[1]:
    try:
      logging.info('Processing user %s', username)
      try:
        credentials = get_credentials(username)
        http = credentials.authorize(set_user_agent(
            httplib2.Http(),
            '%s-%s' % (APPLICATION_NAME, APPLICATION_VERSION)))
        service = discovery.build('gmail', 'v1', http=http)
      except Exception:
        logging.error("Can't get access token for user %s", username)
        raise

      try:
        results = service.users().labels().list(
            userId=username,
            fields='labels(id,name)').execute(num_retries=args.num_retries)
        labels = results.get('labels', [])
      except Exception:
        logging.error("Can't get labels for user %s", username)
        raise

      try:
        result = process_mbox_files(username, service, labels)
      except Exception:
        logging.error("Can't process mbox files for user %s", username)
        raise
      if result[2] == 0 and result[4] == 0:
        number_of_users_imported_without_error += 1
      elif result[0] > 0 or result[3] > 0:
        number_of_users_imported_with_some_errors += 1
      else:
        number_of_users_failed += 1
      number_of_labels_imported_without_error += result[0]
      number_of_labels_imported_with_some_errors += result[1]
      number_of_labels_failed += result[2]
      number_of_messages_imported_without_error += result[3]
      number_of_messages_failed += result[4]
      logging.info('Done importing user %s. Labels: %d succeeded, %d with some '
                   'errors, %d failed. Messages: %d succeeded, %d failed.',
                   username,
                   result[0],
                   result[1],
                   result[2],
                   result[3],
                   result[4])
    except Exception:
      number_of_users_failed += 1
      logging.exception("Can't process user %s", username)
  logging.info("*** Done importing all users from directory '%s'", args.dir)
  logging.info('*** Import summary:')
  logging.info('    %d users imported with no failures',
               number_of_users_imported_without_error)
  logging.info('    %d users imported with some failures',
               number_of_users_imported_with_some_errors)
  logging.info('    %d users failed',
               number_of_users_failed)
  logging.info('    %d labels (mbox files) imported with no failures',
               number_of_labels_imported_without_error)
  logging.info('    %d labels (mbox files) imported with some failures',
               number_of_labels_imported_with_some_errors)
  logging.info('    %d labels (mbox files) failed',
               number_of_labels_failed)
  logging.info('    %d messages imported successfully',
               number_of_messages_imported_without_error)
  logging.info('    %d messages failed\n',
               number_of_messages_failed)
  if (number_of_messages_failed + number_of_labels_failed +
      number_of_users_failed > 0):
    logging.info('*** Check log file %s for detailed errors.', args.log)
  logging.info('Finished.\n\n')
def main ( ) : httplib2 . debuglevel = args . httplib2debuglevel # Use args.logging_level if defined. try : logging_level = args . logging_level except AttributeError : logging_level = 'INFO' # Default logging to standard output logging . basicConfig ( level = logging_level , format = '%(asctime)s %(levelname)s %(funcName)s@%(filename)s %(message)s' , datefmt = '%H:%M:%S' ) # More detailed logging to file file_handler = logging . handlers . RotatingFileHandler ( args . log , maxBytes = 1024 * 1024 * 32 , backupCount = 8 ) file_formatter = logging . Formatter ( '%(asctime)s %(process)d %(levelname)s %(funcName)s ' '(%(filename)s:%(lineno)d) %(message)s' ) file_formatter . datefmt = '%Y-%m-%dT%H:%M:%S (%z)' file_handler . setFormatter ( file_formatter ) logging . getLogger ( ) . addHandler ( file_handler ) logging . info ( '*** Starting %s %s on Python %s ***' , APPLICATION_NAME , APPLICATION_VERSION , sys . version ) logging . info ( 'Arguments:' ) for arg , value in sorted ( vars ( args ) . items ( ) ) : logging . info ( '\t%s: %r' , arg , value ) number_of_labels_imported_without_error = 0 number_of_labels_imported_with_some_errors = 0 number_of_labels_failed = 0 number_of_messages_imported_without_error = 0 number_of_messages_failed = 0 number_of_users_imported_without_error = 0 number_of_users_imported_with_some_errors = 0 number_of_users_failed = 0 for username in next ( os . walk ( unicode ( args . dir ) ) ) [ 1 ] : try : logging . info ( 'Processing user %s' , username ) try : credentials = get_credentials ( username ) http = credentials . authorize ( set_user_agent ( httplib2 . Http ( ) , '%s-%s' % ( APPLICATION_NAME , APPLICATION_VERSION ) ) ) service = discovery . build ( 'gmail' , 'v1' , http = http ) except Exception : logging . error ( "Can't get access token for user %s" , username ) raise try : results = service . users ( ) . labels ( ) . list ( userId = username , fields = 'labels(id,name)' ) . execute ( num_retries = args . num_retries ) labels = results . get ( 'labels' , [ ] ) except Exception : logging . error ( "Can't get labels for user %s" , username ) raise try : result = process_mbox_files ( username , service , labels ) except Exception : logging . error ( "Can't process mbox files for user %s" , username ) raise if result [ 2 ] == 0 and result [ 4 ] == 0 : number_of_users_imported_without_error += 1 elif result [ 0 ] > 0 or result [ 3 ] > 0 : number_of_users_imported_with_some_errors += 1 else : number_of_users_failed += 1 number_of_labels_imported_without_error += result [ 0 ] number_of_labels_imported_with_some_errors += result [ 1 ] number_of_labels_failed += result [ 2 ] number_of_messages_imported_without_error += result [ 3 ] number_of_messages_failed += result [ 4 ] logging . info ( 'Done importing user %s. Labels: %d succeeded, %d with some ' 'errors, %d failed. Messages: %d succeeded, %d failed.' , username , result [ 0 ] , result [ 1 ] , result [ 2 ] , result [ 3 ] , result [ 4 ] ) except Exception : number_of_users_failed += 1 logging . exception ( "Can't process user %s" , username ) logging . info ( "*** Done importing all users from directory '%s'" , args . dir ) logging . info ( '*** Import summary:' ) logging . info ( '    %d users imported with no failures' , number_of_users_imported_without_error ) logging . info ( '    %d users imported with some failures' , number_of_users_imported_with_some_errors ) logging . info ( '    %d users failed' , number_of_users_failed ) logging . info ( '    %d labels (mbox files) imported with no failures' , number_of_labels_imported_without_error ) logging . info ( '    %d labels (mbox files) imported with some failures' , number_of_labels_imported_with_some_errors ) logging . info ( '    %d labels (mbox files) failed' , number_of_labels_failed ) logging . info ( '    %d messages imported successfully' , number_of_messages_imported_without_error ) logging . info ( '    %d messages failed\n' , number_of_messages_failed ) if ( number_of_messages_failed + number_of_labels_failed + number_of_users_failed > 0 ) : logging . info ( '*** Check log file %s for detailed errors.' , args . log ) logging . info ( 'Finished.\n\n' )