<commit_before>
import collections
import json
from . import core, dsl, path, props


class Context(dsl.BaseDslContext):

  def __init__(self, build_directory, build_mode='debug', backend_name=None):
    self.path = ['.', path.join(path.dir(__file__), 'lib')]
    self.options = {}
    self.modules = {}
    self.build_directory = build_directory
    self.build_mode = build_mode
    self.backend_name = backend_name or 'backend.ninja'

    self.builtins = props.Namespace('builtins')
    self.builtins.context = self
    self.load_file(path.join(path.dir(__file__), 'builtins.py'), self.builtins)

    self.build_graph = core.BuildGraph()

  def translate_targets(self, module):
    seen = set()
    def translate(target):
      for dep in target.dependencies():
        if dep.target():
          translate(dep.target())
        else:
          for target in dep.module().targets():
            translate(target)
      if target not in seen:
        seen.add(target)
        for handler in target.target_handlers():
          handler.translate_target(target, target.handler_data(handler))
    for target in module.targets():
      translate(target)
    for module in self.modules.values():
      for target in module.targets():
        self.build_graph.add_actions(target.actions())

  def load_module_file(self, filename):
    with open(filename) as fp:
      project = dsl.Parser().parse(fp.read())
    if project.name in self.modules:
      raise RuntimeError('modules {!r} already loaded'.format(project.name))
    module = dsl.Interpreter(self, filename)(project)
    self.modules[module.name()] = module
    return module

  def to_json(self):
    root = collections.OrderedDict()
    root['path'] = self.path
    root['backend'] = self.backend_name
    root['mode'] = self.build_mode
    root['directory'] = self.build_directory
    root['options'] = None  # TODO: Include options specified via the command-line.
    root['graph'] = self.build_graph.to_json()
    return root

  def from_json(self, root):
    self.path = root['path']
    self.backend_name = root['backend']
    self.build_mode = root['mode']
    if self.build_directory != root['directory']:
      print('warning: stored build directory does not match current build directory')
    # TODO: Read options
    self.build_graph.from_json(root['graph'])

  def serialize(self):
    path.makedirs(self.build_directory)
    with open(path.join(self.build_directory, 'CraftrBuildGraph.json'), 'w') as fp:
      json.dump(self.to_json(), fp)

  def deserialize(self):
    with open(path.join(self.build_directory, 'CraftrBuildGraph.json')) as fp:
      root = json.load(fp)
    self.from_json(root)

  # dsl.Context<commit_msg>Context.deserialize() reads in OrderedDicts now<commit_after>
import collections
import json
from . import core, dsl, path, props


class Context(dsl.BaseDslContext):

  def __init__(self, build_directory, build_mode='debug', backend_name=None):
    self.path = ['.', path.join(path.dir(__file__), 'lib')]
    self.options = {}
    self.modules = {}
    self.build_directory = build_directory
    self.build_mode = build_mode
    self.backend_name = backend_name or 'backend.ninja'

    self.builtins = props.Namespace('builtins')
    self.builtins.context = self
    self.load_file(path.join(path.dir(__file__), 'builtins.py'), self.builtins)

    self.build_graph = core.BuildGraph()

  def translate_targets(self, module):
    seen = set()
    def translate(target):
      for dep in target.dependencies():
        if dep.target():
          translate(dep.target())
        else:
          for target in dep.module().targets():
            translate(target)
      if target not in seen:
        seen.add(target)
        for handler in target.target_handlers():
          handler.translate_target(target, target.handler_data(handler))
    for target in module.targets():
      translate(target)
    for module in self.modules.values():
      for target in module.targets():
        self.build_graph.add_actions(target.actions())

  def load_module_file(self, filename):
    with open(filename) as fp:
      project = dsl.Parser().parse(fp.read())
    if project.name in self.modules:
      raise RuntimeError('modules {!r} already loaded'.format(project.name))
    module = dsl.Interpreter(self, filename)(project)
    self.modules[module.name()] = module
    return module

  def to_json(self):
    root = collections.OrderedDict()
    root['path'] = self.path
    root['backend'] = self.backend_name
    root['mode'] = self.build_mode
    root['directory'] = self.build_directory
    root['options'] = None  # TODO: Include options specified via the command-line.
    root['graph'] = self.build_graph.to_json()
    return root

  def from_json(self, root):
    self.path = root['path']
    self.backend_name = root['backend']
    self.build_mode = root['mode']
    if self.build_directory != root['directory']:
      print('warning: stored build directory does not match current build directory')
    # TODO: Read options
    self.build_graph.from_json(root['graph'])

  def serialize(self):
    path.makedirs(self.build_directory)
    with open(path.join(self.build_directory, 'CraftrBuildGraph.json'), 'w') as fp:
      json.dump(self.to_json(), fp)

  def deserialize(self):
    with open(path.join(self.build_directory, 'CraftrBuildGraph.json')) as fp:
      root = json.load(fp, object_pairs_hook=collections.OrderedDict)
    self.from_json(root)

  # dsl.Context
{'fraction_non_alphanumeric': 0.12730931040790197, 'fraction_numerical': 0.0, 'mean_word_length': 3.4893267651888342, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3901088', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1645, 'n_words': 372}