Skip to content

Commit 32fd8ba

Browse files
author
Takashi Matsuo
committed
Merge pull request #6 from GoogleCloudPlatform/tox
Added tox config with flake8 and nosetests.
2 parents e6bd547 + ac005e7 commit 32fd8ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+588
-705
lines changed

.coveragerc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[run]
2+
include =
3+
datastore/*
4+
localtesting/*
5+
bigquery/*
6+
[report]
7+
exclude_lines =
8+
pragma: NO COVER

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
*.pyc
3+
.coverage
4+
.tox
5+
coverage.xml
6+
nosetests.xml
7+
python-docs-samples.json

.travis.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
sudo: false
22
#add language, etc. here
3+
language: python
34

45
cache:
56
directories:
67
- $HOME/gcloud/
78
env:
8-
- PATH=$PATH:$HOME/gcloud/google-cloud-sdk/bin GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/client_secrets.json #Other environment variables on same line
9+
- PATH=$PATH:$HOME/gcloud/google-cloud-sdk/bin GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/python-docs-samples.json PYTHONPATH=${HOME}/gcloud/google-cloud-sdk/platform/google_appengine #Other environment variables on same line
910

1011
before_install:
1112
#ENCRYPT YOUR PRIVATE KEY (If you need authentication)
@@ -27,13 +28,14 @@ before_install:
2728
printf '\ny\n\ny\ny\n' | ./google-cloud-sdk/install.sh &&
2829
cd $TRAVIS_BUILD_DIR;
2930
fi
30-
- printf 'y\n' | gcloud components update
31-
- if [ -a client_secrets.json ]; then
32-
gcloud auth activate-service-account --key-file client_secrets.json;
31+
- gcloud -q components update gae-python
32+
- openssl aes-256-cbc -K $encrypted_4fda24e244ca_key -iv $encrypted_4fda24e244ca_iv -in python-docs-samples.json.enc -out python-docs-samples.json -d
33+
- if [ -a python-docs-samples.json ]; then
34+
gcloud auth activate-service-account --key-file python-docs-samples.json;
3335
fi
3436

3537
install:
36-
#Add app specific setup here
38+
- pip install tox
3739

3840
script:
39-
#Test and/or deploy your app with gcloud commands here!
41+
- tox

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ For more detailed introduction to a product, check the README in the correspondi
88

99
* See [CONTRIBUTING.md](CONTRIBUTING.md)
1010

11+
### How to run the test
12+
13+
To run the tests, please install App Engine Python SDK and tox and run
14+
tox with the environment variable PYTHONPATH to the App Engine Python
15+
SDK.
16+
17+
You can install App Engine Python SDK with
18+
[Google Cloud SDK](https://cloud.google.com/sdk/) with the following
19+
command:
20+
21+
$ gcloud components update gae-python
22+
23+
Here is instructions to run the tests with virtualenv, $GCLOUD is your
24+
Google Cloud SDK installation path.
25+
26+
$ virtualenv -p python2.7 --no-site-packages /some/where
27+
$ source /some/where/bin/activate
28+
$ pip install tox
29+
$ env PYTHONPATH=${GCLOUD}/platform/google_appengine tox
30+
1131
## Licensing
1232

1333
* See [LICENSE](LICENSE)

datastore/ndb/app.yaml renamed to app.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# dummy app.yaml for nosegae
22

3-
application: ndb-snippets
4-
version: 1
53
api_version: 1
64
runtime: python27
75
threadsafe: true

bigquery/__init__.py

Whitespace-only changes.

bigquery/samples/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +0,0 @@
1-
# Copyright 2015, Google, Inc.
2-
# Licensed under the Apache License, Version 2.0 (the "License");
3-
# you may not use this file except in compliance with the License.
4-
# You may obtain a copy of the License at
5-
#
6-
# http://www.apache.org/licenses/LICENSE-2.0
7-
#
8-
# Unless required by applicable law or agreed to in writing, software
9-
# distributed under the License is distributed on an "AS IS" BASIS,
10-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
# See the License for the specific language governing permissions and
12-
# limitations under the License.
13-
#

bigquery/samples/async_query.py

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
# Copyright 2015, Google, Inc.
2-
# Licensed under the Apache License, Version 2.0 (the "License");
3-
# you may not use this file except in compliance with the License.
4-
# You may obtain a copy of the License at
5-
#
6-
# http://www.apache.org/licenses/LICENSE-2.0
7-
#
8-
# Unless required by applicable law or agreed to in writing, software
9-
# distributed under the License is distributed on an "AS IS" BASIS,
10-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
# See the License for the specific language governing permissions and
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
#
1414
from __future__ import print_function # For python 2/3 interoperability
15-
from samples.utils import get_service, paging, poll_job
16-
import uuid
15+
1716
import json
17+
import uuid
18+
19+
from bigquery.samples.utils import get_service
20+
from bigquery.samples.utils import paging
21+
from bigquery.samples.utils import poll_job
1822

1923

2024
# [START async_query]
@@ -23,19 +27,19 @@ def async_query(service, project_id, query, batch=False, num_retries=5):
2327
# don't accidentally duplicate query
2428
job_data = {
2529
'jobReference': {
26-
'projectId': project_id,
27-
'job_id': str(uuid.uuid4())
28-
},
30+
'projectId': project_id,
31+
'job_id': str(uuid.uuid4())
32+
},
2933
'configuration': {
30-
'query': {
31-
'query': query,
32-
'priority': 'BATCH' if batch else 'INTERACTIVE',
33-
},
34-
}
34+
'query': {
35+
'query': query,
36+
'priority': 'BATCH' if batch else 'INTERACTIVE'
37+
}
3538
}
39+
}
3640
return service.jobs().insert(
37-
projectId=project_id,
38-
body=job_data).execute(num_retries=num_retries)
41+
projectId=project_id,
42+
body=job_data).execute(num_retries=num_retries)
3943
# [END async_query]
4044

4145

@@ -55,7 +59,6 @@ def run(project_id, query_string, batch, num_retries, interval):
5559
interval,
5660
num_retries)
5761

58-
5962
for page in paging(service,
6063
service.jobs().getQueryResults,
6164
num_retries=num_retries,
@@ -69,18 +72,13 @@ def run(project_id, query_string, batch, num_retries, interval):
6972
def main():
7073
project_id = raw_input("Enter the project ID: ")
7174
query_string = raw_input("Enter the Bigquery SQL Query: ")
72-
batch = raw_input("Run query as batch (y/n)?: ") in ('True',
73-
'true',
74-
'y',
75-
'Y',
76-
'yes',
77-
'Yes')
78-
75+
batch = raw_input("Run query as batch (y/n)?: ") in (
76+
'True', 'true', 'y', 'Y', 'yes', 'Yes')
7977

8078
num_retries = raw_input(
81-
"Enter number of times to retry in case of 500 error: ")
79+
"Enter number of times to retry in case of 500 error: ")
8280
interval = raw_input(
83-
"Enter how often to poll the query for completion (seconds): ")
81+
"Enter how often to poll the query for completion (seconds): ")
8482

8583
for result in run(project_id, query_string, batch, num_retries, interval):
8684
print(result)

bigquery/samples/discovery_doc.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
# Copyright 2015, Google, Inc.
2-
# Licensed under the Apache License, Version 2.0 (the "License");
3-
# you may not use this file except in compliance with the License.
4-
# You may obtain a copy of the License at
5-
#
6-
# http://www.apache.org/licenses/LICENSE-2.0
7-
#
8-
# Unless required by applicable law or agreed to in writing, software
9-
# distributed under the License is distributed on an "AS IS" BASIS,
10-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
# See the License for the specific language governing permissions and
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
#
14-
import os
14+
"""
15+
A module that takes care of caching and updating discovery docs for
16+
google-api-python-clients (until such a feature is integrated).
17+
"""
18+
1519
import json
16-
import httplib2
20+
import os
1721
import time
1822

19-
# [START build_and_update]
23+
import httplib2
2024

21-
RESOURCE_PATH='..' #look for discovery docs in the parent folder
22-
MAX_AGE = 86400 #update discovery docs older than a day
25+
# [START build_and_update]
2326

24-
# A module that takes care of caching and updating discovery docs
25-
# for google-api-python-clients (until such a feature is integrated)
27+
RESOURCE_PATH = '..' # look for discovery docs in the parent folder
28+
MAX_AGE = 86400 # update discovery docs older than a day
29+
BIGQUERY_SCOPES = ['https://www.googleapis.com/auth/bigquery']
2630

2731

2832
def build_and_update(api, version):
2933
from oauth2client.client import GoogleCredentials
3034
from googleapiclient.discovery import build_from_document
3135

32-
3336
path = os.path.join(RESOURCE_PATH, '{}.{}'.format(api, version))
3437
try:
3538
age = time.time() - os.path.getmtime(path)
@@ -38,11 +41,14 @@ def build_and_update(api, version):
3841
except os.error:
3942
_update_discovery_doc(api, version, path)
4043

44+
credentials = GoogleCredentials.get_application_default()
45+
if credentials.create_scoped_required():
46+
credentials = credentials.create_scoped(BIGQUERY_SCOPES)
4147
with open(path, 'r') as discovery_doc:
4248
return build_from_document(discovery_doc.read(),
43-
http=httplib2.Http(),
44-
credentials=GoogleCredentials
45-
.get_application_default())
49+
http=httplib2.Http(),
50+
credentials=credentials)
51+
4652

4753
def _update_discovery_doc(api, version, path):
4854
from apiclient.discovery import DISCOVERY_URI
@@ -61,5 +67,5 @@ def _update_discovery_doc(api, version, path):
6167
json.dump(discovery_json, discovery_doc)
6268
except ValueError:
6369
raise InvalidJsonError(
64-
'Bad JSON: %s from %s.' % (content, requested_url))
70+
'Bad JSON: %s from %s.' % (content, requested_url))
6571
# [END build_and_update]

bigquery/samples/export_data_to_cloud_storage.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
from samples.utils import get_service, poll_job
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
#
214
import uuid
315

16+
from bigquery.samples.utils import get_service
17+
from bigquery.samples.utils import poll_job
18+
419

520
# [START export_table]
621
def export_table(service, cloud_storage_path,
@@ -9,21 +24,21 @@ def export_table(service, cloud_storage_path,
924
# Generate a unique job_id so retries
1025
# don't accidentally duplicate export
1126
job_data = {
12-
'jobReference': {
27+
'jobReference': {
28+
'projectId': projectId,
29+
'jobId': str(uuid.uuid4())
30+
},
31+
'configuration': {
32+
'extract': {
33+
'sourceTable': {
1334
'projectId': projectId,
14-
'jobId': str(uuid.uuid4())
15-
},
16-
'configuration': {
17-
'extract': {
18-
'sourceTable': {
19-
'projectId': projectId,
20-
'datasetId': datasetId,
21-
'tableId': tableId,
22-
},
23-
'destinationUris': [cloud_storage_path],
24-
}
25-
}
35+
'datasetId': datasetId,
36+
'tableId': tableId,
37+
},
38+
'destinationUris': [cloud_storage_path],
2639
}
40+
}
41+
}
2742
return service.jobs().insert(
2843
projectId=projectId,
2944
body=job_data).execute(num_retries=num_retries)
@@ -52,11 +67,11 @@ def main():
5267
datasetId = raw_input("Enter a dataset ID: ")
5368
tableId = raw_input("Enter a table name to copy: ")
5469
cloud_storage_path = raw_input(
55-
"Enter a Google Cloud Storage URI: ")
70+
"Enter a Google Cloud Storage URI: ")
5671
interval = raw_input(
57-
"Enter how often to poll the job (in seconds): ")
72+
"Enter how often to poll the job (in seconds): ")
5873
num_retries = raw_input(
59-
"Enter the number of retries in case of 500 error: ")
74+
"Enter the number of retries in case of 500 error: ")
6075

6176
run(cloud_storage_path,
6277
projectId, datasetId, tableId,

bigquery/samples/load_data_by_post.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
# Copyright 2015, Google, Inc.
2-
# Licensed under the Apache License, Version 2.0 (the "License");
3-
# you may not use this file except in compliance with the License.
4-
# You may obtain a copy of the License at
5-
#
6-
# http://www.apache.org/licenses/LICENSE-2.0
7-
#
8-
# Unless required by applicable law or agreed to in writing, software
9-
# distributed under the License is distributed on an "AS IS" BASIS,
10-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
# See the License for the specific language governing permissions and
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
#
1414
import json
15+
16+
from bigquery.samples.utils import get_service, poll_job
17+
1518
import httplib2
16-
from samples.utils import get_service, poll_job
19+
1720
from oauth2client.client import GoogleCredentials
1821

1922

@@ -64,7 +67,7 @@ def main():
6467
datasetId = raw_input('Enter a dataset ID: ')
6568
tableId = raw_input('Enter a table name to load the data to: ')
6669
schema_path = raw_input(
67-
'Enter the path to the schema file for the table: ')
70+
'Enter the path to the schema file for the table: ')
6871

6972
with open(schema_path, 'r') as schema_file:
7073
schema = schema_file.read()

0 commit comments

Comments
 (0)