<commit_before># -*- coding: utf-8 -*-
"""Orangetool system functions."""
import subprocess as sub
import time
import requests
logo='''
________                                      __                .__
\_____  \____________    ____    ____   _____/  |_  ____   ____ |  |
 /   |   \_  __ \__  \  /    \  / ___\_/ __ \   __\/  _ \ /  _ \|  |
/    |    \  | \// __ \|   |  \/ /_/  >  ___/|  | (  <_> |  <_> )  |__
\_______  /__|  (____  /___|  /\___  / \___  >__|  \____/ \____/|____/
        \/           \/     \//_____/      \/
'''
ip_pattern=r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}"
api_1="http://ipinfo.io/ip"
VERSION = "0.25"
UPDATE_URL="http://www.orangetool.ir/version"
def check_update(DEBUG=False):
    """
    Check orangetool site for new version.

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: True if new version is available
    """
    try:
        new_version=requests.get(UPDATE_URL).text
        if float(new_version)>float(VERSION):
            print("New Version ("+new_version+") Of Orangetool Is Available")
            return True
        else:
            print("Update!")
            return False
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def get_temp(Zone=0,DEBUG=False):
    """
    Read cpu temperature.

    :param DEBUG : flag for using Debug mode
    :param Zone : thermal Zone Index
    :type DEBUG:bool
    :type Zone:int
    :return: board temp as string in celsius
    """
    try:
        command=open("/sys/class/thermal/thermal_zone"+str(Zone)+"/temp")
        #command=sub.Popen(["cat","/sys/class/thermal/thermal_zone"+str(Zone)+"/temp"],stderr=sub.PIPE,stdin=sub.PIPE,stdout=sub.PIPE)
        #response=list(command.communicate())
        response=command.read()
        return response[:-1]
        #if len(response[0])!=0:
            #return str(response[0])[2:-3]
        #else:
            #return "Error"
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def zero_insert(input_string):
    """
    Get a string as input if input is one digit add a zero.

    :param input_string: input digit az string
    :type input_string:str
    :return: modified output as str
    """
    if len(input_string)==1:
        return "0"+input_string
    return input_string

def time_convert(input_string):
    """
    Convert input_string from uptime from sec to DD,HH,MM,SS Format.

    :param input_string: input time string  in sec
    :type input_string:str
    :return: converted time as string
    """
    input_sec=float(input_string)
    input_minute=input_sec//60
    input_sec=int(input_sec-input_minute*60)
    input_hour=input_minute//60
    input_minute=int(input_minute-input_hour*60)
    input_day=int(input_hour//24)
    input_hour=int(input_hour-input_day*24)
    return zero_insert(str(input_day))+" days, "+zero_insert(str(input_hour))+" hour, "+zero_insert(str(input_minute))+" minutes, "+zero_insert(str(input_sec))+" seconds"

def uptime(DEBUG=False):
    """
    Return system uptime.

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: system uptime as string
    """
    try:
        command=open("/proc/uptime")
        response=command.read()
        return time_convert(response[:-1].split(" ")[0])
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"
def idletime(DEBUG=False):
    """
    Return system idletime.

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: system idle as string
    """
    try:
        command=open("/proc/uptime")
        response=command.read()
        return time_convert(response[:-1].split(" ")[1])
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def version():
    """
    Return orangetool version.

    :return: return orangetool-version number as string
    """
    print(logo)
    return "orangetool-v"+VERSION

def wakeup(day=0,hour=0,minute=0,DEBUG=False):
    """
    Set wakeup time for kernel RTC (need sudo).

    :param day: days for wakeup
    :param hour: hout for wakeup
    :param minute: minute for wakeup
    :param DEBUG: flag for using Debug mode
    :type day:int
    :type hour:int
    :type minute:int
    :type DEBUG:bool
    :return: bool
    """
    try:
        total_time=day*24*60+hour*60+minute
        epoch=time.time()+total_time*60
        file=open("/sys/class/rtc/rtc0/wakealarm","w")
        file.write("0")
        file.close()
        file = open("/sys/class/rtc/rtc0/wakealarm", "w")
        file.write(str(epoch))
        file.close()
        return True
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def sleep(DEBUG=False):
    """
    Shortcut for sleep command (need sudo).

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: None
    """
    try:
        command=sub.Popen("pm-suspend",stderr=sub.PIPE,stdout=sub.PIPE,stdin=sub.PIPE)
        response=list(command.communicate())
        if len(response[1])>0:
            raise Exception('Root Error')
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def halt(DEBUG=False):
    """
    Shortcut for poweroff (need sudo).

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: None
    """
    try:
        command = sub.Popen("poweroff", stderr=sub.PIPE, stdout=sub.PIPE, stdin=sub.PIPE)
        response = list(command.communicate())
        if len(response[1]) > 0:
            raise Exception('Root Error')
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def restart(DEBUG=False):
    """
    Shortcut for reboot (need sudo).

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: None
    """
    try:
        command = sub.Popen("reboot", stderr=sub.PIPE, stdout=sub.PIPE, stdin=sub.PIPE)
        response = list(command.communicate())
        if len(response[1]) > 0:
            raise Exception('Root Error')
    except Exception as e:
        if DEBUG == True:
            print(str(e))
        return "Error"

<commit_msg>fix : duplication in orangetool_system fixed<commit_after># -*- coding: utf-8 -*-
"""Orangetool system functions."""
import subprocess as sub
import time
import requests
logo='''
________                                      __                .__
\_____  \____________    ____    ____   _____/  |_  ____   ____ |  |
 /   |   \_  __ \__  \  /    \  / ___\_/ __ \   __\/  _ \ /  _ \|  |
/    |    \  | \// __ \|   |  \/ /_/  >  ___/|  | (  <_> |  <_> )  |__
\_______  /__|  (____  /___|  /\___  / \___  >__|  \____/ \____/|____/
        \/           \/     \//_____/      \/
'''
ip_pattern=r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}"
api_1="http://ipinfo.io/ip"
VERSION = "0.25"
UPDATE_URL="http://www.orangetool.ir/version"
def check_update(DEBUG=False):
    """
    Check orangetool site for new version.

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: True if new version is available
    """
    try:
        new_version=requests.get(UPDATE_URL).text
        if float(new_version)>float(VERSION):
            print("New Version ("+new_version+") Of Orangetool Is Available")
            return True
        else:
            print("Update!")
            return False
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def get_temp(Zone=0,DEBUG=False):
    """
    Read cpu temperature.

    :param DEBUG : flag for using Debug mode
    :param Zone : thermal Zone Index
    :type DEBUG:bool
    :type Zone:int
    :return: board temp as string in celsius
    """
    try:
        command=open("/sys/class/thermal/thermal_zone"+str(Zone)+"/temp")
        #command=sub.Popen(["cat","/sys/class/thermal/thermal_zone"+str(Zone)+"/temp"],stderr=sub.PIPE,stdin=sub.PIPE,stdout=sub.PIPE)
        #response=list(command.communicate())
        response=command.read()
        return response[:-1]
        #if len(response[0])!=0:
            #return str(response[0])[2:-3]
        #else:
            #return "Error"
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def zero_insert(input_string):
    """
    Get a string as input if input is one digit add a zero.

    :param input_string: input digit az string
    :type input_string:str
    :return: modified output as str
    """
    if len(input_string)==1:
        return "0"+input_string
    return input_string

