<s> import os <EOL> import re <EOL> import json <EOL> import subprocess <EOL> import time <EOL> import datetime <EOL> from os . path import join as pjoin <EOL> def dump_config ( cfg , fname ) : <EOL> json . dump ( cfg , open ( fname , '<STR_LIT:w>' ) , sort_keys = True , indent = <NUM_LIT:4> , <EOL> separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) <EOL> def load_config ( fname ) : <EOL> return json . load ( open ( fname , '<STR_LIT:r>' ) ) <EOL> class CfgStruct : <EOL> def __init__ ( self , ** entries ) : <EOL> self . __dict__ . update ( entries ) <EOL> def add_config_val ( key , val , fname ) : <EOL> with open ( fname , '<STR_LIT:r>' ) as fin : <EOL> cfg = json . load ( fin ) <EOL> cfg [ key ] = val <EOL> dump_config ( cfg , fname ) <EOL> def get_git_revision ( ) : <EOL> return subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> stdout = subprocess . PIPE ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> def get_hostname ( ) : <EOL> import socket <EOL> hostname = socket . gethostname ( ) <EOL> hostname = hostname . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> return hostname <EOL> def touch_file ( fname ) : <EOL> try : <EOL> os . utime ( fname , None ) <EOL> except : <EOL> open ( fname , '<STR_LIT:a>' ) . close ( ) <EOL> def last_modified ( fname ) : <EOL> return os . path . getmtime ( fname ) <EOL> def file_alive ( fname , max_dur_sec = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:2> ) : <EOL> return ( int ( time . time ( ) ) - last_modified ( fname ) ) < max_dur_sec <EOL> class TimeString ( object ) : <EOL> def __init__ ( self , time = datetime . datetime . today ( ) ) : <EOL> self . time = time <EOL> def __str__ ( self ) : <EOL> s = str ( datetime . datetime . today ( ) ) <EOL> s = s . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> s = s . replace ( '<STR_LIT:->' , '<STR_LIT>' ) <EOL> s = s . replace ( '<STR_LIT::>' , '<STR_LIT>' ) <EOL> return s <EOL> @ classmethod <EOL> def match ( cls , s ) : <EOL> return re . match ( '<STR_LIT>' , s ) <EOL> @ classmethod <EOL> def from_string ( cls , string ) : <EOL> year = int ( string [ <NUM_LIT:0> : <NUM_LIT:4> ] ) <EOL> month = int ( string [ <NUM_LIT:4> : <NUM_LIT:6> ] ) <EOL> day = int ( string [ <NUM_LIT:6> : <NUM_LIT:8> ] ) <EOL> hour = int ( string [ <NUM_LIT:8> : <NUM_LIT:10> ] ) <EOL> minute = int ( string [ <NUM_LIT:10> : <NUM_LIT:12> ] ) <EOL> second = int ( string [ <NUM_LIT:12> : <NUM_LIT> ] ) <EOL> return TimeString ( time = datetime . datetime ( year , month , day , hour , minute , second ) ) <EOL> def get_run_dirs ( parent_dir ) : <EOL> run_dirs = list ( ) <EOL> for d in os . listdir ( parent_dir ) : <EOL> if TimeString . match ( d ) and not d . endswith ( '<STR_LIT>' ) : <EOL> run_dirs . append ( pjoin ( parent_dir , d ) ) <EOL> return run_dirs </s>
