<commit_before># Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Rules for manipulation of various packaging."""
load(":path.bzl", "dest_path", "compute_data_path")

# Filetype to restrict inputs
tar_filetype = [".tar", ".tar.gz", ".tgz", ".tar.xz", ".tar.bz2"]
deb_filetype = [".deb", ".udeb"]

def _pkg_tar_impl(ctx):
  """Implementation of the pkg_tar rule."""
  # Compute the relative path
  data_path = compute_data_path(ctx.outputs.out, ctx.attr.strip_prefix)

  build_tar = ctx.executable.build_tar
  args = [
      "--output=" + ctx.outputs.out.path,
      "--directory=" + ctx.attr.package_dir,
      "--mode=" + ctx.attr.mode,
      "--owner=" + ctx.attr.owner,
      "--owner_name=" + ctx.attr.ownername,
      ]
  file_inputs = ctx.files.srcs[:]
  args += ["--file=%s=%s" % (f.path, dest_path(f, data_path))
           for f in ctx.files.srcs]
  for target, f_dest_path in ctx.attr.files.items():
    target_files = target.files.to_list()
    if len(target_files) != 1:
      fail("Inputs to pkg_tar.files_map must describe exactly one file.")
    file_inputs += [target_files[0]]
    args += ["--file=%s=%s" % (target_files[0].path, f_dest_path)]
  if ctx.attr.modes:
    args += ["--modes=%s=%s" % (key, ctx.attr.modes[key]) for key in ctx.attr.modes]
  if ctx.attr.owners:
    args += ["--owners=%s=%s" % (key, ctx.attr.owners[key]) for key in ctx.attr.owners]
  if ctx.attr.ownernames:
    args += ["--owner_names=%s=%s" % (key, ctx.attr.ownernames[key])
             for key in ctx.attr.ownernames]
  if ctx.attr.empty_files:
    args += ["--empty_file=%s" % empty_file for empty_file in ctx.attr.empty_files]
  if ctx.attr.empty_dirs:
    args += ["--empty_dir=%s" % empty_dir for empty_dir in ctx.attr.empty_dirs]
  if ctx.attr.extension:
    dotPos = ctx.attr.extension.find('.')
    if dotPos > 0:
      dotPos += 1
      args += ["--compression=%s" % ctx.attr.extension[dotPos:]]
  args += ["--tar=" + f.path for f in ctx.files.deps]
  args += ["--link=%s:%s" % (k, ctx.attr.symlinks[k])
           for k in ctx.attr.symlinks]
  arg_file = ctx.actions.declare_file(ctx.label.name + ".args")
  ctx.actions.write(arg_file, "\n".join(args))

  ctx.actions.run_shell(
      command = "%s --flagfile=%s" % (build_tar.path, arg_file.path),
      inputs = file_inputs + ctx.files.deps + [arg_file, build_tar],
      outputs = [ctx.outputs.out],
      mnemonic="PackageTar",
      use_default_shell_env = True,
      )

def _pkg_deb_impl(ctx):
  """The implementation for the pkg_deb rule."""
  files = [ctx.file.data]
  args = [
      "--output=" + ctx.outputs.deb.path,
      "--changes=" + ctx.outputs.changes.path,
      "--data=" + ctx.file.data.path,
      "--package=" + ctx.attr.package,
      "--architecture=" + ctx.attr.architecture,
      "--maintainer=" + ctx.attr.maintainer,
      ]
  if ctx.attr.preinst:
    args += ["--preinst=@" + ctx.file.preinst.path]
    files += [ctx.file.preinst]
  if ctx.attr.postinst:
    args += ["--postinst=@" + ctx.file.postinst.path]
    files += [ctx.file.postinst]
  if ctx.attr.prerm:
    args += ["--prerm=@" + ctx.file.prerm.path]
    files += [ctx.file.prerm]
  if ctx.attr.postrm:
    args += ["--postrm=@" + ctx.file.postrm.path]
    files += [ctx.file.postrm]

  # Conffiles can be specified by a file or a string list
  if ctx.attr.conffiles_file:
    if ctx.attr.conffiles:
      fail("Both conffiles and conffiles_file attributes were specified")
    args += ["--conffile=@" + ctx.file.conffiles_file.path]
    files += [ctx.file.conffiles_file]
  elif ctx.attr.conffiles:
    args += ["--conffile=%s" % cf for cf in ctx.attr.conffiles]

  # Version and description can be specified by a file or inlined
  if ctx.attr.version_file:
    if ctx.attr.version:
      fail("Both version and version_file attributes were specified")
    args += ["--version=@" + ctx.file.version_file.path]
    files += [ctx.file.version_file]
  elif ctx.attr.version:
    args += ["--version=" + ctx.attr.version]
  else:
    fail("Neither version_file nor version attribute was specified")

  if ctx.attr.description_file:
    if ctx.attr.description:
      fail("Both description and description_file attributes were specified")
    args += ["--description=@" + ctx.file.description_file.path]
    files += [ctx.file.description_file]
  elif ctx.attr.description:
    args += ["--description=" + ctx.attr.description]
  else:
    fail("Neither description_file nor description attribute was specified")

  # Built using can also be specified by a file or inlined (but is not mandatory)
  if ctx.attr.built_using_file:
    if ctx.attr.built_using:
      fail("Both build_using and built_using_file attributes were specified")
    args += ["--built_using=@" + ctx.file.built_using_file.path]
    files += [ctx.file.built_using_file]
  elif ctx.attr.built_using:
    args += ["--built_using=" + ctx.attr.built_using]

  if ctx.attr.priority:
    args += ["--priority=" + ctx.attr.priority]
  if ctx.attr.section:
    args += ["--section=" + ctx.attr.section]
  if ctx.attr.homepage:
    args += ["--homepage=" + ctx.attr.homepage]

  args += ["--distribution=" + ctx.attr.distribution]
  args += ["--urgency=" + ctx.attr.urgency]
  args += ["--depends=" + d for d in ctx.attr.depends]
  args += ["--suggests=" + d for d in ctx.attr.suggests]
  args += ["--enhances=" + d for d in ctx.attr.enhances]
  args += ["--conflicts=" + d for d in ctx.attr.conflicts]
  args += ["--pre_depends=" + d for d in ctx.attr.predepends]
  args += ["--recommends=" + d for d in ctx.attr.recommends]

  ctx.action(
      executable = ctx.executable.make_deb,
      arguments = args,
      inputs = files,
      outputs = [ctx.outputs.deb, ctx.outputs.changes],
      mnemonic="MakeDeb"
      )
  ctx.action(
      command = "ln -s %s %s" % (ctx.outputs.deb.basename, ctx.outputs.out.path),
      inputs = [ctx.outputs.deb],
      outputs = [ctx.outputs.out])

# A rule for creating a tar file, see README.md
_real_pkg_tar = rule(
    implementation = _pkg_tar_impl,
    attrs = {
        "strip_prefix": attr.string(),
        "package_dir": attr.string(default="/"),
        "deps": attr.label_list(allow_files=tar_filetype),
        "srcs": attr.label_list(allow_files=True),
        "files": attr.label_keyed_string_dict(allow_files=True),
        "mode": attr.string(default="0555"),
        "modes": attr.string_dict(),
        "owner": attr.string(default="0.0"),
        "ownername": attr.string(default="."),
        "owners": attr.string_dict(),
        "ownernames": attr.string_dict(),
        "extension": attr.string(default="tar"),
        "symlinks": attr.string_dict(),
        "empty_files": attr.string_list(),
        "empty_dirs": attr.string_list(),
        # Implicit dependencies.
        "build_tar": attr.label(
            default=Label("//tools/build_defs/pkg:build_tar"),
            cfg="host",
            executable=True,
            allow_files=True)
    },
    outputs = {
        "out": "%{name}.%{extension}",
    },
    executable = False)

def pkg_tar(**kwargs):
  # Compatibility with older versions of pkg_tar that define files as
  # a flat list of labels.
  if "srcs" not in kwargs:
    if "files" in kwargs:
      if not hasattr(kwargs["files"], "items"):
        label = "%s//%s:%s" % (REPOSITORY_NAME, native.package_name(), kwargs["name"])
        print("%s: you provided a non dictionary to the pkg_tar `files` attribute. " % (label,) +
              "This attribute was renamed to `srcs`. " +
              "Consider renaming it in your BUILD file.")
        kwargs["srcs"] = kwargs.pop("files")
  _real_pkg_tar(**kwargs)

# A rule for creating a deb file, see README.md
pkg_deb = rule(
    implementation = _pkg_deb_impl,
    attrs = {
        "data": attr.label(mandatory=True, allow_files=tar_filetype, single_file=True),
        "package": attr.string(mandatory=True),
        "architecture": attr.string(default="all"),
        "distribution": attr.string(default="unstable"),
        "urgency": attr.string(default="medium"),
        "maintainer": attr.string(mandatory=True),
        "preinst": attr.label(allow_files=True, single_file=True),
        "postinst": attr.label(allow_files=True, single_file=True),
        "prerm": attr.label(allow_files=True, single_file=True),
        "postrm": attr.label(allow_files=True, single_file=True),
        "conffiles_file": attr.label(allow_files=True, single_file=True),
        "conffiles": attr.string_list(default=[]),
        "version_file": attr.label(allow_files=True, single_file=True),
        "version": attr.string(),
        "description_file": attr.label(allow_files=True, single_file=True),
        "description": attr.string(),
        "built_using_file": attr.label(allow_files=True, single_file=True),
        "built_using": attr.string(),
        "priority": attr.string(),
        "section": attr.string(),
        "homepage": attr.string(),
        "depends": attr.string_list(default=[]),
        "suggests": attr.string_list(default=[]),
        "enhances": attr.string_list(default=[]),
        "conflicts": attr.string_list(default=[]),
        "predepends": attr.string_list(default=[]),
        "recommends": attr.string_list(default=[]),
        # Implicit dependencies.
        "make_deb": attr.label(
            default=Label("//tools/build_defs/pkg:make_deb"),
            cfg="host",
            executable=True,
            allow_files=True)
    },
    outputs = {
        "out": "%{name}.deb",
        "deb": "%{package}_%{version}_%{architecture}.deb",
        "changes": "%{package}_%{version}_%{architecture}.changes"
    },
    executable = False)
<commit_msg>Automatic code cleanup.<commit_after># Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Rules for manipulation of various packaging."""
load(":path.bzl", "dest_path", "compute_data_path")

# Filetype to restrict inputs
tar_filetype = [".tar", ".tar.gz", ".tgz", ".tar.xz", ".tar.bz2"]
deb_filetype = [".deb", ".udeb"]

