<s> """<STR_LIT>""" <EOL> import sys , os <EOL> import re <EOL> from pycopia import proctools <EOL> DOW = { <EOL> <NUM_LIT:0> : "<STR_LIT>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" , <EOL> <NUM_LIT:4> : "<STR_LIT>" , <EOL> <NUM_LIT:5> : "<STR_LIT>" , <EOL> <NUM_LIT:6> : "<STR_LIT>" , <EOL> <NUM_LIT:7> : "<STR_LIT>" <EOL> } <EOL> MONTHS = { <EOL> <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" , <EOL> <NUM_LIT:4> : "<STR_LIT>" , <EOL> <NUM_LIT:5> : "<STR_LIT>" , <EOL> <NUM_LIT:6> : "<STR_LIT>" , <EOL> <NUM_LIT:7> : "<STR_LIT>" , <EOL> <NUM_LIT:8> : "<STR_LIT>" , <EOL> <NUM_LIT:9> : "<STR_LIT>" , <EOL> <NUM_LIT:10> : "<STR_LIT>" , <EOL> <NUM_LIT:11> : "<STR_LIT>" , <EOL> <NUM_LIT:12> : "<STR_LIT>" , <EOL> } <EOL> class Comment ( object ) : <EOL> def __init__ ( self , text ) : <EOL> self . text = text <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . text , ) <EOL> def match ( self , pattern ) : <EOL> if re . match ( pattern , self . text ) : <EOL> return True <EOL> return False <EOL> class Variable ( object ) : <EOL> def __init__ ( self , name , value ) : <EOL> self . name = name <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . name , self . value ) <EOL> def match ( self , value = None , name = None ) : <EOL> nv = vv = False <EOL> if value : <EOL> if re . search ( value , self . value ) : <EOL> vv = True <EOL> else : <EOL> vv = False <EOL> if name : <EOL> if re . search ( name , self . name ) : <EOL> nv = True <EOL> else : <EOL> nv = False <EOL> if name and value : <EOL> return nv and vv <EOL> if name and not value : <EOL> return nv <EOL> if not name and value : <EOL> return vv <EOL> if not name and not value : <EOL> return False <EOL> class BlankLine ( object ) : <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def match ( self , pattern = None ) : <EOL> return False <EOL> class CrontabLine ( object ) : <EOL> def __init__ ( self , cmd , minute = None , hour = None , day_of_month = None , <EOL> month = None , day_of_week = None ) : <EOL> self . minute = Minutes ( minute ) <EOL> self . hour = Hours ( hour ) <EOL> self . day_of_month = DayOfMonth ( day_of_month ) <EOL> self . month = Month ( month ) <EOL> self . day_of_week = DayOfWeek ( day_of_week ) <EOL> self . command = cmd <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . minute , <EOL> self . hour , <EOL> self . day_of_month , <EOL> self . month , <EOL> self . day_of_week , <EOL> self . command ) <EOL> def match ( self , command = None , minute = None , hour = None , day_of_month = None , <EOL> month = None , day_of_week = None ) : <EOL> if command : <EOL> if re . search ( command , self . command ) is None : <EOL> return False <EOL> for attr , check in ( ( self . minute , minute ) , ( self . hour , hour ) , <EOL> ( self . day_of_month , day_of_month ) , <EOL> ( self . month , month ) , ( self . day_of_week , day_of_week ) ) : <EOL> if check is not None : <EOL> checker = attr . new ( check ) <EOL> if checker != attr : <EOL> return False <EOL> return True <EOL> class Selection ( object ) : <EOL> def __init__ ( self , start , end ) : <EOL> self . start = start <EOL> self . end = end <EOL> class CrontabFile ( object ) : <EOL> """<STR_LIT>""" <EOL> BLANK_RE = re . compile ( r"<STR_LIT>" ) <EOL> VARIABLE_RE = re . compile ( r"<STR_LIT>" ) <EOL> COMMENT_RE = re . compile ( r"<STR_LIT>" ) <EOL> def __init__ ( self , text = None , filename = None , username = None ) : <EOL> self . filename = filename <EOL> self . username = username <EOL> if text : <EOL> self . _lines = self . parse ( text ) <EOL> else : <EOL> self . _lines = [ ] <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT:\n>" . join ( map ( str , self . _lines ) ) + "<STR_LIT:\n>" <EOL> def __getitem__ ( self , index ) : <EOL> return self . _lines [ index ] <EOL> def __setitem__ ( self , index , value ) : <EOL> self . _lines [ index ] = value <EOL> def __delitem__ ( self , index ) : <EOL> del self . _lines [ index ] <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _lines ) <EOL> def parse ( self , text ) : <EOL> lines = [ ] <EOL> for line in text . splitlines ( ) : <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> mo = CrontabFile . COMMENT_RE . match ( line ) <EOL> if mo : <EOL> lines . append ( Comment ( mo . group ( <NUM_LIT:1> ) . strip ( ) ) ) <EOL> continue <EOL> mo = CrontabFile . VARIABLE_RE . match ( line ) <EOL> if mo : <EOL> lines . append ( Variable ( mo . group ( <NUM_LIT:1> ) , mo . group ( <NUM_LIT:2> ) ) ) <EOL> continue <EOL> mo = CrontabFile . BLANK_RE . match ( line ) <EOL> if mo : <EOL> lines . append ( BlankLine ( ) ) <EOL> continue <EOL> try : <EOL> [ minute , hour , dom , month , dow , cmd ] = line . split ( None , <NUM_LIT:5> ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> lines . append ( CrontabLine ( cmd , minute , hour , dom , month , dow ) ) <EOL> return lines <EOL> def add_comment ( self , text , lineno = None ) : <EOL> cmnt = Comment ( text ) <EOL> if lineno is None : <EOL> self . _lines . append ( cmnt ) <EOL> else : <EOL> self . _lines . insert ( lineno , cmnt ) <EOL> def add_variable ( self , name , value , lineno = None ) : <EOL> var = Variable ( name , value ) <EOL> if lineno is None : <EOL> self . _lines . append ( var ) <EOL> else : <EOL> self . _lines . insert ( lineno , var ) <EOL> def add_blank ( self , lineno = None ) : <EOL> if lineno is None : <EOL> self . _lines . append ( BlankLine ( ) ) <EOL> else : <EOL> self . _lines . insert ( lineno , BlankLine ( ) ) <EOL> def add_crontab ( self , cmd , minute = None , hour = None , day_of_month = None , <EOL> month = None , dow = None , lineno = None ) : <EOL> ctl = CrontabLine ( cmd , minute , hour , day_of_month , month , dow ) <EOL> if lineno is None : <EOL> self . _lines . append ( ctl ) <EOL> else : <EOL> self . _lines . insert ( lineno , ctl ) <EOL> def add_event ( self , ical ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def append ( self , obj , lineno = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , ( CrontabLine , BlankLine , Variable , Comment ) ) : <EOL> if lineno is None : <EOL> self . _lines . append ( obj ) <EOL> else : <EOL> self . _lines . insert ( lineno , obj ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def insert ( self , lineno , obj ) : <EOL> self . append ( obj , lineno ) <EOL> def extend ( self , objlist ) : <EOL> for obj in objlist : <EOL> self . append ( obj ) <EOL> def _find ( self , start , end , ** kwargs ) : <EOL> for entry in self . _lines [ start : end ] : <EOL> if entry . match ( ** kwargs ) : <EOL> return entry <EOL> return None <EOL> def _find_i ( self , start , end , cttype , ** kwargs ) : <EOL> for i , entry in enumerate ( self . _lines [ start : end ] ) : <EOL> if type ( entry ) is cttype and entry . match ( ** kwargs ) : <EOL> return i + start <EOL> return - <NUM_LIT:1> <EOL> def find ( self , pattern , start = <NUM_LIT:0> , end = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . _find ( start , end , command = pattern ) <EOL> def find_variable ( self , namepattern = None , valuepattern = None , start = <NUM_LIT:0> ) : <EOL> for i , obj in enumerate ( self . _lines [ start : ] ) : <EOL> if type ( obj ) is Variable : <EOL> if obj . match ( valuepattern , namepattern ) : <EOL> return i , obj <EOL> return - <NUM_LIT:1> , None <EOL> def find_crontab ( self , command = None , minute = None , hour = None , day_of_month = None , <EOL> month = None , day_of_week = None , start = <NUM_LIT:0> ) : <EOL> for i , obj in enumerate ( self . _lines [ start : ] ) : <EOL> if type ( obj ) is CrontabLine : <EOL> if obj . match ( command = command , minute = minute , hour = hour , <EOL> day_of_month = day_of_month , month = month , <EOL> day_of_week = day_of_week ) : <EOL> return i , obj <EOL> return - <NUM_LIT:1> , None <EOL> def find_comment ( self , pattern , start = <NUM_LIT:0> ) : <EOL> for i , obj in enumerate ( self . _lines [ start : ] ) : <EOL> if type ( obj ) is Comment : <EOL> if obj . match ( pattern ) : <EOL> return i , obj <EOL> return - <NUM_LIT:1> , None <EOL> def findall ( self , pattern , start = <NUM_LIT:0> , end = None ) : <EOL> rv = [ ] <EOL> for entry in self . _lines [ start : end ] : <EOL> if entry . match ( pattern ) : <EOL> rv . append ( entry ) <EOL> return rv <EOL> def select ( self , start , end ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( start , int ) : <EOL> start = self . _find_i ( <NUM_LIT:0> , - <NUM_LIT:1> , CrontabLine , command = start ) <EOL> if not isinstance ( end , int ) : <EOL> if end == "<STR_LIT:$>" : <EOL> end = len ( self . _lines ) <EOL> end = self . _find_i ( start , - <NUM_LIT:1> , CrontabLine , command = end ) <EOL> return Selection ( start , end ) <EOL> def replace ( self , selection , cronobjects ) : <EOL> """<STR_LIT>""" <EOL> self . _lines [ selection . start : selection . end ] = cronobjects <EOL> def delete ( self , start , end ) : <EOL> del self . _lines [ start : end ] <EOL> def delete_crontab ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> i , line = self . find_crontab ( ** kwargs ) <EOL> if line : <EOL> del self . _lines [ i ] <EOL> return i <EOL> def delete_all_crontab ( self , ** kwargs ) : <EOL> pos = kwargs . get ( "<STR_LIT:start>" , <NUM_LIT:0> ) <EOL> while pos >= <NUM_LIT:0> : <EOL> pos = self . delete_crontab ( ** kwargs ) <EOL> def delete_variable ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> i , line = self . find_variable ( ** kwargs ) <EOL> if line : <EOL> del self . _lines [ i ] <EOL> return i <EOL> def replace_variable ( self , name , value ) : <EOL> i , line = self . find_variable ( namepattern = name ) <EOL> if line : <EOL> del self . _lines [ i ] <EOL> var = Variable ( name , value ) <EOL> self . _lines . insert ( i , var ) <EOL> def delete_comment ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> i , line = self . find_comment ( ** kwargs ) <EOL> if line : <EOL> del self . _lines [ i ] <EOL> return i <EOL> def write ( self , filename = None ) : <EOL> name = filename or self . filename <EOL> if name : <EOL> fo = open ( name , "<STR_LIT:w>" ) <EOL> try : <EOL> fo . write ( str ( self ) ) <EOL> finally : <EOL> fo . close ( ) <EOL> else : <EOL> submit ( self ) <EOL> def submit ( self , username = None ) : <EOL> username = username or self . username <EOL> return submit ( self , username = username ) <EOL> def make_crontab ( self , filename = None ) : <EOL> return CrontabFile ( name = filename ) <EOL> def open ( filename = None , username = None ) : <EOL> if filename is None : <EOL> cmd = "<STR_LIT>" <EOL> if username : <EOL> cmd += "<STR_LIT>" % ( username , ) <EOL> ct = proctools . spawnpipe ( cmd ) <EOL> text = ct . read ( ) <EOL> ct . close ( ) <EOL> else : <EOL> text = open ( filename ) . read ( ) <EOL> return CrontabFile ( text , filename , username ) <EOL> def submit ( crontab , username = None , password = None ) : <EOL> """<STR_LIT>""" <EOL> if username is None : <EOL> ct = proctools . spawnpipe ( "<STR_LIT>" ) <EOL> ct . write ( str ( crontab ) ) <EOL> ct . close ( ) <EOL> return ct . wait ( ) <EOL> else : <EOL> if os . getuid ( ) == <NUM_LIT:0> : <EOL> ct = proctools . spawnpipe ( "<STR_LIT>" % ( username , ) ) <EOL> ct . write ( str ( crontab ) ) <EOL> ct . close ( ) <EOL> return ct . wait ( ) <EOL> else : <EOL> from pycopia import sudo <EOL> if password is None : <EOL> from pycopia import tty <EOL> password = tty . getpass ( "<STR_LIT>" ) <EOL> ct = sudo . sudo ( "<STR_LIT>" % ( username , ) , password = password ) <EOL> ct . write ( str ( crontab ) ) <EOL> ct . close ( ) <EOL> return ct . wait ( ) <EOL> class IntSet ( object ) : <EOL> """<STR_LIT>""" <EOL> MIN = <NUM_LIT:0> <EOL> MAX = <NUM_LIT> <EOL> def __init__ ( self , data = None , sep = '<STR_LIT:U+002C>' , rng = '<STR_LIT:->' ) : <EOL> self . pairs = [ ] <EOL> self . sep = sep <EOL> self . rng = rng <EOL> if data : <EOL> self . parse ( str ( data ) ) <EOL> else : <EOL> self . pairs . append ( ( self . MIN , self . MAX ) ) <EOL> def reset ( self ) : <EOL> self . pairs = [ ] <EOL> def __hash__ ( self ) : <EOL> return hash ( self . pairs ) <EOL> def __eq__ ( self , other ) : <EOL> return self . pairs == other . pairs <EOL> def __ne__ ( self , other ) : <EOL> return self . pairs != other . pairs <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , <EOL> self . __str__ ( ) , self . sep , self . rng ) <EOL> def __str__ ( self ) : <EOL> if ( len ( self . pairs ) == <NUM_LIT:1> <EOL> and self . pairs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == self . MIN <EOL> and self . pairs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] == self . MAX ) : <EOL> return "<STR_LIT:*>" <EOL> s = '<STR_LIT>' <EOL> for lo , hi in self . pairs : <EOL> if lo == hi : <EOL> t = repr ( lo ) <EOL> else : <EOL> t = repr ( lo ) + self . rng + repr ( hi ) <EOL> if s : <EOL> s += ( self . sep + t ) <EOL> else : <EOL> s = t <EOL> return s <EOL> def normalize ( self ) : <EOL> self . pairs . sort ( ) <EOL> i = <NUM_LIT:1> <EOL> while i < len ( self . pairs ) : <EOL> alo , ahi = self . pairs [ i - <NUM_LIT:1> ] <EOL> blo , bhi = self . pairs [ i ] <EOL> if ahi >= blo - <NUM_LIT:1> : <EOL> self . pairs [ i - <NUM_LIT:1> : i + <NUM_LIT:1> ] = [ ( alo , max ( ahi , bhi ) ) ] <EOL> else : <EOL> i = i + <NUM_LIT:1> <EOL> def tolist ( self ) : <EOL> l = [ ] <EOL> for lo , hi in self . pairs : <EOL> m = range ( lo , hi + <NUM_LIT:1> ) <EOL> l . extend ( m ) <EOL> return l <EOL> def fromlist ( self , list ) : <EOL> for i in list : <EOL> self . append ( i ) <EOL> def copy ( self ) : <EOL> new = self . __class__ ( sep = self . sep , rng = self . rng ) <EOL> new . pairs = self . pairs [ : ] <EOL> return new <EOL> def new ( self , data = None , sep = None , rng = None ) : <EOL> return self . __class__ ( data , sep or self . sep , rng or self . rng ) <EOL> def min ( self ) : <EOL> return self . pairs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> def max ( self ) : <EOL> return self . pairs [ - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] <EOL> def contains ( self , x ) : <EOL> for lo , hi in self . pairs : <EOL> if lo <= x <= hi : <EOL> return True <EOL> return False <EOL> __contains__ = contains <EOL> def append ( self , x ) : <EOL> for i in range ( len ( self . pairs ) ) : <EOL> lo , hi = self . pairs [ i ] <EOL> if x < lo : <EOL> if x + <NUM_LIT:1> == lo : <EOL> self . pairs [ i ] = ( x , hi ) <EOL> else : <EOL> self . pairs . insert ( i , ( x , x ) ) <EOL> if i > <NUM_LIT:0> and x - <NUM_LIT:1> == self . pairs [ i - <NUM_LIT:1> ] [ <NUM_LIT:1> ] : <EOL> self . pairs [ i - <NUM_LIT:1> : i + <NUM_LIT:1> ] = [ <EOL> ( self . pairs [ i - <NUM_LIT:1> ] [ <NUM_LIT:0> ] , <EOL> self . pairs [ i ] [ <NUM_LIT:1> ] ) <EOL> ] <EOL> return <EOL> if x <= hi : <EOL> return <EOL> i = len ( self . pairs ) - <NUM_LIT:1> <EOL> if i >= <NUM_LIT:0> : <EOL> lo , hi = self . pairs [ i ] <EOL> if x - <NUM_LIT:1> == hi : <EOL> self . pairs [ i ] = lo , x <EOL> return <EOL> self . pairs . append ( ( x , x ) ) <EOL> def addpair ( self , xlo , xhi ) : <EOL> if xlo > xhi : <EOL> return <EOL> self . pairs . append ( ( xlo , xhi ) ) <EOL> self . normalize ( ) <EOL> def parse ( self , data ) : <EOL> new = [ ] <EOL> for part in data . split ( self . sep ) : <EOL> if part == "<STR_LIT:*>" : <EOL> new . append ( ( self . MIN , self . MAX ) ) <EOL> break <EOL> tmplist = [ ] <EOL> for subp in part . split ( self . rng ) : <EOL> s = subp . strip ( ) <EOL> tmplist . append ( int ( s ) ) <EOL> if len ( tmplist ) == <NUM_LIT:1> : <EOL> new . append ( ( tmplist [ <NUM_LIT:0> ] , tmplist [ <NUM_LIT:0> ] ) ) <EOL> elif len ( tmplist ) == <NUM_LIT:2> and tmplist [ <NUM_LIT:0> ] <= tmplist [ <NUM_LIT:1> ] : <EOL> new . append ( ( tmplist [ <NUM_LIT:0> ] , tmplist [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( data , ) ) <EOL> self . pairs . extend ( new ) <EOL> self . normalize ( ) <EOL> class Minutes ( IntSet ) : <EOL> MIN = <NUM_LIT:0> <EOL> MAX = <NUM_LIT> <EOL> class Hours ( IntSet ) : <EOL> MIN = <NUM_LIT:0> <EOL> MAX = <NUM_LIT> <EOL> class DayOfMonth ( IntSet ) : <EOL> MIN = <NUM_LIT:1> <EOL> MAX = <NUM_LIT> <EOL> class Month ( IntSet ) : <EOL> MIN = <NUM_LIT:1> <EOL> MAX = <NUM_LIT:12> <EOL> class DayOfWeek ( IntSet ) : <EOL> MIN = <NUM_LIT:0> <EOL> MAX = <NUM_LIT:7> </s>
