# HG changeset patch
# User William Stein <wstein@gmail.com>
# Date 1262288009 28800
# Node ID d5c94309582791ac3c3585d5edb5102c1bdd42c4
# Parent f19a0ed4113db84008f43930ffac2204cb8d361a
trac 7514 sagenb-part2: proper tracebacks; make source code introspection of functions defined in the notebook finally work in the notebook; properly cleanup worksheet files when notebook server is killed.
diff --git a/sagenb/notebook/run_notebook.py b/sagenb/notebook/run_notebook.py
a
|
b
|
|
185 | 185 | import signal, sys, random |
186 | 186 | def save_notebook(): |
187 | 187 | from twisted.internet.error import ReactorNotRunning |
| 188 | print "Quitting all running worksheets..." |
| 189 | twist.notebook.quit() |
188 | 190 | print "Saving notebook..." |
189 | 191 | twist.notebook.save() |
190 | 192 | try: |
diff --git a/sagenb/notebook/worksheet.py b/sagenb/notebook/worksheet.py
a
|
b
|
|
49 | 49 | whitespace = re.compile('\s') # Match any whitespace character |
50 | 50 | non_whitespace = re.compile('\S') |
51 | 51 | |
| 52 | # The file to which the Sage code that will be evaluated is written. |
| 53 | CODE_PY = "___code___.py" |
| 54 | |
52 | 55 | # Constants that control the behavior of the worksheet. |
53 | 56 | INTERRUPT_TRIES = 3 # number of times to send control-c to subprocess before giving up |
54 | 57 | INITIAL_NUM_CELLS = 1 # number of empty cells in new worksheets |
… |
… |
|
3179 | 3182 | if C.is_no_output(): |
3180 | 3183 | # Clean up the temp directories associated to C, and do not set any output |
3181 | 3184 | # text that C might have got. |
3182 | | shutil.rmtree(self.cell_directory(C), ignore_errors=True) |
| 3185 | d = self.cell_directory(C) |
| 3186 | for X in os.path.listdir(d): |
| 3187 | if os.path.split(X)[-1] != CODE_PY: |
| 3188 | Y = os.path.join(d, X) |
| 3189 | if os.path.isfile(Y): |
| 3190 | try: os.unlink(Y) |
| 3191 | except: pass |
| 3192 | else: |
| 3193 | shutil.rmtree(Y, ignore_errors=True) |
3183 | 3194 | return 'd', C |
3184 | 3195 | |
3185 | 3196 | if not C.introspect(): |
… |
… |
|
3190 | 3201 | if not os.path.exists(cell_dir): |
3191 | 3202 | os.makedirs(cell_dir) |
3192 | 3203 | for X in filenames: |
| 3204 | if os.path.split(X)[-1] == CODE_PY: continue |
3193 | 3205 | target = os.path.join(cell_dir, os.path.split(X)[1]) |
3194 | 3206 | try: |
3195 | 3207 | if os.path.exists(target): |
… |
… |
|
3202 | 3214 | else: |
3203 | 3215 | shutil.copy(X, target) |
3204 | 3216 | set_restrictive_permissions(target) |
| 3217 | if os.path.isfile(X): |
| 3218 | try: os.unlink(X) |
| 3219 | except: pass |
| 3220 | else: |
| 3221 | shutil.rmtree(X, ignore_errors=True) |
3205 | 3222 | except Exception, msg: |
3206 | 3223 | print "Error copying file from worksheet process:", msg |
3207 | 3224 | # Generate html, etc. |
… |
… |
|
3622 | 3639 | return support.get_rightmost_identifier(s) |
3623 | 3640 | |
3624 | 3641 | def preparse(self, s): |
3625 | | return 'exec _support_.preparse_worksheet_cell(base64.b64decode("%s"),globals())'%base64.b64encode(s) |
| 3642 | return 'open("%s","w").write(_support_.preparse_worksheet_cell(base64.b64decode("%s"),globals())); execfile(os.path.abspath("%s"))'%(CODE_PY, base64.b64encode(s), CODE_PY) |
3626 | 3643 | |
3627 | 3644 | ########################################################## |
3628 | 3645 | # Loading and attaching files |