def _pkg_tar_impl(ctx):
  """Implementation of the pkg_tar rule."""
  # Compute the relative path
  data_path = compute_data_path(ctx.outputs.out, ctx.attr.strip_prefix)

  build_tar = ctx.executable.build_tar
  args = [
      "--output=" + ctx.outputs.out.path,
      "--directory=" + ctx.attr.package_dir,
      "--mode=" + ctx.attr.mode,
      "--owner=" + ctx.attr.owner,
      "--owner_name=" + ctx.attr.ownername,
      ]
  file_inputs = ctx.files.srcs[:]
  args += ["--file=%s=%s" % (f.path, dest_path(f, data_path))
           for f in ctx.files.srcs]
  for target, f_dest_path in ctx.attr.files.items():
    target_files = target.files.to_list()
    if len(target_files) != 1:
      fail("Inputs to pkg_tar.files_map must describe exactly one file.")
    file_inputs += [target_files[0]]
    args += ["--file=%s=%s" % (target_files[0].path, f_dest_path)]
  if ctx.attr.modes:
    args += ["--modes=%s=%s" % (key, ctx.attr.modes[key]) for key in ctx.attr.modes]
  if ctx.attr.owners:
    args += ["--owners=%s=%s" % (key, ctx.attr.owners[key]) for key in ctx.attr.owners]
  if ctx.attr.ownernames:
    args += ["--owner_names=%s=%s" % (key, ctx.attr.ownernames[key])
             for key in ctx.attr.ownernames]
  if ctx.attr.empty_files:
    args += ["--empty_file=%s" % empty_file for empty_file in ctx.attr.empty_files]
  if ctx.attr.empty_dirs:
    args += ["--empty_dir=%s" % empty_dir for empty_dir in ctx.attr.empty_dirs]
  if ctx.attr.extension:
    dotPos = ctx.attr.extension.find('.')
    if dotPos > 0:
      dotPos += 1
      args += ["--compression=%s" % ctx.attr.extension[dotPos:]]
  args += ["--tar=" + f.path for f in ctx.files.deps]
  args += ["--link=%s:%s" % (k, ctx.attr.symlinks[k])
           for k in ctx.attr.symlinks]
  arg_file = ctx.actions.declare_file(ctx.label.name + ".args")
  ctx.actions.write(arg_file, "\n".join(args))

  ctx.actions.run_shell(
      command = "%s --flagfile=%s" % (build_tar.path, arg_file.path),
      inputs = file_inputs + ctx.files.deps + [arg_file, build_tar],
      outputs = [ctx.outputs.out],
      mnemonic="PackageTar",
      use_default_shell_env = True,
      )

def _pkg_deb_impl(ctx):
  """The implementation for the pkg_deb rule."""
  files = [ctx.file.data]
  args = [
      "--output=" + ctx.outputs.deb.path,
      "--changes=" + ctx.outputs.changes.path,
      "--data=" + ctx.file.data.path,
      "--package=" + ctx.attr.package,
      "--architecture=" + ctx.attr.architecture,
      "--maintainer=" + ctx.attr.maintainer,
      ]
  if ctx.attr.preinst:
    args += ["--preinst=@" + ctx.file.preinst.path]
    files += [ctx.file.preinst]
  if ctx.attr.postinst:
    args += ["--postinst=@" + ctx.file.postinst.path]
    files += [ctx.file.postinst]
  if ctx.attr.prerm:
    args += ["--prerm=@" + ctx.file.prerm.path]
    files += [ctx.file.prerm]
  if ctx.attr.postrm:
    args += ["--postrm=@" + ctx.file.postrm.path]
    files += [ctx.file.postrm]

  # Conffiles can be specified by a file or a string list
  if ctx.attr.conffiles_file:
    if ctx.attr.conffiles:
      fail("Both conffiles and conffiles_file attributes were specified")
    args += ["--conffile=@" + ctx.file.conffiles_file.path]
    files += [ctx.file.conffiles_file]
  elif ctx.attr.conffiles:
    args += ["--conffile=%s" % cf for cf in ctx.attr.conffiles]

  # Version and description can be specified by a file or inlined
  if ctx.attr.version_file:
    if ctx.attr.version:
      fail("Both version and version_file attributes were specified")
    args += ["--version=@" + ctx.file.version_file.path]
    files += [ctx.file.version_file]
  elif ctx.attr.version:
    args += ["--version=" + ctx.attr.version]
  else:
    fail("Neither version_file nor version attribute was specified")

  if ctx.attr.description_file:
    if ctx.attr.description:
      fail("Both description and description_file attributes were specified")
    args += ["--description=@" + ctx.file.description_file.path]
    files += [ctx.file.description_file]
  elif ctx.attr.description:
    args += ["--description=" + ctx.attr.description]
  else:
    fail("Neither description_file nor description attribute was specified")

  # Built using can also be specified by a file or inlined (but is not mandatory)
  if ctx.attr.built_using_file:
    if ctx.attr.built_using:
      fail("Both build_using and built_using_file attributes were specified")
    args += ["--built_using=@" + ctx.file.built_using_file.path]
    files += [ctx.file.built_using_file]
  elif ctx.attr.built_using:
    args += ["--built_using=" + ctx.attr.built_using]

  if ctx.attr.priority:
    args += ["--priority=" + ctx.attr.priority]
  if ctx.attr.section:
    args += ["--section=" + ctx.attr.section]
  if ctx.attr.homepage:
    args += ["--homepage=" + ctx.attr.homepage]

  args += ["--distribution=" + ctx.attr.distribution]
  args += ["--urgency=" + ctx.attr.urgency]
  args += ["--depends=" + d for d in ctx.attr.depends]
  args += ["--suggests=" + d for d in ctx.attr.suggests]
  args += ["--enhances=" + d for d in ctx.attr.enhances]
  args += ["--conflicts=" + d for d in ctx.attr.conflicts]
  args += ["--pre_depends=" + d for d in ctx.attr.predepends]
  args += ["--recommends=" + d for d in ctx.attr.recommends]

  ctx.action(
      executable = ctx.executable.make_deb,
      arguments = args,
      inputs = files,
      outputs = [ctx.outputs.deb, ctx.outputs.changes],
      mnemonic="MakeDeb"
      )
  ctx.action(
      command = "ln -s %s %s" % (ctx.outputs.deb.basename, ctx.outputs.out.path),
      inputs = [ctx.outputs.deb],
      outputs = [ctx.outputs.out])

# A rule for creating a tar file, see README.md
_real_pkg_tar = rule(
    implementation = _pkg_tar_impl,
    attrs = {
        "strip_prefix": attr.string(),
        "package_dir": attr.string(default="/"),
        "deps": attr.label_list(allow_files=tar_filetype),
        "srcs": attr.label_list(allow_files=True),
        "files": attr.label_keyed_string_dict(allow_files=True),
        "mode": attr.string(default="0555"),
        "modes": attr.string_dict(),
        "owner": attr.string(default="0.0"),
        "ownername": attr.string(default="."),
        "owners": attr.string_dict(),
        "ownernames": attr.string_dict(),
        "extension": attr.string(default="tar"),
        "symlinks": attr.string_dict(),
        "empty_files": attr.string_list(),
        "empty_dirs": attr.string_list(),
        # Implicit dependencies.
        "build_tar": attr.label(
            default=Label("//tools/build_defs/pkg:build_tar"),
            cfg="host",
            executable=True,
            allow_files=True)
    },
    outputs = {
        "out": "%{name}.%{extension}",
    },
    executable = False)

def pkg_tar(**kwargs):
  # Compatibility with older versions of pkg_tar that define files as
  # a flat list of labels.
  if "srcs" not in kwargs:
    if "files" in kwargs:
      if not hasattr(kwargs["files"], "items"):
        label = "%s//%s:%s" % (native.repository_name(), native.package_name(), kwargs["name"])
        print("%s: you provided a non dictionary to the pkg_tar `files` attribute. " % (label,) +
              "This attribute was renamed to `srcs`. " +
              "Consider renaming it in your BUILD file.")
        kwargs["srcs"] = kwargs.pop("files")
  _real_pkg_tar(**kwargs)

# A rule for creating a deb file, see README.md
pkg_deb = rule(
    implementation = _pkg_deb_impl,
    attrs = {
        "data": attr.label(mandatory=True, allow_files=tar_filetype, single_file=True),
        "package": attr.string(mandatory=True),
        "architecture": attr.string(default="all"),
        "distribution": attr.string(default="unstable"),
        "urgency": attr.string(default="medium"),
        "maintainer": attr.string(mandatory=True),
        "preinst": attr.label(allow_files=True, single_file=True),
        "postinst": attr.label(allow_files=True, single_file=True),
        "prerm": attr.label(allow_files=True, single_file=True),
        "postrm": attr.label(allow_files=True, single_file=True),
        "conffiles_file": attr.label(allow_files=True, single_file=True),
        "conffiles": attr.string_list(default=[]),
        "version_file": attr.label(allow_files=True, single_file=True),
        "version": attr.string(),
        "description_file": attr.label(allow_files=True, single_file=True),
        "description": attr.string(),
        "built_using_file": attr.label(allow_files=True, single_file=True),
        "built_using": attr.string(),
        "priority": attr.string(),
        "section": attr.string(),
        "homepage": attr.string(),
        "depends": attr.string_list(default=[]),
        "suggests": attr.string_list(default=[]),
        "enhances": attr.string_list(default=[]),
        "conflicts": attr.string_list(default=[]),
        "predepends": attr.string_list(default=[]),
        "recommends": attr.string_list(default=[]),
        # Implicit dependencies.
        "make_deb": attr.label(
            default=Label("//tools/build_defs/pkg:make_deb"),
            cfg="host",
            executable=True,
            allow_files=True)
    },
    outputs = {
        "out": "%{name}.deb",
        "deb": "%{package}_%{version}_%{architecture}.deb",
        "changes": "%{package}_%{version}_%{architecture}.changes"
    },
    executable = False)
<|endoftext|>


<commit_before>#!/usr/bin/python
import pygame
from level import Level
from gi.repository import Gtk

class MathIsland:
    def __init__(self):
        # Set up a clock for managing the frame rate.
        self.clock = pygame.time.Clock()

        self.tile_size = 64
        self.board_x = 0
        self.board_y = 0

        self.paused = False
        self.direction = 1

        self.load_map()

    def load_map(self):
        self.level = Level('levels/001.ilv')
        board_width = self.level.width
        board_height = self.level.height

        self.board = []
        for i in range(board_width):
            self.board.append([])
            for j in range(board_height):
                self.board.append(pygame.image.load(self.level[i,j].image))

    def set_paused(self, paused):
        self.paused = paused

    # Called to save the state of the game to the Journal.
    def write_file(self, file_path):
        pass

    # Called to load the state of the game from the Journal.
    def read_file(self, file_path):
        pass

    # The main game loop.
    def run(self):
        self.running = True

        screen = pygame.display.get_surface()

        while self.running:
            # Pump GTK messages.
            while Gtk.events_pending():
                Gtk.main_iteration()

            # Pump PyGame messages.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                elif event.type == pygame.VIDEORESIZE:
                    pygame.display.set_mode(event.size, pygame.RESIZABLE)
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        self.direction = -1
                    elif event.key == pygame.K_RIGHT:
                        self.direction = 1

            # Draw the game board
            for x in range(len(self.board)):
                for y in range(len(self.board[0])):
                    screen.blit(self.board[x][y],
                            (self.board_x + self.tile_size * x,
                             self.board_y * self.tile_size * y))

            #TODO: draw the fuel, other special objects

            #TODO: draw the player

            # Clear Display
            screen.fill((255, 255, 255))  # 255 for white

            # Draw the ball
            pygame.draw.circle(screen, (255, 0, 0), (screen.get_width() / 2, screen.get_height() / 2), 100)

            # Flip Display
            pygame.display.flip()

            # Try to stay at 30 FPS
            self.clock.tick(30)

