def test_run_cancels_timer_thread_on_keyboard_interrupt(): mock_process = mock.Mock() mock_timer_object = mock.Mock() with contextlib.nested( mock.patch("utils.Popen", autospec=True, return_value=mock_process), mock.patch("utils.threading.Timer", autospec=True, return_value=mock_timer_object), ) as (mock_popen, mock_timer): mock_process.stdout.readline.side_effect = KeyboardInterrupt with raises(KeyboardInterrupt): utils._run("sh echo foo", timeout=10) assert mock_timer_object.cancel.call_count == 1
def test_run_cancels_timer_thread_on_keyboard_interrupt(): mock_process = mock.Mock() mock_timer_object = mock.Mock() with contextlib.nested( mock.patch('utils.Popen', autospec=True, return_value=mock_process), mock.patch('utils.threading.Timer', autospec=True, return_value=mock_timer_object), ) as (mock_popen, mock_timer): mock_process.stdout.readline.side_effect = KeyboardInterrupt with raises(KeyboardInterrupt): utils._run('sh echo foo', timeout=10) assert mock_timer_object.cancel.call_count == 1
def check_ims(tfrecords_filename="data/train-00000-of-00001", nIm=1): record_iterator = tf.python_io.tf_record_iterator(path=tfrecords_filename) shitfiles = [] k = 0 for string_record in record_iterator: # Parse the next example example = tf.train.Example() example.ParseFromString(string_record) # Get the features you stored (change to match your tfrecord writing code) height = int( example.features.feature['image/height'].int64_list.value[0]) width = int( example.features.feature['image/width'].int64_list.value[0]) img_string = ( example.features.feature['image/encoded'].bytes_list.value[0]) #text = example.features.feature['image/class/text'].bytes_list.value[0] #k = k+1 name = example.features.feature['image/filename'].bytes_list.value[ 0].decode('utf-8') # Convert to a numpy array (change dtype to the datatype you stored) #img_1d = np.fromstring(img_string, dtype=np.uint8) #tfimg_1d = tf.decode_raw(example.features.feature['image/encoded'],tf.uint8) #tfimg_1d = tf.decode_raw(img_string,tf.uint8) try: lab = example.features.feature[ 'image/class/label'].int64_list.value[0] except IndexError: lab = 'no Lab' shitfiles = shitfiles + [name] k = k + 1 if k > nIm: break if k % 500 == 0: image = _run(tf.image.decode_jpeg(img_string, channels=1)) print(image.shape) print(str(lab) + ' ' + name) plt.imshow(image[:, :, 0]) plt.title('lab= ' + str(lab) + ' example ' + str(k)) plt.pause(0.1) #Print the image shape; does it match your expectations? #if k>nIm: break return name, _run(tf.image.decode_jpeg(img_string, channels=1))
def setup_config_session(self): """ Setup vyos session. A random uuid is generated as a sesssion identifier ($PPID -Shell PID- could be used as well). """ identifier = uuid4() env = {} env['VYATTA_CHANGES_ONLY_DIR'] = '/opt/vyatta/config/tmp/changes_only_{}'.format( identifier) env['VYATTA_CONFIG_TEMPLATE'] = '/opt/vyatta/share/vyatta-cfg/templates' env['VYATTA_ACTIVE_CONFIGURATION_DIR'] = '/opt/vyatta/config/active' env['VYATTA_EDIT_LEVEL'] = '/' env['VYATTA_TEMP_CONFIG_DIR'] = '/opt/vyatta/config/tmp/new_config_{}'.format( identifier) env['VYATTA_TEMPLATE_LEVEL'] = '/' env['VYATTA_CONFIG_TMP'] = '/opt/vyatta/config/tmp/tmp_{}'.format( identifier) # Add vyos session environment to system environment. This is not good but actually it seems # that is the only way to handle a persistant vyos session after spawning a shell. os.environ.update(env) logger.info('Setting up a configuration session for Vyos') # Spawn shell and setup vyos config session if _run('{} setupSession'.format(VYOS_SHELL_API)): # Unset vyos session environment and raise an exception logger.error('Could not create configuration session') logger.info('Cleaning up session environment variables') clean_environ(env) raise SetupSessionFailed('Could not create session !') self.session_id = identifier self.session_envs = env logger.debug('Session identifier is %s', identifier) logger.debug('Session environment variables: %s', env) logger.info('Configuration session is set up') return True
def setup_config_session(self): """ Setup vyos session. A random uuid is generated as a sesssion identifier ($PPID -Shell PID- could be used as well). """ identifier = uuid4() env = {} env['VYATTA_CHANGES_ONLY_DIR'] = '/opt/vyatta/config/tmp/changes_only_{}'.format(identifier) env['VYATTA_CONFIG_TEMPLATE'] = '/opt/vyatta/share/vyatta-cfg/templates' env['VYATTA_ACTIVE_CONFIGURATION_DIR'] = '/opt/vyatta/config/active' env['VYATTA_EDIT_LEVEL'] = '/' env['VYATTA_TEMP_CONFIG_DIR'] = '/opt/vyatta/config/tmp/new_config_{}'.format(identifier) env['VYATTA_TEMPLATE_LEVEL'] = '/' env['VYATTA_CONFIG_TMP'] = '/opt/vyatta/config/tmp/tmp_{}'.format(identifier) # Add vyos session environment to system environment. This is not good but actually it seems # that is the only way to handle a persistant vyos session after spawning a shell. os.environ.update(env) logger.info('Setting up a configuration session for Vyos') # Spawn shell and setup vyos config session if _run('{} setupSession'.format(VYOS_SHELL_API)): # Unset vyos session environment and raise an exception logger.error('Could not create configuration session') logger.info('Cleaning up session environment variables') clean_environ(env) raise SetupSessionFailed('Could not create session !') self.session_id = identifier self.session_envs = env logger.debug('Session identifier is %s', identifier) logger.debug('Session environment variables: %s', env) logger.info('Configuration session is set up') return True
def session_changed(self): """ Returns if Vyos configuration was changed from current session """ if _run('{} sessionChanged'.format(VYOS_SHELL_API)): return False logger.warn('Vyos configuration was changed from current session') return True
def discard(self): """ Undo config modifications """ out = _run(os.path.join(VYOS_SBIN_DIR ,'my_discard'), output=True) if out == False: raise OperationFailed('[ERROR] Discard changes failed !') return out.splitlines()[0]
def discard(self): """ Undo config modifications """ out = _run(os.path.join(VYOS_SBIN_DIR, 'my_discard'), output=True) if out == False: raise OperationFailed('[ERROR] Discard changes failed !') return out.splitlines()[0]
def save(self): """ Save applied modifications. Changes still persistent even after system reboot. """ out = _run(os.path.join(VYOS_SBIN_DIR, VYOS_SAVE_SCRIPT), output=True) if out == False: logger.error('Saving changes failed') raise OperationFailed('[ERROR] Save changes failed !') logger.info('%s', out) return True
def commit(self): """ Returns True if commit action succeed. False otherwise. """ out = _run(os.path.join(VYOS_SBIN_DIR ,'my_commit'), output=True) if out == False: logger.error('Commit changes failed') raise OperationFailed('[ERROR] Commit changes failed !') logger.info('Changes successfully commited') return True
def commit(self): """ Returns True if commit action succeed. False otherwise. """ out = _run(os.path.join(VYOS_SBIN_DIR, 'my_commit'), output=True) if out == False: logger.error('Commit changes failed') raise OperationFailed('[ERROR] Commit changes failed !') logger.info('Changes successfully commited') return True
def teardown_config_session(self): """ End current configuration session. """ if not self.session_exists(): logger.warn('Teardown failed. No session available !') return False if not _run('{} teardownSession'.format(VYOS_SHELL_API)): logger.info('Cleaning up session environment variables') logger.info('Closing Vyos config session') clean_environ(self.session_envs) return True logger.error('Failed to teardown current config session') logger.warn('The Vyos config session may still open !') return False
def session_exists(self): """ Test if a vyos config session is set up """ return False if _run('{} inSession'.format(VYOS_SHELL_API)) else True