• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights
2# reserved. Use of this source code is governed by a BSD-style license that
3# can be found in the LICENSE file
4
5from __future__ import absolute_import
6from __future__ import print_function
7from exec_util import exec_cmd
8import os
9import sys
10
11# Script directory.
12script_dir = os.path.dirname(__file__)
13root_dir = os.path.join(script_dir, os.pardir)
14
15if sys.platform == 'win32':
16  # Force use of the clang-format version bundled with depot_tools.
17  clang_format_exe = 'clang-format.bat'
18else:
19  clang_format_exe = 'clang-format'
20
21
22def clang_format(file_name, file_contents):
23  # -assume-filename is necessary to find the .clang-format file and determine
24  # the language when specifying contents via stdin.
25  result = exec_cmd("%s -assume-filename=%s" % (clang_format_exe, file_name), \
26                    root_dir, file_contents.encode('utf-8'))
27  if result['err'] != '':
28    print("clang-format error: %s" % result['err'])
29  if result['out'] != '':
30    output = result['out']
31    if sys.platform == 'win32':
32      # Convert to Unix line endings.
33      output = output.replace("\r", "")
34    return output
35  return None
36