# This function is called when the game is run directly from the command line:
# ./TestGame.py
def main():
    pygame.init()
    pygame.display.set_mode((0, 0), pygame.RESIZABLE)
    game = MathIsland()
    game.run()

if __name__ == '__main__':
    main()
<commit_msg>Checked level image for Nones<commit_after>#!/usr/bin/python
import pygame
import Level
from gi.repository import Gtk

class MathIsland:
    def __init__(self):
        # Set up a clock for managing the frame rate.
        self.clock = pygame.time.Clock()

        self.tile_size = 64
        self.board_x = 0
        self.board_y = 0

        self.paused = False
        self.direction = 1

        self.load_map()

    def load_map(self):
        self.level = Level('levels/001.ilv')
        board_width = self.level.width
        board_height = self.level.height

        self.board = []
        for i in range(board_width):
            self.board.append([])
            for j in range(board_height):
                if self.level[i,j].image == None:
                    self.board.append(pygame.image.load("grass.png"))
                else:
                    self.board.append(pygame.image.load(self.level[i,j].image))

    def set_paused(self, paused):
        self.paused = paused

    # Called to save the state of the game to the Journal.
    def write_file(self, file_path):
        pass

    # Called to load the state of the game from the Journal.
    def read_file(self, file_path):
        pass

    # The main game loop.
    def run(self):
        self.running = True

        screen = pygame.display.get_surface()

        while self.running:
            # Pump GTK messages.
            while Gtk.events_pending():
                Gtk.main_iteration()

            # Pump PyGame messages.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                elif event.type == pygame.VIDEORESIZE:
                    pygame.display.set_mode(event.size, pygame.RESIZABLE)
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        self.direction = -1
                    elif event.key == pygame.K_RIGHT:
                        self.direction = 1

            # Draw the game board
            for x in range(len(self.board)):
                for y in range(len(self.board[0])):
                    screen.blit(self.board[x][y],
                            (self.board_x + self.tile_size * x,
                             self.board_y * self.tile_size * y))

            #TODO: draw the fuel, other special objects

            #TODO: draw the player

            # Clear Display
            screen.fill((255, 255, 255))  # 255 for white

            # Draw the ball
            pygame.draw.circle(screen, (255, 0, 0), (screen.get_width() / 2, screen.get_height() / 2), 100)

            # Flip Display
            pygame.display.flip()

            # Try to stay at 30 FPS
            self.clock.tick(30)

# This function is called when the game is run directly from the command line:
# ./TestGame.py
def main():
    pygame.init()
    pygame.display.set_mode((0, 0), pygame.RESIZABLE)
    game = MathIsland()
    game.run()

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
JAR parameters representation.
"""
from django import forms

from cosmos.forms import ABSOLUTE_PATH_VALIDATOR, ID_VALIDATOR, HDFSFileChooser

class AbstractParameter(object):
    """Base class for custom JAR parameters"""

    def __init__(self, name, default_value=None):
        self.name = name
        self.default_value = default_value
        self.__value = None

    def set_value(self, value):
        self.__value = value

    def get_value(self):
        if self.has_value():
            return self.__value
        else:
            return self.default_value

    def has_value(self):
        return self.__value is not None

    def form_field(self):
        raise NotImplemented()

    def as_job_argument(self, job):
        return ["-D", "%s=%s" % (self.name, self.get_value())]

class StringParameter(AbstractParameter):
    MAX_LENGTH = 255

    def form_field(self):
        return forms.CharField(label=self.name,
                               max_length=self.MAX_LENGTH,
                               initial=self.default_value)

class FilePathParameter(AbstractParameter):

    def form_field(self):
        return forms.CharField(label=self.name,
                               max_length=StringParameter.MAX_LENGTH,
                               initial=self.default_value,
                               widget=HDFSFileChooser(),
                               validators=[ABSOLUTE_PATH_VALIDATOR])

class MongoCollParameter(AbstractParameter):

    def form_field(self):
        return forms.CharField(label=self.name,
                               max_length=StringParameter.MAX_LENGTH,
                               initial=self.default_value,
                               validators=[ID_VALIDATOR])

    def as_job_argument(self, job):
        mongo_url = job.mongo_url(collection=self.get_value())
        return ["-D", "%s=%s" % (self.name, mongo_url)]

PARAMETER_MAP = {'string': StringParameter,
                 'filepath': FilePathParameter,
                 'mongocoll': MongoCollParameter}

def make_parameter(name, value):
    if name.find('=') >= 0:
        raise ValueError("invalid parameter name '%s'" % name)
    tokens = value.split('|', 1)
    type_name = tokens[0]
    if not PARAMETER_MAP.has_key(type_name):
        raise ValueError('unknown type "%s"' % type_name)
    if len(tokens) > 1:
        return PARAMETER_MAP[type_name](name, default_value=tokens[1])
    else:
        return PARAMETER_MAP[type_name](name)
<commit_msg>Add docstrings<commit_after># -*- coding: utf-8 -*-
"""
JAR parameters representation.
"""
from django import forms

from cosmos.forms import ABSOLUTE_PATH_VALIDATOR, ID_VALIDATOR, HDFSFileChooser

class AbstractParameter(object):
    """Base class for custom JAR parameters.

    Do not use it directly, only through subclassing.
    """

    def __init__(self, name, default_value=None):
        self.name = name
        self.default_value = default_value
        self.__value = None

    def set_value(self, value):
        self.__value = value

    def get_value(self):
        if self.has_value():
            return self.__value
        else:
            return self.default_value

    def has_value(self):
        return self.__value is not None

    def form_field(self):
        """Generate a field suitable for a django form.

        To be override by subclasses.
        """
        raise NotImplemented("AbstractParameter#form_field must be override")

    def as_job_argument(self, job):
        """Returns a list of command line arguments to inject this
        parameter into a job.

        Can be overriden by subclasses.
        """
        return ["-D", "%s=%s" % (self.name, self.get_value())]

class StringParameter(AbstractParameter):
    MAX_LENGTH = 255

    def form_field(self):
        return forms.CharField(label=self.name,
                               max_length=self.MAX_LENGTH,
                               initial=self.default_value)

class FilePathParameter(AbstractParameter):

    def form_field(self):
        return forms.CharField(label=self.name,
                               max_length=StringParameter.MAX_LENGTH,
                               initial=self.default_value,
                               widget=HDFSFileChooser(),
                               validators=[ABSOLUTE_PATH_VALIDATOR])

class MongoCollParameter(AbstractParameter):

    def form_field(self):
        return forms.CharField(label=self.name,
                               max_length=StringParameter.MAX_LENGTH,
                               initial=self.default_value,
                               validators=[ID_VALIDATOR])

    def as_job_argument(self, job):
        mongo_url = job.mongo_url(collection=self.get_value())
        return ["-D", "%s=%s" % (self.name, mongo_url)]

PARAMETER_MAP = {'string': StringParameter,
                 'filepath': FilePathParameter,
                 'mongocoll': MongoCollParameter}

def make_parameter(name, value):
    if name.find('=') >= 0:
        raise ValueError("invalid parameter name '%s'" % name)
    tokens = value.split('|', 1)
    type_name = tokens[0]
    if not PARAMETER_MAP.has_key(type_name):
        raise ValueError('unknown type "%s"' % type_name)
    if len(tokens) > 1:
        return PARAMETER_MAP[type_name](name, default_value=tokens[1])
    else:
        return PARAMETER_MAP[type_name](name)
<|endoftext|>


<commit_before># Copyright 2014 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""These are bare-bones Go rules.

In order of priority:

- No support for build tags

- BUILD file must be written by hand.

- No C++ interop (SWIG, cgo).

- No test sharding or test XML.

"""

_DEFAULT_LIB = "go_default_library"

_VENDOR_PREFIX = "/vendor/"

go_filetype = FileType([".go"])

################

# In Go, imports are always fully qualified with a URL,
# eg. github.com/user/project. Hence, a label //foo:bar from within a
# Bazel workspace must be referred to as
# "github.com/user/project/foo/bar". To make this work, each rule must
# know the repository's URL. This is achieved, by having all go rules
# depend on a globally unique target that has a "go_prefix" transitive
# info provider.

def _go_prefix_impl(ctx):
  """go_prefix_impl provides the go prefix to use as a transitive info provider."""
  return struct(go_prefix = ctx.attr.prefix)

def _go_prefix(ctx):
  """slash terminated go-prefix"""
  prefix = ctx.attr.go_prefix.go_prefix
  if not prefix.endswith("/"):
    prefix = prefix + "/"
  return prefix

_go_prefix_rule = rule(
    _go_prefix_impl,
    attrs = {
        "prefix": attr.string(mandatory = True),
    },
)

def go_prefix(prefix):
  """go_prefix sets the Go import name to be used for this workspace."""
  _go_prefix_rule(name = "go_prefix",
    prefix = prefix,
    visibility = ["//visibility:public" ]
  )

################

# TODO(bazel-team): it would be nice if Bazel had this built-in.
def symlink_tree_commands(dest_dir, artifact_dict):
  """Symlink_tree_commands returns a list of commands to create the
  dest_dir, and populate it according to the given dict.

  Args:
    dest_dir: The destination directory, a string.
    artifact_dict: The mapping of exec-path => path in the dest_dir.

  Returns:
    A list of commands that will setup the symlink tree.
  """
  cmds = [
    "rm -rf " + dest_dir,
    "mkdir -p " + dest_dir,
  ]

  for old_path, new_path in artifact_dict.items():
    new_dir = new_path[:new_path.rfind('/')]
    up = (new_dir.count('/') + 1 +
          dest_dir.count('/') + 1)
    cmds += [
      "mkdir -p %s/%s" % (dest_dir, new_dir),
      "ln -s %s%s %s/%s" % ('../' * up, old_path, dest_dir, new_path),
    ]
  return cmds

def go_environment_vars(ctx):
  """Return a map of environment variables for use with actions, based on
  the arguments. Uses the ctx.fragments.cpp.cpu attribute, if present,
  and picks a default of target_os="linux" and target_arch="amd64"
  otherwise.

  Args:
    The skylark Context.

  Returns:
    A dict of environment variables for running Go tool commands that build for
    the target OS and architecture.
  """
  bazel_to_go_toolchain = {"k8": {"GOOS": "linux",
                                  "GOARCH": "amd64"},
                           "piii": {"GOOS": "linux",
                                    "GOARCH": "386"},
                           "darwin": {"GOOS": "darwin",
                                      "GOARCH": "amd64"},
                           "freebsd": {"GOOS": "freebsd",
                                       "GOARCH": "amd64"},
                           "armeabi-v7a": {"GOOS": "linux",
                                           "GOARCH": "arm"},
                           "arm": {"GOOS": "linux",
                                   "GOARCH": "arm"}}
  return bazel_to_go_toolchain.get(ctx.fragments.cpp.cpu,
                                   {"GOOS": "linux",
                                    "GOARCH": "amd64"})

