| # Copyright 2017 Google Inc. |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """Download Franky test cases from Google Sheets as a CSV file. |
| """ |
| |
| from googleapiclient import discovery |
| import oauth2client |
| import oauth2client.file |
| import oauth2client.tools |
| import os |
| import re |
| |
| |
| # Permission required to authorize users to access Google Drive. |
| DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive.readonly' |
| |
| # Authoriztion details for accessing Google APIs using OAuth2.0 |
| CLIENT_SECRET_FILE = 'client_secret.json' |
| |
| # Name of the Google Drive API. |
| DRIVE_API_NAME = 'drive' |
| |
| # Version of the Google Drive API. |
| DRIVE_API_VERSION = 'v3' |
| |
| # Franky test cases Spreadsheet ID. |
| FRANKY_SPREADSHEET_KEY = '13OFTNzXpBqcLcGEXEYOA6OwFv0whuAQ3GwYuXti4uKE' |
| |
| # URL for downloading spreadsheet as CSV. |
| SPREADSHEET_CSV_URL_TEMPLATE = ('https://docs.google.com/spreadsheets/export?' |
| 'id={key}&format=csv&gid={id}') |
| |
| # CSV filename format of the franky testcases. |
| CSV_OUTPUT_FILE_NAME_TEMPLATE = 'P%d-%s.csv' |
| |
| FILE_NAME_PATTERN = r'filename="(.*)"' |
| TIME_STAMP_PATTERN = r'date\': \'(.*) GMT' |
| |
| # Sheets IDs for P0, P1 and P2 for Canary and Beta, required for |
| # downloading in CSV format. |
| FRANKY_TEST_CASES_SHEET_KEYS = { |
| 'Canary': ['0', '401970238', '17308040'], |
| 'Beta': ['1745684198', '187391865', '840609477'] |
| } |
| |
| |
| def get_credentials(): |
| """Gets valid user credentials from storage. |
| |
| If nothing has been stored, or if the stored credentials are invalid, |
| the OAuth2 flow is completed to obtain the new credentials. |
| |
| Returns: |
| Credentials, the obtained credential. |
| """ |
| home_dir = os.path.expanduser('~') |
| credential_dir = os.path.join(home_dir, '.credentials') |
| if not os.path.exists(credential_dir): |
| os.makedirs(credential_dir) |
| credential_path = os.path.join(credential_dir, 'credentials.json') |
| store = oauth2client.file.Storage(credential_path) |
| credentials = store.get() |
| if not credentials or credentials.invalid: |
| flow = oauth2client.client.flow_from_clientsecrets(CLIENT_SECRET_FILE, |
| DRIVE_SCOPE) |
| credentials = oauth2client.tools.run_flow(flow, store) |
| return credentials |
| |
| |
| def DownloadCSVFromGoogleSheet(channel): |
| """Download Franky test cases spreadsheet as CSV file into current directory. |
| |
| Args: |
| channel: A string indicating the Chrome channel to run tests against. |
| Example, Canary or Beta. |
| """ |
| if not (channel == 'Canary' or channel == 'Beta'): |
| print 'Invalid channel name. Channel name should be \'Canary\' or \'Beta\'' |
| return |
| credentials = get_credentials() |
| drive_service = discovery.build(DRIVE_API_NAME, DRIVE_API_VERSION, |
| credentials=credentials) |
| for sheet_id in FRANKY_TEST_CASES_SHEET_KEYS[channel]: |
| response, content = drive_service._http.request( |
| SPREADSHEET_CSV_URL_TEMPLATE.format(key=FRANKY_SPREADSHEET_KEY, |
| id=sheet_id)) |
| if response.status == 200: |
| csv_file_name = re.search(FILE_NAME_PATTERN, str(response)).group(1) |
| timestamp = re.search(TIME_STAMP_PATTERN, str(response)).group(1) |
| with open('%s %s' % (timestamp, csv_file_name), 'w') as csv_file: |
| csv_file.write(content) |
| else: |
| print 'An error occurred with the status code: %s' % response.status |