forked from openid/php-openid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackagexml.py
110 lines (86 loc) · 3.12 KB
/
packagexml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python
import os
import os.path
def makeLeadXML(leads):
lead_template = """
<lead>
<name>%(name)s</name>
<user>%(user)s</user>
<email>%(email)s</email>
<active>%(active)s</active>
</lead>
"""
return "".join([lead_template % l for l in leads])
INDENT_STRING = " "
def buildContentsXMLFordir(dir_or_file, roles, depth=0, dir_role=None, all_files=False):
"""
Returns a list of strings, each of which is either a <file> XML
element for the given file or a <dir> element which contains other
<file> elements.
"""
try:
entries = os.listdir(dir_or_file)
dir_role_s = ''
if dir_role:
dir_role_s = ' role="%s"' % (dir_role)
lines = ['%s<dir name="%s"%s>' % (INDENT_STRING * depth, os.path.basename(dir_or_file),
dir_role_s)]
for entry in entries:
lines += buildContentsXMLFordir(dir_or_file + os.sep + entry, roles, depth + 1, dir_role, all_files)
lines.append('%s</dir>' % (INDENT_STRING * depth))
return lines
except OSError:
try:
extension = dir_or_file.split(".")[-1]
except:
if not all_files:
return []
if extension in roles: # Ends in an extension we care about
return ['%s<file name="%s" role="%s" />' %
(INDENT_STRING * depth, os.path.basename(dir_or_file), roles[extension])]
elif all_files:
return ['%s<file name="%s" />' %
(INDENT_STRING * depth, os.path.basename(dir_or_file))]
else:
print "FOOB for %s %s" % (all_files, dir_or_file)
return []
def buildContentsXML(roles, *dirs):
lines = []
for directory in dirs:
lines.append("\n".join(buildContentsXMLFordir(directory, roles, 1)))
return "\n".join(lines)
def buildDocsXML(*dirs):
lines = []
for directory in dirs:
lines.append("\n".join(buildContentsXMLFordir(directory, {}, 1, 'doc', all_files=True)))
return "\n".join(lines)
if __name__ == "__main__":
import sys
import time
try:
import xmlconfig
except:
print "Could not import XML configuration module xmlconfig"
sys.exit(1)
try:
template_f = open(xmlconfig.template, 'r')
except Exception, e:
print "Could not open template file:", str(e)
sys.exit(1)
# Expect sys.argv[1] to be the version number to include in the
# package.xml file.
try:
version = sys.argv[1]
except:
print "Usage: %s <package version>" % (sys.argv[0])
sys.exit(2)
data = xmlconfig.__dict__.copy()
contents = '<dir name="/">\n' + buildContentsXML({'php': 'php'}, *xmlconfig.contents_dirs) + \
"\n" + buildDocsXML(*xmlconfig.docs_dirs) + '\n </dir>'
data['contents'] = contents
data['leads'] = makeLeadXML(xmlconfig.leads)
data['date'] = time.strftime("%Y-%m-%d")
data['version'] = version
data['uri'] = "%s%s-%s.tgz" % (data['package_base_uri'], data['package_name'], version)
template_data = template_f.read()
print template_data % data