def emit_go_compile_action(ctx, sources, deps, out_lib):
  """Construct the command line for compiling Go code.
  Constructs a symlink tree to accomodate for workspace name.

  Args:
    ctx: The skylark Context.
    sources: an iterable of source code artifacts (or CTs? or labels?)
    deps: an iterable of dependencies. Each dependency d should have an
      artifact in d.go_library_object representing an imported library.
    out_lib: the artifact (configured target?) that should be produced
  """
  config_strip = len(ctx.configuration.bin_dir.path) + 1

  out_dir = out_lib.path + ".dir"
  out_depth = out_dir.count('/') + 1
  tree_layout = {}
  inputs = []
  prefix = _go_prefix(ctx)
  import_map = {}
  for d in deps:
    library_artifact_path = d.go_library_object.path[config_strip:]
    tree_layout[d.go_library_object.path] = prefix + library_artifact_path
    inputs += [d.go_library_object]

    source_import = prefix + d.label.package + "/" + d.label.name
    actual_import = prefix + d.label.package + "/" + d.label.name
    if d.label.name == _DEFAULT_LIB:
      source_import = prefix + d.label.package

    if source_import.rfind(_VENDOR_PREFIX) != -1:
      source_import = source_import[len(_VENDOR_PREFIX) + source_import.rfind(_VENDOR_PREFIX):]

    if source_import != actual_import:
      if source_import in import_map:
        fail("duplicate import %s: adding %s and have %s"
             % (source_import, actual_import, import_map[source_import]))
      import_map[source_import] = actual_import

  inputs += list(sources)
  for s in sources:
    tree_layout[s.path] = prefix + s.path

  cmds = symlink_tree_commands(out_dir, tree_layout)
  args = [
      "cd ", out_dir, "&&",
      ('../' * out_depth) + ctx.file.go_tool.path,
      "tool", "compile",
      "-o", ('../' * out_depth) + out_lib.path, "-pack",

      # Import path.
      "-I", "."] + [
        "-importmap=%s=%s" % (k,v) for k, v in import_map.items()
      ]

  # Set -p to the import path of the library, ie.
  # (ctx.label.package + "/" ctx.label.name) for now.
  cmds += [ "export GOROOT=$(pwd)/" + ctx.file.go_tool.dirname + "/..",
    ' '.join(args + cmd_helper.template(sources, prefix + "%{path}"))]

  ctx.action(
      inputs = inputs + ctx.files.toolchain,
      outputs = [out_lib],
      mnemonic = "GoCompile",
      command =  " && ".join(cmds),
      env = go_environment_vars(ctx))

def go_library_impl(ctx):
  """Implements the go_library() rule."""

  sources = set(ctx.files.srcs)
  deps = ctx.attr.deps
  if ctx.attr.library:
    sources += ctx.attr.library.go_sources
    deps += ctx.attr.library.direct_deps

  if not sources:
    fail("may not be empty", "srcs")

  out_lib = ctx.outputs.lib
  emit_go_compile_action(ctx, set(sources), deps, out_lib)

  transitive_libs = set([out_lib])
  for dep in ctx.attr.deps:
     transitive_libs += dep.transitive_go_library_object

  runfiles = ctx.runfiles(collect_data = True)
  return struct(
    label = ctx.label,
    files = set([out_lib]),
    direct_deps = deps,
    runfiles = runfiles,
    go_sources = sources,
    go_library_object = out_lib,
    transitive_go_library_object = transitive_libs)

def emit_go_link_action(ctx, transitive_libs, lib, executable):
  """Sets up a symlink tree to libraries to link together."""
  out_dir = executable.path + ".dir"
  out_depth = out_dir.count('/') + 1
  tree_layout = {}

  config_strip = len(ctx.configuration.bin_dir.path) + 1
  prefix = _go_prefix(ctx)

  for l in transitive_libs:
    library_artifact_path = l.path[config_strip:]
    tree_layout[l.path] = prefix + library_artifact_path

  tree_layout[lib.path] = prefix + lib.path[config_strip:]
  tree_layout[executable.path] = prefix + executable.path[config_strip:]

  cmds = symlink_tree_commands(out_dir, tree_layout)
  cmds += [
    "export GOROOT=$(pwd)/" + ctx.file.go_tool.dirname + "/..",
    "cd " + out_dir,
    ' '.join([
      ('../' * out_depth) + ctx.file.go_tool.path,
      "tool", "link", "-L", ".",
      "-o", prefix + executable.path[config_strip:],
      prefix + lib.path[config_strip:]])]

  ctx.action(
      inputs = list(transitive_libs) + [lib] + ctx.files.toolchain,
      outputs = [executable],
      command = ' && '.join(cmds),
      mnemonic = "GoLink",
      env = go_environment_vars(ctx))

def go_binary_impl(ctx):
  """go_binary_impl emits actions for compiling and linking a go executable."""
  lib_result = go_library_impl(ctx)
  executable = ctx.outputs.executable
  lib_out = ctx.outputs.lib

  emit_go_link_action(
    ctx, lib_result.transitive_go_library_object, lib_out, executable)

  runfiles = ctx.runfiles(collect_data = True,
                          files = ctx.files.data)
  return struct(files = set([executable]) + lib_result.files,
                runfiles = runfiles)

def go_test_impl(ctx):
  """go_test_impl implements go testing.

  It emits an action to run the test generator, and then compiles the
  test into a binary."""

  lib_result = go_library_impl(ctx)
  main_go = ctx.outputs.main_go
  prefix = _go_prefix(ctx)

  go_import = prefix + ctx.label.package + "/" + ctx.label.name

  args = (["--package", go_import, "--output", ctx.outputs.main_go.path] +
          cmd_helper.template(lib_result.go_sources, "%{path}"))

  inputs = list(lib_result.go_sources) + list(ctx.files.toolchain)
  ctx.action(
      inputs = inputs,
      executable = ctx.executable.test_generator,
      outputs = [main_go],
      mnemonic = "GoTestGenTest",
      arguments = args,
      env = dict(go_environment_vars(ctx), RUNDIR=ctx.label.package))

  emit_go_compile_action(
    ctx, set([main_go]), ctx.attr.deps + [lib_result], ctx.outputs.main_lib)

  emit_go_link_action(
    ctx, lib_result.transitive_go_library_object,
    ctx.outputs.main_lib, ctx.outputs.executable)

  # TODO(bazel-team): the Go tests should do a chdir to the directory
  # holding the data files, so open-source go tests continue to work
  # without code changes.
  runfiles = ctx.runfiles(collect_data = True,
                          files = ctx.files.data + [ctx.outputs.executable])
  return struct(runfiles=runfiles)

go_library_attrs = {
    "data": attr.label_list(
        allow_files = True,
        cfg = DATA_CFG,
    ),
    "srcs": attr.label_list(allow_files = go_filetype),
    "deps": attr.label_list(
        providers = [
            "direct_deps",
            "go_library_object",
            "transitive_go_library_object",
        ],
    ),
    "toolchain": attr.label(
        default = Label("//tools/build_rules/go/toolchain:toolchain"),
        allow_files = True,
        cfg = HOST_CFG,
    ),
    "go_tool": attr.label(
        default = Label("//tools/build_rules/go/toolchain:go_tool"),
        single_file = True,
        allow_files = True,
        cfg = HOST_CFG,
    ),
    "library": attr.label(
        providers = ["go_sources"],
    ),
    "go_prefix": attr.label(
        providers = ["go_prefix"],
        default = Label("//external:go_prefix"),
        allow_files = False,
        cfg = HOST_CFG,
    ),
}

go_library_outputs = {
    "lib": "%{name}.a",
}

go_library = rule(
    go_library_impl,
    attrs = go_library_attrs,
    fragments = ["cpp"],
    outputs = go_library_outputs,
)

go_binary = rule(
    go_binary_impl,
    attrs = go_library_attrs + {
        "stamp": attr.bool(default = False),
    },
    executable = True,
    fragments = ["cpp"],
    outputs = go_library_outputs,
)

go_test = rule(
    go_test_impl,
    attrs = go_library_attrs + {
        "test_generator": attr.label(
            executable = True,
            default = Label("//tools/build_rules/go/tools:generate_test_main"),
            cfg = HOST_CFG,
        ),
    },
    executable = True,
    fragments = ["cpp"],
    outputs = {
        "lib": "%{name}.a",
        "main_lib": "%{name}_main_test.a",
        "main_go": "%{name}_main_test.go",
    },
    test = True,
)

# TODO(bazel-team): support darwin
def go_repositories():
  native.bind(name  = "go_prefix",
    actual = "//:go_prefix",
  )

  native.new_http_archive(
    name=  "golang-linux-amd64",
    url = "https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz",
    build_file = "tools/build_rules/go/toolchain/BUILD.go-toolchain",
    sha256 = "2593132ca490b9ee17509d65ee2cd078441ff544899f6afb97a03d08c25524e7"
  )

  native.new_http_archive(
    name=  "golang-darwin-amd64",
    url = "https://storage.googleapis.com/golang/go1.5.1.darwin-amd64.tar.gz",
    build_file = "tools/build_rules/go/toolchain/BUILD.go-toolchain",
    sha256 = "e94487b8cd2e0239f27dc51e6c6464383b10acb491f753584605e9b28abf48fb"
  )
<commit_msg>Make the Go rules depend on the Go toolchain targets in the main repository.<commit_after># Copyright 2014 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""These are bare-bones Go rules.

In order of priority:

- No support for build tags

- BUILD file must be written by hand.

- No C++ interop (SWIG, cgo).

- No test sharding or test XML.

