1#!/usr/bin/env python 2# Copyright (c) 2012 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6'''The 'grit sdiff' tool. 7''' 8 9import os 10import getopt 11import tempfile 12 13from grit.node import structure 14from grit.tool import interface 15 16from grit import constants 17from grit import util 18 19# Builds the description for the tool (used as the __doc__ 20# for the DiffStructures class). 21_class_doc = """\ 22Allows you to view the differences in the structure of two files, 23disregarding their translateable content. Translateable portions of 24each file are changed to the string "TTTTTT" before invoking the diff program 25specified by the P4DIFF environment variable. 26 27Usage: grit sdiff [-t TYPE] [-s SECTION] [-e ENCODING] LEFT RIGHT 28 29LEFT and RIGHT are the files you want to diff. SECTION is required 30for structure types like 'dialog' to identify the part of the file to look at. 31ENCODING indicates the encoding of the left and right files (default 'cp1252'). 32TYPE can be one of the following, defaults to 'tr_html': 33""" 34for gatherer in structure._GATHERERS: 35 _class_doc += " - %s\n" % gatherer 36 37 38class DiffStructures(interface.Tool): 39 __doc__ = _class_doc 40 41 def __init__(self): 42 self.section = None 43 self.left_encoding = 'cp1252' 44 self.right_encoding = 'cp1252' 45 self.structure_type = 'tr_html' 46 47 def ShortDescription(self): 48 return 'View differences without regard for translateable portions.' 49 50 def Run(self, global_opts, args): 51 (opts, args) = getopt.getopt(args, 's:e:t:', 52 ['left_encoding=', 'right_encoding=']) 53 for key, val in opts: 54 if key == '-s': 55 self.section = val 56 elif key == '-e': 57 self.left_encoding = val 58 self.right_encoding = val 59 elif key == '-t': 60 self.structure_type = val 61 elif key == '--left_encoding': 62 self.left_encoding = val 63 elif key == '--right_encoding': 64 self.right_encoding == val 65 66 if len(args) != 2: 67 print "Incorrect usage - 'grit help sdiff' for usage details." 68 return 2 69 70 if 'P4DIFF' not in os.environ: 71 print "Environment variable P4DIFF not set; defaulting to 'windiff'." 72 diff_program = 'windiff' 73 else: 74 diff_program = os.environ['P4DIFF'] 75 76 left_trans = self.MakeStaticTranslation(args[0], self.left_encoding) 77 try: 78 try: 79 right_trans = self.MakeStaticTranslation(args[1], self.right_encoding) 80 81 os.system('%s %s %s' % (diff_program, left_trans, right_trans)) 82 finally: 83 os.unlink(right_trans) 84 finally: 85 os.unlink(left_trans) 86 87 def MakeStaticTranslation(self, original_filename, encoding): 88 """Given the name of the structure type (self.structure_type), the filename 89 of the file holding the original structure, and optionally the "section" key 90 identifying the part of the file to look at (self.section), creates a 91 temporary file holding a "static" translation of the original structure 92 (i.e. one where all translateable parts have been replaced with "TTTTTT") 93 and returns the temporary file name. It is the caller's responsibility to 94 delete the file when finished. 95 96 Args: 97 original_filename: 'c:\\bingo\\bla.rc' 98 99 Return: 100 'c:\\temp\\werlkjsdf334.tmp' 101 """ 102 original = structure._GATHERERS[self.structure_type](original_filename, 103 extkey=self.section, 104 encoding=encoding) 105 original.Parse() 106 translated = original.Translate(constants.CONSTANT_LANGUAGE, False) 107 108 fname = tempfile.mktemp() 109 with util.WrapOutputStream(open(fname, 'w')) as writer: 110 writer.write("Original filename: %s\n=============\n\n" 111 % original_filename) 112 writer.write(translated) # write in UTF-8 113 114 return fname 115