Changeset 3039


Ignore:
Timestamp:
Jul 6, 2010, 12:28:05 AM (14 years ago)
Author:
gdb
Message:

Cleaned up atomic file-writing code in invirt-build-conf

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/invirt-dev/invirt-build-conf

    r2766 r3039  
    77the git user that developers can push through.
    88"""
     9from __future__ import with_statement
    910
    10 
     11import contextlib
    1112import os
    1213import tempfile
     
    2728    return princ.replace('.', '/') + '@' + realm
    2829
     30def acl_path(pocket):
     31    return '/etc/remctl/acl/build-%s' % pocket
     32
     33@contextlib.contextmanager
     34def atomic_write(file):
     35    tmp_fd, tmp_name = tempfile.mkstemp()
     36    tmp = os.fdopen(tmp_fd, 'r+')
     37    yield tmp
     38    tmp.close()
     39    os.rename(tmp_name, file)
    2940
    3041def main():
    3142    all_devs = set()
    32 
    33     # Python could really use a file-like object that gets written to
    34     # a temporary path and moved to its final resting place on
    35     # .close(). Oh well.
    36     conf_fd, conf_name = tempfile.mkstemp()
    37     conf = os.fdopen(conf_fd, 'r+')
    3843    build_handler = '/usr/bin/invirt-submit-build'
    3944
    4045    for pocket in config.build.pockets:
    4146        acl = authz.expandAdmin(getattr(config.build.pockets, pocket).acl, None)
     47        with atomic_write(acl_path(pocket)) as f:
     48            princs = [userToPrinc(a) for a in acl]
     49            print >>f, '\n'.join(princs)
     50            all_devs.update(set(princs))
    4251
    43         acl_fd, acl_name = tempfile.mkstemp()
    44         acl_fd = os.fdopen(acl_fd, 'r+')
    45         print >>acl_fd, '\n'.join(userToPrinc(a) for a in acl)
    46 
    47         all_devs.update(set(userToPrinc(a) for a in acl))
    48 
    49         acl_path = os.path.join('/etc/remctl/acl/build-%s' % pocket)
    50 
    51         os.rename(acl_name, acl_path)
    52         print >>conf, 'build %s %s %s' % (pocket, build_handler, acl_path)
     52    with atomic_write('/etc/remctl/conf.d/build') as f:
     53        for pocket in config.build.pockets:
     54            print >>f, 'build %s %s %s' % (pocket, build_handler, acl_path(pocket))
    5355
    5456    os.rename(conf_name, '/etc/remctl/conf.d/build')
     
    5860    print >>k5login, '\n'.join(all_devs)
    5961
    60     os.rename(k5login_name, os.path.join(builder._REPO_DIR, '.k5login'))
     62    with atomic_write(os.path.join(builder._REPO_DIR, '.k5login')) as f:
     63        print >>f, '\n'.join(all_devs)
    6164
    6265
Note: See TracChangeset for help on using the changeset viewer.