"""

_DEFAULT_LIB = "go_default_library"

_VENDOR_PREFIX = "/vendor/"

go_filetype = FileType([".go"])

################

# In Go, imports are always fully qualified with a URL,
# eg. github.com/user/project. Hence, a label //foo:bar from within a
# Bazel workspace must be referred to as
# "github.com/user/project/foo/bar". To make this work, each rule must
# know the repository's URL. This is achieved, by having all go rules
# depend on a globally unique target that has a "go_prefix" transitive
# info provider.

def _go_prefix_impl(ctx):
  """go_prefix_impl provides the go prefix to use as a transitive info provider."""
  return struct(go_prefix = ctx.attr.prefix)

def _go_prefix(ctx):
  """slash terminated go-prefix"""
  prefix = ctx.attr.go_prefix.go_prefix
  if not prefix.endswith("/"):
    prefix = prefix + "/"
  return prefix

_go_prefix_rule = rule(
    _go_prefix_impl,
    attrs = {
        "prefix": attr.string(mandatory = True),
    },
)

def go_prefix(prefix):
  """go_prefix sets the Go import name to be used for this workspace."""
  _go_prefix_rule(name = "go_prefix",
    prefix = prefix,
    visibility = ["//visibility:public" ]
  )

################

# TODO(bazel-team): it would be nice if Bazel had this built-in.
def symlink_tree_commands(dest_dir, artifact_dict):
  """Symlink_tree_commands returns a list of commands to create the
  dest_dir, and populate it according to the given dict.

  Args:
    dest_dir: The destination directory, a string.
    artifact_dict: The mapping of exec-path => path in the dest_dir.

  Returns:
    A list of commands that will setup the symlink tree.
  """
  cmds = [
    "rm -rf " + dest_dir,
    "mkdir -p " + dest_dir,
  ]

  for old_path, new_path in artifact_dict.items():
    new_dir = new_path[:new_path.rfind('/')]
    up = (new_dir.count('/') + 1 +
          dest_dir.count('/') + 1)
    cmds += [
      "mkdir -p %s/%s" % (dest_dir, new_dir),
      "ln -s %s%s %s/%s" % ('../' * up, old_path, dest_dir, new_path),
    ]
  return cmds

def go_environment_vars(ctx):
  """Return a map of environment variables for use with actions, based on
  the arguments. Uses the ctx.fragments.cpp.cpu attribute, if present,
  and picks a default of target_os="linux" and target_arch="amd64"
  otherwise.

  Args:
    The skylark Context.

  Returns:
    A dict of environment variables for running Go tool commands that build for
    the target OS and architecture.
  """
  bazel_to_go_toolchain = {"k8": {"GOOS": "linux",
                                  "GOARCH": "amd64"},
                           "piii": {"GOOS": "linux",
                                    "GOARCH": "386"},
                           "darwin": {"GOOS": "darwin",
                                      "GOARCH": "amd64"},
                           "freebsd": {"GOOS": "freebsd",
                                       "GOARCH": "amd64"},
                           "armeabi-v7a": {"GOOS": "linux",
                                           "GOARCH": "arm"},
                           "arm": {"GOOS": "linux",
                                   "GOARCH": "arm"}}
  return bazel_to_go_toolchain.get(ctx.fragments.cpp.cpu,
                                   {"GOOS": "linux",
                                    "GOARCH": "amd64"})

def emit_go_compile_action(ctx, sources, deps, out_lib):
  """Construct the command line for compiling Go code.
  Constructs a symlink tree to accomodate for workspace name.

  Args:
    ctx: The skylark Context.
    sources: an iterable of source code artifacts (or CTs? or labels?)
    deps: an iterable of dependencies. Each dependency d should have an
      artifact in d.go_library_object representing an imported library.
    out_lib: the artifact (configured target?) that should be produced
  """
  config_strip = len(ctx.configuration.bin_dir.path) + 1

  out_dir = out_lib.path + ".dir"
  out_depth = out_dir.count('/') + 1
  tree_layout = {}
  inputs = []
  prefix = _go_prefix(ctx)
  import_map = {}
  for d in deps:
    library_artifact_path = d.go_library_object.path[config_strip:]
    tree_layout[d.go_library_object.path] = prefix + library_artifact_path
    inputs += [d.go_library_object]

    source_import = prefix + d.label.package + "/" + d.label.name
    actual_import = prefix + d.label.package + "/" + d.label.name
    if d.label.name == _DEFAULT_LIB:
      source_import = prefix + d.label.package

    if source_import.rfind(_VENDOR_PREFIX) != -1:
      source_import = source_import[len(_VENDOR_PREFIX) + source_import.rfind(_VENDOR_PREFIX):]

    if source_import != actual_import:
      if source_import in import_map:
        fail("duplicate import %s: adding %s and have %s"
             % (source_import, actual_import, import_map[source_import]))
      import_map[source_import] = actual_import

  inputs += list(sources)
  for s in sources:
    tree_layout[s.path] = prefix + s.path

  cmds = symlink_tree_commands(out_dir, tree_layout)
  args = [
      "cd ", out_dir, "&&",
      ('../' * out_depth) + ctx.file.go_tool.path,
      "tool", "compile",
      "-o", ('../' * out_depth) + out_lib.path, "-pack",

      # Import path.
      "-I", "."] + [
        "-importmap=%s=%s" % (k,v) for k, v in import_map.items()
      ]

  # Set -p to the import path of the library, ie.
  # (ctx.label.package + "/" ctx.label.name) for now.
  cmds += [ "export GOROOT=$(pwd)/" + ctx.file.go_tool.dirname + "/..",
    ' '.join(args + cmd_helper.template(sources, prefix + "%{path}"))]

  ctx.action(
      inputs = inputs + ctx.files.toolchain,
      outputs = [out_lib],
      mnemonic = "GoCompile",
      command =  " && ".join(cmds),
      env = go_environment_vars(ctx))

def go_library_impl(ctx):
  """Implements the go_library() rule."""

  sources = set(ctx.files.srcs)
  deps = ctx.attr.deps
  if ctx.attr.library:
    sources += ctx.attr.library.go_sources
    deps += ctx.attr.library.direct_deps

  if not sources:
    fail("may not be empty", "srcs")

  out_lib = ctx.outputs.lib
  emit_go_compile_action(ctx, set(sources), deps, out_lib)

  transitive_libs = set([out_lib])
  for dep in ctx.attr.deps:
     transitive_libs += dep.transitive_go_library_object

  runfiles = ctx.runfiles(collect_data = True)
  return struct(
    label = ctx.label,
    files = set([out_lib]),
    direct_deps = deps,
    runfiles = runfiles,
    go_sources = sources,
    go_library_object = out_lib,
    transitive_go_library_object = transitive_libs)

def emit_go_link_action(ctx, transitive_libs, lib, executable):
  """Sets up a symlink tree to libraries to link together."""
  out_dir = executable.path + ".dir"
  out_depth = out_dir.count('/') + 1
  tree_layout = {}

  config_strip = len(ctx.configuration.bin_dir.path) + 1
  prefix = _go_prefix(ctx)

  for l in transitive_libs:
    library_artifact_path = l.path[config_strip:]
    tree_layout[l.path] = prefix + library_artifact_path

  tree_layout[lib.path] = prefix + lib.path[config_strip:]
  tree_layout[executable.path] = prefix + executable.path[config_strip:]

  cmds = symlink_tree_commands(out_dir, tree_layout)
  cmds += [
    "export GOROOT=$(pwd)/" + ctx.file.go_tool.dirname + "/..",
    "cd " + out_dir,
    ' '.join([
      ('../' * out_depth) + ctx.file.go_tool.path,
      "tool", "link", "-L", ".",
      "-o", prefix + executable.path[config_strip:],
      prefix + lib.path[config_strip:]])]

  ctx.action(
      inputs = list(transitive_libs) + [lib] + ctx.files.toolchain,
      outputs = [executable],
      command = ' && '.join(cmds),
      mnemonic = "GoLink",
      env = go_environment_vars(ctx))

def go_binary_impl(ctx):
  """go_binary_impl emits actions for compiling and linking a go executable."""
  lib_result = go_library_impl(ctx)
  executable = ctx.outputs.executable
  lib_out = ctx.outputs.lib

  emit_go_link_action(
    ctx, lib_result.transitive_go_library_object, lib_out, executable)

  runfiles = ctx.runfiles(collect_data = True,
                          files = ctx.files.data)
  return struct(files = set([executable]) + lib_result.files,
                runfiles = runfiles)

def go_test_impl(ctx):
  """go_test_impl implements go testing.

  It emits an action to run the test generator, and then compiles the
  test into a binary."""

  lib_result = go_library_impl(ctx)
  main_go = ctx.outputs.main_go
  prefix = _go_prefix(ctx)

  go_import = prefix + ctx.label.package + "/" + ctx.label.name

  args = (["--package", go_import, "--output", ctx.outputs.main_go.path] +
          cmd_helper.template(lib_result.go_sources, "%{path}"))

  inputs = list(lib_result.go_sources) + list(ctx.files.toolchain)
  ctx.action(
      inputs = inputs,
      executable = ctx.executable.test_generator,
      outputs = [main_go],
      mnemonic = "GoTestGenTest",
      arguments = args,
      env = dict(go_environment_vars(ctx), RUNDIR=ctx.label.package))

  emit_go_compile_action(
    ctx, set([main_go]), ctx.attr.deps + [lib_result], ctx.outputs.main_lib)

  emit_go_link_action(
    ctx, lib_result.transitive_go_library_object,
    ctx.outputs.main_lib, ctx.outputs.executable)

  # TODO(bazel-team): the Go tests should do a chdir to the directory
  # holding the data files, so open-source go tests continue to work
  # without code changes.
  runfiles = ctx.runfiles(collect_data = True,
                          files = ctx.files.data + [ctx.outputs.executable])
  return struct(runfiles=runfiles)

go_library_attrs = {
    "data": attr.label_list(
        allow_files = True,
        cfg = DATA_CFG,
    ),
    "srcs": attr.label_list(allow_files = go_filetype),
    "deps": attr.label_list(
        providers = [
            "direct_deps",
            "go_library_object",
            "transitive_go_library_object",
        ],
    ),
    "toolchain": attr.label(
        default = Label("@//tools/build_rules/go/toolchain:toolchain"),
        allow_files = True,
        cfg = HOST_CFG,
    ),
    "go_tool": attr.label(
        default = Label("@//tools/build_rules/go/toolchain:go_tool"),
        single_file = True,
        allow_files = True,
        cfg = HOST_CFG,
    ),
    "library": attr.label(
        providers = ["go_sources"],
    ),
    "go_prefix": attr.label(
        providers = ["go_prefix"],
        default = Label("//external:go_prefix"),
        allow_files = False,
        cfg = HOST_CFG,
    ),
}

go_library_outputs = {
    "lib": "%{name}.a",
}

go_library = rule(
    go_library_impl,
    attrs = go_library_attrs,
    fragments = ["cpp"],
    outputs = go_library_outputs,
)

go_binary = rule(
    go_binary_impl,
    attrs = go_library_attrs + {
        "stamp": attr.bool(default = False),
    },
    executable = True,
    fragments = ["cpp"],
    outputs = go_library_outputs,
)

go_test = rule(
    go_test_impl,
    attrs = go_library_attrs + {
        "test_generator": attr.label(
            executable = True,
            default = Label("//tools/build_rules/go/tools:generate_test_main"),
            cfg = HOST_CFG,
        ),
    },
    executable = True,
    fragments = ["cpp"],
    outputs = {
        "lib": "%{name}.a",
        "main_lib": "%{name}_main_test.a",
        "main_go": "%{name}_main_test.go",
    },
    test = True,
)

# TODO(bazel-team): support darwin
def go_repositories():
  native.bind(name  = "go_prefix",
    actual = "//:go_prefix",
  )

  native.new_http_archive(
    name=  "golang-linux-amd64",
    url = "https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz",
    build_file = "tools/build_rules/go/toolchain/BUILD.go-toolchain",
    sha256 = "2593132ca490b9ee17509d65ee2cd078441ff544899f6afb97a03d08c25524e7"
  )

  native.new_http_archive(
    name=  "golang-darwin-amd64",
    url = "https://storage.googleapis.com/golang/go1.5.1.darwin-amd64.tar.gz",
    build_file = "tools/build_rules/go/toolchain/BUILD.go-toolchain",
    sha256 = "e94487b8cd2e0239f27dc51e6c6464383b10acb491f753584605e9b28abf48fb"
  )
<|endoftext|>


<commit_before>from csacompendium.research.models import (
    Author,
    ResearchAuthor,
)
from csacompendium.utils.hyperlinkedidentity import hyperlinked_identity
from csacompendium.utils.serializersutils import (
    CreateSerializerUtil,
    get_related_content_url,
    FieldMethodSerializer
)
from rest_framework.serializers import (
    ModelSerializer,
    SerializerMethodField,
    ValidationError,
)

def research_author_serializers():
    """
    Research author serializers
    :return: All research author serializers
    :rtype: Object
    """

    def create_research_author_serializer(model_type=None, pk=None, user=None):
        """
        Creates a model serializer
        :param model_type: Model
        :param pk: Primary key
        :param user: Object owner
        :return: Serializer class
        :rtype: Object
        """

        class ResearchAuthorCreateSerializer(ModelSerializer, CreateSerializerUtil):
            """
            Create a record
            """
            class Meta:
                model = ResearchAuthor
                fields = [
                    'id',
                    'author',
                    'last_update',
                    'time_created',
                ]

            def __init__(self, *args, **kwargs):
                super(ResearchAuthorCreateSerializer, self).__init__(*args, **kwargs)
                self.model_type = model_type
                self.key = pk
                self.user = user
                self.slugify = False
                self.auth_user = self.get_authenticated_user(self.user)

            def create(self, validated_data):
                """
                Created record from validated data
                :param validated_data: Validated data
                :return: Research author object
                :rtype: Object
                """
                author = validated_data.get('author')
                author_relation = ResearchAuthor.objects.create_by_model_type(
                    self.model_type,
                    self.key,
                    author=author,
                    user=self.auth_user,
                    modified_by=self.auth_user
                )
                if author_relation:
                    return author_relation
                else:
                    raise ValidationError({"non_field_errors": ["This is not a valid content type"]})

        return ResearchAuthorCreateSerializer

    class ResearchAuthorListSerializer(ModelSerializer, FieldMethodSerializer):
        """
        Serialize all records in given fields into an API
        """
        author_url = SerializerMethodField()
        content_type_url = SerializerMethodField()
        research_author_url = hyperlinked_identity('research_api:research_author_detail', 'pk')

        class Meta:
            model = ResearchAuthor
            fields = [
                'id',
                'author',
                'author_url',
                'content_type_url',
                'research_author_url',
            ]

        def get_author_url(self, obj):
            """
            Get related content type/object url
            :param obj: Current record object
            :return: URL to related object
            :rtype: String
            """
            return get_related_content_url(Author, obj.author.id)

    class ResearchAuthorSerializer(ModelSerializer):
        """
        Serialize all records in given fields into an API
        """
        author_url = SerializerMethodField()
        relation_id = SerializerMethodField()
        research_author_url = hyperlinked_identity('research_api:research_author_detail', 'pk')

        class Meta:
            model = ResearchAuthor
            fields = [
                'relation_id',
                'author_id',
                'author_url',
                'research_author_url',
            ]

        def get_author_url(self, obj):
            """
            Get related content type/object url
            :param obj: Current record object
            :return: URL to related object
            :rtype: String
            """
            return get_related_content_url(Author, obj.author.id)

        def get_relation_id (self, obj):
            """
            :param obj: Current record object
            :return: Research author id
            :rtype: Integer
            """
            return obj.id

    class ResearchAuthorContentTypeSerializer(ModelSerializer, FieldMethodSerializer):
        """
        Serialize all records in given fields into an API
        """
        relation_id = SerializerMethodField()
        content_type_url = SerializerMethodField()
        research_author_url = hyperlinked_identity('research_api:research_author_detail', 'pk')

        class Meta:
            model = ResearchAuthor
            fields = [
                'relation_id',
                'object_id',
                'content_type_url',
                'research_author_url',
            ]

        def get_relation_id (self, obj):
            """
            :param obj: Current record object
            :return: Research author id
            :rtype: String
            """
            return obj.id

    class ResearchAuthorDetailSerializer(ModelSerializer, FieldMethodSerializer):
        """
        Serialize single record into an API. This is dependent on fields given.
        """
        author_url = SerializerMethodField()
        user = SerializerMethodField()
        modified_by = SerializerMethodField()
        content_type_url = SerializerMethodField()

        class Meta:
            model = ResearchAuthor
            fields = [
                'id',
                'author',
                'author_url',
                'user',
                'modified_by',
                'last_update',
                'time_created',
                'content_type_url',
            ]
            read_only_fields = [
                'id',
                'user',
                'modified_by',
                'last_update',
                'time_created',
                'author_url',
                'content_type_url',
            ]

        def get_author_url(self, obj):
            """
            Get related content type/object url
            :param obj: Current record object
            :return: URL to related object
            :rtype: String
            """
            return get_related_content_url(Author, obj.author.id)

    return {
        'create_research_author_serializer': create_research_author_serializer,
        'ResearchAuthorListSerializer': ResearchAuthorListSerializer,
        'ResearchAuthorSerializer': ResearchAuthorSerializer,
        'ResearchAuthorContentTypeSerializer': ResearchAuthorContentTypeSerializer,
        'ResearchAuthorDetailSerializer': ResearchAuthorDetailSerializer
    }
<commit_msg>Added base serializer to "Research author" serializer<commit_after>from csacompendium.research.models import (
    Author,
    ResearchAuthor,
)
from csacompendium.utils.hyperlinkedidentity import hyperlinked_identity
from csacompendium.utils.serializersutils import (
    CreateSerializerUtil,
    get_related_content_url,
    FieldMethodSerializer
)
from rest_framework.serializers import (
    ModelSerializer,
    SerializerMethodField,
    ValidationError,
)

def research_author_serializers():
    """
    Research author serializers
    :return: All research author serializers
    :rtype: Object
    """

    class ResearchAuthorBaseSerializer(ModelSerializer):
        """
        Base serializer for DRY implementation.
        """
        class Meta:
            model = ResearchAuthor
            fields = [
                'id',
                'author',
            ]

    class ResearchAuthorRelationBaseSerializer(ModelSerializer):
        """
        Base serializer for DRY implementation.
        """
        author_url = SerializerMethodField()
        content_type_url = SerializerMethodField()

        class Meta:
            model = ResearchAuthor
            fields = [
                'content_type_url',
                'author_url',
            ]

    class ResearchAuthorFieldMethodSerializer:
        """
        Serialize an object based on a provided field
        """
        def get_author_url(self, obj):
            """
            Get related content type/object url
            :param obj: Current record object
            :return: URL to related object
            :rtype: String
            """
            return get_related_content_url(Author, obj.author.id)

    def create_research_author_serializer(model_type=None, pk=None, user=None):
        """
        Creates a model serializer
        :param model_type: Model
        :param pk: Primary key
        :param user: Object owner
        :return: Serializer class
        :rtype: Object
        """

        class ResearchAuthorCreateSerializer(ResearchAuthorBaseSerializer, CreateSerializerUtil):
            """
            Create a record
            """
            class Meta:
                model = ResearchAuthor
                fields = ResearchAuthorBaseSerializer.Meta.fields + ['last_update', 'time_created', ]

            def __init__(self, *args, **kwargs):
                super(ResearchAuthorCreateSerializer, self).__init__(*args, **kwargs)
                self.model_type = model_type
                self.key = pk
                self.user = user
                self.slugify = False
                self.auth_user = self.get_authenticated_user(self.user)

            def create(self, validated_data):
                """
                Created record from validated data
                :param validated_data: Validated data
                :return: Research author object
                :rtype: Object
                """
                author = validated_data.get('author')
                author_relation = ResearchAuthor.objects.create_by_model_type(
                    self.model_type,
                    self.key,
                    author=author,
                    user=self.auth_user,
                    modified_by=self.auth_user
                )
                if author_relation:
                    return author_relation
                else:
                    raise ValidationError({"non_field_errors": ["This is not a valid content type"]})

        return ResearchAuthorCreateSerializer

    class ResearchAuthorListSerializer(
        ResearchAuthorBaseSerializer, ResearchAuthorRelationBaseSerializer,
        FieldMethodSerializer, ResearchAuthorFieldMethodSerializer
    ):
        """
        Serialize all records in given fields into an API
        """
        research_author_url = hyperlinked_identity('research_api:research_author_detail', 'pk')

        class Meta:
            model = ResearchAuthor
            fields = ResearchAuthorBaseSerializer.Meta.fields + ['research_author_url', ] + \
                     ResearchAuthorRelationBaseSerializer.Meta.fields

    class ResearchAuthorSerializer(ModelSerializer):
        """
        Serialize all records in given fields into an API
        """
        author_url = SerializerMethodField()
        relation_id = SerializerMethodField()
        research_author_url = hyperlinked_identity('research_api:research_author_detail', 'pk')

        class Meta:
            model = ResearchAuthor
            fields = [
                'relation_id',
                'author_id',
                'author_url',
                'research_author_url',
            ]

        def get_author_url(self, obj):
            """
            Get related content type/object url
            :param obj: Current record object
            :return: URL to related object
            :rtype: String
            """
            return get_related_content_url(Author, obj.author.id)

        def get_relation_id (self, obj):
            """
            :param obj: Current record object
            :return: Research author id
            :rtype: Integer
            """
            return obj.id

    class ResearchAuthorContentTypeSerializer(ModelSerializer, FieldMethodSerializer):
        """
        Serialize all records in given fields into an API
        """
        relation_id = SerializerMethodField()
        content_type_url = SerializerMethodField()
        research_author_url = hyperlinked_identity('research_api:research_author_detail', 'pk')

        class Meta:
            model = ResearchAuthor
            fields = [
                'relation_id',
                'object_id',
                'content_type_url',
                'research_author_url',
            ]

        def get_relation_id (self, obj):
            """
            :param obj: Current record object
            :return: Research author id
            :rtype: String
            """
            return obj.id

    class ResearchAuthorDetailSerializer(
        ResearchAuthorBaseSerializer, ResearchAuthorRelationBaseSerializer,
        FieldMethodSerializer, ResearchAuthorFieldMethodSerializer
    ):
        """
        Serialize single record into an API. This is dependent on fields given.
        """
        user = SerializerMethodField()
        modified_by = SerializerMethodField()

        class Meta:
            common_fields = [
                'user',
                'modified_by',
                'last_update',
                'time_created',
            ] + ResearchAuthorRelationBaseSerializer.Meta.fields
            model = ResearchAuthor
            fields = ResearchAuthorBaseSerializer.Meta.fields + common_fields
            read_only_fields = ['id', ] + common_fields
    return {
        'create_research_author_serializer': create_research_author_serializer,
        'ResearchAuthorListSerializer': ResearchAuthorListSerializer,
        'ResearchAuthorSerializer': ResearchAuthorSerializer,
        'ResearchAuthorContentTypeSerializer': ResearchAuthorContentTypeSerializer,
        'ResearchAuthorDetailSerializer': ResearchAuthorDetailSerializer
    }
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- cpy-indent-level: 4; indent-tabs-mode: nil -*-
# ex: set expandtab softtabstop=4 shiftwidth=4:
#
# Copyright (C) 2013,2014,2015,2016  Contributor
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
    This module produces some HTML docuemation of the the database model.
    It does so by inspecting the database module to produce a set of
    diagrams.  For each of the classes in the diagrams we add the
    documenation contained in the source iself.
"""

