blob: a57121b557379acab04c4a8101477610a240ac92 [file] [log] [blame]
# Copyright (c) 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import collections
import os
import re
from import check_common
from trace_viewer import trace_viewer_project
class _Token(object):
def __init__(self, data, id=None): = data
if id: = id
else: = 'plain'
class BuildFile(object):
def __init__(self, text, file_groups):
self._file_groups = file_groups
self._tokens = [token for token in self._Tokenize(text)]
def _Tokenize(self, text):
rest = text
token_regex = self._TokenRegex()
while len(rest):
m =
if not m:
# In `rest', we couldn't find a match.
# So, lump the entire `rest' into a token
# and stop producing any more tokens.
yield _Token(rest)
min_index, end_index, matched_token = self._ProcessMatch(m)
if min_index > 0:
yield _Token(rest[:min_index])
yield matched_token
rest = rest[end_index:]
def Update(self, files_by_group):
for token in self._tokens:
if in files_by_group: = self._GetReplacementListAsString(,
def Write(self, f):
for token in self._tokens:
def _ProcessMatch(self, match):
raise Exception("Not implemented.")
def _TokenRegex(self):
raise Exception("Not implemented.")
def _GetReplacementListAsString(self, existing_list_as_string, filelist):
raise Exception("Not implemented.")
class GypiFile(BuildFile):
def _ProcessMatch(self, match):
min_index = match.start(2)
end_index = match.end(2)
token = _Token(match.string[min_index:end_index],
return min_index, end_index, token
def _TokenRegex(self):
# regexp to match the following:
# 'file_group_name': [
# 'path/to/one/file.extension',
# 'another/file.ex',
# ]
# In the match,
# group 1 is : 'file_group_name'
# group 2 is : """ 'path/to/one/file.extension',\n 'another/file.ex',\n"""
regexp_str = "'(%s)': \[\n(.+?) +\],?\n" % "|".join(self._file_groups)
return re.compile(regexp_str, re.MULTILINE | re.DOTALL)
def _GetReplacementListAsString(self, existing_list_as_string, filelist):
list_entry = existing_list_as_string.splitlines()[0]
prefix, entry, suffix = list_entry.split("'")
return "".join(["'".join([prefix, filename, suffix + '\n'])
for filename in filelist])
def _GroupFiles(fileNameToGroupNameFunc, filenames):
file_groups = collections.defaultdict(lambda: [])
for filename in filenames:
for group in file_groups:
return file_groups
def _UpdateBuildFile(filename, build_file_class):
updated_content = None
with open(filename, 'r') as f:
build_file = build_file_class(, check_common.FILE_GROUPS)
files_by_group = _GroupFiles(check_common.GetFileGroupFromFileName,
with open(filename, 'w') as f:
def UpdateGypi():
tvp = trace_viewer_project.TraceViewerProject()
os.path.join(tvp.trace_viewer_path, 'trace_viewer.gypi'), GypiFile)
def Update():