Ticket #549: unicode-supportv3.diff
File unicode-supportv3.diff, 19.4 KB (added by [email protected]…, 15 years ago) |
---|
-
src/lib/Options.py
247 247 cf=('communication', 'agent-port')) 248 248 AGENT_HOST = Option('Remote host', default=False, cmd='-H', odesc='<hostname>') 249 249 250 ENCODING = Option('Encoding of cfg files', default=sys.getdefaultencoding(), cmd='-E', odesc='<encoding>', 251 cf=('components', 'encoding')) 252 250 253 class OptionParser(OptionSet): 251 254 '''OptionParser bootstraps option parsing, getting the value of the config file''' 252 255 def __init__(self, args): -
src/lib/Server/Core.py
200 200 class Core(object): 201 201 '''The Core object is the container for all Bcfg2 Server logic, and modules''' 202 202 203 def __init__(self, repo, structures, generators, password, svn ):203 def __init__(self, repo, structures, generators, password, svn, encoding): 204 204 object.__init__(self) 205 205 self.datastore = repo 206 206 try: … … 215 215 self.revision = '-1' 216 216 self.password = password 217 217 self.svn = svn 218 self.encoding = encoding 218 219 try: 219 220 if self.svn: 220 221 self.read_svn_revision() -
src/lib/Server/Plugins/Cfg.py
49 49 return self.basefile_reg.match(fname) 50 50 51 51 class CfgEntrySet(Bcfg2.Server.Plugin.EntrySet): 52 def __init__(self, basename, path, props, entry_type ):53 Bcfg2.Server.Plugin.EntrySet.__init__(self, basename, path, props, entry_type )52 def __init__(self, basename, path, props, entry_type, encoding): 53 Bcfg2.Server.Plugin.EntrySet.__init__(self, basename, path, props, entry_type, encoding) 54 54 self.specific = CfgMatcher(path.split('/')[-1]) 55 55 56 56 def sort_by_specific(self, one, other): … … 79 79 if entry.get('encoding') == 'base64': 80 80 entry.text = binascii.b2a_base64(data) 81 81 else: 82 entry.text = data82 entry.text = unicode(data, self.encoding) 83 83 if entry.text in ['', None]: 84 84 entry.set('empty', 'true') 85 85 -
src/lib/Server/Plugins/TGenshi.py
17 17 18 18 class TemplateFile: 19 19 '''Template file creates Genshi template structures for the loaded file''' 20 def __init__(self, name, properties, specific ):20 def __init__(self, name, properties, specific, encoding): 21 21 self.name = name 22 22 self.properties = properties 23 23 self.specific = specific 24 self.encoding = encoding 24 25 if self.specific.all: 25 26 matchname = self.name 26 27 elif self.specific.group: … … 52 53 name=fname, metadata=metadata, path=self.name, 53 54 properties=self.properties).filter(removecomment) 54 55 if isinstance(self.template, TextTemplate): 55 entry.text = stream.render('text') 56 textdata = stream.render('text') 57 if type(textdata) == unicode: 58 entry.text = textdata 59 else: 60 logger.debug("Override encoding of template to %s" % self.encoding) 61 entry.text = unicode(textdata, self.encoding) 56 62 else: 57 entry.text = stream.render('xml') 63 xmldata = stream.render('xml') 64 if type(xmldata) == unicode: 65 entry.text = xmldata 66 else: 67 logger.debug("Override encoding of template to %s" % self.encoding) 68 entry.text = unicode(xmldata, self.encoding) 58 69 except TemplateError, terror: 59 70 logger.error('Genshi template error: %s' % terror) 60 71 raise Bcfg2.Server.Plugin.PluginExecutionError -
src/lib/Server/Plugins/Metadata.py
296 296 value=self.probedata[client][probe]) 297 297 for group in self.cgroups[client]: 298 298 lxml.etree.SubElement(cx, "Group", name=group) 299 data = lxml.etree.tostring(top )299 data = lxml.etree.tostring(top, encoding='UTF-8', xml_declaration=True) 300 300 try: 301 301 datafile = open("%s/%s" % (self.data, 'probed.xml'), 'w') 302 302 except IOError: -
src/lib/Server/Plugins/TCheetah.py
9 9 10 10 class TemplateFile: 11 11 '''Template file creates Cheetah template structures for the loaded file''' 12 def __init__(self, name, properties, specific ):12 def __init__(self, name, properties, specific, encoding): 13 13 self.name = name 14 14 self.properties = properties 15 15 self.specific = specific 16 self.encoding = encoding 16 17 self.template = None 17 18 18 19 def handle_event(self, event): … … 34 35 self.template.path = entry.get('realname', entry.get('name')) 35 36 36 37 try: 37 entry.text = str(self.template) 38 if type(self.template) == unicode: 39 entry.text = self.template 40 else : 41 logger.debug("Override encoding of template to %s" % self.encoding) 42 entry.text = unicode(str(self.template), self.encoding) 38 43 except: 39 44 (a, b, c) = sys.exc_info() 40 45 msg = traceback.format_exception(a, b, c, limit=2)[-1][:-1] -
src/lib/Server/Statistics.py
28 28 except IOError, ioerr: 29 29 self.logger.error("Failed to open %s for writing: %s" % (self.filename + '.new', ioerr)) 30 30 else: 31 fout.write(lxml.etree.tostring(self.element ))31 fout.write(lxml.etree.tostring(self.element, encoding='UTF-8', xml_declaration=True)) 32 32 fout.close() 33 33 os.rename(self.filename + '.new', self.filename) 34 34 self.dirty = 0 -
src/lib/Server/Plugin.py
444 444 return False 445 445 446 446 class SpecificData(object): 447 def __init__(self, name, _, specific ):447 def __init__(self, name, _, specific, encoding): 448 448 self.name = name 449 449 self.specific = specific 450 450 … … 459 459 class EntrySet: 460 460 '''Entry sets deal with the host- and group-specific entries''' 461 461 ignore = re.compile("^(.*~|\\..*\\.(tmp|sw[px]))$") 462 def __init__(self, basename, path, props, entry_type ):462 def __init__(self, basename, path, props, entry_type, encoding): 463 463 self.path = path 464 464 self.entry_type = entry_type 465 465 self.entries = {} 466 466 self.properties = props 467 467 self.metadata = default_file_metadata.copy() 468 468 self.infoxml = None 469 self.encoding = encoding 469 470 pattern = '(.*/)?%s(\.((H_(?P<hostname>\S+))|' % basename 470 471 pattern += '(G(?P<prio>\d+)_(?P<group>\S+))))?$' 471 472 self.specific = re.compile(pattern) … … 509 510 return 510 511 self.entries[event.filename] = self.entry_type(fpath, 511 512 self.properties, 512 spec )513 spec, self.encoding) 513 514 self.entries[event.filename].handle_event(event) 514 515 515 516 def specificity_from_filename(self, fname): … … 631 632 self.entries = {} 632 633 self.handles = {} 633 634 self.AddDirectoryMonitor('') 635 self.encoding = core.encoding 634 636 if self.use_props: 635 637 try: 636 638 self.properties = TemplateProperties( \ … … 661 663 self.entries[ident] = self.es_cls(self.filename_pattern, 662 664 dirpath, 663 665 self.properties, 664 self.es_child_cls) 666 self.es_child_cls, 667 self.encoding) 665 668 self.Entries['ConfigFile'][ident] = self.entries[ident].bind_entry 666 669 if not posixpath.isdir(epath): 667 670 # do not pass through directory events -
src/lib/Server/Admin/Pull.py
70 70 new_entry['text'] = '\n'.join(difflib.restore(diff.split('\n'), 1)) 71 71 else: 72 72 print "found no data::" 73 print lxml.etree.tostring(cfentry )73 print lxml.etree.tostring(cfentry, encoding='UTF-8', xml_declaration=True) 74 74 raise SystemExit(1) 75 75 return new_entry 76 76 -
src/lib/Client/Tools/POSIX.py
320 320 self.logger.error("Cannot verify incomplete ConfigFile %s" % (entry.get('name'))) 321 321 return False 322 322 tempdata = entry.text 323 if type(tempdata) == unicode: 324 tempdata = tempdata.encode(self.setup['encoding']) 323 325 try: 324 326 content = open(entry.get('name')).read() 325 327 except IOError, error: 326 328 self.logger.error("Failed to read %s: %s" % (error.filename, error.strerror)) 327 329 return False 330 # comparaison should be done with figerprints or md5sum so it would be faster 331 # for big binary files 328 332 contentStatus = content == tempdata 329 333 if not contentStatus: 330 334 if tbin or not isString(content): … … 348 352 break 349 353 if do_diff: 350 354 diff = '\n'.join(rawdiff) 351 entry.set("current_bdiff", binascii.b2a_base64(diff)) 355 # entry.set("current_bdiff", binascii.b2a_base64(diff)) 356 # entry.set("current_diff", diff) 352 357 udiff = '\n'.join([x for x in \ 353 358 difflib.unified_diff(content.split('\n'), \ 354 359 tempdata.split('\n'))]) … … 356 361 eudiff = udiff.encode('ascii') 357 362 except: 358 363 eudiff = "Binary file: no diff printed" 364 359 365 nqtext = entry.get('qtext', '') 360 366 361 367 if nqtext: … … 417 423 elif entry.get('empty', 'false') == 'true': 418 424 filedata = '' 419 425 else: 420 filedata = entry.text 426 if type(entry.text) == unicode: 427 filedata = entry.text.encode(self.setup['encoding']) 428 else: 429 filedata = entry.text 421 430 newfile.write(filedata) 422 431 newfile.close() 423 432 try: -
src/sbin/bcfg2-ping-sweep
63 63 elm.set("pingable",'N') 64 64 65 65 fout = open(clientdatapath, 'w') 66 fout.write(lxml.etree.tostring(clientElement.getroot() ))66 fout.write(lxml.etree.tostring(clientElement.getroot(), encoding='UTF-8', xml_declaration=True)) 67 67 fout.close() 68 68 -
src/sbin/bcfg2
116 116 'agent-background': Bcfg2.Options.CLIENT_BACKGROUND, 117 117 'key': Bcfg2.Options.SERVER_KEY, 118 118 'decision-list': DECISION_LIST, 119 'encoding': Bcfg2.Options.ENCODING, 119 120 } 120 121 121 122 self.setup = Bcfg2.Options.OptionParser(optinfo) … … 244 245 if len(probes.findall(".//probe")) > 0: 245 246 try: 246 247 # upload probe responses 247 proxy.RecvProbeData(Bcfg2.Client.XML.tostring(probedata ))248 proxy.RecvProbeData(Bcfg2.Client.XML.tostring(probedata, encoding='UTF-8', xml_declaration=True)) 248 249 except: 249 250 self.logger.error("Failed to upload probe data", exc_info=1) 250 251 raise SystemExit(1) … … 293 294 feedback = self.tools.GenerateStats() 294 295 295 296 try: 296 proxy.RecvStats(Bcfg2.Client.XML.tostring(feedback ))297 proxy.RecvStats(Bcfg2.Client.XML.tostring(feedback, encoding='UTF-8', xml_declaration=True)) 297 298 except xmlrpclib.Fault: 298 299 self.logger.error("Failed to upload configuration statistics") 299 300 raise SystemExit(2) -
src/sbin/bcfg2-server
41 41 42 42 try: 43 43 self.Core = Core(setup['repo'], setup['structures'], 44 setup['generators'], setup['password'], setup['svn']) 44 setup['generators'], setup['password'], 45 setup['svn'], setup['encoding']) 45 46 except CoreInitError, msg: 46 47 logger.critical("Fatal error: %s" % (msg)) 47 48 raise SystemExit, 1 … … 104 105 if isinstance(p, Bcfg2.Server.Plugin.ProbingPlugin)]: 105 106 for probe in plugin.GetProbes(meta): 106 107 resp.append(probe) 107 return tostring(resp )108 return tostring(resp, encoding='UTF-8', xml_declaration=True) 108 109 except Bcfg2.Server.Plugins.Metadata.MetadataConsistencyError: 109 110 warning = 'Client metadata resolution error for %s; check server log' % address[0] 110 111 self.logger.warning(warning) … … 158 159 '''Build config for a client''' 159 160 try: 160 161 client = self.Core.metadata.resolve_client(address) 161 return tostring(self.Core.BuildConfiguration(client) )162 return tostring(self.Core.BuildConfiguration(client), encoding='UTF-8', xml_declaration=True) 162 163 except Bcfg2.Server.Plugins.Metadata.MetadataConsistencyError: 163 164 self.logger.warning("Metadata consistency failure for %s" % (address)) 164 165 raise Fault, (6, "Metadata consistency failure") … … 201 202 'location' : Bcfg2.Options.SERVER_LOCATION, 202 203 'passwd' : Bcfg2.Options.SERVER_PASSWORD, 203 204 'static' : Bcfg2.Options.SERVER_STATIC, 205 'encoding' : Bcfg2.Options.ENCODING, 204 206 }) 205 207 206 208 -
src/sbin/bcfg2-build-reports
100 100 for item in items: 101 101 channel.append(item) 102 102 103 tree = "<?xml version=\"1.0\"?>" + tostring(rssdata)103 tree = tostring(rssdata, encoding='UTF-8', xml_declaration=True) 104 104 fil.write(tree) 105 105 fil.close() 106 106 … … 249 249 250 250 #apply XSLT, different ones based on report type, and options 251 251 if deliverymechanism == 'null-operator': #Special Cases 252 fileout(tostring(ElementTree(procnodereport).getroot() ), deliv)252 fileout(tostring(ElementTree(procnodereport).getroot(), encoding='UTF-8', xml_declaration=True), deliv) 253 253 break 254 254 transform = delivtype + '-' + deliverymechanism + '.xsl' 255 255 … … 301 301 (toastring, socket.getfqdn(), outputstring) 302 302 mail(outputstring, c) #call function to send 303 303 else: 304 outputstring = tostring(stylesheet.apply(ElementTree(procnodereport)).getroot() )304 outputstring = tostring(stylesheet.apply(ElementTree(procnodereport)).getroot(), encoding='UTF-8', xml_declaration=True) 305 305 if deliverymechanism == 'rss': 306 306 rss(outputstring, deliv, reprt) 307 307 else: # must be deliverymechanism == 'www': -
src/sbin/bcfg2-info
23 23 print fstring % row 24 24 25 25 class infoCore(cmd.Cmd, Bcfg2.Server.Core.Core): 26 def __init__(self, repo, struct, gens, passwd, svn ):26 def __init__(self, repo, struct, gens, passwd, svn, encoding): 27 27 cmd.Cmd.__init__(self) 28 28 try: 29 Bcfg2.Server.Core.Core.__init__(self, repo, struct, gens, passwd, svn )29 Bcfg2.Server.Core.Core.__init__(self, repo, struct, gens, passwd, svn, encoding) 30 30 except Bcfg2.Server.Core.CoreInitError, msg: 31 31 print "Core load failed because %s" % msg 32 32 raise SystemExit(1) … … 97 97 if len(args.split()) == 2: 98 98 client, ofile = args.split() 99 99 output = open(ofile, 'w') 100 data = lxml.etree.tostring(self.BuildConfiguration(client) )100 data = lxml.etree.tostring(self.BuildConfiguration(client), encoding='UTF-8', xml_declaration=True) 101 101 output.write(data) 102 102 output.close() 103 103 else: … … 121 121 entry = lxml.etree.Element('ConfigFile', name=fname) 122 122 metadata = self.metadata.get_metadata(client) 123 123 self.Bind(entry, metadata) 124 print lxml.etree.tostring(entry )124 print lxml.etree.tostring(entry, encoding="UTF-8", xml_declaration=True) 125 125 else: 126 126 print 'Usage: buildfile filename hostname' 127 127 … … 271 271 'svn': Bcfg2.Options.SERVER_SVN, 272 272 'structures': Bcfg2.Options.SERVER_STRUCTURES, 273 273 'generators': Bcfg2.Options.SERVER_GENERATORS, 274 'password': Bcfg2.Options.SERVER_PASSWORD}) 274 'password': Bcfg2.Options.SERVER_PASSWORD, 275 'encoding': Bcfg2.Options.ENCODING}) 275 276 setup = Bcfg2.Options.OptionParser(optinfo) 276 277 setup.parse(sys.argv[1:]) 277 278 278 279 loop = infoCore(setup['repo'], setup['structures'], setup['generators'], 279 setup['password'], setup['svn'] )280 setup['password'], setup['svn'], setup['encoding']) 280 281 loop.plugins['Metadata'] 281 282 if "args" in setup and setup['args']: 282 283 loop.onecmd(" ".join(setup['args']))