###############################################################################

import os
import sys

_DIR = os.path.dirname(os.path.realpath(__file__))
_LIBDIR = os.path.join(_DIR, '..', 'lib')
sys.path.insert(0, _LIBDIR)

import aquilon.aqdb.depends

###############################################################################

import argparse

parser = argparse.ArgumentParser(description='generate schema graphs')
parser.add_argument('--outputdir', '-o', dest='dir',
                    default='%s/public_html/aqdb' % os.getenv('HOME', '/tmp'),
                    help='directory to put generated files')
parser.add_argument('--prefix', '-p', dest='prefix', default='aqdb',
                    help='basename of files to generate')
opts = parser.parse_args()

if not os.path.exists(opts.dir):
    os.makedirs(opts.dir)

###############################################################################

from aquilon.config import Config, lookup_file_path

config = Config(configfile=lookup_file_path('aqd.conf.mem'))

###############################################################################

import ms.modulecmd
ms.modulecmd.load("fsf/graphviz/2.6")

from aquilon.aqdb.db_factory import DbFactory
from aquilon.aqdb.model import *  # pylint: disable=W0401,W0614
from aquilon.aqdb.utils import schema2dot

from aquilon.aqdb.model.location import LocationLink
from aquilon.aqdb.model.service import (__ServiceListItem,
                                        PersonalityServiceListItem,
                                        __OSServiceListItem)