def time_convert(input_string):
    """
    Convert input_string from uptime from sec to DD,HH,MM,SS Format.

    :param input_string: input time string  in sec
    :type input_string:str
    :return: converted time as string
    """
    input_sec=float(input_string)
    input_minute=input_sec//60
    input_sec=int(input_sec-input_minute*60)
    input_hour=input_minute//60
    input_minute=int(input_minute-input_hour*60)
    input_day=int(input_hour//24)
    input_hour=int(input_hour-input_day*24)
    return zero_insert(str(input_day))+" days, "+zero_insert(str(input_hour))+" hour, "+zero_insert(str(input_minute))+" minutes, "+zero_insert(str(input_sec))+" seconds"

def uptime(DEBUG=False):
    """
    Return system uptime.

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: system uptime as string
    """
    try:
        command=open("/proc/uptime")
        response=command.read()
        return time_convert(response[:-1].split(" ")[0])
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"
def idletime(DEBUG=False):
    """
    Return system idletime.

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: system idle as string
    """
    try:
        command=open("/proc/uptime")
        response=command.read()
        return time_convert(response[:-1].split(" ")[1])
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def version():
    """
    Return orangetool version.

    :return: return orangetool-version number as string
    """
    print(logo)
    return "orangetool-v"+VERSION

def wakeup(day=0,hour=0,minute=0,DEBUG=False):
    """
    Set wakeup time for kernel RTC (need sudo).

    :param day: days for wakeup
    :param hour: hout for wakeup
    :param minute: minute for wakeup
    :param DEBUG: flag for using Debug mode
    :type day:int
    :type hour:int
    :type minute:int
    :type DEBUG:bool
    :return: bool
    """
    try:
        total_time=day*24*60+hour*60+minute
        epoch=time.time()+total_time*60
        file=open("/sys/class/rtc/rtc0/wakealarm","w")
        file.write("0")
        file.close()
        file = open("/sys/class/rtc/rtc0/wakealarm", "w")
        file.write(str(epoch))
        file.close()
        return True
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def power_control(command, DEBUG=False):
    """
    Control different power options.

    :param command: input command
    :type command: str
    :param DEBUG: flag for using Debug mode
    :type DEBUG: bool
    :return: None
    """
    try:
        command=sub.Popen(command,stderr=sub.PIPE,stdout=sub.PIPE,stdin=sub.PIPE)
        response=list(command.communicate())
        if len(response[1])>0:
            raise Exception('Root Error')
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"

def sleep(DEBUG=False):
    """
    Shortcut for sleep command (need sudo).

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: None
    """
    power_control("pm-suspend", DEBUG)

def halt(DEBUG=False):
    """
    Shortcut for poweroff (need sudo).

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: None
    """
    power_control("poweroff", DEBUG)

def restart(DEBUG=False):
    """
    Shortcut for reboot (need sudo).

    :param DEBUG: flag for using Debug mode
    :type DEBUG:bool
    :return: None
    """
    power_control("reboot", DEBUG)

<|endoftext|>


<commit_before>#!/usr/bin/python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Set of utilities to add commands to a buildbot factory.

Contains the Dart specific commands. Based on commands.py
"""

from buildbot.steps import shell
from buildbot.process.properties import WithProperties

from master import chromium_step
from master.factory import commands

class DartCommands(commands.FactoryCommands):
  """Encapsulates methods to add dart commands to a buildbot factory."""

  def __init__(self, factory=None, target=None, build_dir=None,
               target_platform=None, env=None):

    commands.FactoryCommands.__init__(self, factory, target, build_dir,
                                      target_platform)

    # Two additional directories up compared to normal chromium scripts due
    # to using runtime as runtime dir inside dart directory inside
    # build directory.
    self._script_dir = self.PathJoin('..', self._script_dir)
    self._tools_dir = self.PathJoin('tools')

    # Where the chromium slave scripts are.
    self._chromium_script_dir = self.PathJoin(self._script_dir, 'chromium')
    self._private_script_dir = self.PathJoin(self._script_dir, '..', 'private')

    self._slave_dir = self.PathJoin(self._script_dir,
                                            '..', '..', '..',
                                            'build', 'scripts',
                                            'slave', 'dart')

    self._dart_util = self.PathJoin(self._slave_dir, 'dart_util.py')
    self._dart_build_dir = self.PathJoin('build', 'dart')
    self._repository_root = ''
    self._target_platform = target_platform
    self._custom_env = env or {}

  def AddMaybeClobberStep(self, clobber, options=None, timeout=1200):
    """Possibly clobber.

    Either clobber unconditionally (e.g. nuke-and-pave builder, set at
    factory build time), or at runtime (clobber checkbox).  If the
    former, the clobber arg is set.  If the latter, we use a buildbot
    Properties object.

    TODO(jrg); convert into a doStepIf with a closure referencing
    step.build.getProperties().  E.g.
    http://permalink.gmane.org/gmane.comp.python.buildbot.devel/6039
    """
    options = options or {}
    clobber_cmd = [self._python, self._dart_util]
    clobber_cmd.append(WithProperties('%(clobber:+--clobber)s'))
    workdir = self._dart_build_dir
    self._factory.addStep(shell.ShellCommand,
                          name='maybe clobber',
                          description='maybe clobber',
                          timeout=timeout,
                          haltOnFailure=True,
                          workdir=workdir,
                          command=clobber_cmd)

  # pylint: disable=W0221
  def AddCompileStep(self, options=None, timeout=1200):
    options = options or {}
    cmd = 'python ' + self._tools_dir + '/build.py --mode=%s' % \
        (options['mode'])
    workdir = self._dart_build_dir
    is_dartc = (options.get('name') != None and
                options.get('name').startswith('dartc'))
    is_dart2dart = (options.get('name') != None and
                    options.get('name').startswith('dart2dart'))
    if not is_dartc and not is_dart2dart:
      cmd += ' --arch=%s' % (options['arch'])
      cmd += ' runtime'

    if is_dart2dart:
      cmd += ' create_sdk'
    self._factory.addStep(shell.ShellCommand,
                          name='build',
                          description='build',
                          timeout=timeout,
                          env = self._custom_env,
                          haltOnFailure=True,
                          workdir=workdir,
                          command=cmd)

  def AddTests(self, options=None, timeout=1200):
    options = options or {}
    is_dartc = (options.get('name') != None and
                options.get('name').startswith('dartc'))
    is_dart2dart = (options.get('name') != None and
                    options.get('name').startswith('dart2dart'))

    arch = options.get('arch')
    if is_dartc:
      compiler = 'dartc'
      runtime = 'none'
      configuration = (options['mode'], arch, compiler, runtime)
      base_cmd = ('python ' + self._tools_dir + '/test.py '
                  ' --progress=line --report --time --mode=%s --arch=%s '
                  ' --compiler=%s --runtime=%s') % configuration
    elif is_dart2dart:
      compiler = 'dart2dart'
      runtime = 'vm'
      # TODO(ricow): Remove shard functionality when we move to annotated.
      shards = 1
      shard = 1
      if options.get('shards') != None and options.get('shard') != None:
        shards = options['shards']
        shard = options['shard']
      configuration = (options['mode'], arch, compiler, shards, shard)
      base_cmd = ('python ' + self._tools_dir + '/test.py '
                  ' --progress=buildbot --report --time --mode=%s --arch=%s '
                  ' --compiler=%s --shards=%s --shard=%s') % configuration
    else:
      compiler = 'none'
      runtime = 'vm'
      configuration = (options['mode'], arch, compiler, runtime)
      base_cmd = ('python ' + self._tools_dir + '/test.py '
                  ' --progress=line --report --time --mode=%s --arch=%s '
                  ' --compiler=%s --runtime=%s') % configuration

    if is_dartc:
      cmd = base_cmd
      self._factory.addStep(shell.ShellCommand,
                            name='tests',
                            description='tests',
                            timeout=timeout,
                            env = self._custom_env,
                            haltOnFailure=True,
                            workdir=self._dart_build_dir,
                            command=cmd)
    elif is_dart2dart:
      cmd = base_cmd
      self._factory.addStep(shell.ShellCommand,
                            name='tests',
                            description='tests',
                            timeout=timeout,
                            env = self._custom_env,
                            haltOnFailure=True,
                            workdir=self._dart_build_dir,
                            command=cmd)
      cmd = base_cmd + ' --minified'
      self._factory.addStep(shell.ShellCommand,
                            name='minified tests',
                            description='minified tests',
                            timeout=timeout,
                            env = self._custom_env,
                            haltOnFailure=True,
                            workdir=self._dart_build_dir,
                            command=cmd)
    else:
      if options.get('flags') != None:
        base_cmd += options.get('flags')
      cmd = base_cmd
      self._factory.addStep(shell.ShellCommand,
                            name='tests',
                            description='tests',
                            timeout=timeout,
                            env = self._custom_env,
                            haltOnFailure=True,
                            workdir=self._dart_build_dir,
                            command=cmd)
      # Rerun all tests in checked mode (assertions and type tests).
      cmd = base_cmd + ' --checked'
      self._factory.addStep(shell.ShellCommand,
                            name='checked_tests',
                            description='checked_tests',
                            timeout=timeout,
                            env = self._custom_env,
                            haltOnFailure=True,
                            workdir=self._dart_build_dir,
                            command=cmd)

  def AddAnnotatedSteps(self, python_script, timeout=1200):
    self._factory.addStep(chromium_step.AnnotatedCommand,
                          name='annotated_steps',
                          description='annotated_steps',
                          timeout=timeout,
                          haltOnFailure=True,
                          env = self._custom_env,
                          workdir=self._dart_build_dir,
                          command=[self._python, python_script])
<commit_msg>Add flaky log to vm buildbot status.<commit_after>#!/usr/bin/python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Set of utilities to add commands to a buildbot factory.

Contains the Dart specific commands. Based on commands.py
"""

from buildbot.steps import shell
from buildbot.process.properties import WithProperties

from master import chromium_step
from master.factory import commands

class DartCommands(commands.FactoryCommands):
  """Encapsulates methods to add dart commands to a buildbot factory."""

  def __init__(self, factory=None, target=None, build_dir=None,
               target_platform=None, env=None):

    commands.FactoryCommands.__init__(self, factory, target, build_dir,
                                      target_platform)

    # Two additional directories up compared to normal chromium scripts due
    # to using runtime as runtime dir inside dart directory inside
    # build directory.
    self._script_dir = self.PathJoin('..', self._script_dir)
    self._tools_dir = self.PathJoin('tools')

    # Where the chromium slave scripts are.
    self._chromium_script_dir = self.PathJoin(self._script_dir, 'chromium')
    self._private_script_dir = self.PathJoin(self._script_dir, '..', 'private')

    self._slave_dir = self.PathJoin(self._script_dir,
                                            '..', '..', '..',
                                            'build', 'scripts',
                                            'slave', 'dart')

    self._dart_util = self.PathJoin(self._slave_dir, 'dart_util.py')
    self._dart_build_dir = self.PathJoin('build', 'dart')
    self._repository_root = ''
    self._target_platform = target_platform
    self._custom_env = env or {}

  def AddMaybeClobberStep(self, clobber, options=None, timeout=1200):
    """Possibly clobber.

    Either clobber unconditionally (e.g. nuke-and-pave builder, set at
    factory build time), or at runtime (clobber checkbox).  If the
    former, the clobber arg is set.  If the latter, we use a buildbot
    Properties object.

    TODO(jrg); convert into a doStepIf with a closure referencing
    step.build.getProperties().  E.g.
    http://permalink.gmane.org/gmane.comp.python.buildbot.devel/6039
    """
    options = options or {}
    clobber_cmd = [self._python, self._dart_util]
    clobber_cmd.append(WithProperties('%(clobber:+--clobber)s'))
    workdir = self._dart_build_dir
    self._factory.addStep(shell.ShellCommand,
                          name='maybe clobber',
                          description='maybe clobber',
                          timeout=timeout,
                          haltOnFailure=True,
                          workdir=workdir,
                          command=clobber_cmd)

  # pylint: disable=W0221
  def AddCompileStep(self, options=None, timeout=1200):
    options = options or {}
    cmd = 'python ' + self._tools_dir + '/build.py --mode=%s' % \
        (options['mode'])
    workdir = self._dart_build_dir
    is_dartc = (options.get('name') != None and
                options.get('name').startswith('dartc'))
    is_dart2dart = (options.get('name') != None and
                    options.get('name').startswith('dart2dart'))
    if not is_dartc and not is_dart2dart:
      cmd += ' --arch=%s' % (options['arch'])
      cmd += ' runtime'

    if is_dart2dart:
      cmd += ' create_sdk'
    self._factory.addStep(shell.ShellCommand,
                          name='build',
                          description='build',
                          timeout=timeout,
                          env = self._custom_env,
                          haltOnFailure=True,
                          workdir=workdir,
                          command=cmd)

  def AddTests(self, options=None, timeout=1200):
    options = options or {}
    is_dartc = (options.get('name') != None and
                options.get('name').startswith('dartc'))
    is_dart2dart = (options.get('name') != None and
                    options.get('name').startswith('dart2dart'))

    arch = options.get('arch')
    if is_dartc:
      compiler = 'dartc'
      runtime = 'none'
      configuration = (options['mode'], arch, compiler, runtime)
      base_cmd = ('python ' + self._tools_dir + '/test.py '
                  ' --progress=line --report --time --mode=%s --arch=%s '
                  ' --compiler=%s --runtime=%s') % configuration
    elif is_dart2dart:
      compiler = 'dart2dart'
      runtime = 'vm'
      # TODO(ricow): Remove shard functionality when we move to annotated.
      shards = 1
      shard = 1
      if options.get('shards') != None and options.get('shard') != None:
        shards = options['shards']
        shard = options['shard']
      configuration = (options['mode'], arch, compiler, shards, shard)
      base_cmd = ('python ' + self._tools_dir + '/test.py '
                  ' --progress=buildbot --report --time --mode=%s --arch=%s '
                  ' --compiler=%s --shards=%s --shard=%s') % configuration
    else:
      compiler = 'none'
      runtime = 'vm'
      configuration = (options['mode'], arch, compiler, runtime)
      base_cmd = ('python ' + self._tools_dir + '/test.py '
                  ' --progress=line --report --time --mode=%s --arch=%s '
                  ' --compiler=%s --runtime=%s') % configuration

    if is_dartc:
      cmd = base_cmd
      self._factory.addStep(shell.ShellCommand,
                            name='tests',
                            description='tests',
                            timeout=timeout,
                            env = self._custom_env,
                            haltOnFailure=True,
                            workdir=self._dart_build_dir,
                            command=cmd)
    elif is_dart2dart:
      cmd = base_cmd
      self._factory.addStep(shell.ShellCommand,
                            name='tests',
                            description='tests',
                            timeout=timeout,
                            env = self._custom_env,
                            haltOnFailure=True,
                            workdir=self._dart_build_dir,
                            command=cmd)
      cmd = base_cmd + ' --minified'
      self._factory.addStep(shell.ShellCommand,
                            name='minified tests',
                            description='minified tests',
                            timeout=timeout,
                            env = self._custom_env,
                            haltOnFailure=True,
                            workdir=self._dart_build_dir,
                            command=cmd)
    else:
      if options.get('flags') != None:
        base_cmd += options.get('flags')
      cmd = base_cmd
      self._factory.addStep(shell.ShellCommand,
                            name='tests',
                            description='tests',
                            timeout=timeout,
                            env = self._custom_env,
                            haltOnFailure=True,
                            workdir=self._dart_build_dir,
                            command=cmd,
                            logfiles={"flakylog": ".flaky.log"},
                            lazylogfiles=True)
      # Rerun all tests in checked mode (assertions and type tests).
      cmd = base_cmd + ' --checked'
      self._factory.addStep(shell.ShellCommand,
                            name='checked_tests',
                            description='checked_tests',
                            timeout=timeout,
                            env = self._custom_env,
                            haltOnFailure=True,
                            workdir=self._dart_build_dir,
                            command=cmd,
                            logfiles={"flakylog": ".flaky.log"},
                            lazylogfiles=True)

  def AddAnnotatedSteps(self, python_script, timeout=1200):
    self._factory.addStep(chromium_step.AnnotatedCommand,
                          name='annotated_steps',
                          description='annotated_steps',
                          timeout=timeout,
                          haltOnFailure=True,
                          env = self._custom_env,
                          workdir=self._dart_build_dir,
                          command=[self._python, python_script])
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 nu

from __future__ import (unicode_literals, absolute_import,
                        division, print_function)
import logging

from django.core.management.base import BaseCommand

from snisi_core.models.Projects import Cluster
from snisi_core.models.Periods import MonthPeriod
from snisi_core.models.Reporting import ExpectedReporting

logger = logging.getLogger(__name__)

class Command(BaseCommand):

    def handle(self, *args, **options):

        cluster = Cluster.get_or_none("malaria_monthly_routine")
        periods = MonthPeriod.all_from(MonthPeriod.from_url_str("06-2011"))

        for period in periods:
            logger.info(period)

            for entity in cluster.members():
                qs = ExpectedReporting.objects.filter(
                    entity=entity, period=period,
                    report_class__slug="malaria_monthly_routine")

                if qs.count() == 2:
                    logger.info("Removing {}".format(qs[1]))
                    qs[1].delete()
<commit_msg>fixed maint script to include aggregates<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 nu

from __future__ import (unicode_literals, absolute_import,
                        division, print_function)
import logging

from django.core.management.base import BaseCommand

from snisi_core.models.Projects import Cluster
from snisi_core.models.Periods import MonthPeriod
from snisi_core.models.Reporting import ExpectedReporting

logger = logging.getLogger(__name__)

class Command(BaseCommand):

    def handle(self, *args, **options):

        cluster = Cluster.get_or_none("malaria_monthly_routine")
        periods = MonthPeriod.all_from(MonthPeriod.from_url_str("06-2011"))

        for period in periods:
            logger.info(period)

            for entity in cluster.members():
                qs = ExpectedReporting.objects.filter(
                    entity=entity, period=period,
                    report_class__slug__in=["malaria_monthly_routine",
                                            "malaria_monthly_routine_aggregated"])

                if qs.count() == 2:
                    logger.info("Removing {}".format(qs[1]))
                    qs[1].delete()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.encoding import python_2_unicode_compatible
try:
    from django.utils.encoding import force_unicode
except ImportError:
    from django.utils.encoding import force_text as force_unicode
from django.utils.text import slugify as default_slugify
from django.utils.timezone import now
from django.utils.translation import get_language, ugettext_lazy as _
from django.contrib.auth.models import User
from cms.models.fields import PlaceholderField
from cms.models.pluginmodel import CMSPlugin
from djangocms_text_ckeditor.fields import HTMLField
from filer.fields.image import FilerImageField

from aldryn_categories.fields import CategoryManyToManyField
from aldryn_categories.models import Category
from aldryn_people.models import Person
from aldryn_reversion.core import version_controlled_content
from parler.models import TranslatableModel, TranslatedFields
from taggit.managers import TaggableManager
from sortedm2m.fields import SortedManyToManyField

from .cms_appconfig import NewsBlogConfig
from .managers import RelatedManager
from .utils import strip_tags, get_plugin_index_data, get_request

import django.core.validators

if settings.LANGUAGES:
    LANGUAGE_CODES = [language[0] for language in settings.LANGUAGES]
elif settings.LANGUAGE:
    LANGUAGE_CODES = [settings.LANGUAGE]
else:
    raise ImproperlyConfigured(
        'Neither LANGUAGES nor LANGUAGE was found in settings.')

@python_2_unicode_compatible
@version_controlled_content
class Article(TranslatableModel):
    translations = TranslatedFields(
        title=models.CharField(_('title'), max_length=234),
        slug=models.SlugField(
            verbose_name=_('slug'),
            max_length=255,
            db_index=True,
            blank=True,
            help_text=_(
                'Used in the URL. If changed, the URL will change. '
                'Clear it to have it re-created automatically.'),
        ),
        lead_in=HTMLField(
            verbose_name=_('Optional lead-in'), default='',
            help_text=_('Will be displayed in lists, and at the start of the '
                        'detail page (in bold)'),
            blank=True,
        ),
        meta_title=models.CharField(
            max_length=255, verbose_name=_('meta title'),
            blank=True, default=''),
        meta_description=models.TextField(
            verbose_name=_('meta description'), blank=True, default=''),
        meta_keywords=models.TextField(
            verbose_name=_('meta keywords'), blank=True, default=''),
        meta={'unique_together': (('language_code', 'slug', ), )},

        search_data=models.TextField(blank=True, editable=False)
    )

    content = PlaceholderField('newsblog_article_content',
                               related_name='newsblog_article_content')
    author = models.ForeignKey(Person, null=True, blank=True,
                               verbose_name=_('author'))
    owner = models.ForeignKey(User, verbose_name=_('owner'))
    app_config = models.ForeignKey(NewsBlogConfig,
                                   verbose_name=_('app. config'))
    categories = CategoryManyToManyField('aldryn_categories.Category',
                                         verbose_name=_('categories'),
                                         blank=True)
    publishing_date = models.DateTimeField(_('publishing date'),
                                           default=now())
    is_published = models.BooleanField(_('is published'), default=True,
                                       db_index=True)
    is_featured = models.BooleanField(_('is featured'), default=False,
                                      db_index=True)
    featured_image = FilerImageField(null=True, blank=True)

    tags = TaggableManager(blank=True)

    related = SortedManyToManyField('self', verbose_name=_('related articles'),
                                    blank=True)

    objects = RelatedManager()

    class Meta:
        ordering = ['-publishing_date']

    def __str__(self):
        return self.safe_translation_getter('title', any_language=True)

    def get_absolute_url(self):
        #
        # NB: It is important that this is safe to run even when the user has
        # not created the apphook yet, as some user work-flows involve creating
        # articles before the page exists.
        #
        try:
            return reverse(
                '{namespace}:article-detail'.format(
                    namespace=self.app_config.namespace
                ), kwargs={
                    'slug': self.safe_translation_getter('slug', any_language=True)
                }
            )
        except:
            return ''  # Note NOT None here

    def slugify(self, source_text, i=None):
        slug = default_slugify(source_text)
        if i is not None:
            slug += "_%d" % i
        return slug

    def get_search_data(self, language=None, request=None):
        """
        Provides an index for use with Haystack, or, for populating
        Article.translations.search_data.
        """
        if not self.pk:
            return ''
        if language is None:
            language = get_language()
        if request is None:
            request = get_request(language=language)
        description = self.safe_translation_getter('lead_in')
        text_bits = [strip_tags(description)]
        for category in self.categories.all():
            text_bits.append(
                force_unicode(category.safe_translation_getter('name')))
        for tag in self.tags.all():
            text_bits.append(force_unicode(tag.name))
        if self.content:
            plugins = self.content.cmsplugin_set.filter(language=language)
            for base_plugin in plugins:
                plugin_text_content = ' '.join(
                    get_plugin_index_data(base_plugin, request))
                text_bits.append(plugin_text_content)
        return ' '.join(text_bits)

    def save(self, *args, **kwargs):
        # Update the search index
        self.search_data = self.get_search_data()

        # Ensure there is an owner.
        if self.app_config.create_authors and self.author is None:
            self.author = Person.objects.get_or_create(
                user=self.owner,
                defaults={
                    'name': u' '.join((self.owner.first_name,
                                       self.owner.last_name))
                })[0]

        # Start with a naïve approach, if none provided.
        if not self.slug:
            self.slug = default_slugify(self.title)

        # Ensure we aren't colliding with an existing slug *for this language*.
        if Article.objects.translated(
                slug=self.slug).exclude(id=self.id).count() == 0:
            return super(Article, self).save(*args, **kwargs)

        for lang in LANGUAGE_CODES:
            #
            # We'd much rather just do something like:
            # Article.objects.translated(lang,
            # slug__startswith=self.slug)
            # But sadly, this isn't supported by Parler/Django, see:
            # http://django-parler.readthedocs.org/en/latest/api/\
            # parler.managers.html#the-translatablequeryset-class
            #
            slugs = []
            all_slugs = Article.objects.language(lang).exclude(
                id=self.id).values_list('translations__slug', flat=True)
            for slug in all_slugs:
                if slug and slug.startswith(self.slug):
                    slugs.append(slug)
            i = 1
            while True:
                slug = self.slugify(self.title, i)
                if slug not in slugs:
                    self.slug = slug
                    return super(Article, self).save(*args, **kwargs)
                i += 1

class NewsBlogCMSPlugin(CMSPlugin):
    """AppHookConfig aware abstract CMSPlugin class for Aldryn Newsblog"""
    # avoid reverse relation name clashes by not adding a related_name
    # to the parent plugin
    cmsplugin_ptr = models.OneToOneField(
        CMSPlugin, related_name='+', parent_link=True)

    app_config = models.ForeignKey(NewsBlogConfig)

    def copy_relations(self, old_instance):
        self.app_config = old_instance.app_config

    class Meta:
        abstract = True

@python_2_unicode_compatible
class NewsBlogArchivePlugin(NewsBlogCMSPlugin):
    def __str__(self):
        return _('%s archive') % (self.app_config.get_app_title(), )

class NewsBlogArticleSearchPlugin(NewsBlogCMSPlugin):
    max_articles = models.PositiveIntegerField(
        _('max articles'), default=10,
        validators=[django.core.validators.MinValueValidator(1)],
        help_text=_('The maximum number of found articles display.')
    )

    def __str__(self):
        return _('%s archive') % (self.app_config.get_app_title(), )

@python_2_unicode_compatible
class NewsBlogAuthorsPlugin(NewsBlogCMSPlugin):
    def __str__(self):
        return _('%s authors') % (self.app_config.get_app_title(), )

    def get_authors(self):
        author_list = Article.objects.published().filter(
            app_config=self.app_config).values_list('author',
                                                    flat=True).distinct()
        author_list = list(author_list)
        qs = Person.objects.filter(
            id__in=author_list, article__app_config=self.app_config
        ).annotate(count=models.Count('article'))
        return qs

@python_2_unicode_compatible
class NewsBlogCategoriesPlugin(NewsBlogCMSPlugin):
    def __str__(self):
        return _('%s categories') % (self.app_config.get_app_title(), )

    def get_categories(self):
        category_list = Article.objects.published().filter(
            app_config=self.app_config).values_list('categories',
                                                    flat=True).distinct()
        category_list = list(category_list)
        qs = Category.objects.filter(
            id__in=category_list,
            article__app_config=self.app_config,
        ).annotate(count=models.Count('article')).order_by('-count')
        return qs

@python_2_unicode_compatible
class NewsBlogFeaturedArticlesPlugin(NewsBlogCMSPlugin):
    article_count = models.PositiveIntegerField(
        default=1,
        validators=[django.core.validators.MinValueValidator(1)],
        help_text=_('The maximum number of featured articles display.')
    )

    def get_articles(self):
        if not self.article_count:
            return Article.objects.none()
        articles = Article.objects.published().active_translations(
            get_language()
        ).filter(
            app_config=self.app_config,
            is_featured=True
        )
        return articles[:self.article_count]

    def __str__(self):
        if not self.pk:
            return 'featured articles'
        prefix = self.app_config.get_app_title()
        if self.article_count == 1:
            title = _('featured article')
        else:
            title = _('featured articles: %(count)s') % {
                'count': self.article_count,
            }
        return '{0} {1}'.format(prefix, title)

@python_2_unicode_compatible
class NewsBlogLatestArticlesPlugin(NewsBlogCMSPlugin):
    latest_articles = models.IntegerField(
        default=5,
        help_text=_('The maximum number of latest articles to display.')
    )

    def __str__(self):
        return _('%s latest articles: %s') % (
            self.app_config.get_app_title(), self.latest_articles, )

    def get_articles(self):
        articles = Article.objects.published().active_translations(
            get_language()).filter(app_config=self.app_config)
        return articles[:self.latest_articles]

@python_2_unicode_compatible
class NewsBlogRelatedPlugin(CMSPlugin):
    # NOTE: This one does NOT subclass NewsBlogCMSPlugin
    cmsplugin_ptr = models.OneToOneField(
        CMSPlugin, related_name='+', parent_link=True)

    def __str__(self):
        return _('Related articles')

@python_2_unicode_compatible
class NewsBlogTagsPlugin(NewsBlogCMSPlugin):
    def __str__(self):
        return _('%s tags') % (self.app_config.get_app_title(), )

    def get_tags(self):
        tags = {}
        articles = Article.objects.published().filter(
            app_config=self.app_config)
        for article in articles:
            for tag in article.tags.all():
                if tag.id in tags:
                    tags[tag.id].count += 1
                else:
                    tag.count = 1
                    tags[tag.id] = tag
        # Return most frequently used tags first
        return sorted(tags.values(), key=lambda x: x.count, reverse=True)

@receiver(post_save)
def update_seach_index(sender, instance, **kwargs):
    """
    Upon detecting changes in a plugin used in an Article's content
    (PlaceholderField), update the article's search_index so that we can
    perform simple searches even without Haystack, etc.
    """
    if issubclass(instance.__class__, CMSPlugin):
        placeholder = instance._placeholder_cache
        if hasattr(placeholder, '_attached_model_cache'):
            if placeholder._attached_model_cache == Article:
                article = placeholder._attached_model_cache.objects.get(content=placeholder.id)
                article.search_data = article.get_search_data(instance.language)
                article.save()
<commit_msg>Whitespace re-arrange imports<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals

from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
try:
    from django.utils.encoding import force_unicode
except ImportError:
    from django.utils.encoding import force_text as force_unicode
from django.utils.encoding import python_2_unicode_compatible
from django.utils.text import slugify as default_slugify
from django.utils.timezone import now
from django.utils.translation import get_language, ugettext_lazy as _

from aldryn_categories.fields import CategoryManyToManyField
from aldryn_categories.models import Category
from aldryn_people.models import Person
from aldryn_reversion.core import version_controlled_content

from cms.models.fields import PlaceholderField
from cms.models.pluginmodel import CMSPlugin

from djangocms_text_ckeditor.fields import HTMLField
from filer.fields.image import FilerImageField
from parler.models import TranslatableModel, TranslatedFields
from sortedm2m.fields import SortedManyToManyField
from taggit.managers import TaggableManager

from .cms_appconfig import NewsBlogConfig
from .managers import RelatedManager
from .utils import strip_tags, get_plugin_index_data, get_request

import django.core.validators

if settings.LANGUAGES:
    LANGUAGE_CODES = [language[0] for language in settings.LANGUAGES]
elif settings.LANGUAGE:
    LANGUAGE_CODES = [settings.LANGUAGE]
else:
    raise ImproperlyConfigured(
        'Neither LANGUAGES nor LANGUAGE was found in settings.')

@python_2_unicode_compatible
@version_controlled_content
class Article(TranslatableModel):
    translations = TranslatedFields(
        title=models.CharField(_('title'), max_length=234),
        slug=models.SlugField(
            verbose_name=_('slug'),
            max_length=255,
            db_index=True,
            blank=True,
            help_text=_(
                'Used in the URL. If changed, the URL will change. '
                'Clear it to have it re-created automatically.'),
        ),
        lead_in=HTMLField(
            verbose_name=_('Optional lead-in'), default='',
            help_text=_('Will be displayed in lists, and at the start of the '
                        'detail page (in bold)'),
            blank=True,
        ),
        meta_title=models.CharField(
            max_length=255, verbose_name=_('meta title'),
            blank=True, default=''),
        meta_description=models.TextField(
            verbose_name=_('meta description'), blank=True, default=''),
        meta_keywords=models.TextField(
            verbose_name=_('meta keywords'), blank=True, default=''),
        meta={'unique_together': (('language_code', 'slug', ), )},

        search_data=models.TextField(blank=True, editable=False)
    )

    content = PlaceholderField('newsblog_article_content',
                               related_name='newsblog_article_content')
    author = models.ForeignKey(Person, null=True, blank=True,
                               verbose_name=_('author'))
    owner = models.ForeignKey(User, verbose_name=_('owner'))
    app_config = models.ForeignKey(NewsBlogConfig,
                                   verbose_name=_('app. config'))
    categories = CategoryManyToManyField('aldryn_categories.Category',
                                         verbose_name=_('categories'),
                                         blank=True)
    publishing_date = models.DateTimeField(_('publishing date'),
                                           default=now())
    is_published = models.BooleanField(_('is published'), default=True,
                                       db_index=True)
    is_featured = models.BooleanField(_('is featured'), default=False,
                                      db_index=True)
    featured_image = FilerImageField(null=True, blank=True)

    tags = TaggableManager(blank=True)

    related = SortedManyToManyField('self', verbose_name=_('related articles'),
                                    blank=True)

    objects = RelatedManager()

    class Meta:
        ordering = ['-publishing_date']

    def __str__(self):
        return self.safe_translation_getter('title', any_language=True)

    def get_absolute_url(self):
        #
        # NB: It is important that this is safe to run even when the user has
        # not created the apphook yet, as some user work-flows involve creating
        # articles before the page exists.
        #
        try:
            return reverse(
                '{namespace}:article-detail'.format(
                    namespace=self.app_config.namespace
                ), kwargs={
                    'slug': self.safe_translation_getter('slug', any_language=True)
                }
            )
        except:
            return ''  # Note NOT None here

    def slugify(self, source_text, i=None):
        slug = default_slugify(source_text)
        if i is not None:
            slug += "_%d" % i
        return slug

    def get_search_data(self, language=None, request=None):
        """
        Provides an index for use with Haystack, or, for populating
        Article.translations.search_data.
        """
        if not self.pk:
            return ''
        if language is None:
            language = get_language()
        if request is None:
            request = get_request(language=language)
        description = self.safe_translation_getter('lead_in')
        text_bits = [strip_tags(description)]
        for category in self.categories.all():
            text_bits.append(
                force_unicode(category.safe_translation_getter('name')))
        for tag in self.tags.all():
            text_bits.append(force_unicode(tag.name))
        if self.content:
            plugins = self.content.cmsplugin_set.filter(language=language)
            for base_plugin in plugins:
                plugin_text_content = ' '.join(
                    get_plugin_index_data(base_plugin, request))
                text_bits.append(plugin_text_content)
        return ' '.join(text_bits)

    def save(self, *args, **kwargs):
        # Update the search index
        self.search_data = self.get_search_data()

        # Ensure there is an owner.
        if self.app_config.create_authors and self.author is None:
            self.author = Person.objects.get_or_create(
                user=self.owner,
                defaults={
                    'name': u' '.join((self.owner.first_name,
                                       self.owner.last_name))
                })[0]

        # Start with a naïve approach, if none provided.
        if not self.slug:
            self.slug = default_slugify(self.title)

        # Ensure we aren't colliding with an existing slug *for this language*.
        if Article.objects.translated(
                slug=self.slug).exclude(id=self.id).count() == 0:
            return super(Article, self).save(*args, **kwargs)

        for lang in LANGUAGE_CODES:
            #
            # We'd much rather just do something like:
            # Article.objects.translated(lang,
            # slug__startswith=self.slug)
            # But sadly, this isn't supported by Parler/Django, see:
            # http://django-parler.readthedocs.org/en/latest/api/\
            # parler.managers.html#the-translatablequeryset-class
            #
            slugs = []
            all_slugs = Article.objects.language(lang).exclude(
                id=self.id).values_list('translations__slug', flat=True)
            for slug in all_slugs:
                if slug and slug.startswith(self.slug):
                    slugs.append(slug)
            i = 1
            while True:
                slug = self.slugify(self.title, i)
                if slug not in slugs:
                    self.slug = slug
                    return super(Article, self).save(*args, **kwargs)
                i += 1

class NewsBlogCMSPlugin(CMSPlugin):
    """AppHookConfig aware abstract CMSPlugin class for Aldryn Newsblog"""
    # avoid reverse relation name clashes by not adding a related_name
    # to the parent plugin
    cmsplugin_ptr = models.OneToOneField(
        CMSPlugin, related_name='+', parent_link=True)

    app_config = models.ForeignKey(NewsBlogConfig)

    def copy_relations(self, old_instance):
        self.app_config = old_instance.app_config

    class Meta:
        abstract = True

@python_2_unicode_compatible
class NewsBlogArchivePlugin(NewsBlogCMSPlugin):
    def __str__(self):
        return _('%s archive') % (self.app_config.get_app_title(), )

class NewsBlogArticleSearchPlugin(NewsBlogCMSPlugin):
    max_articles = models.PositiveIntegerField(
        _('max articles'), default=10,
        validators=[django.core.validators.MinValueValidator(1)],
        help_text=_('The maximum number of found articles display.')
    )

    def __str__(self):
        return _('%s archive') % (self.app_config.get_app_title(), )

@python_2_unicode_compatible
class NewsBlogAuthorsPlugin(NewsBlogCMSPlugin):
    def __str__(self):
        return _('%s authors') % (self.app_config.get_app_title(), )

    def get_authors(self):
        author_list = Article.objects.published().filter(
            app_config=self.app_config).values_list('author',
                                                    flat=True).distinct()
        author_list = list(author_list)
        qs = Person.objects.filter(
            id__in=author_list, article__app_config=self.app_config
        ).annotate(count=models.Count('article'))
        return qs

@python_2_unicode_compatible
class NewsBlogCategoriesPlugin(NewsBlogCMSPlugin):
    def __str__(self):
        return _('%s categories') % (self.app_config.get_app_title(), )

    def get_categories(self):
        category_list = Article.objects.published().filter(
            app_config=self.app_config).values_list('categories',
                                                    flat=True).distinct()
        category_list = list(category_list)
        qs = Category.objects.filter(
            id__in=category_list,
            article__app_config=self.app_config,
        ).annotate(count=models.Count('article')).order_by('-count')
        return qs

@python_2_unicode_compatible
class NewsBlogFeaturedArticlesPlugin(NewsBlogCMSPlugin):
    article_count = models.PositiveIntegerField(
        default=1,
        validators=[django.core.validators.MinValueValidator(1)],
        help_text=_('The maximum number of featured articles display.')
    )

    def get_articles(self):
        if not self.article_count:
            return Article.objects.none()
        articles = Article.objects.published().active_translations(
            get_language()
        ).filter(
            app_config=self.app_config,
            is_featured=True
        )
        return articles[:self.article_count]

    def __str__(self):
        if not self.pk:
            return 'featured articles'
        prefix = self.app_config.get_app_title()
        if self.article_count == 1:
            title = _('featured article')
        else:
            title = _('featured articles: %(count)s') % {
                'count': self.article_count,
            }
        return '{0} {1}'.format(prefix, title)

@python_2_unicode_compatible
class NewsBlogLatestArticlesPlugin(NewsBlogCMSPlugin):
    latest_articles = models.IntegerField(
        default=5,
        help_text=_('The maximum number of latest articles to display.')
    )

    def __str__(self):
        return _('%s latest articles: %s') % (
            self.app_config.get_app_title(), self.latest_articles, )

    def get_articles(self):
        articles = Article.objects.published().active_translations(
            get_language()).filter(app_config=self.app_config)
        return articles[:self.latest_articles]

@python_2_unicode_compatible
class NewsBlogRelatedPlugin(CMSPlugin):
    # NOTE: This one does NOT subclass NewsBlogCMSPlugin
    cmsplugin_ptr = models.OneToOneField(
        CMSPlugin, related_name='+', parent_link=True)

    def __str__(self):
        return _('Related articles')

@python_2_unicode_compatible
class NewsBlogTagsPlugin(NewsBlogCMSPlugin):
    def __str__(self):
        return _('%s tags') % (self.app_config.get_app_title(), )

    def get_tags(self):
        tags = {}
        articles = Article.objects.published().filter(
            app_config=self.app_config)
        for article in articles:
            for tag in article.tags.all():
                if tag.id in tags:
                    tags[tag.id].count += 1
                else:
                    tag.count = 1
                    tags[tag.id] = tag
        # Return most frequently used tags first
        return sorted(tags.values(), key=lambda x: x.count, reverse=True)

@receiver(post_save)
def update_seach_index(sender, instance, **kwargs):
    """
    Upon detecting changes in a plugin used in an Article's content
    (PlaceholderField), update the article's search_index so that we can
    perform simple searches even without Haystack, etc.
    """
    if issubclass(instance.__class__, CMSPlugin):
        placeholder = instance._placeholder_cache
        if hasattr(placeholder, '_attached_model_cache'):
            if placeholder._attached_model_cache == Article:
                article = placeholder._attached_model_cache.objects.get(content=placeholder.id)
                article.search_data = article.get_search_data(instance.language)
                article.save()
<|endoftext|>


<commit_before>import urllib

from buildbot.status import builder
from buildbot.status.web.base import HtmlResource

class StatsBuilderStatusResource(HtmlResource):
  def __init__(self, builder_status):
    HtmlResource.__init__(self)
    self.builder_status = builder_status

  def getBuilderVariables(self, cxt, maxBuilds):
    # 1. build time over <maxBuilds> builds
    # 2. pie chart, success failures
    # 3. pie chart, which steps fails the most
    # 4. 1 graph per step showing last <maxBuilds> builds
    # click on a graph makes it go bigger.
    buildTimes = []
    stepTimes = {}
    numberOfSuccess = 0
    numberOfFailures = 0
    failingSteps = {}

    for build in self.builder_status.generateFinishedBuilds(
        max_search=maxBuilds):
      buildNum = build.getNumber()
      if build.getResults() == builder.SUCCESS or \
         build.getResults() == builder.WARNINGS:
        (start, end) = build.getTimes()
        buildTimes.append((buildNum, float(end-start) / 60.0))
        numberOfSuccess += 1
      else:
        numberOfFailures += 1

      for step in build.getSteps():
        stepName = step.getName().translate(None, '- /[]{}():.,')
        stepTime = stepTimes.setdefault(stepName, [])
        failCount = failingSteps.setdefault(stepName, 0)
        (result, output) = step.getResults()
        if result == builder.SUCCESS or result == builder.WARNINGS:
          (start, end) = step.getTimes()
          elapsed = end - start
          stepTime.append((buildNum, elapsed))
        if result == builder.FAILURE:
          failingSteps[stepName] = failCount + 1

    # Put data in forward order
    buildTimes.reverse()
    map(list.reverse, stepTimes.values())

    slowest = reduce(
        max, [reduce(max, [y[1] for y in x], 0) for x in stepTimes.values()], 0)
    timeRange = slowest + 1
    yTicks = '[%s]' % ', '.join(["{v:%d}" % i for i in range(int(timeRange+1))])

    cxt['builder_status'] = self.builder_status
    cxt['buildTimes'] = buildTimes
    cxt['failingSteps'] = failingSteps
    cxt['stepTimes'] = stepTimes
    cxt['numberOfSuccess'] = numberOfSuccess
    cxt['numberOfFailures'] = numberOfFailures
    cxt['yTicks'] = yTicks
    cxt['timeRange'] = timeRange
    cxt['colorMap'] = { 'compile': 1, 'update': 1 };
    cxt['data_in_forward_order'] = '1'

  def content(self, request, cxt):
    maxBuilds = int(request.args.get(
        'max', [self.builder_status.buildCacheSize/2])[0])
    self.getBuilderVariables(cxt, maxBuilds)
    templates = request.site.buildbot_service.templates
    template = templates.get_template("builder_stats.html")
    return template.render(cxt)

class StatsStatusResource(HtmlResource):
  def __init__(self, allowForce=True, css=None):
    HtmlResource.__init__(self)

    self.status = None
    self.control = None
    self.changemaster = None
    self.allowForce = allowForce
    self.css = css

  def getMainVariables(self, status, cxt, maxBuilds=None):
    builderNames = []
    builderTimes = []
    builderFailures = []

    for builderName in status.getBuilderNames():
      builderNames.append(builderName)
      builderObj = status.getBuilder(builderName)
      if maxBuilds is None:
        builderMaxBuilds = builderObj.buildCacheSize/2
      else:
        builderMaxBuilds = maxBuilds

      goodCount = 0
      badCount = 0
      buildTimes = []

      for build in builderObj.generateFinishedBuilds(
          max_search=builderMaxBuilds):
        buildNum = build.getNumber()
        if (build.getResults() == builder.SUCCESS or
            build.getResults() == builder.WARNINGS):
          (start, end) = build.getTimes()
          buildTimes.append(end - start)
          goodCount += 1
        else:
          badCount += 1

      # Get the average time per build in minutes
      avg = float(sum(buildTimes)) / float(max(len(buildTimes), 1))
      builderTimes.append(avg / 60.0)

      # Get the proportion of failed builds.
      avg = float(badCount) / float(max(goodCount + badCount, 1))
      builderFailures.append(avg)

    cxt['builderNames'] = builderNames
    cxt['builderTimes'] = builderTimes
    cxt['builderFailures'] = builderFailures

  def content(self, request, cxt):
    try:
      maxBuilds = int(request.args.get('max')[0])
    except (TypeError, ValueError):
      maxBuilds = None
    self.getMainVariables(self.getStatus(request), cxt, maxBuilds=maxBuilds)
    templates = request.site.buildbot_service.templates
    template = templates.get_template('stats.html')
    return template.render(cxt)

  def getChild(self, path, req):
    return StatsBuilderStatusResource(self.getStatus(req).getBuilder(path))
<commit_msg>Eliminate more special characters from javascript variable names.<commit_after>import urllib

from buildbot.status import builder
from buildbot.status.web.base import HtmlResource

class StatsBuilderStatusResource(HtmlResource):
  def __init__(self, builder_status):
    HtmlResource.__init__(self)
    self.builder_status = builder_status

  def getBuilderVariables(self, cxt, maxBuilds):
    # 1. build time over <maxBuilds> builds
    # 2. pie chart, success failures
    # 3. pie chart, which steps fails the most
    # 4. 1 graph per step showing last <maxBuilds> builds
    # click on a graph makes it go bigger.
    buildTimes = []
    stepTimes = {}
    numberOfSuccess = 0
    numberOfFailures = 0
    failingSteps = {}

    for build in self.builder_status.generateFinishedBuilds(
        max_search=maxBuilds):
      buildNum = build.getNumber()
      if build.getResults() == builder.SUCCESS or \
         build.getResults() == builder.WARNINGS:
        (start, end) = build.getTimes()
        buildTimes.append((buildNum, float(end-start) / 60.0))
        numberOfSuccess += 1
      else:
        numberOfFailures += 1

      for step in build.getSteps():
        stepName = step.getName().translate(None, '- /[]{}():.,\'"')
        stepTime = stepTimes.setdefault(stepName, [])
        failCount = failingSteps.setdefault(stepName, 0)
        (result, output) = step.getResults()
        if result == builder.SUCCESS or result == builder.WARNINGS:
          (start, end) = step.getTimes()
          elapsed = end - start
          stepTime.append((buildNum, elapsed))
        if result == builder.FAILURE:
          failingSteps[stepName] = failCount + 1

    # Put data in forward order
    buildTimes.reverse()
    map(list.reverse, stepTimes.values())

    slowest = reduce(
        max, [reduce(max, [y[1] for y in x], 0) for x in stepTimes.values()], 0)
    timeRange = slowest + 1
    yTicks = '[%s]' % ', '.join(["{v:%d}" % i for i in range(int(timeRange+1))])

    cxt['builder_status'] = self.builder_status
    cxt['buildTimes'] = buildTimes
    cxt['failingSteps'] = failingSteps
    cxt['stepTimes'] = stepTimes
    cxt['numberOfSuccess'] = numberOfSuccess
    cxt['numberOfFailures'] = numberOfFailures
    cxt['yTicks'] = yTicks
    cxt['timeRange'] = timeRange
    cxt['colorMap'] = { 'compile': 1, 'update': 1 };
    cxt['data_in_forward_order'] = '1'

  def content(self, request, cxt):
    maxBuilds = int(request.args.get(
        'max', [self.builder_status.buildCacheSize/2])[0])
    self.getBuilderVariables(cxt, maxBuilds)
    templates = request.site.buildbot_service.templates
    template = templates.get_template("builder_stats.html")
    return template.render(cxt)

class StatsStatusResource(HtmlResource):
  def __init__(self, allowForce=True, css=None):
    HtmlResource.__init__(self)

    self.status = None
    self.control = None
    self.changemaster = None
    self.allowForce = allowForce
    self.css = css

  def getMainVariables(self, status, cxt, maxBuilds=None):
    builderNames = []
    builderTimes = []
    builderFailures = []

    for builderName in status.getBuilderNames():
      builderNames.append(builderName)
      builderObj = status.getBuilder(builderName)
      if maxBuilds is None:
        builderMaxBuilds = builderObj.buildCacheSize/2
      else:
        builderMaxBuilds = maxBuilds

      goodCount = 0
      badCount = 0
      buildTimes = []

      for build in builderObj.generateFinishedBuilds(
          max_search=builderMaxBuilds):
        buildNum = build.getNumber()
        if (build.getResults() == builder.SUCCESS or
            build.getResults() == builder.WARNINGS):
          (start, end) = build.getTimes()
          buildTimes.append(end - start)
          goodCount += 1
        else:
          badCount += 1

      # Get the average time per build in minutes
      avg = float(sum(buildTimes)) / float(max(len(buildTimes), 1))
      builderTimes.append(avg / 60.0)

      # Get the proportion of failed builds.
      avg = float(badCount) / float(max(goodCount + badCount, 1))
      builderFailures.append(avg)

    cxt['builderNames'] = builderNames
    cxt['builderTimes'] = builderTimes
    cxt['builderFailures'] = builderFailures

  def content(self, request, cxt):
    try:
      maxBuilds = int(request.args.get('max')[0])
    except (TypeError, ValueError):
      maxBuilds = None
    self.getMainVariables(self.getStatus(request), cxt, maxBuilds=maxBuilds)
    templates = request.site.buildbot_service.templates
    template = templates.get_template('stats.html')
    return template.render(cxt)

  def getChild(self, path, req):
    return StatsBuilderStatusResource(self.getStatus(req).getBuilder(path))
<|endoftext|>


<commit_before>import os
import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

class ExampleTest(unittest.TestCase):

    def setUp(self):
        if (os.environ.get('TRAVIS_SECURE_ENV_VARS'):
            self.capabilities['tunnel-identifier'] = os.environ['TRAVIS_JOB_NUMBER']
            self.capabilities['build'] = os.environ['TRAVIS_BUILD_NUMBER']
            self.capabilities['browserName'] = 'chrome'
            self.capabilities['platform'] = 'LINUX'

        self.name = 'Example test!'
        self.url = 'http://localhost:8000'
        self.username = os.environ['SAUCE_USERNAME']
        self.key = os.environ['SAUCE_ACCESS_KEY']
        hub_url = "%s%s@localhost:4445" % (self.username, self.key)
        self.driver = webdriver.Remote(desired_capabilities=self.capabilities,
                                       command_executor = "http://%s/wd/hub" % hub_url)
        self.jobid = self.driver.session_id
        print "Sauce Labs job: https://saucelabs.com/jobs/%s" % self.jobid
        self.driver.implicitly_wait(30)

    def example_test(self):
        self.driver.get(self.url)
        toc_link_1005_1 = self.driver.find_element_by_xpath('//*[@id="toc"]/ol/li[1]/a')
        self.assertEquals(toc_link_1005_1.get_attribute('data-section-id'), '1005-1')

    def tearDown(self):
        self.driver.quit()
        self.report_test_result()
<commit_msg>hopefully triggering travis on my fork<commit_after>import os
import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

class ExampleTest(unittest.TestCase):

    def setUp(self):
        if (os.environ.get('TRAVIS_SECURE_ENV_VARS'):
            self.capabilities['tunnel-identifier'] = os.environ['TRAVIS_JOB_NUMBER']
            self.capabilities['build'] = os.environ['TRAVIS_BUILD_NUMBER']
            self.capabilities['browserName'] = 'chrome'
            self.capabilities['platform'] = 'LINUX'

        self.name = 'Example test!!'
        self.url = 'http://localhost:8000'
        self.username = os.environ['SAUCE_USERNAME']
        self.key = os.environ['SAUCE_ACCESS_KEY']
        hub_url = "%s%s@localhost:4445" % (self.username, self.key)
        self.driver = webdriver.Remote(desired_capabilities=self.capabilities,
                                       command_executor = "http://%s/wd/hub" % hub_url)
        self.jobid = self.driver.session_id
        print "Sauce Labs job: https://saucelabs.com/jobs/%s" % self.jobid
        self.driver.implicitly_wait(30)

    def example_test(self):
        self.driver.get(self.url)
        toc_link_1005_1 = self.driver.find_element_by_xpath('//*[@id="toc"]/ol/li[1]/a')
        self.assertEquals(toc_link_1005_1.get_attribute('data-section-id'), '1005-1')

    def tearDown(self):
        self.driver.quit()
        self.report_test_result()
<|endoftext|>


<commit_before>
from VisualisationPlugin import VisualisationPlugin

import pygame
import logging

from DDRPi import FloorCanvas

class LoveHeartVisualisationPlugin(VisualisationPlugin):

	logger = logging.getLogger(__name__)

	def __init__(self):
		self.clock = pygame.time.Clock()

	# Nothing specific to be done before this starts, although we could
	#  set self.clock here. Stash any config so we can use it later
	def configure(self, config):
		self.config = config

		self.pulse_rate = 2000
		self.pulse_increasing = 1
		self.pulse_last_ratio = 0

		self.upper_name = ""
		self.lower_name = ""

		if config is not None:

			try:
				self.upper_name = "%s" % self.config["upper_name"]
			except (ValueError, KeyError):
				pass

			try:
				self.lower_name = "%s" % self.config["lower_name"]
			except (ValueError, KeyError):
				pass

		self.logger.info("Config: %s" % config)

	# This will just keep running, nothing specific to do for start(), stop(),
	#  pause() or resume()
	def start(self):
		pass

	def stop(self):
		pass

	def pause(self):
		pass

	def resume(self):
		pass

	def draw_frame(self, canvas):

		w = canvas.get_width()
		h = canvas.get_height()

		canvas.set_colour(FloorCanvas.BLACK)

		has_text = False
		if (self.upper_name is not None and self.upper_name != ""):
			self.draw_text(canvas, self.upper_name, 0, 0)
			has_text = True
		if (self.lower_name is not None and self.lower_name != ""):
			self.draw_text(canvas, self.lower_name, 0, 11)
			has_text = True

		# Drawing entirely black doesn't work very well, so draw a border
		#  if there are no names drawn
		if has_text is False:
			#Draw a border
			canvas.draw_line(0,0,0,h-1, FloorCanvas.WHITE)
			canvas.draw_line(0,h-1,w-1,h-1, FloorCanvas.WHITE)
			canvas.draw_line(w-1,h-1,w-1,0, FloorCanvas.WHITE)
			canvas.draw_line(w-1,0,0,0, FloorCanvas.WHITE)

		# Calculate the red value for the heart's centre
		ratio = int(255.0 * (float(pygame.time.get_ticks() % self.pulse_rate) / float(self.pulse_rate)))

		# Increase then decrease the value
		self.pulse_increasing = 1
		pulse_mod = pygame.time.get_ticks() % (2*self.pulse_rate)

		# Calculate which
		if (pygame.time.get_ticks() % (2*self.pulse_rate) > self.pulse_rate):
			self.pulse_increasing = -1

		# Work out the red value
		red_value = ratio
		if (self.pulse_increasing == -1):
			red_value = 255 - ratio

		# Draw the fading heart...
		self.draw_heart(canvas, (red_value, 0x00, 0x00), w/2 -4, h/2 - 2, 1)
		# .. and a solid outline
		self.draw_heart(canvas, (0xFF, 0x00, 0x00), w/2 -4, h/2 - 2, 0)

		# Limit the frame rate.
		# This sleeps so that at least 25ms has passed since tick()
		#  was last called. It is a no-op if the loop is running slow
		self.clock.tick(25)
		# Draw whatever this plugin does.
		# We need to return our decorated surface
		return canvas

	def draw_splash(self,canvas):

		canvas.set_colour(FloorCanvas.BLACK)

		# Draw a solid red heart in the middle (ish)
		canvas = self.draw_heart(canvas, (0xFF, 0x00, 0x00), canvas.get_width()//2 -4, canvas.get_height()//2 - 2, 1)
		return canvas

	def draw_heart(self, canvas, colour, x_pos, y_pos, fill):

		heart = (0x06, 0x09, 0x11, 0x22, 0x11, 0x09, 0x06);
		if (fill > 0):
			heart = (0x06, 0x0F, 0x1F, 0x3E, 0x1F, 0x0F, 0x06);
		heart_height = 6
		heart_width = len(heart)

		for x in range(0, heart_width):
			for y in range(0, heart_height):
				pixel_value = (heart[x] >> y) & 0x01
				if (pixel_value == 1):
					canvas.set_pixel(x+x_pos,y+y_pos, colour)

	def draw_text(self, canvas, text, x, y):

		if text == "Phil":
			self.draw_phil(canvas, x, y)
		elif text == "Naomi":
			self.draw_naomi(canvas, x, y)
		else:
			canvas.draw_text(self.upper_name, (0xFF,0xFF,0xFF), x, y)

		return canvas

	def draw_phil(self, canvas, x, y) :

		# To make it central
		offset = 4

		canvas.draw_text("P", (0xFF,0xFF,0xFF), x+offset, y)

		# Use the narrow i that we have to use for Naomi, and a h
		custom_text = {"h": (0x7F, 0x08, 0x04, 0x78, 0), "i": (0x7D, 0, 0, 0 ,0), "l": (0x7F, 0,0,0,0),}
		canvas.draw_text("h", (0xFF,0xFF,0xFF), x+offset+6, y, custom_text)
		canvas.draw_text("i", (0xFF,0xFF,0xFF), x+offset+11, y, custom_text)
		canvas.draw_text("l", (0xFF,0xFF,0xFF), x+offset+13, y, custom_text)

		return canvas

	def draw_naomi(self, canvas, x, y) :

		offset = 0
		canvas.draw_text("N", (0xFF,0xFF,0xFF), x+offset, y)

		# Hand craft a narrower a & o
		custom_text = {"o": (0x38, 0x44, 0x44, 0x38, 0x00), "a": (0x20, 0x54, 0x54, 0x78, 0x00), "i": (0x7D, 0, 0, 0 ,0)}
		canvas.draw_text("a", (0xFF,0xFF,0xFF), x+offset+6, y, custom_text)
		canvas.draw_text("o", (0xFF,0xFF,0xFF), x+offset+11, y, custom_text)
		canvas.draw_text("m", (0xFF,0xFF,0xFF), x+offset+16, y)
		canvas.draw_text("i", (0xFF,0xFF,0xFF), x+offset+22, y, custom_text)

		return canvas

<commit_msg>Fixed always writing the upper name for the LoveHeart<commit_after>
from VisualisationPlugin import VisualisationPlugin

import pygame
import logging

from DDRPi import FloorCanvas

class LoveHeartVisualisationPlugin(VisualisationPlugin):

	logger = logging.getLogger(__name__)

	def __init__(self):
		self.clock = pygame.time.Clock()

	# Nothing specific to be done before this starts, although we could
	#  set self.clock here. Stash any config so we can use it later
	def configure(self, config):
		self.config = config

		self.pulse_rate = 2000
		self.pulse_increasing = 1
		self.pulse_last_ratio = 0

		self.upper_name = ""
		self.lower_name = ""

		if config is not None:

			try:
				self.upper_name = "%s" % self.config["upper_name"]
			except (ValueError, KeyError):
				pass

			try:
				self.lower_name = "%s" % self.config["lower_name"]
			except (ValueError, KeyError):
				pass

		self.logger.info("Config: %s" % config)

	# This will just keep running, nothing specific to do for start(), stop(),
	#  pause() or resume()
	def start(self):
		pass

	def stop(self):
		pass

	def pause(self):
		pass

	def resume(self):
		pass

	def draw_frame(self, canvas):

		w = canvas.get_width()
		h = canvas.get_height()

		canvas.set_colour(FloorCanvas.BLACK)

		has_text = False
		if (self.upper_name is not None and self.upper_name != ""):
			self.draw_text(canvas, self.upper_name, 0, 0)
			has_text = True
		if (self.lower_name is not None and self.lower_name != ""):
			self.draw_text(canvas, self.lower_name, 0, 11)
			has_text = True

		# Drawing entirely black doesn't work very well, so draw a border
		#  if there are no names drawn
		if has_text is False:
			#Draw a border
			canvas.draw_line(0,0,0,h-1, FloorCanvas.WHITE)
			canvas.draw_line(0,h-1,w-1,h-1, FloorCanvas.WHITE)
			canvas.draw_line(w-1,h-1,w-1,0, FloorCanvas.WHITE)
			canvas.draw_line(w-1,0,0,0, FloorCanvas.WHITE)

		# Calculate the red value for the heart's centre
		ratio = int(255.0 * (float(pygame.time.get_ticks() % self.pulse_rate) / float(self.pulse_rate)))

		# Increase then decrease the value
		self.pulse_increasing = 1
		pulse_mod = pygame.time.get_ticks() % (2*self.pulse_rate)

		# Calculate which
		if (pygame.time.get_ticks() % (2*self.pulse_rate) > self.pulse_rate):
			self.pulse_increasing = -1

		# Work out the red value
		red_value = ratio
		if (self.pulse_increasing == -1):
			red_value = 255 - ratio

		# Draw the fading heart...
		self.draw_heart(canvas, (red_value, 0x00, 0x00), w/2 -4, h/2 - 2, 1)
		# .. and a solid outline
		self.draw_heart(canvas, (0xFF, 0x00, 0x00), w/2 -4, h/2 - 2, 0)

		# Limit the frame rate.
		# This sleeps so that at least 25ms has passed since tick()
		#  was last called. It is a no-op if the loop is running slow
		self.clock.tick(25)
		# Draw whatever this plugin does.
		# We need to return our decorated surface
		return canvas

	def draw_splash(self,canvas):

		canvas.set_colour(FloorCanvas.BLACK)

		# Draw a solid red heart in the middle (ish)
		canvas = self.draw_heart(canvas, (0xFF, 0x00, 0x00), canvas.get_width()//2 -4, canvas.get_height()//2 - 2, 1)
		return canvas

	def draw_heart(self, canvas, colour, x_pos, y_pos, fill):

		heart = (0x06, 0x09, 0x11, 0x22, 0x11, 0x09, 0x06);
		if (fill > 0):
			heart = (0x06, 0x0F, 0x1F, 0x3E, 0x1F, 0x0F, 0x06);
		heart_height = 6
		heart_width = len(heart)

		for x in range(0, heart_width):
			for y in range(0, heart_height):
				pixel_value = (heart[x] >> y) & 0x01
				if (pixel_value == 1):
					canvas.set_pixel(x+x_pos,y+y_pos, colour)

	def draw_text(self, canvas, text, x, y):

		if text == "Phil":
			self.draw_phil(canvas, x, y)
		elif text == "Naomi":
			self.draw_naomi(canvas, x, y)
		else:
			canvas.draw_text(text, (0xFF,0xFF,0xFF), x, y)

		return canvas

	def draw_phil(self, canvas, x, y) :

		# To make it central
		offset = 4

		canvas.draw_text("P", (0xFF,0xFF,0xFF), x+offset, y)

		# Use the narrow i that we have to use for Naomi, and a h
		custom_text = {"h": (0x7F, 0x08, 0x04, 0x78, 0), "i": (0x7D, 0, 0, 0 ,0), "l": (0x7F, 0,0,0,0),}
		canvas.draw_text("h", (0xFF,0xFF,0xFF), x+offset+6, y, custom_text)
		canvas.draw_text("i", (0xFF,0xFF,0xFF), x+offset+11, y, custom_text)
		canvas.draw_text("l", (0xFF,0xFF,0xFF), x+offset+13, y, custom_text)

		return canvas

	def draw_naomi(self, canvas, x, y) :

		offset = 0
		canvas.draw_text("N", (0xFF,0xFF,0xFF), x+offset, y)

		# Hand craft a narrower a & o
		custom_text = {"o": (0x38, 0x44, 0x44, 0x38, 0x00), "a": (0x20, 0x54, 0x54, 0x78, 0x00), "i": (0x7D, 0, 0, 0 ,0)}
		canvas.draw_text("a", (0xFF,0xFF,0xFF), x+offset+6, y, custom_text)
		canvas.draw_text("o", (0xFF,0xFF,0xFF), x+offset+11, y, custom_text)
		canvas.draw_text("m", (0xFF,0xFF,0xFF), x+offset+16, y)
		canvas.draw_text("i", (0xFF,0xFF,0xFF), x+offset+22, y, custom_text)

		return canvas

<|endoftext|>


<commit_before>__version__ = '0.2.4'
<commit_msg>version bump to 0.2.5<commit_after>__version__ = '0.2.5'
<|endoftext|>


<commit_before># Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# 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.

import argparse
import json
import logging
import os
import subprocess
import sys
import tempfile

from pystache import context

from os_apply_config import config_exception as exc
from os_apply_config import renderers
from os_apply_config import value_types

TEMPLATES_DIR = os.environ.get('OS_CONFIG_APPLIER_TEMPLATES', None)
if TEMPLATES_DIR is None:
    TEMPLATES_DIR = '/opt/stack/os-apply-config/templates'
    if not os.path.isdir(TEMPLATES_DIR):
        # Backwards compat with the old name.
        TEMPLATES_DIR = '/opt/stack/os-config-applier/templates'

def install_config(config_path, template_root,
                   output_path, validate, subhash=None):
    config = strip_hash(read_config(config_path), subhash)
    tree = build_tree(template_paths(template_root), config)
    if not validate:
        for path, contents in tree.items():
            write_file(os.path.join(
                output_path, strip_prefix('/', path)), contents)

def print_key(config_path, key, type_name, default=None):
    config = read_config(config_path)
    keys = key.split('.')
    for key in keys:
        try:
            config = config[key]
        except KeyError:
            if default is not None:
                print default
                return
            else:
                raise exc.ConfigException(
                    'key %s does not exist in %s' % (key, config_path))
    value_types.ensure_type(config, type_name)
    print config

def write_file(path, contents):
    logger.info("writing %s", path)
    d = os.path.dirname(path)
    os.path.exists(d) or os.makedirs(d)
    with tempfile.NamedTemporaryFile(dir=d, delete=False) as newfile:
        newfile.write(contents)
        os.chmod(newfile.name, 0644)
        os.rename(newfile.name, path)

# return a map of filenames->filecontents

def build_tree(templates, config):
    res = {}
    for in_file, out_file in templates:
        res[out_file] = render_template(in_file, config)
    return res

def render_template(template, config):
    if is_executable(template):
        return render_executable(template, config)
    else:
        try:
            return render_moustache(open(template).read(), config)
        except context.KeyNotFoundError as e:
            raise exc.ConfigException(
                "key '%s' from template '%s' does not exist in metadata file."
                % (e.key, template))
        except Exception as e:
            raise exc.ConfigException(
                "could not render moustache template %s" % template)

def is_executable(path):
    return os.path.isfile(path) and os.access(path, os.X_OK)

def render_moustache(text, config):
    r = renderers.JsonRenderer(missing_tags='ignore')
    return r.render(text, config)

def render_executable(path, config):
    p = subprocess.Popen([path],
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    stdout, stderr = p.communicate(json.dumps(config))
    p.wait()
    if p.returncode != 0:
        raise exc.ConfigException(
            "config script failed: %s\n\nwith output:\n\n%s" %
            (path, stdout + stderr))
    return stdout

def read_config(paths):
    for path in paths:
        if os.path.exists(path):
            try:
                return json.loads(open(path).read())
            except Exception:
                raise exc.ConfigException("invalid metadata file: %s" % path)
    raise exc.ConfigException("No metadata found.")

def template_paths(root):
    res = []
    for cur_root, subdirs, files in os.walk(root):
        for f in files:
            inout = (os.path.join(cur_root, f), os.path.join(
                strip_prefix(root, cur_root), f))
            res.append(inout)
    return res

def strip_prefix(prefix, s):
    return s[len(prefix):] if s.startswith(prefix) else s

def strip_hash(h, keys):
    if not keys:
        return h
    for k in keys.split('.'):
        if k in h and isinstance(h[k], dict):
            h = h[k]
        else:
            raise exc.ConfigException(
                "key '%s' does not correspond to a hash in the metadata file"
                % keys)
    return h

def parse_opts(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--templates', metavar='TEMPLATE_ROOT',
                        help="""path to template root directory (default:
                        %(default)s)""",
                        default=TEMPLATES_DIR)
    parser.add_argument('-o', '--output', metavar='OUT_DIR',
                        help='root directory for output (default:%(default)s)',
                        default='/')
    parser.add_argument('-m', '--metadata', metavar='METADATA_FILE', nargs='*',
                        help='path to metadata files. First one that exists'
                        ' will be used. (default: %(default)s)',
                        default=['/var/cache/heat-cfntools/last_metadata',
                                 '/var/lib/heat-cfntools/cfn-init-data',
                                 '/var/lib/cloud/data/cfn-init-data'])
    parser.add_argument(
        '-v', '--validate', help='validate only. do not write files',
        default=False, action='store_true')
    parser.add_argument(
        '--print-templates', default=False, action='store_true',
        help='Print templates root and exit.')
    parser.add_argument('-s', '--subhash',
                        help='use the sub-hash named by this key,'
                             ' instead of the full metadata hash')
    parser.add_argument('--key', metavar='KEY', default=None,
                        help='print the specified key and exit.'
                             ' (may be used with --type and --key-default)')
    parser.add_argument('--type', default='default',
                        help='exit with error if the specified --key does not'
                             ' match type. Valid types are'
                             ' <int|default|netaddress|dsn|raw>')
    parser.add_argument('--key-default',
                        help='This option only affects running with --key.'
                             ' Print this if key is not found. This value is'
                             ' not subject to type restrictions. If --key is'
                             ' specified and no default is specified, program'
                             ' exits with an error on missing key.')
    opts = parser.parse_args(argv[1:])

    return opts

def main(argv=sys.argv):
    opts = parse_opts(argv)
    if opts.print_templates:
        print(opts.templates)
        return 0

    try:
        if opts.templates is None:
            raise exc.ConfigException('missing option --templates')

        if opts.key:
            print_key(opts.metadata,
                      opts.key,
                      opts.type,
                      opts.key_default)
        else:
            install_config(opts.metadata, opts.templates, opts.output,
                           opts.validate, opts.subhash)
            logger.info("success")
    except exc.ConfigException as e:
        logger.error(e)
        return 1
    return 0

# logging
LOG_FORMAT = '[%(asctime)s] [%(levelname)s] %(message)s'
DATE_FORMAT = '%Y/%m/%d %I:%M:%S %p'

def add_handler(logger, handler):
    handler.setFormatter(logging.Formatter(LOG_FORMAT, datefmt=DATE_FORMAT))
    logger.addHandler(handler)
logger = logging.getLogger('os-apply-config')
logger.setLevel(logging.INFO)
add_handler(logger, logging.StreamHandler())
if os.geteuid() == 0:
    add_handler(logger, logging.FileHandler('/var/log/os-apply-config.log'))
<commit_msg>Fix print statements for python 3 compatibility<commit_after># Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
#
# 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.

import argparse
import json
import logging
import os
import subprocess
import sys
import tempfile

from pystache import context

from os_apply_config import config_exception as exc
from os_apply_config import renderers
from os_apply_config import value_types

TEMPLATES_DIR = os.environ.get('OS_CONFIG_APPLIER_TEMPLATES', None)
if TEMPLATES_DIR is None:
    TEMPLATES_DIR = '/opt/stack/os-apply-config/templates'
    if not os.path.isdir(TEMPLATES_DIR):
        # Backwards compat with the old name.
        TEMPLATES_DIR = '/opt/stack/os-config-applier/templates'

def install_config(config_path, template_root,
                   output_path, validate, subhash=None):
    config = strip_hash(read_config(config_path), subhash)
    tree = build_tree(template_paths(template_root), config)
    if not validate:
        for path, contents in tree.items():
            write_file(os.path.join(
                output_path, strip_prefix('/', path)), contents)

def print_key(config_path, key, type_name, default=None):
    config = read_config(config_path)
    keys = key.split('.')
    for key in keys:
        try:
            config = config[key]
        except KeyError:
            if default is not None:
                print(str(default))
                return
            else:
                raise exc.ConfigException(
                    'key %s does not exist in %s' % (key, config_path))
    value_types.ensure_type(config, type_name)
    print(str(config))

def write_file(path, contents):
    logger.info("writing %s", path)
    d = os.path.dirname(path)
    os.path.exists(d) or os.makedirs(d)
    with tempfile.NamedTemporaryFile(dir=d, delete=False) as newfile:
        newfile.write(contents)
        os.chmod(newfile.name, 0o644)
        os.rename(newfile.name, path)

# return a map of filenames->filecontents

def build_tree(templates, config):
    res = {}
    for in_file, out_file in templates:
        res[out_file] = render_template(in_file, config)
    return res

def render_template(template, config):
    if is_executable(template):
        return render_executable(template, config)
    else:
        try:
            return render_moustache(open(template).read(), config)
        except context.KeyNotFoundError as e:
            raise exc.ConfigException(
                "key '%s' from template '%s' does not exist in metadata file."
                % (e.key, template))
        except Exception as e:
            raise exc.ConfigException(
                "could not render moustache template %s" % template)

def is_executable(path):
    return os.path.isfile(path) and os.access(path, os.X_OK)

def render_moustache(text, config):
    r = renderers.JsonRenderer(missing_tags='ignore')
    return r.render(text, config)

def render_executable(path, config):
    p = subprocess.Popen([path],
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    stdout, stderr = p.communicate(json.dumps(config))
    p.wait()
    if p.returncode != 0:
        raise exc.ConfigException(
            "config script failed: %s\n\nwith output:\n\n%s" %
            (path, stdout + stderr))
    return stdout

def read_config(paths):
    for path in paths:
        if os.path.exists(path):
            try:
                return json.loads(open(path).read())
            except Exception:
                raise exc.ConfigException("invalid metadata file: %s" % path)
    raise exc.ConfigException("No metadata found.")

def template_paths(root):
    res = []
    for cur_root, subdirs, files in os.walk(root):
        for f in files:
            inout = (os.path.join(cur_root, f), os.path.join(
                strip_prefix(root, cur_root), f))
            res.append(inout)
    return res

def strip_prefix(prefix, s):
    return s[len(prefix):] if s.startswith(prefix) else s

def strip_hash(h, keys):
    if not keys:
        return h
    for k in keys.split('.'):
        if k in h and isinstance(h[k], dict):
            h = h[k]
        else:
            raise exc.ConfigException(
                "key '%s' does not correspond to a hash in the metadata file"
                % keys)
    return h

def parse_opts(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--templates', metavar='TEMPLATE_ROOT',
                        help="""path to template root directory (default:
                        %(default)s)""",
                        default=TEMPLATES_DIR)
    parser.add_argument('-o', '--output', metavar='OUT_DIR',
                        help='root directory for output (default:%(default)s)',
                        default='/')
    parser.add_argument('-m', '--metadata', metavar='METADATA_FILE', nargs='*',
                        help='path to metadata files. First one that exists'
                        ' will be used. (default: %(default)s)',
                        default=['/var/cache/heat-cfntools/last_metadata',
                                 '/var/lib/heat-cfntools/cfn-init-data',
                                 '/var/lib/cloud/data/cfn-init-data'])
    parser.add_argument(
        '-v', '--validate', help='validate only. do not write files',
        default=False, action='store_true')
    parser.add_argument(
        '--print-templates', default=False, action='store_true',
        help='Print templates root and exit.')
    parser.add_argument('-s', '--subhash',
                        help='use the sub-hash named by this key,'
                             ' instead of the full metadata hash')
    parser.add_argument('--key', metavar='KEY', default=None,
                        help='print the specified key and exit.'
                             ' (may be used with --type and --key-default)')
    parser.add_argument('--type', default='default',
                        help='exit with error if the specified --key does not'
                             ' match type. Valid types are'
                             ' <int|default|netaddress|dsn|raw>')
    parser.add_argument('--key-default',
                        help='This option only affects running with --key.'
                             ' Print this if key is not found. This value is'
                             ' not subject to type restrictions. If --key is'
                             ' specified and no default is specified, program'
                             ' exits with an error on missing key.')
    opts = parser.parse_args(argv[1:])

    return opts

def main(argv=sys.argv):
    opts = parse_opts(argv)
    if opts.print_templates:
        print(opts.templates)
        return 0

    try:
        if opts.templates is None:
            raise exc.ConfigException('missing option --templates')

        if opts.key:
            print_key(opts.metadata,
                      opts.key,
                      opts.type,
                      opts.key_default)
        else:
            install_config(opts.metadata, opts.templates, opts.output,
                           opts.validate, opts.subhash)
            logger.info("success")
    except exc.ConfigException as e:
        logger.error(e)
        return 1
    return 0

# logging
LOG_FORMAT = '[%(asctime)s] [%(levelname)s] %(message)s'
DATE_FORMAT = '%Y/%m/%d %I:%M:%S %p'

def add_handler(logger, handler):
    handler.setFormatter(logging.Formatter(LOG_FORMAT, datefmt=DATE_FORMAT))
    logger.addHandler(handler)
logger = logging.getLogger('os-apply-config')
logger.setLevel(logging.INFO)
add_handler(logger, logging.StreamHandler())
if os.geteuid() == 0:
    add_handler(logger, logging.FileHandler('/var/log/os-apply-config.log'))
<|endoftext|>