xinclude: added pic and latests plugins
[treecutter.git] / xinclude / reprepro.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 import os
5 import sys
6 from shutil import rmtree,copytree
7
8 from lxml import etree
9 from lxml.builder import ElementMaker
10
11 from treecutter import constants as const
12
13 class Reprepro(object):
14     def __init__(self, uri):
15         self.uri = uri
16         self.filelist = []
17
18     def files(self):
19         dirs = ['dists','pool']
20         rootdirs = [self.uri+'/'+d for d in dirs]
21         for rootdir,d in zip(rootdirs,dirs):
22             if os.path.exists(d):
23                 rmtree(d)
24             copytree(rootdir, d)
25         for d in dirs:
26             for root, subdir, files in os.walk(d):
27                 for file in files:
28                     self.filelist.append(os.path.join(root,file))
29
30     def db_xml(self):
31         db = ElementMaker(namespace=const.DB_NS, nsmap=const.NSMAP)
32         co = db.computeroutput()
33         for f in self.filelist:
34             co.append(db.filename(db.link(f,**{const.XLINK+"href": f})))
35         return db.para(co)
36
37 def recursively_empty(e):
38     if e.text:
39         return False
40     return all((recursively_empty(c) for c in e.iterchildren()))
41
42 def clean_db(xml):
43     context = etree.iterwalk(xml)
44     for action, elem in context:
45         parent = elem.getparent()
46         if recursively_empty(elem):
47             parent.remove(elem)
48
49 if __name__ == "__main__":
50     for arg in sys.argv[1:]:
51         al = arg.split("=",1)
52         if al[0] == "lang":
53             lang = al[1]
54         if al[0] == "xptr":
55             argument = al[1]
56
57     repo = Reprepro(argument)
58     repo.files()
59     rxml = repo.db_xml()
60     clean_db(rxml)
61
62     #print(etree.tostring(cxml, pretty_print=True))
63     #sys.stdout.write(out.encode('utf-8'))
64     sys.stdout.write(etree.tostring(rxml,encoding='UTF-8',pretty_print=True))