Commit 46eb9884 authored by Julien Muchembled's avatar Julien Muchembled

BT: make genbt5list safer for reuse in another process

- stop using chdir
- make logging to stdout/stderr optional
- generateInformation: return a StringIO instead of writing to a temporary file
parent a82324ca
...@@ -34,9 +34,8 @@ ...@@ -34,9 +34,8 @@
import tarfile import tarfile
import os import os
import sys import sys
import tempfile
import shutil
import cgi import cgi
from cStringIO import StringIO
property_list = ''' property_list = '''
title title
...@@ -47,20 +46,10 @@ license ...@@ -47,20 +46,10 @@ license
dependency_list dependency_list
provision_list provision_list
copyright_list copyright_list
'''.strip().splitlines() '''.split()
bt_title_path = os.path.join('bt', 'title') bt_title_path = os.path.join('bt', 'title')
def info(message):
"""Print a message to stdout.
"""
sys.stdout.write(message)
def err(message):
"""Print a message to stderr.
"""
sys.stderr.write(message)
def readProperty(property_dict, property_name, property_file): def readProperty(property_dict, property_name, property_file):
try: try:
text = property_file.read() text = property_file.read()
...@@ -96,64 +85,56 @@ def readBusinessTemplateDirectory(dir): ...@@ -96,64 +85,56 @@ def readBusinessTemplateDirectory(dir):
return property_dict return property_dict
def generateInformation(fd): def generateInformation(dir, info=id, err=None):
os.write(fd, '<?xml version="1.0"?>\n') xml = StringIO()
os.write(fd, '<repository>\n') xml.write('<?xml version="1.0"?>\n<repository>\n')
for name in sorted(os.listdir(dir)):
for file in sorted(os.listdir(os.getcwd())): path = os.path.join(dir, name)
if file.endswith('.bt5'): if name.endswith('.bt5'):
info('Reading %s... ' % (file,)) info('Reading %s... ' % name)
try: try:
tar = tarfile.open(file, 'r:gz') tar = tarfile.open(path, 'r:gz')
except tarfile.TarError: except tarfile.TarError:
err('An error happened in %s; skipping\n' % (file,)) if err:
continue err('An error happened in %s; skipping\n' % name)
continue
raise
try: try:
property_dict = readBusinessTemplate(tar) property_dict = readBusinessTemplate(tar)
finally: finally:
tar.close() tar.close()
elif os.path.isfile(os.path.join(file, bt_title_path)): elif os.path.isfile(os.path.join(path, bt_title_path)):
info('Reading Directory %s... ' % (file,)) info('Reading Directory %s... ' % name)
property_dict = readBusinessTemplateDirectory(file) property_dict = readBusinessTemplateDirectory(path)
else: else:
continue continue
os.write(fd, ' <template id="%s">\n' % (file,)) xml.write(' <template id="%s">\n' % name)
for property_id, property_value in sorted(property_dict.items()): for k, v in sorted(property_dict.iteritems()):
if type(property_value) is str: for v in (v,) if type(v) is str else v:
os.write(fd, ' <%s>%s</%s>\n' % ( xml.write(' <%s>%s</%s>\n' % (k, cgi.escape(v), k))
property_id, cgi.escape(property_value), property_id)) xml.write(' </template>\n')
else:
for value in property_value:
os.write(fd, ' <%s>%s</%s>\n' % (
property_id, cgi.escape(value), property_id))
os.write(fd, ' </template>\n')
info('done\n') info('done\n')
os.write(fd, '</repository>\n') xml.write('</repository>\n')
return xml
def main(): def main(dir_list=None, **kw):
if len(sys.argv) < 2: if dir_list is None:
dir_list = ['.'] kw.setdefault('info', sys.stdout.write)
else: kw.setdefault('err', sys.stderr.write)
dir_list = sys.argv[1:] dir_list = sys.argv[1:] or '.'
cur_umask = os.umask(0666)
os.umask(cur_umask)
cwd = os.getcwd()
for d in dir_list: for d in dir_list:
os.chdir(d) bt5list = generateInformation(d, **kw).getvalue()
d = os.path.join(d, 'bt5list.new')
try: try:
fd, path = tempfile.mkstemp(dir='.') with open(d, 'wb') as f:
try: f.write(bt5list)
generateInformation(fd) os.rename(d, d[:-4])
os.fchmod(fd, 0666 & ~cur_umask)
os.rename(path, 'bt5list')
except:
os.remove(path)
raise
finally:
os.close(fd)
finally: finally:
os.chdir(cwd) try:
os.remove(d)
except OSError:
pass
if __name__ == "__main__": if __name__ == "__main__":
main() main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment