<s> import os <EOL> from synapse . synapse_exceptions import ResourceException <EOL> src_dir = "<STR_LIT>" <EOL> def get_repos ( name , details = False ) : <EOL> repos = [ ] <EOL> for src_file in os . listdir ( src_dir ) : <EOL> full_path = os . path . join ( src_dir , src_file ) <EOL> if name and name == src_file . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] : <EOL> repos . append ( _load_repo ( full_path ) ) <EOL> break <EOL> elif name is None : <EOL> repos . append ( _load_repo ( full_path ) ) <EOL> return repos <EOL> def create_repo ( name , attributes = { } ) : <EOL> repo = { } <EOL> repo_file = os . path . join ( src_dir , name + '<STR_LIT>' ) <EOL> if os . path . isfile ( repo_file ) : <EOL> repo = _load_repo ( repo_file ) <EOL> newrepo = { } <EOL> try : <EOL> baseurl = attributes [ '<STR_LIT>' ] <EOL> if not _full_entry ( baseurl ) : <EOL> url = baseurl <EOL> distribution = attributes [ '<STR_LIT>' ] <EOL> components = attributes [ '<STR_LIT>' ] <EOL> if not isinstance ( components , list ) : <EOL> components = set ( '<STR_LIT>' . join ( components . split ( ) ) . split ( '<STR_LIT:U+002C>' ) ) <EOL> else : <EOL> url = baseurl . split ( ) [ <NUM_LIT:0> ] <EOL> distribution = baseurl . split ( ) [ <NUM_LIT:1> ] <EOL> components = set ( baseurl . split ( ) [ <NUM_LIT:2> : ] ) <EOL> newrepo = { '<STR_LIT>' : url , <EOL> '<STR_LIT>' : distribution , <EOL> '<STR_LIT>' : components } <EOL> except KeyError as err : <EOL> raise ResourceException ( "<STR_LIT>" % err ) <EOL> if name in repo : <EOL> for index , rep in enumerate ( repo [ name ] ) : <EOL> if ( rep [ '<STR_LIT>' ] == newrepo [ '<STR_LIT>' ] and <EOL> rep [ '<STR_LIT>' ] == newrepo [ '<STR_LIT>' ] ) : <EOL> del repo [ name ] [ index ] <EOL> repo [ name ] . append ( newrepo ) <EOL> else : <EOL> repo [ name ] = [ newrepo ] <EOL> _dump_repo ( repo ) <EOL> def _full_entry ( entry ) : <EOL> items = entry . split ( ) <EOL> if len ( items ) == <NUM_LIT:1> : <EOL> return False <EOL> elif len ( items ) >= <NUM_LIT:2> : <EOL> return True <EOL> else : <EOL> raise ResourceException ( "<STR_LIT>" ) <EOL> def delete_repo ( name , attributes ) : <EOL> repo = { } <EOL> repo_file = os . path . join ( src_dir , name + '<STR_LIT>' ) <EOL> if os . path . isfile ( repo_file ) : <EOL> repo = _load_repo ( repo_file ) <EOL> try : <EOL> baseurl = attributes [ '<STR_LIT>' ] <EOL> if len ( baseurl . split ( ) ) == <NUM_LIT:1> : <EOL> url = baseurl <EOL> distribution = attributes [ '<STR_LIT>' ] <EOL> elif len ( baseurl . split ( ) ) > <NUM_LIT:1> : <EOL> url = baseurl . split ( ) [ <NUM_LIT:0> ] <EOL> distribution = baseurl . split ( ) [ <NUM_LIT:1> ] <EOL> deleterepo = { '<STR_LIT>' : url , <EOL> '<STR_LIT>' : distribution } <EOL> except KeyError as err : <EOL> raise ResourceException ( "<STR_LIT>" % err ) <EOL> if name in repo : <EOL> for index , rep in enumerate ( repo [ name ] ) : <EOL> if ( rep [ '<STR_LIT>' ] == deleterepo [ '<STR_LIT>' ] and <EOL> rep [ '<STR_LIT>' ] == deleterepo [ '<STR_LIT>' ] ) : <EOL> del repo [ name ] [ index ] <EOL> if not len ( repo [ name ] ) : <EOL> os . remove ( repo_file ) <EOL> def _load_repo ( full_path ) : <EOL> name = full_path . split ( os . path . sep ) [ - <NUM_LIT:1> ] . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> repo = { name : [ ] } <EOL> with open ( full_path , '<STR_LIT:r>' ) as fd : <EOL> for line in fd : <EOL> tmp_repo = { } <EOL> if not line : <EOL> break <EOL> elements = line . split ( ) <EOL> if len ( elements ) > <NUM_LIT:1> and elements [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> tmp_repo [ '<STR_LIT>' ] = elements [ <NUM_LIT:1> ] <EOL> tmp_repo [ '<STR_LIT>' ] = elements [ <NUM_LIT:2> ] <EOL> components = elements [ <NUM_LIT:3> : ] <EOL> if len ( components ) : <EOL> tmp_repo [ '<STR_LIT>' ] = components <EOL> repo [ name ] . append ( tmp_repo ) <EOL> return repo <EOL> def _dump_repo ( repodict ) : <EOL> for reponame , repos in repodict . iteritems ( ) : <EOL> repo_file = os . path . join ( src_dir , reponame + '<STR_LIT>' ) <EOL> with open ( repo_file , '<STR_LIT:w>' ) as fd : <EOL> for item in repos : <EOL> debstr = [ ] <EOL> debstr . append ( '<STR_LIT>' ) <EOL> debstr . append ( item [ '<STR_LIT>' ] ) <EOL> debstr . append ( item [ '<STR_LIT>' ] ) <EOL> for comp in item [ '<STR_LIT>' ] : <EOL> debstr . append ( comp ) <EOL> fd . write ( '<STR_LIT:U+0020>' . join ( debstr ) + '<STR_LIT:\n>' ) </s>
