Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The PDFium Authors. All rights reserved. | 2 # Copyright 2014 The PDFium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Expands a hand-written PDF testcase (template) into a valid PDF file. | 6 """Expands a hand-written PDF testcase (template) into a valid PDF file. |
| 7 | 7 |
| 8 There are several places in a PDF file where byte-offsets are required. This | 8 There are several places in a PDF file where byte-offsets are required. This |
| 9 script replaces {{name}}-style variables in the input with calculated results | 9 script replaces {{name}}-style variables in the input with calculated results |
| 10 | 10 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 if self.STARTXREF_TOKEN in line: | 63 if self.STARTXREF_TOKEN in line: |
| 64 replacement = self.STARTXREF_REPLACEMENT % self.xref_offset | 64 replacement = self.STARTXREF_REPLACEMENT % self.xref_offset |
| 65 line = line.replace(self.STARTXREF_TOKEN, replacement) | 65 line = line.replace(self.STARTXREF_TOKEN, replacement) |
| 66 match = re.match(self.OBJECT_PATTERN, line) | 66 match = re.match(self.OBJECT_PATTERN, line) |
| 67 if match: | 67 if match: |
| 68 self.insert_xref_entry(int(match.group(1)), int(match.group(2))) | 68 self.insert_xref_entry(int(match.group(1)), int(match.group(2))) |
| 69 line = re.sub(self.OBJECT_PATTERN, self.OBJECT_REPLACEMENT, line) | 69 line = re.sub(self.OBJECT_PATTERN, self.OBJECT_REPLACEMENT, line) |
| 70 self.offset += len(line) | 70 self.offset += len(line) |
| 71 return line | 71 return line |
| 72 | 72 |
| 73 def expand_file(input_filename): | 73 def expand_file(input_filename, output_dir): |
|
Lei Zhang
2015/02/10 23:09:29
Two blank lines between top-level definitions: htt
Tom Sepez
2015/02/10 23:33:31
Done.
| |
| 74 (input_root, extension) = os.path.splitext(input_filename) | 74 (input_root, extension) = os.path.splitext(input_filename) |
|
Lei Zhang
2015/02/10 23:09:29
BTW, if you don't need |extension|, you can just p
Tom Sepez
2015/02/10 23:33:31
Done.
| |
| 75 output_filename = input_root + '.pdf' | 75 output_filename = os.path.join(output_dir, input_root + '.pdf') |
| 76 output_dirname = os.path.dirname(output_filename) | |
| 77 if not os.path.exists(output_dirname): | |
| 78 os.makedirs(output_dirname) | |
| 76 processor = TemplateProcessor() | 79 processor = TemplateProcessor() |
| 77 try: | 80 try: |
| 78 with open(input_filename, 'r') as infile: | 81 with open(input_filename, 'r') as infile: |
| 79 with open(output_filename, 'w') as outfile: | 82 with open(output_filename, 'w') as outfile: |
| 80 for line in infile: | 83 for line in infile: |
| 81 outfile.write(processor.process_line(line)) | 84 outfile.write(processor.process_line(line)) |
| 82 except IOError: | 85 except IOError: |
| 83 print >> sys.stderr, 'failed to process %s' % input_filename | 86 print >> sys.stderr, 'failed to process %s' % input_filename |
| 84 | 87 |
| 85 def main(): | 88 def main(): |
| 86 for arg in sys.argv[1:]: | 89 parser = optparse.OptionParser() |
| 87 expand_file(arg) | 90 parser.add_option('--build-dir', default='') |
| 91 (options, args) = parser.parse_args() | |
| 92 working_dir = options.build_dir | |
| 93 if working_dir: | |
| 94 working_dir = os.path.join(options.build_dir, 'gen', 'pdfium') | |
| 95 for arg in args: | |
| 96 expand_file(arg, working_dir) | |
| 88 return 0 | 97 return 0 |
| 89 | 98 |
| 90 if __name__ == '__main__': | 99 if __name__ == '__main__': |
| 91 sys.exit(main()) | 100 sys.exit(main()) |
| OLD | NEW |