Exemple #1
0
class SetupCfg(object):
    """ A context manager that will restore the local repository when it exits
    
        When SetupCfg is created it takes a list of 3-tuples that look like:
          (<setup.cfg section>, <option name>, <new value>)
        
        When the context is exited, an hg revert is applied to the local
        repository so that any changes made are erased.
    """
    
    def __init__(self, configuration_options):
        self._options = configuration_options
        self._hg = HgUtil('.')
        # if setup.cfg doesn't exist, we may create one. hg revert
        # won't delete the file so we need to know if we'll have to
        # later.
        self._setup_cfg_exists = os.path.exists('daemon/setup.cfg')

    def __enter__(self):
        for application, option, value in self._options:
            self._set_option(application, option, value)
       
    def __exit__(self, *args, **kwargs):
        if self._setup_cfg_exists:
            self._hg.revert('daemon/setup.cfg')
        else:
            os.remove('daemon/setup.cfg')
        # return False; we never have a reason to suppress an exception
        return False
    
    @staticmethod
    def _set_option(app, opt, value):
        print app, opt, value
        # use the setup.py setopt command to modify setup.cfg.
        args = ['--command', app,
                '--option', opt,
                '--set-value', value]
        return setup_py('setopt', args)