Skip to content

Commit

Permalink
Rewrite BulkFileUploader and support aborting.
Browse files Browse the repository at this point in the history
issue #67
  • Loading branch information
sedwards2009 committed Jan 27, 2018
1 parent a2f4345 commit be7bb9a
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 80 deletions.
20 changes: 16 additions & 4 deletions src/commands/exfrom.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def __init__(self, message):
self.message = message


def readStdinLine():
line = sys.stdin.readline()
return line.strip()


def requestFrame(frame_name):
"""Returns a generator which outputs the frame contents as blocks of binary data.
"""
Expand All @@ -61,7 +66,7 @@ def restoreTty():
# Request the frame contents from the terminal.
extratermclient.requestFrame(frame_name)

line = sys.stdin.readline().strip()
line = readStdinLine()

if not line.startswith("#M:"):
yield FrameReadError("Error while reading in frame data. Expected '#M:...', but didn't receive it.")
Expand Down Expand Up @@ -92,12 +97,12 @@ def restoreTty():
# Read stdin until an empty buffer is returned.
try:
while True:
line = sys.stdin.readline().strip()
line = readStdinLine()

if len(line) < COMMAND_PREFIX_LENGTH + 1 + HASH_LENGTH:
return FrameReadError("Error while reading frame body data. Line is too short.")

if line.startswith("#D:") or line.startswith("#E:"):
if line.startswith("#D:") or line.startswith("#E:") or line.startswith("#A:"):
# Data
b64data = line[COMMAND_PREFIX_LENGTH:-HASH_LENGTH-1]
contents = base64.b64decode(b64data)
Expand All @@ -110,12 +115,15 @@ def restoreTty():

# Check the hash.
if lineHash != hash.hexdigest():
yield FrameReadError("Error: Hash didn't match for data line.")
yield FrameReadError("Error: Upload failed. (Hash didn't match for data line. Expected " + hash.hexdigest() + " got " + lineHash + ")")
return

if line.startswith("#E:"):
# EOF
break
elif line.startswith("#A:"):
yield FrameReadError("Upload aborted")
return
else:
# Send the input to stdout.
yield BodyData(contents)
Expand All @@ -131,6 +139,7 @@ def restoreTty():
#Ignore further SIG_PIPE signals and don't throw exceptions
signal(SIGPIPE,SIG_DFL)


def outputFrame(frame_name):
rc = 0
for block in requestFrame(frame_name):
Expand All @@ -147,6 +156,7 @@ def outputFrame(frame_name):
sys.stdout.flush()
return rc


def xargs(frame_names, command_list):
temp_files = []
rc = 0
Expand All @@ -171,6 +181,7 @@ def xargs(frame_names, command_list):
os.unlink(temp_file.name)
return rc


def readFrameToTempFile(frame_name):
rc = 0
fhandle = tempfile.NamedTemporaryFile('w+b', delete=False)
Expand All @@ -189,6 +200,7 @@ def readFrameToTempFile(frame_name):

return rc, fhandle


def main():
parser = argparse.ArgumentParser(prog='from', description='Fetch data from an Extraterm frame.')
parser.add_argument('frames', metavar='frame_ID', type=str, nargs='+', help='a frame ID')
Expand Down
2 changes: 1 addition & 1 deletion src/render_process/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,7 @@ export class EtTerminal extends ThemeableElementBase implements Commandable, Acc
// Abort the upload.
uploader.abort();
inputFilterRegistration.dispose();
return input.substr(ctrlCIndex);
return input.substr(ctrlCIndex + 1);
} else {
return "";
}
Expand Down
Loading

0 comments on commit be7bb9a

Please sign in to comment.