| 152 | return output |
| 153 | |
| 154 | def process(filename, lint, should_format): |
| 155 | with open(filename, 'r') as content_file: |
| 156 | content = content_file.read() |
| 157 | |
| 158 | original_input = content |
| 159 | |
| 160 | style = {"BasedOnStyle": "Google", "NamespaceIndentation": "None"} |
| 161 | |
| 162 | if sys.platform.startswith('win'): |
| 163 | p = Popen( |
| 164 | ['clang-format', '-assume-filename=.ts', '-style=' + json.dumps(style)], |
| 165 | stdin=PIPE, |
| 166 | stdout=PIPE, |
| 167 | stderr=PIPE, |
| 168 | shell=True) |
| 169 | else: |
| 170 | p = Popen( |
| 171 | ['clang-format', '-assume-filename=.ts', '-style=' + json.dumps(style)], |
| 172 | stdin=PIPE, |
| 173 | stdout=PIPE, |
| 174 | stderr=PIPE) |
| 175 | output, err = p.communicate(encode(preprocess(content))) |
| 176 | output = postprocess(decode(output)) |
| 177 | rc = p.returncode |
| 178 | if (rc != 0): |
| 179 | print("error code " + str(rc) + " running clang-format. Exiting...") |
| 180 | sys.exit(rc); |
| 181 | |
| 182 | if (output != original_input): |
| 183 | if lint: |
| 184 | print(filename + ' requires formatting', file=sys.stderr) |
| 185 | |
| 186 | if should_format: |
| 187 | output_file = open(filename, 'wb') |
| 188 | output_file.write(encode(output)) |
| 189 | output_file.close() |
| 190 | |
| 191 | def print_usage(): |
| 192 | print('format-torque -i file1[, file2[, ...]]') |