[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

slcomp.py - sources.list compactor



Being annoyed by editing sources.list files, I made something to help. I have in mind to add too it, but first I want to make sure there isn't already something like it.

The next step is to say "for all dists that have main/restricted, add universe. or remove multiverse.

And maybe figure out something reasonable with comments.

If you have any sort of odd sources.list, I would appreciate if you would run this and verify the resulting compacted sources.list (just prints, does not touch your files) is equivalent.

Thanks,
Carl K

#!/usr/bin/python

# slcomp.py - sources.list compactor
# reads in /etc/apt/sources.list and sources.d/* files
# consolidates redundancies,
# like multiple lines for the same repository/distribution
# the resulting lines are functionally equivalent.
# all comments are lost.

from aptsources.sourceslist import SourcesList

def sourcedict(sources):
  sourcesd={}
  for e in sources:
    if not e.disabled and not e.invalid:
        print e
        print e.type, e.uri, e.dist, e.comps, e.comment
        # create an empty repo, or return the exising:
        repo  = sourcesd.setdefault(e.uri,{})
        # comps for this dist
        comps = repo.setdefault(e.dist,{})
	# src or binary:
        sb    = comps.setdefault(e.type,[])
        # add the comps that don't already exist
        for comp in e.comps:
	  if comp not in comps: sb.append(comp)	
  print
  return sourcesd

def sdtolist(sourcesd):
  for repo, dists in sourcesd.items():
    for dist, sbs in dists.items():
      for sb, comps in sbs.items():
        print "%s %s %s %s" %(sb, repo, dist, ' '.join(comps))
      print


if __name__ == "__main__":

  # read in current sources.list
  sources = SourcesList()

  # convert list of objects to one big dict:
  sourcesd=sourcedict(sources)
  print "sourcesd:", sourcesd
  import pprint
  pprint.pprint(sourcesd)
  print

  # use the dict to create a file
  print "# compacted sources.list"
  sdtolist(sourcesd)


  # make a dict from scratch:
  comps = ['main', 'restricted', 'universe', 'multiverse']
  bands = {'deb': comps, 'deb-src': comps}
  new   = {'http://cp333:9999/ubuntu':
             {'jaunty-updates': bands, 'jaunty': bands},
           'http://security.ubuntu.com/ubuntu':
             {'jaunty-security': bands}
          }

  # use the hard coded dict to make a file:
  print "# generated sources.list"
  sdtolist(new)


Reply to: