Skip to content

Commit 97b1047

Browse files
authored
Merge pull request MartinSahlen#9 from MartinSahlen/better-output
Better output
2 parents 1406a4a + f2c88c0 commit 97b1047

File tree

7 files changed

+81
-16
lines changed

7 files changed

+81
-16
lines changed

cloudfn/cli.py

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
from __future__ import print_function
12
import subprocess
23
import os
34
import sys
45
import argparse
56
from jinja2 import Template
7+
from datetime import datetime
8+
import json
9+
from pyspin.spin import make_spin, Default
10+
import emoji
611

712

813
def package_root():
@@ -99,20 +104,69 @@ def build_cmd(file_name, python_version, production):
99104
return build(file_name, python_version, production)
100105

101106

102-
def build_function(function_name, file_name,
103-
trigger_type, python_version, production):
104-
exit_code = subprocess.call(
107+
def log(statement):
108+
d = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
109+
print(d + ': ' + statement)
110+
111+
112+
def build_function(
113+
function_name,
114+
file_name,
115+
trigger_type,
116+
python_version,
117+
production,
118+
verbose):
119+
120+
build_config = {
121+
'function_name': function_name,
122+
'file_name': file_name,
123+
'trigger_type': trigger_type,
124+
'python_version': python_version,
125+
'production': production,
126+
}
127+
128+
log('Config: \n' +
129+
json.dumps(build_config, indent=4))
130+
131+
stdout = subprocess.PIPE
132+
stderr = subprocess.STDOUT
133+
if verbose:
134+
stdout = sys.stdout
135+
stderr = sys.stderr
136+
137+
(p, output) = run_build_cmd(
105138
' '.join(build_cmd(file_name, python_version, production)),
106-
stdout=sys.stdout,
107-
stdin=sys.stdin,
108-
shell=True)
109-
if exit_code == 0:
139+
stdout,
140+
stderr)
141+
if p.returncode == 0:
110142
build_javascript(function_name, trigger_type)
111143
else:
112-
sys.exit(exit_code)
113-
sys.exit(cleanup())
144+
log('Build failed!'
145+
'See the build output below for what might have went wrong:')
146+
print(output[0])
147+
sys.exit(p.returncode)
148+
(c, co) = cleanup()
149+
if c.returncode == 0:
150+
log('Success! Your function can now be deployed from '
151+
'./cloudfn/target/')
152+
else:
153+
log('Something went wrong when cleaning up: ' + co[0])
154+
sys.exit(c.returncode)
155+
156+
157+
@make_spin(Default, emoji.emojize('Building :wrench:') +
158+
emoji.emojize(', go grab a :coffee:'))
159+
def run_build_cmd(cmd, stdout, stderr):
160+
p = subprocess.Popen(
161+
cmd,
162+
stdout=stdout,
163+
stderr=stderr,
164+
shell=True)
165+
output = p.communicate()
166+
return (p, output)
114167

115168

169+
@make_spin(Default, u'Generating javascript')
116170
def build_javascript(function_name, trigger_type):
117171
js = open(package_root() + 'template/index.js').read()
118172
t = Template(js)
@@ -125,11 +179,16 @@ def build_javascript(function_name, trigger_type):
125179
open('cloudfn/index.js', 'w').write(rendered_js)
126180

127181

182+
@make_spin(Default, u'Cleaning up')
128183
def cleanup():
129-
return subprocess.call(
184+
p = subprocess.Popen(
130185
'cd cloudfn && rm -rf target && mkdir target && mv index.js target ' +
131186
'&& mv dist target',
187+
stdout=subprocess.PIPE,
188+
stderr=subprocess.STDOUT,
132189
shell=True)
190+
output = p.communicate()
191+
return (p, output)
133192

134193

135194
def main():
@@ -149,11 +208,15 @@ def main():
149208
help='The python version you are targeting, '
150209
'only applies when building for production',
151210
choices=['2.7', '3.5'])
211+
parser.add_argument('-v', '--verbose', action='store_true',
212+
help='Build in verbose mode '
213+
'showing full build output')
152214

153215
args = parser.parse_args()
154216
build_function(args.function_name,
155217
args.file_name,
156218
args.trigger_type,
157219
args.python_version,
158220
args.production,
221+
args.verbose,
159222
)

examples/django/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
django==1.11
2-
pycloudfn==0.1.174
2+
pycloudfn==0.1.188
33
djangorestframework==3.6.3

examples/flask/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pycloudfn==0.1.174
1+
pycloudfn==0.1.188
22
flask==0.12
33
google-cloud-bigquery==0.24.0

examples/http/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pycloudfn==0.1.174
1+
pycloudfn==0.1.188

examples/pubsub/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pycloudfn==0.1.174
1+
pycloudfn==0.1.188
22
jsonpickle==0.9.4

examples/storage/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pycloudfn==0.1.174
1+
pycloudfn==0.1.188
22
jsonpickle==0.9.4

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='pycloudfn',
5-
version='0.1.174',
5+
version='0.1.188',
66
description='GCP Cloud functions in python',
77
url='https://github.com/MartinSahlen/cloud-functions-python',
88
author='Martin Sahlen',
@@ -27,6 +27,8 @@
2727
'django==1.11.1',
2828
'six==1.10.0',
2929
'Jinja2==2.9.6',
30+
'pyspin==1.1.1',
31+
'emoji==0.4.5',
3032
],
3133
include_package_data=True,
3234
packages=['cloudfn'],

0 commit comments

Comments
 (0)