forked from newsapps/tarbell-0.8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
222 lines (194 loc) · 8.02 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Tarbell template fabfile
from fabric import api as fab
import os
import jinja2
import codecs
from tarbell.app import TarbellSite as _TarbellSite
import inspect
from apiclient import errors
from apiclient import discovery
from apiclient.http import MediaFileUpload as _MediaFileUpload
from oauth2client import client
from oauth2client import keyring_storage
from oauth2client import tools
import getpass
import gflags
import httplib2
import s3config
"""
Base configuration
"""
fab.env.oauth_scope = 'https://www.googleapis.com/auth/drive.file'
fab.env.oauth_redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
fab.env.project = ''
def project(project=None):
"""
Set project
"""
if project:
fab.env.project = project
def deploy():
"""
Deploy from fab.locally rendered files.
"""
fab.local('python render_templates.py %(project)s' % fab.env)
fab.local('python s3deploy.py')
def runserver():
"""Run a fab.local development server."""
print "Point your browser to http://localhost:5000/"
print "Type ctrl-c to quit."
fab.local('python runserver.py' % fab.env)
def runpreviewserver():
"""Render static HTML and preview with a simple HTTP server."""
fab.local('python render_templates.py %(project)s' % fab.env)
print "Point your browser to http://localhost:5001/<projectname>/"
print "Type ctrl-c to quit."
with fab.lcd('out'):
fab.local('python -m SimpleHTTPServer 5001')
def newproject(project_name=None):
"""Create new project in the current directory."""
FLAGS = gflags.FLAGS
context = {}
if project_name is None:
context['project_name'] = raw_input(
"What is the directory name for the project? ")
else:
context['project_name'] = project_name
context['long_name'] = context['project_name']
long_name = raw_input("What is your project's full title? ")
if long_name:
context['long_name'] = long_name
proj_dir = os.path.join(os.path.dirname(__file__), context['project_name'])
try:
os.mkdir(proj_dir)
except OSError, e:
if e.errno == 17:
print ("ABORTING: Directory %s "
"already exists.") % context['project_name']
else:
print "ABORTING: OSError %s" % e
return
# if client secrets exists
setup_google = raw_input("Do you want a Google doc associated with this "
"project? [Y/n]: ")
if setup_google.lower() == 'y':
print "Generating Google spreadsheet"
context['spreadsheet_key'] = _create_google_spreadsheet(
context['long_name'])
# Get and walk project template
loader = jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), '_project_template'))
fab.env = jinja2.Environment(loader=loader)
for template in loader.list_templates():
if template.endswith('.xlsx'):
continue
if ('/') in template:
parts = template.split('/')
dirname = '/'.join(parts[:-1])
new_dir = os.path.join(proj_dir, dirname)
try:
os.makedirs(new_dir)
print "Created directory %s" % new_dir
except OSError, e:
if e.errno != 17:
print "Error creating %s: %s" % (new_dir, os.strerror)
content = fab.env.get_template(template).render(**context)
new_file = os.path.join(proj_dir, template)
codecs.open(new_file,"w",encoding="utf-8").write(content)
print 'Created %s' % new_file
if os.path.isdir(os.path.join(os.path.dirname(__file__), '.git')):
branch = raw_input("Would you like to create a new branch and initial "
"commit for this project? [Y/n]: ")
if branch.lower() == 'y':
try:
fab.local('git checkout master; \
git checkout -b %s' % context['project_name'] )
fab.local('git add %s' % context['project_name'])
fab.local('git commit -m "Started new project \
%s"' % context['project_name'])
except:
print "Error checking out branch."
else:
print "Okay! No new branch..."
print
print "Welcome to %s. Great work! What's next?" % context['long_name']
print
print ("- Edit %s to set up template values and adjust project "
"settings.") % os.path.relpath(os.path.join(proj_dir, 'config.py'))
print ("- Edit %s to configure Google spreadsheet authentication "
"variables.") % os.path.relpath(os.path.join(proj_dir, 'secrets.py'))
print "- Edit %s to edit your default template." %\
os.path.relpath(os.path.join(proj_dir, 'templates/index.html'))
print "- Edit %s to edit your default Javascript app." %\
os.path.relpath(os.path.join(proj_dir, 'static/js/app.js'))
print ("- Run `fab runserver` and view your project at "
"http://localhost:5000/%s/") % context['project_name']
print
print ("Run `fab deploy` and `fab project:projectname deploy` to deploy to "
"S3 if you have a bucket configured.")
def _handle_oauth_flow(storage):
"""
Reads the fab.local client secrets file if available (otherwise, opens a
browser tab to walk through the OAuth 2.0 process, and stores the client
secrets for future use) and then authorizes those credentials. Returns an
httplib2.Http object authorized with the fab.local user's credentials.
"""
# Retrieve credentials from fab.local storage, if possible
credentials = storage.get()
if not credentials:
flow = client.flow_from_clientsecrets('client_secrets.json',
scope=fab.env.oauth_scope, redirect_uri=fab.env.oauth_redirect_uri)
credentials = tools.run(flow, storage)
storage.put(credentials)
http = httplib2.Http()
http = credentials.authorize(http)
return http
def _add_user_to_file(file_id, service, user_email='tribgfx@gmail.com',
perm_type='user', role='reader'):
"""
Grants the given set of permissions for a given file_id. service is an
already-credentialed Google Drive service instance.
"""
new_permission = {
'value': user_email,
'type': perm_type,
'role': role
}
try:
service.permissions()\
.insert(fileId=file_id, body=new_permission)\
.execute()
except errors.HttpError, error:
print 'An error occurred: %s' % error
def _create_google_spreadsheet(project_name):
"""
Once credentials are received, uploads a copy of microcopy_template.xlsx
named for this project, puts it in the Trib Docs -> microcopy folder, adds
the default tribapps gmail account as a reader, makes it world-readable and
returns the file ID.
"""
storage = keyring_storage.Storage('fab', getpass.getuser())
http = _handle_oauth_flow(storage)
service = discovery.build('drive', 'v2', http=http)
path = os.path.join(os.path.dirname(inspect.getfile(_TarbellSite)),
'project_template/microcopy_template.xlsx')
media_body = _MediaFileUpload(path, mimetype='application/vnd.ms-excel')
body = {
'title': '%s microcopy' % project_name,
'description': 'Microcopy file for %s project' % project_name,
'mimeType': 'application/vnd.ms-excel',
}
try:
newfile = service.files()\
.insert(body=body, media_body=media_body, convert=True).execute()
_add_user_to_file(newfile['id'], service)
_add_user_to_file(newfile['id'], service, user_email='anyone',
perm_type='anyone', role='reader')
service.revisions()\
.update(fileId=newfile['id'], revisionId='head',
body={'published': True, 'publishAuto': True}).execute()
print ("Success! View the file at "
"https://docs.google.com/spreadsheet/ccc?key=%s") % newfile['id']
return newfile['id']
except errors.HttpError, error:
print 'An error occurred: %s' % error
return '<< INSERT SPREADSHEET KEY >>'