fabric/fabric — pull request #658 by dperetti (2012-05-31 17:25:18+00:00)
Title: New fabric.contrib.modify_config_file tool.

Hello, let me know what you think about it !

```python
def modify_config_file(remote_path, settings=None, comment_char='#', setter_char='=', type=CONFIG_FILE_NORMAL, use_sudo=False, backup=True):
````

Alter the settings of an existing remote config file, trying to uncomment found settings, adding them otherwise.

The config file is loaded to a local temporary file, processed, and uploaded back to the ``remote_path``.

By default, a backup is made remotely. Set ``backup`` to False to prevent this behavior.

Two kinds of config files are currently supported.
Default config file ``type`` is 'normal' where lines look like this:

# - Connection Settings -
listen_addresses = 'localhost' # what IP address(es) to listen on;
#port = 5432

The ``settings`` must be a list of tuples :

settings = [('listen_addresses', "'*'"), ('port', '5433')]

The script will change the config file to :

# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
port = 5433

Notice how the line with the port setting was uncommented and set to a new value.
If an existing line is not found, it is added.

The ``comment_char`` defaults to '#' and ``setter_char``defaults to '='.

A basic 'records' config file ``type`` is also supported. Ex: an pg_hba.conf file :

```
# TYPE DATABASE USER ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5
```

In such a file, we would change the IPv4 local connection to 'trust' and enable local replication like this :
```python
modify_config_file('/etc/postgresql/9.1/main/pg_hba.conf',
[('host', 'all', 'all', '127.0.0.1/32','trust'),
('local', 'replication', '', 'peer')], type="records")
```

By default, the file will be copied to ``remote_path`` as the logged-in user; specify ``use_sudo=True`` to use `sudo` instead.