Ejemplo n.º 1
0
def copy_to_clipboard(str, timeout=10):
    str = force_bytes(str)
    current_clipboard = get_clipboard()
    set_clipboard(str)
    try:
        time.sleep(timeout)
    finally:
        # if the clipboard has changed in the meantime, try not to overwrite the
        # new value. still a race condition, but this should be better
        if str == get_clipboard():
            set_clipboard(current_clipboard)
Ejemplo n.º 2
0
def copy_to_clipboard(str, timeout=10):
    str = force_bytes(str)
    current_clipboard = get_clipboard()
    set_clipboard(str)
    try:
        time.sleep(timeout)
    finally:
        # if the clipboard has changed in the meantime, try not to overwrite the
        # new value. still a race condition, but this should be better
        if str == get_clipboard():
            set_clipboard(current_clipboard)
Ejemplo n.º 3
0
def dencrypt(command, pw, data):
    """
    Encrypts or decrypts, by running command
    """
    if '\n' in pw:
        raise Exception('Newlines not allowed in passwords')
    proc = subprocess.Popen(command,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    proc.stdin.write(force_bytes(pw))
    proc.stdin.write(b'\n')
    proc.stdin.write(data)
    output, erroroutput = proc.communicate()
    if proc.returncode != 0:
        raise gpg_exception_factory(proc.returncode, erroroutput)
    return output
Ejemplo n.º 4
0
    def __init__(self, args, output=sys.stdout, input=sys.stdin, password=None):
        self.args = args
        self.file = args.file
        self.output = output
        self.input = input

        try:
            self.gpg_agent = gpg_agent.GpgAgent()
        except KeyError:
            self.gpg_agent = None

        self.gpg_agent_password_id = 'sdb_m:{file_fingerprint}'.format(
            file_fingerprint=hashlib.md5(force_bytes(self.file)).hexdigest()
        )

        self.password = password
        if not self.password:
            self.password = self.get_master_password()
Ejemplo n.º 5
0
def dencrypt(command, pw, data):
    """
    Encrypts or decrypts, by running command
    """
    if '\n' in pw:
        raise Exception('Newlines not allowed in passwords')
    proc = subprocess.Popen(
        command,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )
    proc.stdin.write(force_bytes(pw))
    proc.stdin.write(b'\n')
    proc.stdin.write(data)
    output, erroroutput = proc.communicate()
    if proc.returncode != 0:
        raise gpg_exception_factory(proc.returncode, erroroutput)
    return output
Ejemplo n.º 6
0
    def __init__(self,
                 args,
                 output=sys.stdout,
                 input=sys.stdin,
                 password=None):
        self.args = args
        self.file = args.file
        self.output = output
        self.input = input

        try:
            self.gpg_agent = gpg_agent.GpgAgent()
        except KeyError:
            self.gpg_agent = None

        self.gpg_agent_password_id = 'sdb_m:{file_fingerprint}'.format(
            file_fingerprint=hashlib.md5(force_bytes(self.file)).hexdigest())

        self.password = password
        if not self.password:
            self.password = self.get_master_password()
Ejemplo n.º 7
0
def set_clipboard_once(str):
    """
    Set the clipboard to str, and wait for it to be retrieved once.
    """
    current = get_clipboard()
    command = ['xsel', '-pi', '-vvvv', '-n']
    proc = subprocess.Popen(command,
                            stdin=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    proc.stdin.write(force_bytes(str))
    proc.stdin.close()
    expected_returncode = 0
    stderr_output = b''
    while True:
        rfds, _, _ = select.select([sys.stdin, proc.stderr], [], [])
        if sys.stdin in rfds:
            # line from std == cancel
            sys.stdin.readline()
            proc.kill()
            expected_returncode = -9
            break
        else:
            line = proc.stderr.readline()
            stderr_output += line
        if not line:
            # xsel quit, probably because someone else took ownership of the
            # selection
            break
        elif b'(UTF8_STRING)' in line or b'(TEXT)' in line or b'(XSEL_DATA)' in line:
            # someone retrieved the selection, all done
            proc.kill()
            expected_returncode = -9
            break
    proc.wait()
    if proc.returncode != expected_returncode:
        raise ClipboardException(proc.returncode, command, stderr_output)
    if get_clipboard() == b'':
        set_clipboard(current)
Ejemplo n.º 8
0
def set_clipboard_once(str):
    """
    Set the clipboard to str, and wait for it to be retrieved once.
    """
    current = get_clipboard()
    command = ['xsel', '-pi', '-vvvv', '-n']
    proc = subprocess.Popen(command, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    proc.stdin.write(force_bytes(str))
    proc.stdin.close()
    expected_returncode = 0
    stderr_output = b''
    while True:
        rfds, _, _ = select.select([sys.stdin, proc.stderr], [], [])
        if sys.stdin in rfds:
            # line from std == cancel
            sys.stdin.readline()
            proc.kill()
            expected_returncode = -9
            break
        else:
            line = proc.stderr.readline()
            stderr_output += line
        if not line:
            # xsel quit, probably because someone else took ownership of the
            # selection
            break
        elif b'(UTF8_STRING)' in line or b'(TEXT)' in line or b'(XSEL_DATA)' in line:
            # someone retrieved the selection, all done
            proc.kill()
            expected_returncode = -9
            break
    proc.wait()
    if proc.returncode != expected_returncode:
        raise ClipboardException(proc.returncode, command, stderr_output)
    if get_clipboard() == b'':
        set_clipboard(current)