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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 | 73 |
74 def expand_file(input_path, output_path): | 74 def expand_file(input_path, output_path): |
75 processor = TemplateProcessor() | 75 processor = TemplateProcessor() |
76 try: | 76 try: |
77 with open(input_path, 'r') as infile: | 77 with open(input_path, 'rb') as infile: |
78 with open(output_path, 'w') as outfile: | 78 with open(output_path, 'wb') as outfile: |
79 for line in infile: | 79 for line in infile: |
80 outfile.write(processor.process_line(line)) | 80 outfile.write(processor.process_line(line)) |
81 except IOError: | 81 except IOError: |
82 print >> sys.stderr, 'failed to process %s' % input_path | 82 print >> sys.stderr, 'failed to process %s' % input_path |
83 | 83 |
84 | 84 |
85 def main(): | 85 def main(): |
86 parser = optparse.OptionParser() | 86 parser = optparse.OptionParser() |
87 parser.add_option('--output-dir', default='') | 87 parser.add_option('--output-dir', default='') |
88 options, args = parser.parse_args() | 88 options, args = parser.parse_args() |
89 for testcase_path in args: | 89 for testcase_path in args: |
90 testcase_filename = os.path.basename(testcase_path) | 90 testcase_filename = os.path.basename(testcase_path) |
91 testcase_root, _ = os.path.splitext(testcase_filename) | 91 testcase_root, _ = os.path.splitext(testcase_filename) |
92 output_dir = os.path.dirname(testcase_path) | 92 output_dir = os.path.dirname(testcase_path) |
93 if options.output_dir: | 93 if options.output_dir: |
94 output_dir = options.output_dir | 94 output_dir = options.output_dir |
95 output_path = os.path.join(output_dir, testcase_root + '.pdf') | 95 output_path = os.path.join(output_dir, testcase_root + '.pdf') |
96 expand_file(testcase_path, output_path) | 96 expand_file(testcase_path, output_path) |
97 return 0 | 97 return 0 |
98 | 98 |
99 | 99 |
100 if __name__ == '__main__': | 100 if __name__ == '__main__': |
101 sys.exit(main()) | 101 sys.exit(main()) |
OLD | NEW |