1import os
2import stat
3import sys
4from xml.dom.minidom import Document
5
6def createFtags(doc, path, isrootpath=True):
7    """
8    create f-tags for packagemaker's contents.xml files recursively replacing
9    owner with "root" and group with "wheel" in each entry
10    """
11
12    statinfo = os.lstat(path)
13    isdir = stat.S_ISDIR(statinfo[0])
14
15    ftag = doc.createElement("f")
16    ftag.setAttribute("n",os.path.split(path)[1])
17    ftag.setAttribute("p","%d" % statinfo[0])
18    ftag.setAttribute("o","root")
19    ftag.setAttribute("g","wheel")
20
21    # we additionally have to create <mod>owner</mod> and <mod>group</mod>
22    # within each f-tag
23    ftag.appendChild(doc.createElement("mod").appendChild(doc.createTextNode("owner")))
24    ftag.appendChild(doc.createElement("mod").appendChild(doc.createTextNode("group")))
25
26    if isrootpath:
27        # needs to be the full path
28        ftag.setAttribute("pt",os.path.abspath(path))
29        # no idea what those attributes mean:
30        ftag.setAttribute("m","false")
31        ftag.setAttribute("t","file")
32
33    if isdir:
34        for item in os.listdir(path):
35            ftag.appendChild(createFtags(doc, os.path.join(path,item), False))
36
37    return ftag
38
39def generateContentsDocument(path):
40    """
41    create new minidom document and generate contents by recursively traver-
42    sing the given path.
43    """
44
45    doc = Document()
46    root = doc.createElement("pkg-contents")
47    root.setAttribute("spec","1.12")
48    root.appendChild(createFtags(doc, path))
49    doc.appendChild(root)
50
51    return doc
52
53if __name__ == "__main__":
54    # construct document
55    doc = generateContentsDocument(sys.argv[1])
56    print doc.toprettyxml(indent="  "),
57