def get_apt_proxy(): is_proxy = False proxy_url = None settings = {'username': None, 'password': None, 'host': None, 'port': None} cfg = read_file_contents_as_lines(apt_cfg) if cfg: for line in cfg: if line.startswith('Acquire::'): url = line.split('//')[-1].replace('/";', '') if '@' in url: credentials, socket = url.split('@') settings['username'], settings[ 'password'] = credentials.split(':') else: socket = url settings['host'], settings['port'] = socket.split(':') is_proxy = True proxy_url = generate_proxy_url(host=settings['host'], port=settings['port'], username=settings['username'], password=settings['password']) return is_proxy, settings, proxy_url
def _cache_pip_packages(progress): """ Downloads all updatable python modules and caches them in pip's internal pacakge cache. """ phase_name = 'downloading-pip-pkgs' progress.start(phase_name) packages = read_file_contents_as_lines(PIP_PACKAGES_LIST) progress.init_steps(phase_name, len(packages)) for pkg in packages: progress.next_step(phase_name, "Downloading {}".format(pkg)) # The `--no-install` parameter has been deprecated in pip. However, the # version of pip in wheezy doesn't yet support the new approach which # is supposed to provide the same behaviour. args = "install --upgrade --no-install {}".format(pkg) success = run_pip_command(args) # TODO: abort the install? if not success: msg = "Downloading the '{}' pip package failed.".format(pkg) logger.error(msg)
def get_apt_proxy(): is_proxy = False proxy_url = None settings = { 'username': None, 'password': None, 'host': None, 'port': None } cfg = read_file_contents_as_lines(apt_cfg) if cfg: for line in cfg: if line.startswith('Acquire::'): url = line.split('//')[-1].replace('/";', '') if '@' in url: credentials, socket = url.split('@') settings['username'], settings['password'] = credentials.split(':') else: socket = url settings['host'], settings['port'] = socket.split(':') is_proxy = True proxy_url = generate_proxy_url( host=settings['host'], port=settings['port'], username=settings['username'], password=settings['password'] ) return is_proxy, settings, proxy_url
def install_pip_packages(progress, priority=Priority.NONE): # Urgent updates don't do PIP updates if priority == Priority.URGENT: return phase_name = progress.get_current_phase().name packages = read_file_contents_as_lines(PIP_PACKAGES_LIST) progress.init_steps(phase_name, len(packages)) for pkg in packages: progress.next_step(phase_name, "Installing {}".format(pkg)) success = run_pip_command( "install --upgrade --no-index --find-links=file://{} '{}'".format( PIP_CACHE_DIR, pkg) ) if not success: msg = "Installing the '{}' pip package failed".format(pkg) logger.error(msg) if not is_internet(): msg = "Network is down, aborting PIP install" logger.error(msg) raise IOError(msg) # Try with the failsafe method success_failsafe = run_pip_command( "install --upgrade '{}'".format(pkg) ) if not success_failsafe: msg = "Installing the '{}' pip package failed (fsafe)".format( pkg) logger.error(msg)
def set_locale_param(param, locale, skip_check=False): # FIXME: Don't use the .xsessionrc file to set the locale XSESSION_RC_FILE = os.path.join(get_home_by_username(get_user_unsudoed()), '.xsessionrc') if not skip_check and not is_locale_installed(locale): install_locale(locale) param_found = False new_param_line = 'export {}={}'.format(param, locale) new_config_file = [] if os.path.exists(XSESSION_RC_FILE): xsession_file = read_file_contents_as_lines(XSESSION_RC_FILE) for line in xsession_file: if param in line: line = new_param_line param_found = True new_config_file.append(line) if not param_found: new_config_file.append(new_param_line) with open(XSESSION_RC_FILE, 'w') as conf_file: conf_file.write('\n'.join(new_config_file)) chown_path(XSESSION_RC_FILE)
def install_pip_packages(progress, priority=Priority.NONE): # Urgent updates don't do PIP updates if priority == Priority.URGENT: return phase_name = progress.get_current_phase().name packages = read_file_contents_as_lines(PIP_PACKAGES_LIST) progress.init_steps(phase_name, len(packages)) for pkg in packages: progress.next_step(phase_name, "Installing {}".format(pkg)) success = run_pip_command( "install --upgrade --no-index --find-links=file://{} '{}'".format( PIP_CACHE_DIR, pkg)) if not success: msg = "Installing the '{}' pip package failed".format(pkg) logger.error(msg) if not is_internet(): msg = "Network is down, aborting PIP install" logger.error(msg) raise IOError(msg) # Try with the failsafe method success_failsafe = run_pip_command( "install --upgrade '{}'".format(pkg)) if not success_failsafe: msg = "Installing the '{}' pip package failed (fsafe)".format( pkg) logger.error(msg)
def test_set_locale(self): with mock_file(locale.XSESSION_RC_FILE): params = [ 'LANGUAGE', 'LC_ADDRESS', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_MEASUREMENT', 'LC_MESSAGES', 'LC_NUMERIC', 'LC_PAPER', 'LC_RESPONSE', 'LC_TELEPHONE', 'LC_TIME' ] test_locale = 'en_US.UTF-8' config_lines = [ 'export {}={}'.format(param, test_locale) for param in params ] locale.set_locale(test_locale) config = utils.read_file_contents_as_lines(locale.XSESSION_RC_FILE) for line in config_lines: if line not in config: self.assertFalse(True) self.assertTrue(True)
def _cache_pip_packages(progress, priority=Priority.NONE): """ Downloads all updatable python modules and caches them in pip's internal pacakge cache. """ # Urgent updates don't do PIP updates if priority == Priority.URGENT: return phase_name = 'downloading-pip-pkgs' progress.start(phase_name) ensure_dir(PIP_CACHE_DIR) packages = read_file_contents_as_lines(PIP_PACKAGES_LIST) progress.init_steps(phase_name, len(packages)) for pkg in packages: progress.next_step(phase_name, _("Downloading {}").format(pkg)) # The `--no-install` parameter has been deprecated in pip. However, the # version of pip in wheezy doesn't yet support the new approach which # is supposed to provide the same behaviour. args = "download --dest '{}' '{}'".format(PIP_CACHE_DIR, pkg) success = run_pip_command(args) # TODO: abort the install? if not success: msg = "Downloading the '{}' pip package failed.".format(pkg) logger.error(msg)
def _cache_pip_packages(progress, priority=Priority.NONE): """ Downloads all updatable python modules and caches them in pip's internal pacakge cache. """ # Urgent updates don't do PIP updates if priority == Priority.URGENT: return phase_name = 'downloading-pip-pkgs' progress.start(phase_name) ensure_dir(PIP_CACHE_DIR) packages = read_file_contents_as_lines(PIP_PACKAGES_LIST) progress.init_steps(phase_name, len(packages)) for pkg in packages: progress.next_step(phase_name, "Downloading {}".format(pkg)) # The `--no-install` parameter has been deprecated in pip. However, the # version of pip in wheezy doesn't yet support the new approach which # is supposed to provide the same behaviour. args = "install --upgrade --download '{}' '{}'".format(PIP_CACHE_DIR, pkg) success = run_pip_command(args) # TODO: abort the install? if not success: msg = "Downloading the '{}' pip package failed.".format(pkg) logger.error(msg)
def set_locale_param(param, locale, skip_check=False): if not skip_check and not is_locale_installed(locale): install_locale(locale) param_found = False new_param_line = 'export {}={}'.format(param, locale) new_config_file = [] if os.path.exists(XSESSION_RC_FILE): xsession_file = read_file_contents_as_lines(XSESSION_RC_FILE) for line in xsession_file: if param in line: line = new_param_line param_found = True new_config_file.append(line) if not param_found: new_config_file.append(new_param_line) with open(XSESSION_RC_FILE, 'w') as conf_file: conf_file.write('\n'.join(new_config_file)) chown_path(XSESSION_RC_FILE)
def run_test_for_param(self, param): with mock_file(locale.XSESSION_RC_FILE): test_locale = 'en_US.UTF-8' config_line = 'export {}={}'.format(param, test_locale) locale.set_locale_param(param, test_locale) config = utils.read_file_contents_as_lines(locale.XSESSION_RC_FILE) self.assertTrue(config_line in config)
def upgrade_python(appstate_before, visible=False): def visible_run(cmd): if visible: return run_print_output_error(cmd) else: return run_cmd_log(cmd) if not os.path.exists(python_modules_file): if visible: print 'python module file doesn\'t exists' return [], [] if 'python-pip' in appstate_before or \ 'python-setuptools' in appstate_before: # remove old pip and setuptools cmd = 'yes "" | apt-get -y purge python-setuptools ' + \ 'python-virtualenv python-pip' visible_run(cmd) # installing/upgrading pip o, _, _ = run_cmd('pip -V') if 'pip 1.' in o: cmd = 'pip install --upgrade pip' visible_run(cmd) else: cmd = 'wget -q --no-check-certificate ' + \ 'https://raw.github.com/pypa/pip/master/contrib/get-pip.py ' + \ '-O get-pip.py' visible_run(cmd) cmd = 'python get-pip.py' visible_run(cmd) delete_file('get-pip.py') # parse python modules python_modules = read_file_contents_as_lines(python_modules_file) ok_modules = [] error_modules = [] for module in python_modules: o, e, rc = visible_run('pip install --upgrade {}'.format(module)) if rc == 0: if 'Successfully installed' in o: ok_modules.append(module) else: error_modules.append(module) return ok_modules, error_modules
def upgrade_python(appstate_before, visible=False): def visible_run(cmd): if visible: return run_print_output_error(cmd) else: return run_cmd_log(cmd) if not os.path.exists(python_modules_file): if visible: print "python module file doesn't exists" return [], [] if 'python-pip' in appstate_before or \ 'python-setuptools' in appstate_before: # remove old pip and setuptools cmd = 'yes "" | apt-get -y purge python-setuptools ' + \ 'python-virtualenv python-pip' visible_run(cmd) # installing/upgrading pip o, _, _ = run_cmd('pip -V') if 'pip 1.' in o: cmd = 'pip install --upgrade pip' visible_run(cmd) else: cmd = 'wget -q --no-check-certificate ' + \ 'https://raw.github.com/pypa/pip/master/contrib/get-pip.py ' + \ '-O get-pip.py' visible_run(cmd) cmd = 'python get-pip.py' visible_run(cmd) delete_file('get-pip.py') # parse python modules python_modules = read_file_contents_as_lines(python_modules_file) ok_modules = [] error_modules = [] for module in python_modules: o, e, rc = visible_run('pip install --upgrade {}'.format(module)) if rc == 0: if 'Successfully installed' in o: ok_modules.append(module) else: error_modules.append(module) return ok_modules, error_modules
def install_pip_packages(progress): phase_name = progress.get_current_phase().name packages = read_file_contents_as_lines(PIP_PACKAGES_LIST) progress.init_steps(phase_name, len(packages)) for pkg in packages: progress.next_step(phase_name, "Installing {}".format(pkg)) success = run_pip_command("install --upgrade {}".format(pkg)) # TODO: abort the install? if not success: msg = "Installing the '{}' pip package failed".format(pkg) logger.error(msg)
def set_hostname(new_hostname): if os.environ['LOGNAME'] != 'root': logger.error("Error: Settings must be executed with root privileges") # Check username chars new_hostname = re.sub('[^a-zA-Z0-9]', '', new_hostname).lower() if new_hostname == '': logger.error("no letters left in username after removing illegal ones") return if new_hostname == 'kano': logger.info( " not tryng to set hostname as it is the same as the default") return # check for missing hosts file if not os.path.exists(hosts_file): create_empty_hosts() # check if already done curr_hosts = read_file_contents_as_lines(hosts_file) if hosts_mod_comment in curr_hosts: logger.warn("/etc/hosts already modified, not changing") return # actually edit the hosts file edit_hosts_file(hosts_file, new_hostname) # edit the backup file. if os.path.exists(hosts_file_backup): edit_hosts_file(hosts_file_backup, new_hostname) try: write_file_contents('/etc/hostname', new_hostname + '\n') except: logger.error("exception while changing change /etc/hostname")
def set_hostname(new_hostname): if os.environ['LOGNAME'] != 'root': logger.error("Error: Settings must be executed with root privileges") # Check username chars new_hostname = re.sub('[^a-zA-Z0-9]', '', new_hostname).lower() if new_hostname == '': logger.error('no letters left in username after removing illegal ones') return if new_hostname == 'kano': logger.info(' not tryng to set hostname as it is the same as the default') return # check for missing hosts file if not os.path.exists(hosts_file): create_empty_hosts() # check if already done curr_hosts = read_file_contents_as_lines(hosts_file) if hosts_mod_comment in curr_hosts: logger.warn('/etc/hosts already modified, not changing') return # actually edit the hosts file edit_hosts_file(hosts_file, new_hostname) # edit the backup file. if os.path.exists(hosts_file_backup): edit_hosts_file(hosts_file_backup, new_hostname) try: write_file_contents('/etc/hostname', new_hostname + '\n') except: logger.error("exception while changing change /etc/hostname")
def read_listed_sites(): return ( read_file_contents_as_lines(blacklist_file), read_file_contents_as_lines(whitelist_file) )
def read_listed_sites(): return (read_file_contents_as_lines(blacklist_file), read_file_contents_as_lines(whitelist_file))