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