def writeNTPConf(self, ntp_dict): """ Writes a ntp configuration dictionary to the loaded file. :param ntp_dict: NTP configuration dictionary """ import string ntp_config = nixcommon.comment("%s -- Generated on %s by %s\n\n" %(self.file, nixcommon.now(), nixcommon.whoAmI())) for attribute in ntp_dict: for values in ntp_dict[attribute]: ntp_config += "%s %s\n" %(attribute, string.join(values)) ntp_config += '\n' try: ntp_config_file = open(self.file, 'wb') except IOError: raise nixexceptions.FileNotWriteable(self.file) ntp_config_file.write(ntp_config) ntp_config_file.close() return #---
def write(self, ntp_dict = None): """ Writes a ntp configuration dictionary to the loaded file. :param ntp_dict: NTP configuration dictionary """ from lib import nixcommon import string if ntp_dict: self.config = ntp_dict elif not self.config: raise NTPConfigDataError('Config not loaded.') ntp_config = nixcommon.comment("%s -- Generated on %s by %s\n\n" %(self.file_path, nixcommon.now(), nixcommon.whoAmI())) for attribute in self.config: for values in self.config[attribute]: ntp_config += "%s %s\n" %(attribute, string.join(values)) ntp_config += '\n' nixconfigparser.NixConfigParser.write(self,ntp_config) return #--- #---
def write(self, config_dict=None): """ Writes the config dictionary to disc. """ from lib import nixcommon root_values = "" section_data = "" # Make sure the config is loaded if config_dict: self.config = config_dict elif not self.config: raise SSLConfigError("Config not loaded.") config_text = nixcommon.comment( "%s -- Generated on %s by %s\n\n" % (self.file_path, nixcommon.now(), nixcommon.whoAmI()) ) keys = self.config.keys() keys.sort() # Use the dict keys to generate the root and section data for key in keys: if type(self.config[key]) == str or type(self.config[key]) == unicode: root_values += "%s\t= %s\n" % (key, self.config[key]) else: section_data += "\n[ %s ]\n" % key section_data += "".join( sorted(list("%s\t= %s\n" % (pair_key, self.config[key][pair_key]) for pair_key in self.config[key])) ) config_text += "\n%s\n%s" % (root_values, section_data) super(SSLConfigParser, self).write(config_text)
def test_whoAmI(self, getuser): """ Tests to make sure that the whoAmI function returns the proper user. :param getuser: Mocked getpass.getuser to fake data in the whoAmI function. """ nixcommon.getpass.getuser = getuser result = nixcommon.whoAmI() nixcommon.getpass.getuser.assert_called_once_with() assert result == 'UNIT_TEST'
def writeKrb5Conf(self, krb5_dict, comments = ''): """ Writes a Python dictionary back to krb5.conf. If desired *comments* may be set to a string or list of strings which will print comments at the top of the file. :param krb5_dict: Dictionary to convert to a Kerberos config file. :param comments: String or list of strings to use as a comment at top of the file. """ try: krb5Conf = open(self.file, 'wb') except IOError: raise nixexceptions.FileNotWriteable(self.file) confTxt = nixcommon.comment("%s -- Generated on %s by %s\n\n%s\n" %(self.file, nixcommon.now(), nixcommon.whoAmI(), comments)) # Loop through the dictionary, creating text we can write to the file for section in krb5_dict: confTxt += '[' + section + ']\n' for line in krb5_dict[section]: lineType = type(krb5_dict[section][line]) # Multi-line segment if lineType == dict: confTxt += '\t' + line + ' = {\n' for multiLine in krb5_dict[section][line]: confTxt += '\t\t' + multiLine + ' = ' + krb5_dict[section][line][multiLine] + '\n' confTxt += '\t}\n' # Single-line segment elif lineType == str: confTxt += '\t' + line + ' = ' + krb5_dict[section][line] + '\n' confTxt += '\n' krb5Conf.write(confTxt) krb5Conf.close() return
def writeKrb5ACL(self, acls, comments = ''): """ Writes a list of tuples consisting of the principal, operation mask and operation target to a Kerberos ACL. :param acls: List of tuples (principal, operation_mask, operation_target). :param comments: Comments to place at the top of the file. """ try: krb5ACL = open(self.file, 'wb') except IOError: raise nixexceptions.FileNotWriteable(self.file) aclsTxt = nixcommon.comment("%s -- Generated on %s by %s\n\n%s\n" %(self.file, nixcommon.now(), nixcommon.whoAmI(), comments)) for (principal, operation_mask, operation_target) in acls: aclsTxt += "%s\t%s\t%s\n" %(principal , operation_mask, operation_target) krb5ACL.write(aclsTxt) krb5ACL.close() return
con.prints('Some applications were not installed by napkginstall!') exit(3) else: log.info('Writing paths config') # Store the paths to their config file app_path_file = "%s/conf/paths.py" %NA_ROOT # Automated install overwrites by default if os.path.exists(app_path_file) and na_run_from: log.debug('Automatically overwrote paths file (na_run_from is set)') os.remove(app_path_file) # User is running the script, ask them questions elif os.path.exists(app_path_file) and not na_run_from: if con.askBoolQuestion('Should I overwrite ' + app_path_file): log.debug('Overwrote the paths file at user\'s request') os.remove(app_path_file) else: log.debug('Kept existing config, at user\'s request') con.prints('Ok, keeping your existing file!') exit(0) paths_file = nixcommon.comment("%s -- Generated on %s by %s" %(app_path_file, nixcommon.now(), nixcommon.whoAmI())) con.prints('Writing path config to %s... ' %app_path_file, no_newline=True) nixcommon.createTextFile(app_path_file, "%s\n\n%s" %(paths_file,''.join(app_locations))) log.info('PathBuilder complete') con.prints('Done.') exit(0)