packagexml.py 4.5 KB

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