from aquilon.aqdb.model.service_instance import (__BuildItem,
                                                 __ClusterServiceBinding)
from aquilon.aqdb.model.service_address import __ServiceAddressInterface
from aquilon.aqdb.model.personality import (__PersonalityRootUser,
                                            __PersonalityRootNetGroup)
from aquilon.aqdb.model.cluster import (HostClusterMember,
                                        __ClusterAllowedPersonality)
from aquilon.aqdb.model.cluster_group import __ClusterGroupMember
from aquilon.aqdb.model.metacluster import __MetaClusterMember
from aquilon.aqdb.model.vlan import __ObservedVlan
from aquilon.aqdb.model.virtual_switch import (__VSwitchClusterAssignment,
                                               __VSwitchHostAssignment,
                                               __VSwitchPGAssignment)

from sqlalchemy import orm

###############################################################################

model_group = {
    'locations': {
        'title': 'Location Information',
        'classes': [Location, LocationLink, Company, Hub, Continent, Country,
                    City, Campus, Building, Room, Desk, Bunker, Rack],
    },
    'hardware': {
        'title': 'Hardware Information',
        'classes': [HardwareEntity, Model, Vendor, Machine, MachineSpecs,
                    NetworkDevice, Chassis, ChassisSlot],
    },
    'resource': {
        'title': 'Resources',
        'classes': [Resource, ResourceHolder, Application, Filesystem, Hostlink,
                    Intervention, RebootIntervention, RebootSchedule,
                    ResourceGroup, ServiceAddress, __ServiceAddressInterface,
                    Share, VirtualMachine],
    },
    'host': {
        'title': 'Hosts',
        'classes': [Host, OperatingSystem, HostLifecycle, HostGrnMap, Grn,
                    VirtualSwitch, __VSwitchPGAssignment,
                    __VSwitchHostAssignment],
    },
    'personality': {
        'title': 'Personalities',
        'classes': [Personality, PersonalityStage, Archetype, HostEnvironment, Grn,
                    Parameter, PersonalityGrnMap,
                    __PersonalityRootUser, __PersonalityRootNetGroup],
    },
    'feature': {
        'title': 'Features',
        'classes': [Feature, FeatureLink, ParamDefinition, ParamDefHolder],
    },
    'services': {
        'title': 'Services',
        'classes': [Service, PersonalityServiceListItem, __ServiceListItem,
                    __OSServiceListItem, ServiceInstance, __BuildItem,
                    ServiceInstanceServer, __ClusterServiceBinding,
                    ServiceMap],
    },
    'dns': {
        'title': 'DNS',
        'classes': [DnsDomain, DnsRecord, ARecord, SrvRecord, NsRecord, Alias,
                    AddressAlias, ReservedName, Fqdn, DynamicStub,
                    DnsEnvironment, DnsMap],
    },
    'cluster': {
        'title': 'Clusters',
        'classes': [Cluster, EsxCluster, StorageCluster, ComputeCluster,
                    PersonalityClusterInfo, PersonalityESXClusterInfo,
                    ClusterGroup, __ClusterGroupMember,
                    HostClusterMember,
                    MetaCluster, __MetaClusterMember,
                    ClusterLifecycle, __ClusterAllowedPersonality,
                    VirtualSwitch, __VSwitchClusterAssignment,
                    __VSwitchPGAssignment],
    },
    'network': {
        'title': 'Network',
        'classes': [Network, NetworkEnvironment, RouterAddress, StaticRoute,
                    NetworkDevice, VlanInfo, ObservedMac, __ObservedVlan,
                    PortGroup, AddressAssignment, Interface, ServiceAddress,
                    NetworkCompartment],
    },
    'xtn': {
        'title': 'Transactions',
        'classes': [Xtn, XtnEnd, XtnDetail],
    },
    'branch': {
        'title': 'Configuration Branches',
        'classes': [Branch, Sandbox, Domain],
    },
    'users': {
        'title': 'Users',
        'classes': [UserPrincipal, Realm, Role, User, NetGroupWhiteList],
    },
    'storage': {
        'title': 'Storage',
        'classes': [Disk, Share, Filesystem],
    },
}

###############################################################################

db = DbFactory()
Base.metadata.bind = db.engine
orm.configure_mappers()
Base.metadata.create_all()
Base.metadata.reflect()

def table_coverage_check():
    """
        Check that all of the tables that exist in the database have been
        included in model group.  This is to guard agains the model being
        updated and this piece of code not.
    """
    seen = {}
    for group in model_group:
        for cls in model_group[group]['classes']:
            tbl = cls.__tablename__
            seen[tbl] = 1
    for tbl in Base.metadata.tables:
        if tbl in seen:
            del seen[tbl]
        else:
            seen[tbl] = 0
    for tbl in seen:
        print "Table %s is missing from this script" % tbl

def write_schema_group_png(group):
    tables = list((Base.metadata.tables[cls.__tablename__]
                   for cls in model_group[group]['classes']))
    pngfile = os.path.join(opts.dir, "%s.%s.png" % (opts.prefix, group))
    schema2dot.create_schema_graph(tables=tables).write_png(pngfile)

