Skip to content
Snippets Groups Projects
0_setup.py 1.47 KiB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os

import utils


def setup(interactive=True):
    # Get conf
    http_proxy = utils.get_conf('PROXY_HTTP')
    https_proxy = utils.get_conf('PROXY_HTTPS')
    no_proxy = utils.get_conf('PROXY_EXCLUDE')
    # Environment
    environment_path = '/etc/environment'
    environment = 'PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"\n'
    if http_proxy:
        environment += 'http_proxy="%s"\n' % http_proxy
    if https_proxy:
        environment += 'https_proxy="%s"\n' % https_proxy
    if no_proxy:
        environment += 'no_proxy="%s"\n' % no_proxy
    # apt
    apt_proxy_path = '/etc/apt/apt.conf.d/proxy'
    apt_proxy = ''
    if http_proxy:
        apt_proxy += 'Acquire::http::Proxy "%s";\n' % http_proxy
    if https_proxy:
        apt_proxy += 'Acquire::https::Proxy "%s";\n' % https_proxy
    # write changes
    files = (
        (environment_path, environment),
        (apt_proxy_path, apt_proxy),
    )
    for path, content in files:
        if os.path.exists(path):
            with open(path, 'r') as fo:
                current = fo.read()
        else:
            current = ''
        if current != content:
            if content:
                with open(path, 'w') as fo:
                    fo.write(content)
                utils.log('File "%s" updated.' % path)
            else:
                os.remove(path)
                utils.log('File "%s" removed.' % path)