Raw recording mode

This commit is contained in:
Marcin Kulik
2017-11-18 16:21:35 +01:00
parent 0669121c5b
commit 1ea7892b45
5 changed files with 63 additions and 4 deletions

View File

@@ -123,6 +123,7 @@ Available options:
* `--stdin` - Enable stdin (keyboard) recording (see below)
* `--append` - Append to existing recording
* `--raw` - Save raw STDOUT output, without timing information or other metadata
* `-c, --command=<command>` - Specify command to record, defaults to $SHELL
* `-t, --title=<title>` - Specify the title of the asciicast
* `-i, --idle-time-limit=<sec>` - Limit recorded terminal inactivity to max `<sec>` seconds

View File

@@ -80,6 +80,7 @@ For help on a specific command run:
parser_rec = subparsers.add_parser('rec', help='Record terminal session')
parser_rec.add_argument('--stdin', help='enable stdin recording, disabled by default', action='store_true', default=cfg.record_stdin)
parser_rec.add_argument('--append', help='append to existing recording', action='store_true', default=False)
parser_rec.add_argument('--raw', help='save only raw stdout output', action='store_true', default=False)
parser_rec.add_argument('-c', '--command', help='command to record, defaults to $SHELL', default=cfg.record_command)
parser_rec.add_argument('-e', '--env', help='list of environment variables to capture, defaults to ' + config.DEFAULT_RECORD_ENV, default=cfg.record_env)
parser_rec.add_argument('-t', '--title', help='title of the asciicast')

View File

@@ -0,0 +1,49 @@
import os
from multiprocessing import Process, Queue
from asciinema.pty_recorder import PtyRecorder
def write_bytes_from_queue(path, mode, queue):
with open(path, mode=mode+'b', buffering=0) as f:
for data in iter(queue.get, None):
f.write(data)
class writer():
def __init__(self, path, append):
self.path = path
self.mode = 'a' if append else 'w'
self.queue = Queue()
def __enter__(self):
self.process = Process(
target=write_bytes_from_queue,
args=(self.path, self.mode, self.queue)
)
self.process.start()
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
self.queue.put(None)
self.process.join()
def write_stdin(self, data):
pass
def write_stdout(self, data):
self.queue.put(data)
class Recorder:
def __init__(self, pty_recorder=None):
self.pty_recorder = pty_recorder if pty_recorder is not None else PtyRecorder()
def record(self, path, append, command, command_env, _captured_env, _rec_stdin, _title, _idle_time_limit):
if os.path.exists(path) and os.stat(path).st_size == 0: # true for pipes
append = False
with writer(path, append) as w:
self.pty_recorder.record_command(['sh', '-c', command], w, command_env)

View File

@@ -5,12 +5,13 @@ import tempfile
from asciinema.commands.command import Command
import asciinema.asciicast as asciicast
import asciinema.asciicast.v2 as v2
import asciinema.asciicast.raw as raw
from asciinema.api import APIError
class RecordCommand(Command):
def __init__(self, api, args, env=None, recorder=None):
def __init__(self, api, args, env=None):
Command.__init__(self, args.quiet)
self.api = api
self.filename = args.filename
@@ -21,7 +22,8 @@ class RecordCommand(Command):
self.assume_yes = args.yes or args.quiet
self.idle_time_limit = args.idle_time_limit
self.append = args.append
self.recorder = recorder if recorder is not None else v2.Recorder()
self.raw = args.raw
self.recorder = raw.Recorder() if args.raw else v2.Recorder()
self.env = env if env is not None else os.environ
def execute(self):
@@ -29,6 +31,10 @@ class RecordCommand(Command):
append = self.append
if self.filename == "":
if self.raw:
self.print_error("Filename required when recording in raw mode.")
return 1
else:
self.filename = _tmp_path()
upload = True

View File

@@ -49,3 +49,5 @@ bash -c "sleep 1; pkill -9 -n -f 'bash -c echo t3st'" &
asciinema rec -c 'bash -c "echo t3st; sleep 2; echo ok"' "$TMP_DATA_DIR/4.cast"
asciinema rec --stdin -c 'bash -c "echo t3st; sleep 1; echo ok"' "$TMP_DATA_DIR/5.cast"
asciinema rec --raw -c 'bash -c "echo t3st; sleep 1; echo ok"' "$TMP_DATA_DIR/6.raw"