Exemplo n.º 1
0
def retrieve_recipes(dir,dom=None):
    
    #if dom is None:
    #    dom = etree.Element('recipe')
    #    dom.set('name','Recipes')
    
    if os.path.isdir(dir):
        node = etree.Element('recipe')
        if dom is None:
            dom = node
        node.set('name',clean(dir.split('/'))[-1])
        for f in dircache.listdir(dir):
            retrieve_recipes(os.path.join(dir,f),node)
        dom.append(node)

    if os.path.isfile(dir):
        f = file(dir,'r')
        node = etree.Element('recipe')
        #node.set('name',assemble(clean(dir.split('/'))[-1].split('.')[0:-1]))
        node.set('name',clean(dir.split('/'))[-1])
        node.set('recipe','true')

        recipe_dom = None
        try:
            recipe_dom = etree.fromstring(f.read())
        except etree.XMLSyntaxError:
            f.seek(0)
            recipe_dom = generate(f.read())
        
        node.append(recipe_dom)
        if recipe_dom is not None:
            dom.append(node)

    return dom
Exemplo n.º 2
0
    def render(self):
        #updating the current buildout view uid
        self.response.setHeader('Content-Type', 'text/xml')
        
        dom = etree.Element('Buildouts')
        dom.set('name','Buildouts')

        buildout_dir = self.static.get(BUILDOUTS_FOLDER).context.path

        
        valid_buildouts = []
        if os.path.isdir(buildout_dir):
            uid = 0
            buildouts = dircache.listdir(buildout_dir)


            for buildout in buildouts:
                if tar_re.match(buildout) is not None:
                    buildout_files = []
                    tar = tarfile.open(os.path.join(buildout_dir,buildout), "r:gz")
                    for f in tar:
                        if buildout_re.match(f.name) is not None:
                            try:
                                buildout_dom = generate(tar.extractfile(f).read())
                                cp = render(buildout_dom)
                                if not cp.has_section('buildout'):
                                    raise ValueError('No Section: Buildout')
                                
                                buildout_files.append({'tar':tar,
                                                       'directory':'t',
                                                       'file':f.name,
                                                       'dom': buildout_dom,
                                                       'filename':f.name,
                                                       'uid':uid,
                                                       'url':self.url('buildouts')+'?buildout='+str(uid)})
                            
                            
                                uid += 1
                                
                            except ValueError:
                                pass
                                
                            
                            
                    if tar_re.match(buildout):
                        buildout = assemble_list(buildout.split('.')[0:-2],'.')

                    valid_buildouts.append({'toplevel':buildout,
                                            'buildouts':buildout_files,
                                            'tarball':tar})
                    
                #self.context.buildouts = valid_buildouts
                BUILDOUTS.extend(valid_buildouts)

        ### Generate the DOM tree representing the buildouts ###
        for valid_buildout in valid_buildouts:
            #for each toplevel (tar.gz)
            toplevel = etree.Element('toplevel')
            #set the name
            toplevel.set('name',valid_buildout['toplevel'].split()[0])
            
            #for each buildout
            for buildout in valid_buildout['buildouts']:
                tree = '/toplevel'
                last = tree
                for name in buildout['filename'].split('/')[1:]:
                    next = 'node'
                    #if cfg_re.match(name): 
                    #    next = 'file'

                    tree += '/' + next + "[@name='" + name + "']"


                    if len(toplevel.xpath(tree)) == 0:
                        parent = toplevel.xpath(last)[0]

                        new_element = etree.Element('node')
                        if cfg_re.match(name):
                            new_element.set('file','true')
            
                        new_element.set('name',name)
                        parent.append(new_element)
                    
                    last = tree
                        
                        

                
                
                
                
                
                
            dom.append(toplevel)
            
        return etree.tostring(dom,pretty_print=True)