#!/usr/bin/env python # $Id$ import string import os import commands import re # Set some defaults symlinks = 0 verbose = 1 extraverbose = 0 ayobase = '/var/ftp/pub/ayo' yumoptions = '' aptoptions = '--flat --bloat --bz2only --partial' # These can't be overridden statedir = os.getenv('HOME') + '/.ayo-state' archre = {'i386' : '\.(i.86|noarch|athlon)\.rpm$', 'ppc' : '\.(ppc|noarch)\.rpm$', 'x86_64' : '\.(x86_64|noarch|i.86)\.rpm$'} ayo = {} fd = open('ayo.conf', 'r') ayoconf = fd.readlines() fd.close() # walk(path, visit, arg) # Calls visit with arguments (arg, dirname, names) for each directory def gluelinks(moduledir, gluedir, arch): # Function to link all matched files in a given directory def linkit((gluedir, arch), dirname, filenames): for filename in filenames: if os.path.isfile(dirname + '/' + filename): # Match the files against the arch file pattern p = re.compile(archre[arch]) if p.search(filename): # If the file matches, link it in the glue dir! if extraverbose: print 'Linking ' + dirname + '/' + filename + ' to ' + gluedir + '/' + filename os.link(dirname + '/' + filename, gluedir + '/' + filename) # Call to the above for each subdirectory, to link all relevant files os.path.walk(moduledir, linkit, (gluedir, arch)) # Parse the configuration file for line in ayoconf: # Remove trailing \n if line.endswith('\n'): line = line[0:-1] if line == '' or line[0] == '#': continue # If this is a new section in brackets if line[0] == '[': section = line[1:-1] if verbose: print 'Parsing configuration file section : %s' % section if section == 'main': pass else: params = string.split(section) basepath = params[0] version = params[1] arch = params[2] ayo[basepath, version, arch] = [] #print basepath, version, arch # If it's only parameters for a section else: if section == 'main': params = string.split(line, '=') if verbose: print 'Found parameter %s with value %s' % (params[0], params[1]) if params[0] == 'symlinks': symlinks = params[1] if params[0] == 'ayobase': ayobase = params[1] else: params = string.split(line) module = params.pop(0) binsrc = params.pop(0) moduledir = params.pop(0) if params: glue = 1 else: glue = 0 ayo[basepath, version, arch].append([module, binsrc, moduledir, glue]) # Change current working directory, as some operations are relative to it os.chdir(ayobase) # Now we have ayo, follow for key in ayo.keys(): # Get back what we need to work basepath, version, arch = key basepathname = string.replace(basepath, '/', '-') if verbose: print 'Checking %s/%s/%s' % key # Create all the right links and empty glue dirs for module, binsrc, moduledir, glue, in ayo[key]: linkname = '%s-%s-%s-%s.%s' % (basepathname, version, arch, binsrc, module) # Create the main links and glue dirs if not os.path.islink('links/' + linkname): if not glue: os.symlink('../' + moduledir, 'links/' + linkname) if verbose: print 'Created link : %s' % linkname else: if not os.path.isdir('glue/' + linkname): os.mkdir('glue/' + linkname) if verbose: print 'Created glue : %s' % linkname os.symlink('../glue/' + linkname, 'links/' + linkname) if verbose: print 'Created link to glue : %s' % linkname # Create the ayo repository subdir = '%s/%s/%s' % (basepath, version, arch) if not os.path.isdir(subdir): os.makedirs(subdir) if verbose: print 'Created ayo directory : %s' % subdir if not os.path.isdir(subdir + '/' + module): os.mkdir(subdir + '/' + module) goback = '../' * len(string.split(subdir,'/')) # The yum symlink os.symlink(goback + '../links/' + linkname, subdir + '/' + module + '/' + binsrc) # The apt symlink os.symlink(goback + 'links/' + linkname, subdir + '/' + binsrc + '.' + module) if not os.path.isdir(statedir): os.mkdir(statedir) # This is to get always the same consistent ls output os.putenv('LC_ALL', 'C') lscmd = 'ls -1 -R -s --block-size=1 -t ' (status, output) = commands.getstatusoutput(lscmd + moduledir) try: fd = open(statedir + '/' + linkname, 'r') oldoutput = fd.read() fd.close() except: if verbose: print 'Old state file not found : %s/%s' % (statedir, linkname) oldoutput = '' # If the directory's content has changed if output != oldoutput: if verbose: print '* Content of %s has changed' % linkname fd = open(statedir + '/' + linkname, 'w') fd.write(output) fd.close() if glue: if verbose: print 'Glueing!!!' # Check the content of the moduledir and if it has changed, # link update the glue dir for file in os.listdir('glue/' + linkname): os.remove('glue/' + linkname + '/' + file) gluelinks(moduledir, 'glue/' + linkname, arch) # Update the module yumcmd = 'yum-arch %s %s/%s' % (yumoptions, subdir, module) aptcmd = 'genbasedir %s %s/%s %s' % (aptoptions, ayobase, subdir, module) # Generate the yum metadata if verbose: print 'Generating yum data' (status, output) = commands.getstatusoutput(yumcmd) if extraverbose: print output # Generate the apt metadata if verbose: print 'Generating apt data' (status, output) = commands.getstatusoutput(aptcmd) if extraverbose: print output else: if verbose: print '- %s in module %s have not changed' % (binsrc, module)