<s> '''<STR_LIT>''' <EOL> import os <EOL> import sys <EOL> import shutil <EOL> import tempfile <EOL> from nose . tools import assert_equal , assert_false , assert_raises , assert_is_instance , assert_is_none , assert_less , assert_true <EOL> import log_utils as lu <EOL> CURRENT_MODULE_PATH = os . path . abspath ( sys . modules [ __name__ ] . __file__ ) <EOL> ROOT_PKG_PATH = os . path . dirname ( CURRENT_MODULE_PATH ) <EOL> TEST_DATA_DIRNAME = '<STR_LIT>' <EOL> TEST_LOG_FILENAME = '<STR_LIT>' <EOL> class TestPID : <EOL> def test_pid_from_str ( self ) : <EOL> n = <NUM_LIT> <EOL> res = lu . pid_from_str ( '<STR_LIT:%s>' % n ) <EOL> assert_is_instance ( res , int ) <EOL> assert_equal ( res , n ) <EOL> def test_pid_from_str_neg ( self ) : <EOL> n = - <NUM_LIT> <EOL> res = lu . pid_from_str ( '<STR_LIT:%s>' % n ) <EOL> assert_is_instance ( res , int ) <EOL> assert_equal ( res , n ) <EOL> def test_pid_from_str_invalid_alpha ( self ) : <EOL> n = <NUM_LIT> <EOL> x = [ '<STR_LIT>' % n , '<STR_LIT>' % n , '<STR_LIT>' ] <EOL> for s in x : <EOL> res = lu . pid_from_str ( s ) <EOL> assert_is_instance ( res , int ) <EOL> assert_equal ( res , - <NUM_LIT:1> ) <EOL> def test_pid_from_logname ( self ) : <EOL> s = '<STR_LIT>' <EOL> res = lu . pid_from_logname ( s ) <EOL> assert_is_instance ( res , int ) <EOL> assert_equal ( res , <NUM_LIT> ) <EOL> n = <NUM_LIT> <EOL> s = '<STR_LIT>' % n <EOL> res = lu . pid_from_logname ( s ) <EOL> assert_is_instance ( res , int ) <EOL> assert_equal ( res , n ) <EOL> def test_pid_from_logname_invalid ( self ) : <EOL> s = '<STR_LIT>' <EOL> res = lu . pid_from_logname ( s ) <EOL> assert_is_instance ( res , int ) <EOL> assert_equal ( res , - <NUM_LIT:1> ) <EOL> def test_read_pid ( self ) : <EOL> fpath = os . path . join ( os . path . dirname ( ROOT_PKG_PATH ) , <EOL> TEST_DATA_DIRNAME , <EOL> TEST_LOG_FILENAME ) <EOL> result = lu . read_pid ( fpath ) <EOL> assert_is_instance ( result , int ) <EOL> assert_equal ( result , <NUM_LIT> ) <EOL> def test_read_pid_from_content ( self ) : <EOL> fpath = os . path . join ( os . path . dirname ( ROOT_PKG_PATH ) , <EOL> TEST_DATA_DIRNAME , <EOL> TEST_LOG_FILENAME ) <EOL> path_temp_dir = tempfile . mkdtemp ( ) <EOL> fpath2 = os . path . join ( path_temp_dir , "<STR_LIT>" ) <EOL> shutil . copyfile ( fpath , fpath2 ) <EOL> try : <EOL> assert_less ( lu . pid_from_logname ( fpath2 ) , <NUM_LIT:0> ) <EOL> result = lu . read_pid ( fpath2 ) <EOL> assert_is_instance ( result , int ) <EOL> assert_equal ( result , <NUM_LIT> ) <EOL> except Exception : <EOL> pass <EOL> shutil . rmtree ( path_temp_dir ) <EOL> def test_read_pid_invalid ( self ) : <EOL> path_temp_dir = tempfile . mkdtemp ( ) <EOL> fpath = os . path . join ( path_temp_dir , TEST_LOG_FILENAME ) <EOL> with open ( fpath , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> assert_true ( lu . is_caffe_info_log ( fpath ) ) <EOL> fpath2 = os . path . join ( path_temp_dir , "<STR_LIT>" ) <EOL> shutil . copyfile ( fpath , fpath2 ) <EOL> assert_raises ( IOError , lu . read_pid , fpath2 ) <EOL> shutil . rmtree ( path_temp_dir ) <EOL> class TestCaffeLog : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . dir_tmp = tempfile . mkdtemp ( ) <EOL> self . path_real_log = os . path . join ( os . path . dirname ( ROOT_PKG_PATH ) , <EOL> TEST_DATA_DIRNAME , <EOL> TEST_LOG_FILENAME ) <EOL> @ classmethod <EOL> def teardown_class ( self ) : <EOL> shutil . rmtree ( self . dir_tmp ) <EOL> def test_is_caffe_log_invalid_dir ( self ) : <EOL> assert_true ( os . path . isdir ( self . dir_tmp ) ) <EOL> assert_false ( lu . is_caffe_log ( self . dir_tmp ) ) <EOL> def test_is_caffe_log ( self ) : <EOL> assert_true ( lu . is_caffe_log ( self . path_real_log ) ) <EOL> def test_is_caffe_log_invalid_prefix ( self ) : <EOL> fpath = os . path . join ( self . dir_tmp , <EOL> "<STR_LIT>" ) <EOL> with open ( fpath , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> assert_false ( lu . is_caffe_log ( fpath ) ) <EOL> def test_is_caffe_log_invalid_content ( self ) : <EOL> fpath = os . path . join ( self . dir_tmp , <EOL> "<STR_LIT>" ) <EOL> with open ( fpath , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT:foo>' ) <EOL> assert_false ( lu . is_caffe_log ( fpath ) ) <EOL> def test_is_caffe_info_log ( self ) : <EOL> assert_true ( lu . is_caffe_log ( self . path_real_log ) ) <EOL> def test_is_caffe_info_log_invalid_fname ( self ) : <EOL> fpath = os . path . join ( self . dir_tmp , <EOL> "<STR_LIT>" ) <EOL> with open ( fpath , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> assert_false ( lu . is_caffe_info_log ( fpath ) ) <EOL> def test_is_complete ( self ) : <EOL> assert_true ( lu . is_complete ( self . path_real_log ) ) <EOL> fpath = os . path . join ( self . dir_tmp , <EOL> "<STR_LIT>" ) <EOL> with open ( fpath , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> assert_false ( lu . is_complete ( fpath ) ) <EOL> class TestReadMeUtils : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . dir_tmp = tempfile . mkdtemp ( ) <EOL> self . path_real_log = os . path . join ( os . path . dirname ( ROOT_PKG_PATH ) , <EOL> TEST_DATA_DIRNAME , <EOL> TEST_LOG_FILENAME ) <EOL> shutil . copyfile ( self . path_real_log , <EOL> os . path . join ( self . dir_tmp , TEST_LOG_FILENAME ) ) <EOL> self . path_real_log = os . path . join ( self . dir_tmp , TEST_LOG_FILENAME ) <EOL> self . path_readme = os . path . join ( self . dir_tmp , '<STR_LIT>' ) <EOL> with open ( self . path_readme , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT:foo>' ) <EOL> @ classmethod <EOL> def teardown_class ( self ) : <EOL> shutil . rmtree ( self . dir_tmp ) <EOL> def test_get_rel_readme_path_from_dir ( self ) : <EOL> p = lu . get_rel_readme_path ( self . dir_tmp ) <EOL> assert_equal ( p , self . path_readme ) <EOL> def test_get_rel_readme_path_from_log ( self ) : <EOL> p = lu . get_rel_readme_path ( self . path_real_log ) <EOL> assert_equal ( p , self . path_readme ) <EOL> def test_readme_to_str ( self ) : <EOL> assert_equal ( lu . readme_to_str ( self . path_readme ) , "<STR_LIT:foo>" ) <EOL> class TestFindLine : <EOL> @ classmethod <EOL> def setup_class ( self ) : <EOL> self . dir_tmp = tempfile . mkdtemp ( ) <EOL> @ classmethod <EOL> def teardown_class ( self ) : <EOL> shutil . rmtree ( self . dir_tmp ) <EOL> def test_find_line ( self ) : <EOL> fpath = os . path . join ( self . dir_tmp , "<STR_LIT>" ) <EOL> with open ( fpath , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> assert_is_none ( lu . find_line ( fpath , '<STR_LIT:hello>' ) ) <EOL> assert_equal ( lu . find_line ( fpath , '<STR_LIT>' ) , '<STR_LIT>' + os . linesep ) <EOL> assert_equal ( lu . find_line ( fpath , '<STR_LIT>' ) , '<STR_LIT>' + os . linesep ) </s>
