<s> import os , glob , tempfile , shutil <EOL> class Gitolite ( object ) : <EOL> def __init__ ( self , path = '<STR_LIT>' ) : <EOL> self . _repo_path = path <EOL> self . _user_repo_config = path + "<STR_LIT>" <EOL> self . _key_path = path + "<STR_LIT>" <EOL> def addRepo ( self , username , reponame ) : <EOL> """<STR_LIT>""" <EOL> repo_data = self . __load_repo ( ) <EOL> repo = username + '<STR_LIT:/>' + reponame <EOL> if repo in repo_data : <EOL> return False <EOL> repo_data [ repo ] = [ ( '<STR_LIT>' , username ) ] <EOL> self . __save_repo ( repo_data ) <EOL> return True <EOL> def rmRepo ( self , username , reponame ) : <EOL> """<STR_LIT>""" <EOL> repo_data = self . __load_repo ( ) <EOL> repo = username + '<STR_LIT:/>' + reponame <EOL> if repo not in repo_data : <EOL> return False <EOL> del repo_data [ repo ] <EOL> self . __save_repo ( repo_data ) <EOL> return True <EOL> def getRepos ( self ) : <EOL> return self . __load_repo ( ) <EOL> def addSSHKey ( self , username , keyname , sshkey ) : <EOL> key_file_name = self . __get_ssh_key_path ( username , keyname ) <EOL> try : <EOL> with open ( key_file_name ) as f : <EOL> return False <EOL> except IOError as e : <EOL> pass <EOL> new_key_file = open ( key_file_name , '<STR_LIT:w>' ) <EOL> new_key_file . write ( sshkey ) <EOL> new_key_file . close ( ) <EOL> return True <EOL> def rmSSHKey ( self , username , keyname ) : <EOL> key_file_name = self . __get_ssh_key_path ( username , keyname ) <EOL> try : <EOL> os . remove ( key_file_name ) <EOL> except : <EOL> return False <EOL> return True <EOL> def getSSHKeys ( self ) : <EOL> keys = glob . glob ( self . _key_path + '<STR_LIT>' ) <EOL> key_data = { } <EOL> for keyfile in keys : <EOL> filename = os . path . basename ( keyfile ) [ : - <NUM_LIT:4> ] <EOL> filename_split = filename . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> if len ( filename_split ) != <NUM_LIT:2> : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> username = filename_split [ <NUM_LIT:0> ] . strip ( ) <EOL> keyname = filename_split [ <NUM_LIT:1> ] . strip ( ) <EOL> if username not in key_data : <EOL> key_data [ username ] = [ ] <EOL> key_data [ username ] . append ( keyname ) <EOL> return key_data <EOL> def __get_ssh_key_path ( self , username , keyname ) : <EOL> return self . _key_path + username + "<STR_LIT:@>" + keyname + "<STR_LIT>" <EOL> def __load_repo ( self ) : <EOL> """<STR_LIT>""" <EOL> repo_data = { } <EOL> repo_file_content = open ( self . _user_repo_config , '<STR_LIT:r>' ) <EOL> line = repo_file_content . readline ( ) . strip ( ) <EOL> repo = '<STR_LIT>' <EOL> while line != '<STR_LIT>' : <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> line_split = line . split ( None , <NUM_LIT:1> ) <EOL> if len ( line_split ) != <NUM_LIT:2> : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> repo = line_split [ <NUM_LIT:1> ] . strip ( ) <EOL> elif line . startswith ( '<STR_LIT:U+0020>' ) : <EOL> if repo == '<STR_LIT>' : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> line_split = line . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if len ( line_split ) != <NUM_LIT:2> : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> perm = line_split [ <NUM_LIT:0> ] . strip ( ) <EOL> user = line_split [ <NUM_LIT:1> ] . strip ( ) <EOL> if repo not in repo_data : <EOL> repo_data [ repo ] = [ ] <EOL> repo_data [ repo ] . append ( ( perm , user ) ) <EOL> else : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> line = repo_file_content . readline ( ) <EOL> repo_file_content . close ( ) <EOL> return repo_data <EOL> def __save_repo ( self , repo_data ) : <EOL> """<STR_LIT>""" <EOL> tmp_file = tempfile . NamedTemporaryFile ( '<STR_LIT:w>' ) <EOL> for reponame , permlist in repo_data . items ( ) : <EOL> tmp_file . write ( '<STR_LIT>' + reponame + '<STR_LIT:\n>' ) <EOL> for perm , user in permlist : <EOL> tmp_file . write ( "<STR_LIT:U+0020>" + perm + "<STR_LIT>" + user + '<STR_LIT:\n>' ) <EOL> tmp_file . flush ( ) <EOL> shutil . copyfile ( tmp_file . name , self . _user_repo_config ) <EOL> tmp_file . close ( ) </s>
