def testMainEmailPassword(self):
        """Verify that running main with email/password follows flow."""
        csv = 'any.csv'
        email = '*****@*****.**'
        password = '******'

        mocked_creds = self.mox.CreateMock(gdata_lib.Creds)
        creds_file = 'non-existing-file'

        self.mox.StubOutWithMock(ups, 'PrepareCreds')
        self.mox.StubOutWithMock(ups, 'LoadTable')
        self.mox.StubOutWithMock(mps, 'FinalizeTable')
        self.mox.StubOutWithMock(ups.Uploader, 'Upload')

        ups.PrepareCreds(creds_file, None, email,
                         password).AndReturn(mocked_creds)
        ups.LoadTable(csv).AndReturn('csv_table')
        mps.FinalizeTable('csv_table')
        ups.Uploader.Upload(mox.IgnoreArg(), ws_name='Packages')
        ups.Uploader.Upload(mox.IgnoreArg(), ws_name='Dependencies')
        mocked_creds.StoreCredsIfNeeded(creds_file)
        self.mox.ReplayAll()

        ups.main([
            '--email=%s' % email,
            '--password=%s' % password,
            '--cred-file=%s' % creds_file, csv
        ])

        self.mox.VerifyAll()
    def testMainTokenFile(self):
        """Verify that running main with token file follows flow."""
        csv = 'any.csv'
        token_file = self.tempfile
        creds_file = 'non-existing-file'

        mocked_creds = self.mox.CreateMock(gdata_lib.Creds)
        mocked_creds.auth_token_loaded = True

        self.mox.StubOutWithMock(ups, 'PrepareCreds')
        self.mox.StubOutWithMock(ups, 'LoadTable')
        self.mox.StubOutWithMock(mps, 'FinalizeTable')
        self.mox.StubOutWithMock(ups.Uploader, 'Upload')

        ups.PrepareCreds(creds_file, token_file, None,
                         None).AndReturn(mocked_creds)
        ups.LoadTable(csv).AndReturn('csv_table')
        mps.FinalizeTable('csv_table')
        ups.Uploader.Upload(mox.IgnoreArg(), ws_name=ups.PKGS_WS_NAME)
        ups.Uploader.Upload(mox.IgnoreArg(), ws_name=ups.DEPS_WS_NAME)
        mocked_creds.StoreCredsIfNeeded(creds_file)
        mocked_creds.StoreAuthTokenIfNeeded(token_file)
        self.mox.ReplayAll()

        ups.main([
            '--cred-file=%s' % creds_file,
            '--auth-token-file=%s' % token_file, csv
        ])

        self.mox.VerifyAll()
  def testFinalizeTable(self):
    self.assertEqual(3, self._table.GetNumRows())
    self.assertEqual(len(self.COLUMNS), self._table.GetNumColumns())

    with self.OutputCapturer():
      mps.FinalizeTable(self._table)

    self.assertEqual(3, self._table.GetNumRows())
    self.assertEqual(len(self.COLUMNS) + 3, self._table.GetNumColumns())

    final_rows = (self.ROW0_FINAL, self.ROW1_FINAL, self.ROW2_FINAL)
    for ix, row_out in enumerate(final_rows):
      self.assertRowsEqual(row_out, self._table[ix])
Beispiel #4
0
def main(argv):
  """Main function."""
  usage = 'Usage: %prog [options] csv_file'
  parser = optparse.OptionParser(usage=usage)
  parser.add_option('--auth-token-file', dest='token_file', type='string',
                    action='store', default=None,
                    help='File for reading/writing Docs auth token.')
  parser.add_option('--cred-file', dest='cred_file', type='string',
                    action='store', default=None,
                    help='File for reading/writing Docs login email/password.')
  parser.add_option('--email', dest='email', type='string',
                    action='store', default=None,
                    help='Email for Google Doc user')
  parser.add_option('--password', dest='password', type='string',
                    action='store', default=None,
                    help='Password for Google Doc user')
  parser.add_option('--ss-key', dest='ss_key', type='string',
                    action='store', default=None,
                    help='Key of spreadsheet to upload to')
  parser.add_option('--test-spreadsheet', dest='test_ss',
                    action='store_true', default=False,
                    help='Upload to the testing spreadsheet.')
  parser.add_option('--verbose', dest='verbose',
                    action='store_true', default=False,
                    help='Show details about packages.')

  (options, args) = parser.parse_args(argv)

  oper.verbose = options.verbose

  if len(args) < 1:
    parser.print_help()
    oper.Die('One csv_file is required.')

  # If email or password provided, the other is required.  If neither is
  # provided, then either token_file or cred_file must be provided and
  # be a real file.
  if options.email or options.password:
    if not (options.email and options.password):
      parser.print_help()
      oper.Die('The email/password options must be used together.')
  elif not ((options.cred_file and os.path.exists(options.cred_file)) or
            (options.token_file and os.path.exists(options.token_file))):
    parser.print_help()
    oper.Die('Without email/password, cred-file or auth-token-file'
             'must exist.')

  # --ss-key and --test-spreadsheet are mutually exclusive.
  if options.ss_key and options.test_ss:
    parser.print_help()
    oper.Die('Cannot specify --ss-key and --test-spreadsheet together.')

  # Prepare credentials for spreadsheet access.
  creds = PrepareCreds(options.cred_file, options.token_file,
                       options.email, options.password)

  # Load the given csv file.
  csv_table = LoadTable(args[0])

  # Prepare table for upload.
  mps.FinalizeTable(csv_table)

  # Prepare the Google Doc client for uploading.
  uploader = Uploader(creds, csv_table)

  ss_key = options.ss_key
  ws_names = [PKGS_WS_NAME, DEPS_WS_NAME]
  if not ss_key:
    if options.test_ss:
      ss_key = TEST_SS_KEY # For testing with backup spreadsheet
    else:
      ss_key = REAL_SS_KEY

  for ws_name in ws_names:
    uploader.Upload(ss_key, ws_name=ws_name)

  # If cred_file given and new credentials were used then write
  # credentials out to that location.
  if options.cred_file:
    creds.StoreCredsIfNeeded(options.cred_file)

  # If token_file path given and new auth token was used then
  # write auth_token out to that location.
  if options.token_file:
    creds.StoreAuthTokenIfNeeded(options.token_file)