def test_shquote(): WVPASSEQ(qst(""" this is basic \t\n\r text """), ['this', 'is', 'basic', 'text']) WVPASSEQ(qst(r""" \"x\" "help" 'yelp' """), ['"x"', 'help', 'yelp']) WVPASSEQ(qst(r""" "'\"\"'" '\"\'' """), ["'\"\"'", '\\"\'']) WVPASSEQ(shquote.quotesplit(' this is "unfinished'), [(2, 'this'), (7, 'is'), (10, 'unfinished')]) WVPASSEQ(shquote.quotesplit('"silly"\'will'), [(0, 'silly'), (7, 'will')]) WVPASSEQ(shquote.unfinished_word('this is a "billy" "goat'), ('"', 'goat')) WVPASSEQ(shquote.unfinished_word("'x"), ("'", 'x')) WVPASSEQ(shquote.unfinished_word("abra cadabra "), (None, '')) WVPASSEQ(shquote.unfinished_word("abra cadabra"), (None, 'cadabra')) (qtype, word) = shquote.unfinished_word("this is /usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", True), "al") (qtype, word) = shquote.unfinished_word("this is '/usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", True), "al'") (qtype, word) = shquote.unfinished_word("this is \"/usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", True), "al\"") (qtype, word) = shquote.unfinished_word("this is \"/usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", False), "al") (qtype, word) = shquote.unfinished_word("this is \\ hammer\\ \"") WVPASSEQ(word, ' hammer "') WVPASSEQ(shquote.what_to_add(qtype, word, " hammer \"time\"", True), "time\\\"") WVPASSEQ(shquote.quotify_list(['a', '', '"word"', "'third'", "'", "x y"]), "a '' '\"word\"' \"'third'\" \"'\" 'x y'")
def test_shquote(): with no_lingering_errors(): WVPASSEQ(qst(""" this is basic \t\n\r text """), ["this", "is", "basic", "text"]) WVPASSEQ(qst(r""" \"x\" "help" 'yelp' """), ['"x"', "help", "yelp"]) WVPASSEQ(qst(r""" "'\"\"'" '\"\'' """), ["'\"\"'", "\\\"'"]) WVPASSEQ(shquote.quotesplit(' this is "unfinished'), [(2, "this"), (7, "is"), (10, "unfinished")]) WVPASSEQ(shquote.quotesplit('"silly"\'will'), [(0, "silly"), (7, "will")]) WVPASSEQ(shquote.unfinished_word('this is a "billy" "goat'), ('"', "goat")) WVPASSEQ(shquote.unfinished_word("'x"), ("'", "x")) WVPASSEQ(shquote.unfinished_word("abra cadabra "), (None, "")) WVPASSEQ(shquote.unfinished_word("abra cadabra"), (None, "cadabra")) (qtype, word) = shquote.unfinished_word("this is /usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", True), "al") (qtype, word) = shquote.unfinished_word("this is '/usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", True), "al'") (qtype, word) = shquote.unfinished_word('this is "/usr/loc') WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", True), 'al"') (qtype, word) = shquote.unfinished_word('this is "/usr/loc') WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", False), "al") (qtype, word) = shquote.unfinished_word('this is \\ hammer\\ "') WVPASSEQ(word, ' hammer "') WVPASSEQ(shquote.what_to_add(qtype, word, ' hammer "time"', True), 'time\\"') WVPASSEQ( shquote.quotify_list(["a", "", '"word"', "'third'", "'", "x y"]), "a '' '\"word\"' \"'third'\" \"'\" 'x y'" )
def test_shquote(): with no_lingering_errors(): WVPASSEQ(qst(""" this is basic \t\n\r text """), ['this', 'is', 'basic', 'text']) WVPASSEQ(qst(r""" \"x\" "help" 'yelp' """), ['"x"', 'help', 'yelp']) WVPASSEQ(qst(r""" "'\"\"'" '\"\'' """), ["'\"\"'", '\\"\'']) WVPASSEQ(shquote.quotesplit(' this is "unfinished'), [(2,'this'), (7,'is'), (10,'unfinished')]) WVPASSEQ(shquote.quotesplit('"silly"\'will'), [(0,'silly'), (7,'will')]) WVPASSEQ(shquote.unfinished_word('this is a "billy" "goat'), ('"', 'goat')) WVPASSEQ(shquote.unfinished_word("'x"), ("'", 'x')) WVPASSEQ(shquote.unfinished_word("abra cadabra "), (None, '')) WVPASSEQ(shquote.unfinished_word("abra cadabra"), (None, 'cadabra')) (qtype, word) = shquote.unfinished_word("this is /usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", True), "al") (qtype, word) = shquote.unfinished_word("this is '/usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", True), "al'") (qtype, word) = shquote.unfinished_word("this is \"/usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", True), "al\"") (qtype, word) = shquote.unfinished_word("this is \"/usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, "/usr/local", False), "al") (qtype, word) = shquote.unfinished_word("this is \\ hammer\\ \"") WVPASSEQ(word, ' hammer "') WVPASSEQ(shquote.what_to_add(qtype, word, " hammer \"time\"", True), "time\\\"") WVPASSEQ(shquote.quotify_list(['a', '', '"word"', "'third'", "'", "x y"]), "a '' '\"word\"' \"'third'\" \"'\" 'x y'")
def test_shquote(): WVPASSEQ(qst(b""" this is basic \t\n\r text """), [b'this', b'is', b'basic', b'text']) WVPASSEQ(qst(br""" \"x\" "help" 'yelp' """), [b'"x"', b'help', b'yelp']) WVPASSEQ(qst(br""" "'\"\"'" '\"\'' """), [b"'\"\"'", b'\\"\'']) WVPASSEQ(shquote.quotesplit(b' this is "unfinished'), [(2, b'this'), (7, b'is'), (10, b'unfinished')]) WVPASSEQ(shquote.quotesplit(b'"silly"\'will'), [(0, b'silly'), (7, b'will')]) WVPASSEQ(shquote.unfinished_word(b'this is a "billy" "goat'), (b'"', b'goat')) WVPASSEQ(shquote.unfinished_word(b"'x"), (b"'", b'x')) WVPASSEQ(shquote.unfinished_word(b"abra cadabra "), (None, b'')) WVPASSEQ(shquote.unfinished_word(b"abra cadabra"), (None, b'cadabra')) qtype, word = shquote.unfinished_word(b"this is /usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, b"/usr/local", True), b"al") qtype, word = shquote.unfinished_word(b"this is '/usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, b"/usr/local", True), b"al'") qtype, word = shquote.unfinished_word(b"this is \"/usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, b"/usr/local", True), b"al\"") qtype, word = shquote.unfinished_word(b"this is \"/usr/loc") WVPASSEQ(shquote.what_to_add(qtype, word, b"/usr/local", False), b"al") qtype, word = shquote.unfinished_word(b"this is \\ hammer\\ \"") WVPASSEQ(word, b' hammer "') WVPASSEQ(shquote.what_to_add(qtype, word, b" hammer \"time\"", True), b"time\\\"") WVPASSEQ(shquote.quotify_list([b'a', b'', b'"word"', b"'third'", b"'", b"x y"]), b"a '' '\"word\"' \"'third'\" \"'\" 'x y'")
lines = (argv_bytes(arg) for arg in extra) else: if hasattr(_helpers, 'readline'): _helpers.set_completer_word_break_characters(b' \t\n\r/') _helpers.set_attempted_completion_function(attempt_completion) _helpers.set_completion_entry_function(enter_completion) if sys.platform.startswith('darwin'): # MacOS uses a slightly incompatible clone of libreadline _helpers.parse_and_bind(b'bind ^I rl_complete') _helpers.parse_and_bind(b'tab: complete') lines = inputiter() for line in lines: if not line.strip(): continue words = [word for (wordstart, word) in shquote.quotesplit(line)] cmd = words[0].lower() #log('execute: %r %r\n' % (cmd, parm)) try: if cmd == b'ls': # FIXME: respect pwd (perhaps via ls accepting resolve path/parent) do_ls(repo, words[1:], out) out.flush() elif cmd == b'cd': np = pwd for parm in words[1:]: res = vfs.resolve(repo, parm, parent=np) _, leaf_item = res[-1] if not leaf_item: raise Exception('%s does not exist' % path_msg(b'/'.join(name
readline = None if readline: readline.set_completer_delims(' \t\n\r/') readline.set_completer(completer) if sys.platform.startswith('darwin'): # MacOS uses a slighly incompatible clone of libreadline readline.parse_and_bind('bind ^I rl_complete') readline.parse_and_bind('tab: complete') init_readline_vars() lines = inputiter() for line in lines: if not line.strip(): continue words = [word for (wordstart,word) in shquote.quotesplit(line)] cmd = words[0].lower() #log('execute: %r %r\n' % (cmd, parm)) try: if cmd == 'ls': do_ls(words[1:]) elif cmd == 'cd': np = pwd for parm in words[1:]: np = np.resolve(parm) if not stat.S_ISDIR(np.mode): raise vfs.NotDir('%s is not a directory' % parm) pwd = np elif cmd == 'pwd': print pwd.fullname() elif cmd == 'cat':
def qst(line): return [word for offset,word in shquote.quotesplit(line)]
def main(argv): o = options.Options(optspec) opt, flags, extra = o.parse_bytes(argv[1:]) global repo repo = from_opts(opt, reverse=False) sys.stdout.flush() out = byte_stream(sys.stdout) stdin = byte_stream(sys.stdin) pwd = vfs.resolve(repo, b'/') rv = 0 def inputiter(f): if os.isatty(f.fileno()): while 1: prompt = b'bup %s> ' % (b'/'.join(name for name, item in pwd) or b'/', ) if hasattr(_helpers, 'readline'): try: yield _helpers.readline(prompt) except EOFError: print() # Clear the line for the terminal's next prompt break else: out.write(prompt) out.flush() read_line = f.readline() if not read_line: print('') break yield read_line else: for line in f: yield line if extra: lines = (argv_bytes(arg) for arg in extra) else: if hasattr(_helpers, 'readline'): _helpers.set_completer_word_break_characters(b' \t\n\r/') _helpers.set_attempted_completion_function(attempt_completion) _helpers.set_completion_entry_function(enter_completion) if sys.platform.startswith('darwin'): # MacOS uses a slightly incompatible clone of libreadline _helpers.parse_and_bind(b'bind ^I rl_complete') _helpers.parse_and_bind(b'tab: complete') lines = inputiter(stdin) for line in lines: if not line.strip(): continue words = [word for (wordstart,word) in shquote.quotesplit(line)] cmd = words[0].lower() #log('execute: %r %r\n' % (cmd, parm)) try: if cmd == b'ls': do_ls(repo, pwd, words[1:], out) out.flush() elif cmd == b'cd': np = pwd for parm in words[1:]: res = vfs.resolve(repo, parm, parent=np) _, leaf_item = res[-1] if not leaf_item: raise CommandError(b'"%s" does not exist' % b'/'.join(name for name, item in res)) if not stat.S_ISDIR(vfs.item_mode(leaf_item)): raise CommandError(b'"%s" is not a directory' % parm) np = res pwd = np elif cmd == b'pwd': if len(pwd) == 1: out.write(b'/') out.write(b'/'.join(name for name, item in pwd) + b'\n') out.flush() elif cmd == b'cat': for parm in words[1:]: res = vfs.resolve(repo, parm, parent=pwd) _, leaf_item = res[-1] if not leaf_item: raise CommandError(b'"%s" does not exist' % b'/'.join(name for name, item in res)) with vfs.fopen(repo, leaf_item) as srcfile: write_to_file(srcfile, out) out.flush() elif cmd == b'get': if len(words) not in [2,3]: rv = 1 raise CommandError(b'Usage: get <filename> [localname]') rname = words[1] (dir,base) = os.path.split(rname) lname = len(words) > 2 and words[2] or base res = vfs.resolve(repo, rname, parent=pwd) _, leaf_item = res[-1] if not leaf_item: raise CommandError(b'"%s" does not exist' % b'/'.join(name for name, item in res)) with vfs.fopen(repo, leaf_item) as srcfile: with open(lname, 'wb') as destfile: log('Saving %s\n' % path_msg(lname)) write_to_file(srcfile, destfile) elif cmd == b'mget': for parm in words[1:]: dir, base = os.path.split(parm) res = vfs.resolve(repo, dir, parent=pwd) _, dir_item = res[-1] if not dir_item: raise CommandError(b'"%s" does not exist' % dir) for name, item in vfs.contents(repo, dir_item): if name == b'.': continue if fnmatch.fnmatch(name, base): if stat.S_ISLNK(vfs.item_mode(item)): deref = vfs.resolve(repo, name, parent=res) deref_name, deref_item = deref[-1] if not deref_item: raise CommandError(b'"%s" does not exist' % b'/'.join(name for name, item in res)) item = deref_item with vfs.fopen(repo, item) as srcfile: with open(name, 'wb') as destfile: log('Saving %s\n' % path_msg(name)) write_to_file(srcfile, destfile) elif cmd == b'help' or cmd == b'?': out.write(b'Commands: ls cd pwd cat get mget help quit\n') out.flush() elif cmd in (b'quit', b'exit', b'bye'): break else: rv = 1 raise CommandError(b'no such command "%s"' % cmd) except CommandError as e: rv = 1 out.write(b'error: %s\n' % e.args[0]) out.flush() except Exception as e: rv = 1 out.write(b'error: %s\n' % str(e).encode()) out.flush() sys.exit(rv)
def qst(line): return [word for offset, word in shquote.quotesplit(line)]
def present_interface(stdin, out, extra, repo): pwd = vfs.resolve(repo, b'/') if extra: lines = (argv_bytes(arg) for arg in extra) else: if hasattr(_helpers, 'readline'): _helpers.set_completer_word_break_characters(b' \t\n\r/') _helpers.set_attempted_completion_function(attempt_completion) _helpers.set_completion_entry_function(enter_completion) if sys.platform.startswith('darwin'): # MacOS uses a slightly incompatible clone of libreadline _helpers.parse_and_bind(b'bind ^I rl_complete') _helpers.parse_and_bind(b'tab: complete') lines = inputiter(stdin, pwd, out) for line in lines: if not line.strip(): continue words = [word for (wordstart, word) in shquote.quotesplit(line)] cmd = words[0].lower() #log('execute: %r %r\n' % (cmd, parm)) try: if cmd == b'ls': do_ls(repo, pwd, words[1:], out) out.flush() elif cmd == b'cd': np = pwd for parm in words[1:]: res = vfs.resolve(repo, parm, parent=np) _, leaf_item = res[-1] if not leaf_item: raise CommandError('path does not exist: ' + rpath_msg(res)) if not stat.S_ISDIR(vfs.item_mode(leaf_item)): raise CommandError('path is not a directory: ' + path_msg(parm)) np = res pwd = np elif cmd == b'pwd': if len(pwd) == 1: out.write(b'/') out.write(b'/'.join(name for name, item in pwd) + b'\n') out.flush() elif cmd == b'cat': for parm in words[1:]: res = vfs.resolve(repo, parm, parent=pwd) _, leaf_item = res[-1] if not leaf_item: raise CommandError('path does not exist: ' + rpath_msg(res)) with vfs.fopen(repo, leaf_item) as srcfile: write_to_file(srcfile, out) out.flush() elif cmd == b'get': if len(words) not in [2, 3]: raise CommandError('Usage: get <filename> [localname]') rname = words[1] (dir, base) = os.path.split(rname) lname = len(words) > 2 and words[2] or base res = vfs.resolve(repo, rname, parent=pwd) _, leaf_item = res[-1] if not leaf_item: raise CommandError('path does not exist: ' + rpath_msg(res)) with vfs.fopen(repo, leaf_item) as srcfile: with open(lname, 'wb') as destfile: log('Saving %s\n' % path_msg(lname)) write_to_file(srcfile, destfile) elif cmd == b'mget': for parm in words[1:]: dir, base = os.path.split(parm) res = vfs.resolve(repo, dir, parent=pwd) _, dir_item = res[-1] if not dir_item: raise CommandError('path does not exist: ' + path_msg(dir)) for name, item in vfs.contents(repo, dir_item): if name == b'.': continue if fnmatch.fnmatch(name, base): if stat.S_ISLNK(vfs.item_mode(item)): deref = vfs.resolve(repo, name, parent=res) deref_name, deref_item = deref[-1] if not deref_item: raise CommandError( 'path does not exist: ' + rpath_msg(res)) item = deref_item with vfs.fopen(repo, item) as srcfile: with open(name, 'wb') as destfile: log('Saving %s\n' % path_msg(name)) write_to_file(srcfile, destfile) elif cmd in (b'help', b'?'): out.write(b'Commands: ls cd pwd cat get mget help quit\n') out.flush() elif cmd in (b'quit', b'exit', b'bye'): break else: raise CommandError('no such command: ' + cmd.encode(errors='backslashreplace')) except CommandError as ex: out.write(b'error: %s\n' % str(ex).encode(errors='backslashreplace')) out.flush()
def qst(line): return [s[1] for s in shquote.quotesplit(line)]