def verify(self, repair=False): """Verifies the htaccess file and contents. This checks to make sure sections exist. It does""" result = super(Htaccess, self).verify(repair) save = False if not self.exists(): return False for section in self.sections: print('Checking htaccess for ' + section.identifier + ' section...', end=' ') if not section.is_in(self.read()): if repair: self.data = section.apply_to(self.read()) save = True else: print('[FAIL]') return False print('[OK]') if not section.is_applied(self.read()): print('[!] htaccess has ' + section.identifier + ' section, but it is an old' + \ 'version, or it has been altered.') if repair and prompt('Apply template to old/altered version?'): self.data = section.apply_to(self.read(), overwrite=True) save = True if repair and save: result = result and self.write() return result
def main(): """The main test suite.""" ansi.reset() time = timeit(print_all_colors, number=1) print("time: " + str(time)) if not prompt("Continue?"): return False ansi.reset() print(timeit(print_assorted_combinations, number=1)) if not prompt("Continue?"): return False
def disable(self, ask=True): """Disable vhost and restarts apache server.""" if not self.is_enabled(): return True if not ask or prompt('Disable ' + self.domain + ' in apache?'): print('Disabling ' + self.domain + ' vhost...') return run_command(s.CMD_DISABLE_CONFIG + self.domain) and \ run_command(s.CMD_RESTART_APACHE)
def parse(self, flush_memory=False, ask=True): """Returns a dict of attributes. Passing in True as an arguement will force reading from disk.""" # Make sure contents are already read into memory. self.read(flush_memory) # Prompting is necessary (but hopefully unusual) if file on disk # cannot be parsed. for attribute in ['debug', 'disallow_edit']: if not getattr(self, attribute, None): print('Could not parse WordPress attribute ' + attribute + '.') if ask: if prompt('Set ' + attribute + ' to True?'): setattr(self, attribute, 'true') else: setattr(self, attribute, 'false') for attribute in ['table_prefix', 'db_name', 'db_user', 'db_password', 'db_host',]: if not getattr(self, attribute, None): print('Could not parse WordPress ' + attribute + '.') if ask: setattr(self, attribute, prompt_str('What is the WordPress database table_prefix?')) if not getattr(self, 'fs_method', None): print('Could not parse fs_method.') if ask: self.fs_method = prompt_str('What should we set fs_method?', 'direct') for salt in ['auth_key', 'secure_auth_key', 'logged_in_key', 'nonce_key', 'auth_salt', 'secure_auth_salt', 'logged_in_salt', 'nonce_salt']: if not getattr(self, salt, None): print('Salts not parsable. Recreating new salts...') for key, value in WPSalt().secrets(): setattr(self, key, value) break return {'db_name' : getattr(self, 'db_name', None), 'db_host' : getattr(self, 'db_host', None), 'db_user' : getattr(self, 'db_user', None), 'db_password' : getattr(self, 'db_password', None), 'table_prefix' : getattr(self, 'table_prefix', None), 'debug' : getattr(self, 'debug', None), 'disallow_edit' : getattr(self, 'disallow_edit', None), 'fs_method' : getattr(self, 'fs_method', None), 'auth_key' : getattr(self, 'auth_key', None), 'secure_auth_key' : getattr(self, 'secure_auth_key', None), 'logged_in_key' : getattr(self, 'logged_in_key', None), 'nonce_key' : getattr(self, 'nonce_key', None), 'auth_salt' : getattr(self, 'auth_salt', None), 'secure_auth_salt' : getattr(self, 'secure_auth_salt', None), 'logged_in_salt' : getattr(self, 'logged_in_salt', None), 'nonce_salt' : getattr(self, 'nonce_salt', None)}
def verify(self, repair=False, use_default_atts=False): """Verifies the attributes of WPConfig instance. Unless you pass in use_default_atts as True, this assumes the in-memory values are the correct values.""" # pylint: disable=arguments-differ result = True # Assume the best :) save = False if use_default_atts: correct_values = getattr(self, 'wp', None) else: correct_values = self.parse() # Retrieve values in memory (and hold # for comparison and/or repair) self.read(True) # Force reading values in from disk verify_items = { 'db_name' : '[!] WordPress database name is incorrectly set to: ', 'db_host' : '[!] Databse hostname is incorrectly set to: ', 'db_user' : '[!] WordPress database username is incorrectly set to: ', 'db_password' : '[!] WordPress database password is incorrectly set to: ', 'table_prefix' : '[!] WordPress table prefix is incorrectly set to: ', 'debug' : '[!] Debug is incorrectly set to: ', 'disallow_edit' : '[!] DISALLOW_EDIT is incorrectly set to: ', 'fs_method' : '[!] FS_METHOD is incorrectly set to: ', } for attribute, error_message in verify_items.iteritems(): correct_value = correct_values[attribute] current_value = getattr(self, attribute) if not current_value == correct_value: print(error_message + str(current_value)) if repair: print('Setting "' + attribute + '" to: "' + \ correct_value + '"...') setattr(self, attribute, correct_value) save = True else: result = False if repair and save: if prompt('Create new salts?'): print('Creating new salts...') for key, value in WPSalt().secrets(): setattr(self, key, value) self.write(append=False) if self.debug == 'true': print('\n -------------------------------------') print(' [WARN] WordPress Debug mode is on.') print(' Be sure to turn this off in') print(' a production environment!') print(' -------------------------------------\n') return result
def __init__(self, atts): """Initialize an Htaccess class.""" super(Htaccess, self).__init__(atts) self.sections = [] if 'sections' in atts: for sfile in atts['sections']: section = HtaccessSection(sfile) self.sections.append(section) if not self.exists() or not prompt('Use existing htaccess file?'): for section in self.sections: self.data = section.apply_to(self.read())
def test_prompt_no_default(prompt_text, response, expected): """Test prompt function with bad input.""" with mock.patch(_INPUT, return_value=response): assert prompt(prompt_text) == expected
def test_prompt(prompt_text, default, response, expected): """Test prompt function.""" with mock.patch(_INPUT, side_effect=response): assert prompt(prompt_text, default) == expected