-
Notifications
You must be signed in to change notification settings - Fork 13
/
znote.py
215 lines (163 loc) · 8.8 KB
/
znote.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
#!python
from getpass import getuser
default_notebooks_dir = '/home/%s/zeppelin/notebooks' % getuser()
__doc__ = """ Zeppelin notebooks helper tool.
Usage:
znote.py extract [--fetch=interpreter] [--notebooks-dir=directory]
[--skip-select] [--skip-noop] [--no-comments] [--get-disabled]
[--default-interpreter=interpreter] [--sqlc-var=sqlc] [--imports] <noteid>
znote.py clean-output [--notebooks-dir=directory] <noteid>
znote.py (-h | --help)
Commands:
extract Extract code of type --type (defaults to Python)
clean-output Removes output from all paragraphs
(good to do before source rep commits and code reviews)
Examples:
python ./znote.py extract 2CAWV29G2
python ./znote.py clean-output 2C9CTUGT3
Arguments:
<noteid> Note ID - see directories under %s
(also visible in URI when that note is open in Zeppelin)
Options:
--fetch=interpreter Defines which language/interpreter to fetch. [default: pyspark]
--notebooks-dir=dir Directory that stores all Zeppelin notes.
[default: %s]
--no-comments Don't add comments formed from paragraph titles
and markdown paragraphs
--get-disabled Produce code even for disabled paragraphs
--skip-selects Adds heuristics that detect spark-sql paragraphs that
just selects data (and not for example alters tables), and skips those
--skip-noop Adds heuristics that detect no-op paragraphs (like to display data
or schema) and skip those paragraphs
--default-interpreter=interpreter
Sets default interpreter type as configured in Zeppelin.
Because it's possible to not set paragraph type each time for default
interpreter type, this script has to know what's default [default: pyspark]
--sqlc-var=sqlc SQL Context variable [default: sqlc]
--imports Add a header to embed necessary Spark import
-h --help Show this screen
Know bugs / todo-s:
1. This script should work with --fetch other than pyspark, but it was tested only with pyspark.
Some options like --skip-noop should be tweaked for other languages other than pyspark
2. Script will be fed to stdout - make an optional parameter to write/append to a file.
History:
03/07/2017 rdautkhanov@epsilon.com - 1.0 Initial version
""" % \
(default_notebooks_dir, default_notebooks_dir)
###################################################################################################
###################################################################################################
import json
import re
from os.path import isdir, isfile
try:
from docopt import docopt # docopt isn't part of Anaconda - so checking just this pkg
except ImportError:
exit('doctopt Python package not found.')
# Parse command-line arguments using above pattern/description block
args = docopt(__doc__)
## print(args)
noteid = args['<noteid>']
notebooks_dir = args['--notebooks-dir']
note_dir = notebooks_dir + '/' + noteid
json_name = 'note.json'
json_file = note_dir + '/' + json_name
if not isdir(notebooks_dir): exit("Directory specified in --notebooks-dir doesn't exist (%s)" % notebooks_dir)
if not isdir(note_dir): exit("Note directory doesn't exist (%s). Check if NoteID is correct" % note_dir)
if not isfile(json_file): exit("%s file doesn't exist where expected: %s" % (json_name, json_file))
with open(json_file) as data_file:
data = json.load(data_file)
assert data['id'] == noteid, "Unexpected note id in " + json_file
if args['extract']:
fetch_intp = args['--fetch']
default_intp = args['--default-interpreter']
sqlc = args['--sqlc-var']
comments = not args['--no-comments']
if args['--imports']:
from textwrap import dedent
print(dedent('''
from pyspark.sql import HiveContext
from pyspark import SparkConf, SparkContext
# start spark application context with application name = note title
conf = SparkConf().setAppName('%s')
sc = SparkContext(conf=conf)
# after Spark context started, reduce logging level to ERROR:
log4j = sc._jvm.org.apache.log4j
log4j.LogManager.getRootLogger().setLevel(log4j.Level.ERROR)
## sqlc = HiveContext(sc) # this should be part of the notebook
''' % data['name']))
print "## Fetching %s code from Zeppelin note '%s', id %s" % (fetch_intp, data['name'], noteid)
for p in data['paragraphs']:
print
if comments:
print '## Paragraph %s:' % p['id']
# Check if paragraph is disabled
enabled = p['config'].get('enabled', True) # by default assume enabled
if not enabled:
if args['--get-disabled']:
if comments: print "## paragraph '%s' is disabled but will run because of --get-disabled" % p['id']
else:
if comments: print "## paragraph '%s' is disabled and will be skipped" % p['id']
continue
# print title
title = p.get('title', '')
if title and comments:
print "## **** %s ****" % title
text = p.get('text', '') # empty text '' is the default
# Zeppelin thing - 'scala' is default irrespective of default interpreter setting
# para_type = p['config'].get('editorMode', 'ace/mode/scala').split('/')[2] # takes last word
# So editorMode is misleading and below code will infer type from the text instead..
# Detect paragraph type
para_type_re = '^\s*%(\w+)\s+'
m = re.match(para_type_re, text, flags=re.MULTILINE)
if m:
para_type = m.group(1)
text = re.sub(para_type_re, '', text, count=1, flags=re.MULTILINE) # remove para type from the code too
else:
para_type = default_intp
# if comments: print "## (paragraph type is %s)" % para_type
if re.match('^\s*$', text):
if comments: print "## (no code)"
continue
# Now we can append code of the current paragraph
if para_type == fetch_intp:
# first, check if it's a no-op command that we should skip
if args['--skip-noop'] and fetch_intp=='pyspark' and re.match(
r''' \A \s * # any whitespace before code
( \w + \. (show|printSchema) \( \) # SKIP dataframe.show() or printSchema
| z \. show \( . + \) # OR z.show( dataframe )
| print \s * \(? .+ \)? # or a print statement
)
\s * \Z # any whitespace before end of the code
'''
, text
, flags=re.VERBOSE
):
if comments: print("## skipping no-op code because of --skip-noop")
continue # skip to next paragraph
print text
elif para_type == 'sql':
# first, check if it's a pure select statement that we should skip
if args['--skip-select'] and re.match(
r'''^( \s* # optional comment block:
-- # sql syntax comment - double hyphen
.* \n # commented line up to newline is ignored
| \s* \n # OR empty line - since commented and empty lines can interleave
)* # 0 or more commented or empty lines
\s* # ignore spaces before `select` keyword
(SELECT|explain) \s+ # `select` or `explain` keyword followed by whitespace
'''
, text
, flags=re.IGNORECASE|re.VERBOSE
):
if comments: print("## skipping SELECT/explain statement because of --skip-select")
continue # skip to next paragraph
print "%s.sql(''' %s ''')" % (sqlc, text)
elif para_type == 'md':
text = re.sub('^', '# ', text, flags=re.MULTILINE) # show MarkDown as-is, just commented
print text
elif para_type == 'sh':
# TODO:
continue
else:
if comments: print("## skipping non-target language '%s'" % para_type)
exit()