genbt5list 3.63 KB
Newer Older
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
#! /usr/bin/python
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
#                    Yoshinori Okuji <yo@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################


"""Generate repository information on Business Templates.
"""

import tarfile
import os
import sys
import tempfile

property_list = ('title', 'version', 'description', 'license', 'dependency_list', 'copyright_list')

def info(message):
  """Print a message.
  """
  sys.stderr.write(message)

def readBusinessTemplate(tar):
  """Read an archived Business Template.
  """
  property_dict = {}
  for info in tar:
    name_list = info.name.split('/')
    if len(name_list) == 3 and name_list[1] == 'bt' and name_list[2] in property_list:
      f = tar.extractfile(info)
      try:
        text = f.read()
        if name_list[2].endswith('_list'):
          property_dict[name_list[2][:-5]] = text and text.split('\n') or []
        else:
          property_dict[name_list[2]] = text
      finally:
        f.close()
        
  return property_dict
        
def generateInformation(fd):
  os.write(fd, '<?xml version="1.0"?>\n')
  os.write(fd, '<repository>\n')

  for file in os.listdir(os.getcwd()):
    if file.endswith('.bt5'):
      info('Reading %s... ' % (file,))
      try:
        tar = tarfile.open(file, 'r:gz')
      except tarfile.TarError:
        info('an error happened; skipping\n')
        continue
      try:
        property_dict = readBusinessTemplate(tar)
        property_id_list = property_dict.keys()
        property_id_list.sort()
        os.write(fd, '  <template id="%s">\n' % (file,))
        for property_id in property_id_list:
          property_value = property_dict[property_id]
          if type(property_value) == type(''):
            os.write(fd, '    <%s>%s</%s>\n' % (property_id, property_value, property_id))
          else:
            for value in property_value:
              os.write(fd, '    <%s>%s</%s>\n' % (property_id, value, property_id))
        os.write(fd, '  </template>\n')
        info('done\n')
      finally:
        tar.close()
        
  os.write(fd, '</repository>\n')
       
def main():
97 98
  if len(sys.argv) < 2:
    dir_list = ['.']
99
  else:
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    dir_list = sys.argv[1:]

  cwd = os.getcwd()
  for d in dir_list:
    os.chdir(d)
    fd, path = tempfile.mkstemp()
    try:
      try:
        generateInformation(fd)
      finally:
        os.close(fd)
    except:
      os.remove(path)
      raise
    else:
      os.rename(path, 'bt5list')
    os.chdir(cwd)
117 118

main()