def render(self, runner, object): if object is None: return None try: return force_str(self.inner.render(runner, object)) except ValueError: return str(self.inner.render(runner, object))
def echo(self, text, nl=True, **kwargs): text = force_str(text) if threading.current_thread().name != 'MainThread': text = '[{}] {}'.format(threading.current_thread().name, text) if nl: self._echo('{}\n'.format(text)) else: self._echo(text)
def read(self): fp = self.runner.get_service(self.resource.file, 'fileio') config = configparser.ConfigParser() try: config.readfp(six.StringIO(force_str(fp.read().read()))) except FileNotFound: pass return config
def echo(self, text, nl=True, **kwargs): text = force_str(text) if threading.current_thread().name != "MainThread": text = "[{}] {}".format(threading.current_thread().name, text) if nl: self._echo("{}\n".format(text)) else: self._echo(text)
def dsa_private_key(): private_key = force_str(dsa.generate_private_key( key_size=1024, backend=default_backend() )) return private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption(), )
def rsa_private_key(): private_key = rsa.generate_private_key( public_exponent=65537, key_size=4096, backend=default_backend() ) return force_str(private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption(), ))
def render(self, runner, value): private_key = serialization.load_pem_private_key( force_bytes(value), password=None, backend=default_backend(), ) numbers = private_key.public_key().public_numbers() output = b'' parts = [b'ssh-rsa', deflate_long(numbers.e), deflate_long(numbers.n)] for part in parts: output += struct.pack('>I', len(part)) + part return force_str(b'ssh-rsa ' + base64.b64encode(output) + b'\n')
def _run(self, transport, command, input_encoding=None, echo=None): if echo is None: echo = self.ui.echo if input_encoding is None: input_encoding = self.input_encoding channel = transport.open_session() self.setup_forwarded_keys(channel) try: channel.exec_command(command) # We don't want a stdin channel.makefile('wb', -1).close() r_stdout = channel.makefile('r') r_stderr = channel.makefile_stderr('r') # FIXME: It would be better to block with select exit_status_ready = channel.exit_status_ready() while not exit_status_ready: while channel.recv_ready(): echo(force_str(r_stdout.readline()), nl=False) while channel.recv_stderr_ready(): echo(force_str(r_stderr.readline()), nl=False) time.sleep(1) exit_status_ready = channel.exit_status_ready() for line in r_stdout.readlines(): echo(line, nl=False) for line in r_stderr.readlines(): echo(line, nl=False) exit_code = channel.recv_exit_status() if exit_code != 0: raise errors.RemoteCommandFailed(exit_code) finally: channel.close()
def add_describe_launch_configurations_one_response(self, user_data=None): launch_config = { 'LaunchConfigurationName': self.resource.name, 'ImageId': 'ami-cba130bc', 'InstanceType': 't2.micro', 'CreatedTime': datetime.datetime.now(), } if user_data: launch_config['UserData'] = force_str(base64.b64encode(force_bytes(user_data))) return self.add_response( 'describe_launch_configurations', service_response={ 'LaunchConfigurations': [launch_config], }, expected_params={}, )
def pull(kwargs, previous_events): seen = set() filters = {} filters.update(kwargs) results = self.client.filter_log_events(**filters) while True: for event in results.get('events', []): seen.add(event['eventId']) if event['eventId'] in previous_events: continue self.runner.ui.echo('[{timestamp}] [{logStreamName}] {message}'.format(**{ 'logStreamName': event.get('logStreamName', ''), 'message': force_str(event['message'].encode('utf-8', 'ignore')), 'timestamp': datetime.datetime.utcfromtimestamp(int(event['timestamp']) / 1000.0), })) kwargs['startTime'] = event['timestamp'] if 'nextToken' not in results: break filters['nextToken'] = results['nextToken'] results = self.client.filter_log_events(**filters) return seen
def test_str_b(self): self.assertEqual(force_str(b'foo'), 'foo')
def test_str_u(self): self.assertEqual(force_str(u'foo'), 'foo')
def get_possible_objects(self): for obj in super(Describe, self).get_possible_objects(): if 'UserData' in obj and obj['UserData']: obj['UserData'] = force_str(base64.b64decode(obj['UserData'])) yield obj
def execute(self): f = self.runner.get_service(self.resource, 'fileio') return force_str(f.read().read()), True
def test_str_b(self): self.assertEqual(force_str(b"foo"), "foo")
def render(self, runner, object): try: return force_str(self.inner.render(runner, object)) except ValueError: return str(self.inner.render(runner, object))
def test_str_u(self): self.assertEqual(force_str(u"foo"), "foo")