<s> import sys <EOL> import os <EOL> import time <EOL> import shutil <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . abspath ( os . path . join ( os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) , "<STR_LIT:..>" ) ) ) <EOL> import configuration <EOL> import dbutils <EOL> import gitutils <EOL> import background . utils <EOL> class Maintenance ( background . utils . BackgroundProcess ) : <EOL> def __init__ ( self ) : <EOL> service = configuration . services . MAINTENANCE <EOL> super ( Maintenance , self ) . __init__ ( service = service ) <EOL> hour , minute = service [ "<STR_LIT>" ] <EOL> self . register_maintenance ( hour = hour , minute = minute , callback = self . __maintenance ) <EOL> def run ( self ) : <EOL> with dbutils . Database ( ) as db : <EOL> dbutils . loadTimezones ( db ) <EOL> super ( Maintenance , self ) . run ( ) <EOL> def __maintenance ( self ) : <EOL> with dbutils . Database ( ) as db : <EOL> cursor = db . cursor ( ) <EOL> self . debug ( "<STR_LIT>" ) <EOL> dbutils . updateTimezones ( db ) <EOL> if self . terminated : <EOL> return <EOL> if configuration . base . ARCHIVE_REVIEW_BRANCHES : <EOL> repository = None <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> for_update = True ) <EOL> for repository_id , branch_id , branch_name in cursor : <EOL> if not repository or repository . id != repository_id : <EOL> if repository : <EOL> repository . stopBatch ( ) <EOL> repository = gitutils . Repository . fromId ( db , repository_id ) <EOL> self . info ( "<STR_LIT>" + repository . name ) <EOL> self . info ( "<STR_LIT:U+0020>" + branch_name ) <EOL> branch = dbutils . Branch . fromId ( db , branch_id , repository = repository ) <EOL> try : <EOL> branch . archive ( db ) <EOL> except Exception : <EOL> self . exception ( as_warning = True ) <EOL> cursor . execute ( """<STR_LIT>""" ) <EOL> db . commit ( ) <EOL> cursor . execute ( "<STR_LIT>" ) <EOL> for ( repository_name , ) in cursor : <EOL> self . debug ( "<STR_LIT>" % repository_name ) <EOL> try : <EOL> repository = gitutils . Repository . fromName ( db , repository_name ) <EOL> repository . packKeepaliveRefs ( ) <EOL> repository . run ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> repository . stopBatch ( ) <EOL> except Exception : <EOL> self . exception ( "<STR_LIT>" % repository_name ) <EOL> if self . terminated : <EOL> return <EOL> if configuration . extensions . ENABLED : <EOL> now = time . time ( ) <EOL> max_age = <NUM_LIT:7> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> base_path = os . path . join ( configuration . paths . DATA_DIR , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> for user_name in os . listdir ( base_path ) : <EOL> user_dir = os . path . join ( base_path , user_name ) <EOL> for extension_id in os . listdir ( user_dir ) : <EOL> extension_dir = os . path . join ( user_dir , extension_id ) <EOL> for repository_name in os . listdir ( extension_dir ) : <EOL> repository_dir = os . path . join ( extension_dir , <EOL> repository_name ) <EOL> age = now - os . stat ( repository_dir ) . st_mtime <EOL> if age > max_age : <EOL> self . info ( "<STR_LIT>" <EOL> % repository_dir ) <EOL> shutil . rmtree ( repository_dir ) <EOL> def start_service ( ) : <EOL> maintenance = Maintenance ( ) <EOL> return maintenance . start ( ) <EOL> background . utils . call ( "<STR_LIT>" , start_service ) </s>
