Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch to use Python's tempfile library instead of 'temporary_file.temporary' #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions jupyterlab_slurm/slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import asyncio
import html
import os
import tempfile

from notebook.base.handlers import IPythonHandler
from tornado.web import MissingArgumentError
Expand Down Expand Up @@ -61,10 +62,14 @@ class SbatchHandler(ShellExecutionHandler):
# Add `-H "Authorization: token <token>"` to the curl command for any POST request
async def post(self):
def string_to_file(string):
file_object = open('temporary_file.temporary', 'w')
# Helper function to convert script typed into field into a tempfile for job submission.
# Takes in the script contents of the field.
# Returns path to tempfile.
tempscript = tempfile.mkstemp()
file_object = open(tempscript[1], 'w')
file_object.write(string)
file_object.close()
return
return tempscript[1]

inputType = self.get_query_argument('inputType')
outputDir = self.get_query_argument('outputDir', default='')
Expand All @@ -81,13 +86,13 @@ def string_to_file(string):
stdout, stderr, returncode, errorMessage = ("", "Something went wrong. Check console for more details.", 1, str(e))
elif inputType == 'contents':
script_contents = self.get_body_argument('input')
string_to_file(script_contents)
tempscript = string_to_file(script_contents)
try:
stdout, stderr, returncode = await self.run_command(sbatch_command, stdin=open('temporary_file.temporary','rb'), cwd=outputDir)
stdout, stderr, returncode = await self.run_command(sbatch_command, stdin=open(tempscript,'rb'), cwd=outputDir)
errorMessage = ""
except Exception as e:
stdout, stderr, returncode, errorMessage = ("", "Something went wrong. Check console for more details.", 1, str(e))
os.remove('temporary_file.temporary')
os.remove(tempscript)
else:
raise Exception('The query argument inputType needs to be either \'path\' or \'contents\'.')
else:
Expand Down