def write_schema_html():
    htmlfile = os.path.join(opts.dir, "index.html")
    fh = open(htmlfile, 'w')
    fh.write("<title>AQDB Schema</title>")
    fh.write("
AQDB Schema
")
    for group in model_group:
        classes = model_group[group]['classes']
        title = model_group[group]['title']
        pngfile = "%s.%s.png" % (opts.prefix, group)
        fh.write("

%s
" % title)
        fh.write("""""" % pngfile)
        fh.write("

Class	\
            Table	Documentation
")
        for cls in classes:
            fh.write("
%s	%s	%s
"
                     % (cls.__name__, cls.__tablename__, cls.__doc__))
        fh.write("
")
    fh.write("")

table_coverage_check()

for group in model_group:
    write_schema_group_png(group)

write_schema_html()
<commit_msg>Add the Review class to the schema diagram<commit_after>#!/usr/bin/env python
# -*- cpy-indent-level: 4; indent-tabs-mode: nil -*-
# ex: set expandtab softtabstop=4 shiftwidth=4:
#
# Copyright (C) 2013,2014,2015,2016  Contributor
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
    This module produces some HTML docuemation of the the database model.
    It does so by inspecting the database module to produce a set of
    diagrams.  For each of the classes in the diagrams we add the
    documenation contained in the source iself.
"""

###############################################################################

import os
import sys

_DIR = os.path.dirname(os.path.realpath(__file__))
_LIBDIR = os.path.join(_DIR, '..', 'lib')
sys.path.insert(0, _LIBDIR)

import aquilon.aqdb.depends

###############################################################################

import argparse

parser = argparse.ArgumentParser(description='generate schema graphs')
parser.add_argument('--outputdir', '-o', dest='dir',
                    default='%s/public_html/aqdb' % os.getenv('HOME', '/tmp'),
                    help='directory to put generated files')
parser.add_argument('--prefix', '-p', dest='prefix', default='aqdb',
                    help='basename of files to generate')
opts = parser.parse_args()

if not os.path.exists(opts.dir):
    os.makedirs(opts.dir)

###############################################################################

from aquilon.config import Config, lookup_file_path

config = Config(configfile=lookup_file_path('aqd.conf.mem'))

###############################################################################

import ms.modulecmd
ms.modulecmd.load("fsf/graphviz/2.6")

from aquilon.aqdb.db_factory import DbFactory
from aquilon.aqdb.model import *  # pylint: disable=W0401,W0614
from aquilon.aqdb.utils import schema2dot

from aquilon.aqdb.model.location import LocationLink
from aquilon.aqdb.model.service import (__ServiceListItem,
                                        PersonalityServiceListItem,
                                        __OSServiceListItem)
from aquilon.aqdb.model.service_instance import (__BuildItem,
                                                 __ClusterServiceBinding)
from aquilon.aqdb.model.service_address import __ServiceAddressInterface
from aquilon.aqdb.model.personality import (__PersonalityRootUser,
                                            __PersonalityRootNetGroup)
from aquilon.aqdb.model.cluster import (HostClusterMember,
                                        __ClusterAllowedPersonality)
from aquilon.aqdb.model.cluster_group import __ClusterGroupMember
from aquilon.aqdb.model.metacluster import __MetaClusterMember
from aquilon.aqdb.model.vlan import __ObservedVlan
from aquilon.aqdb.model.virtual_switch import (__VSwitchClusterAssignment,
                                               __VSwitchHostAssignment,
                                               __VSwitchPGAssignment)

from sqlalchemy import orm

###############################################################################

model_group = {
    'locations': {
        'title': 'Location Information',
        'classes': [Location, LocationLink, Company, Hub, Continent, Country,
                    City, Campus, Building, Room, Desk, Bunker, Rack],
    },
    'hardware': {
        'title': 'Hardware Information',
        'classes': [HardwareEntity, Model, Vendor, Machine, MachineSpecs,
                    NetworkDevice, Chassis, ChassisSlot],
    },
    'resource': {
        'title': 'Resources',
        'classes': [Resource, ResourceHolder, Application, Filesystem, Hostlink,
                    Intervention, RebootIntervention, RebootSchedule,
                    ResourceGroup, ServiceAddress, __ServiceAddressInterface,
                    Share, VirtualMachine],
    },
    'host': {
        'title': 'Hosts',
        'classes': [Host, OperatingSystem, HostLifecycle, HostGrnMap, Grn,
                    VirtualSwitch, __VSwitchPGAssignment,
                    __VSwitchHostAssignment],
    },
    'personality': {
        'title': 'Personalities',
        'classes': [Personality, PersonalityStage, Archetype, HostEnvironment, Grn,
                    Parameter, PersonalityGrnMap,
                    __PersonalityRootUser, __PersonalityRootNetGroup],
    },
    'feature': {
        'title': 'Features',
        'classes': [Feature, FeatureLink, ParamDefinition, ParamDefHolder],
    },
    'services': {
        'title': 'Services',
        'classes': [Service, PersonalityServiceListItem, __ServiceListItem,
                    __OSServiceListItem, ServiceInstance, __BuildItem,
                    ServiceInstanceServer, __ClusterServiceBinding,
                    ServiceMap],
    },
    'dns': {
        'title': 'DNS',
        'classes': [DnsDomain, DnsRecord, ARecord, SrvRecord, NsRecord, Alias,
                    AddressAlias, ReservedName, Fqdn, DynamicStub,
                    DnsEnvironment, DnsMap],
    },
    'cluster': {
        'title': 'Clusters',
        'classes': [Cluster, EsxCluster, StorageCluster, ComputeCluster,
                    PersonalityClusterInfo, PersonalityESXClusterInfo,
                    ClusterGroup, __ClusterGroupMember,
                    HostClusterMember,
                    MetaCluster, __MetaClusterMember,
                    ClusterLifecycle, __ClusterAllowedPersonality,
                    VirtualSwitch, __VSwitchClusterAssignment,
                    __VSwitchPGAssignment],
    },
    'network': {
        'title': 'Network',
        'classes': [Network, NetworkEnvironment, RouterAddress, StaticRoute,
                    NetworkDevice, VlanInfo, ObservedMac, __ObservedVlan,
                    PortGroup, AddressAssignment, Interface, ServiceAddress,
                    NetworkCompartment],
    },
    'xtn': {
        'title': 'Transactions',
        'classes': [Xtn, XtnEnd, XtnDetail],
    },
    'branch': {
        'title': 'Configuration Branches',
        'classes': [Branch, Sandbox, Domain, Review],
    },
    'users': {
        'title': 'Users',
        'classes': [UserPrincipal, Realm, Role, User, NetGroupWhiteList],
    },
    'storage': {
        'title': 'Storage',
        'classes': [Disk, Share, Filesystem],
    },
}

###############################################################################

db = DbFactory()
Base.metadata.bind = db.engine
orm.configure_mappers()
Base.metadata.create_all()
Base.metadata.reflect()

def table_coverage_check():
    """
        Check that all of the tables that exist in the database have been
        included in model group.  This is to guard agains the model being
        updated and this piece of code not.
    """
    seen = {}
    for group in model_group:
        for cls in model_group[group]['classes']:
            tbl = cls.__tablename__
            seen[tbl] = 1
    for tbl in Base.metadata.tables:
        if tbl in seen:
            del seen[tbl]
        else:
            seen[tbl] = 0
    for tbl in seen:
        print "Table %s is missing from this script" % tbl

def write_schema_group_png(group):
    tables = list((Base.metadata.tables[cls.__tablename__]
                   for cls in model_group[group]['classes']))
    pngfile = os.path.join(opts.dir, "%s.%s.png" % (opts.prefix, group))
    schema2dot.create_schema_graph(tables=tables).write_png(pngfile)

def write_schema_html():
    htmlfile = os.path.join(opts.dir, "index.html")
    fh = open(htmlfile, 'w')
    fh.write("<title>AQDB Schema</title>")
    fh.write("
AQDB Schema
")
    for group in model_group:
        classes = model_group[group]['classes']
        title = model_group[group]['title']
        pngfile = "%s.%s.png" % (opts.prefix, group)
        fh.write("

%s
" % title)
        fh.write("""""" % pngfile)
        fh.write("

Class	\
            Table	Documentation
")
        for cls in classes:
            fh.write("
%s	%s	%s
"
                     % (cls.__name__, cls.__tablename__, cls.__doc__))
        fh.write("
")
    fh.write("")

table_coverage_check()

for group in model_group:
    write_schema_group_png(group)

write_schema_html()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Build report with stdlib modules status
"""

from __future__ import print_function, absolute_import, division
import glob
import jinja2

# ugly hack to avoid duplicating code:
import sys, os
sys.path.append(os.path.dirname(__file__))
from compile_stdlib import module_list

TEMPLATE = jinja2.Template(r"""

<title>VOC stdlib modules status</title>

    Module name
    Compile
    Test

{% for info in modules_info %}

    {{ info.module }}

    {{ info.compile_status }}

{{ info.compile_errors }}

    {{ info.test_status }}

{{ info.test_errors }}

{% endfor %}

""")

def find_result(mod_name):
    result_files = glob.glob(os.path.join('build', 'java', 'python', mod_name + '.*'))
    result_files = [f for f in result_files
                    if not f.endswith('.class') and not f.endswith(mod_name)]
    if not result_files:
        result_files = glob.glob(os.path.join('build', 'java', 'python', mod_name,
                                              '__init__.*'))
        result_files = [f for f in result_files if not f.endswith('.class')]
    if result_files:
        return sorted(result_files)

def read_file(fpath):
    with open(fpath) as f:
        return f.read()

def build_info(mod_name, result_files):
    result_files = find_result(mod_name)
    compile_status = 'N/A'
    test_status = 'N/A'
    compile_errors = ''
    test_errors = ''
    if result_files:
        if result_files[0].endswith('.compile-stderr'):
            compile_status = 'FAILED'
            compile_errors = read_file(result_files[0])
        else:
            compile_status = 'OK'

        if result_files[0].endswith('.test-works'):
            test_status = 'OK'
        elif result_files[0].endswith('.test-notest'):
            test_status = 'NOTEST'
        elif result_files[0].endswith('.test-compile-stderr'):
            test_status = 'NOCOMPILE'
            test_errors = read_file(result_files[0])
        elif '.test-fails' in result_files[0]:
            test_status = 'FAILED'
            test_errors = "\n".join([read_file(f) for f in result_files])

    return dict(
        module=mod_name,
        result_files=result_files,
        compile_status=compile_status,
        compile_errors=compile_errors,
        test_status=test_status,
        test_errors=test_errors
    )

def run(args):
    modules_info = [
        build_info(mod, find_result(mod))
        for mod in
        sorted(module_list('java', False))
    ]
    if args.html:
        html = TEMPLATE.render(modules_info=modules_info)
        with open('report.html', 'w') as f:
            f.write(html)
    else:
        print('{:17}  {:10}  {:10}'.format('Module', 'Compile', 'Test'))
        for info in modules_info:
            print('{module:17}  {compile_status:10}  {test_status:10}'.format(**info))

if '__main__' == __name__:
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--html', action='store_true')

    args = parser.parse_args()
    run(args)
<commit_msg>improving stdlib report output for test failures<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Build report with stdlib modules status
"""

from __future__ import print_function, absolute_import, division
import glob
import jinja2

# ugly hack to avoid duplicating code:
import sys, os
sys.path.append(os.path.dirname(__file__))
from compile_stdlib import module_list

TEMPLATE = jinja2.Template(r"""

<title>VOC stdlib modules status</title>

    Module name
    Compile
    Test

{% for info in modules_info %}

    {{ info.module }}

    {{ info.compile_status }}

{{ info.compile_errors }}

    {{ info.test_status }}

{{ info.test_errors }}

{% endfor %}

""")

def find_result(mod_name):
    result_files = glob.glob(os.path.join('build', 'java', 'python', mod_name + '.*'))
    result_files = [f for f in result_files
                    if not f.endswith('.class') and not f.endswith(mod_name)]
    if not result_files:
        result_files = glob.glob(os.path.join('build', 'java', 'python', mod_name,
                                              '__init__.*'))
        result_files = [f for f in result_files if not f.endswith('.class')]
    if result_files:
        return sorted(result_files)

def read_file(fpath):
    with open(fpath) as f:
        return f.read()

def build_info(mod_name, result_files):
    result_files = find_result(mod_name)
    compile_status = 'N/A'
    test_status = 'N/A'
    compile_errors = ''
    test_errors = ''

    def build_test_error_mesg():
        def find_file_with_ext(ext):
            files = [f for f in result_files if f.endswith(ext)]
            if files:
                return files[0]

        py_stderr = read_file(find_file_with_ext('.test-fails-py_stderr'))
        py_stdout = read_file(find_file_with_ext('.test-fails-py_stdout'))
        voc_stderr = read_file(find_file_with_ext('.test-fails-voc_stderr'))
        voc_stdout = read_file(find_file_with_ext('.test-fails-voc_stdout'))

        out = ''
        marker = '-' * 20
        if py_stdout or voc_stdout:
            out += (
                'Expected output was:\n{marker}\n{py_stdout}{marker}\n\n'
                'Received output was:\n{marker}\n{voc_stdout}{marker}\n\n'
                .format(marker=marker, py_stdout=py_stdout, voc_stdout=voc_stdout)
            )
        if py_stderr or voc_stderr:
            out += (
                'CPython stderr was:\n{marker}\n{py_stderr}{marker}\n\n'
                'Java/VOC stderr was:\n{marker}\n{voc_stderr}{marker}\n\n'
                .format(marker=marker, py_stderr=py_stderr, voc_stderr=voc_stderr)
            )
        return out

    if result_files:
        if result_files[0].endswith('.compile-stderr'):
            compile_status = 'FAILED'
            compile_errors = read_file(result_files[0])
        else:
            compile_status = 'OK'

        if result_files[0].endswith('.test-works'):
            test_status = 'OK'
        elif result_files[0].endswith('.test-notest'):
            test_status = 'NOTEST'
        elif result_files[0].endswith('.test-compile-stderr'):
            test_status = 'NOCOMPILE'
            test_errors = read_file(result_files[0])
        elif '.test-fails' in result_files[0]:
            test_status = 'FAILED'
            test_errors = build_test_error_mesg()

    return dict(
        module=mod_name,
        result_files=result_files,
        compile_status=compile_status,
        compile_errors=compile_errors,
        test_status=test_status,
        test_errors=test_errors
    )

def run(args):
    modules_info = [
        build_info(mod, find_result(mod))
        for mod in
        sorted(module_list('java', False))
    ]
    if args.html:
        html = TEMPLATE.render(modules_info=modules_info)
        with open('report.html', 'w') as f:
            f.write(html)
    else:
        print('{:17}  {:10}  {:10}'.format('Module', 'Compile', 'Test'))
        for info in modules_info:
            print('{module:17}  {compile_status:10}  {test_status:10}'.format(**info))

if '__main__' == __name__:
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--html', action='store_true')

    args = parser.parse_args()
    run(args)
<|endoftext|>