Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Last active September 20, 2018 16:15
Show Gist options
  • Save HalCanary/55d583d0b655e6c4f4724cbb20ccf181 to your computer and use it in GitHub Desktop.
Save HalCanary/55d583d0b655e6c4f4724cbb20ccf181 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
# Copyright 2018 Google LLC.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
import os
import re
import sys
class Repl:
def __init__(self, root):
self.root = root
self.is_cpp = re.compile('\.(h|cpp)$')
bad_dirs = re.compile(
'^('
'out/|'
'src/compute/color/|'
'src/compute/common/|'
'src/compute/hs/|'
'src/compute/skc/|'
'src/compute/ts/|'
'include/c/|'
'third_party/'
')')
bad_files = [
'SkBitmapProcState_filter_neon.h',
'SkBitmapProcState_procs.h',
'SkBitmapProcState_filter.h',
'SkPreConfig.h',
'SkUserConfig.h',
'SkPostConfig.h',
]
allfiles = {}
for dirpath, dirnames, filenames in os.walk(root):
dirpath = os.path.normpath(dirpath)
if bad_dirs.search(dirpath + '/'):
continue
for f in filenames:
if self.is_cpp.search(f) and not f in bad_files:
allfiles[f] = os.path.relpath(os.path.join(dirpath, f), root)
self._allfiles = allfiles
self.regex = re.compile('^( *# *include ")([^"]+)(" *(|//.*))$', flags=re.MULTILINE)
def __call__(self, m):
x = m.group(2)
y = os.path.basename(x)
if y in self._allfiles:
fullpath = self._allfiles[y]
if fullpath != x:
if re.match('^[./]',x) or fullpath.endswith("/" + x):
return m.group(1) + fullpath + m.group(3)
return m.group(0)
def expand(self, path):
if self.is_cpp.search(path):
with open(path, 'r') as f:
x = f.read()
if self.regex.search(x):
y = self.regex.sub(self, x)
if x != y:
with open(path, 'w') as o:
o.write(y)
return True
return False
def walk_and_replace(directory, repl):
sys.stderr.write(directory + ' ' + repl.root + ' ')
for dirpath, dirnames, filenames in os.walk(directory):
for f in filenames:
if repl.expand(os.path.join(dirpath, f)):
sys.stderr.write('.')
sys.stderr.write('\n')
if __name__ == '__main__':
repl = Repl('.')
for directory in [ 'animations', 'bench', 'dm', 'fuzz', 'gm',
'samplecode', 'src', 'tests', 'tools' ]:
walk_and_replace(directory, repl)
repl = Repl('include')
for directory in ['include']:
walk_and_replace(directory, repl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment