<commit_before>#!/usr/bin/python
# Copyright (c) 2011 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.

"""A wrapper script to run layout tests on the buildbots.

Runs the run_webkit_tests.py script found in webkit/tools/layout_tests above
this script. For a complete list of command-line options, pass '--help' on the
command line.

To pass additional options to run_webkit_tests without having them interpreted
as options for this script, include them in an '--options="..."' argument. In
addition, a list of one or more tests or test directories, specified relative
to the main webkit test directory, may be passed on the command line.
"""

import optparse
import os
import sys

from common import chromium_utils
from slave import slave_utils

def layout_test(options, args):
  """Parse options and call run_webkit_tests.py, using Python from the tree."""
  build_dir = os.path.abspath(options.build_dir)

  # Disable the page heap in case it got left enabled by some previous process.
  try:
    slave_utils.SetPageHeap(build_dir, 'test_shell.exe', False)
  except chromium_utils.PathNotFound:
    # If we don't have gflags.exe, report it but don't worry about it.
    print 'Warning: Couldn\'t disable page heap, if it was already enabled.'

  webkit_tests_dir = chromium_utils.FindUpward(build_dir,
                                              'webkit', 'tools', 'layout_tests')
  run_webkit_tests = os.path.join(webkit_tests_dir, 'run_webkit_tests.py')

  slave_name = slave_utils.SlaveBuildName(build_dir)

  command = [run_webkit_tests,
             '--noshow-results',
             '--verbose',  # Verbose output is enabled to support the dashboard.
             '--full-results-html',  # To make debugging failures easier.
             '--clobber-old-results',  # Clobber test results before each run.
            ]

  if options.results_directory:
    # run_webkit_tests expects the results directory to be either an absolute
    # path (starting with '/') or relative to Debug or Release.  We expect it
    # to be relative to the build_dir, which is a parent of Debug or Release.
    # Thus if it's a relative path, we need to move it out one level.
    if not options.results_directory.startswith('/'):
      options.results_directory = os.path.join('..', options.results_directory)
    chromium_utils.RemoveDirectory(options.results_directory)
    command.extend(['--results-directory', options.results_directory])

  if options.target:
    command.extend(['--target', options.target])
  if options.platform:
    command.extend(['--platform', options.platform])

  # Build type is no longer used.
  # TODO(pamg): remove it from the buildbot config too.
  #if options.build_type:
  #  command.extend(['--build-type', options.build_type])

  if options.no_pixel_tests:
    command.append('--no-pixel-tests')
  if options.batch_size:
    command.extend(['--batch-size', options.batch_size])
  if options.run_part:
    command.extend(['--run-part', options.run_part])
  if options.builder_name:
    command.extend(['--builder-name', options.builder_name])
  if options.build_number:
    command.extend(['--build-number', options.build_number])
  command.extend(['--master-name', slave_utils.GetActiveMaster()])
  command.extend(['--build-name', slave_name])
  # On Windows, look for the target in an exact location.
  if sys.platform == 'win32':
    command.extend(['--build-directory', build_dir])
  if options.test_results_server:
    command.extend(['--test-results-server', options.test_results_server])

  if options.enable_pageheap:
    # If we're actually requesting pageheap checking, complain if we don't have
    # gflags.exe.
    slave_utils.SetPageHeap(build_dir, 'test_shell.exe', True)
    command.append('--time-out-ms=120000')

  # The list of tests is given as arguments.
  command.extend(options.options.split(' '))
  command.extend(args)

  # Nuke anything that appears to be stale chrome items in the temporary
  # directory from previous test runs (i.e.- from crashes or unittest leaks).
  chromium_utils.RemoveChromeTemporaryFiles()

  # Run the the tests
  result = slave_utils.RunPythonCommandInBuildDir(build_dir, options.target,
                                                  command)

  if options.enable_pageheap:
    slave_utils.SetPageHeap(build_dir, 'test_shell.exe', False)

  return result

def main():
  option_parser = optparse.OptionParser()
  option_parser.add_option('-o', '--results-directory', default='',
                           help='output results directory')
  option_parser.add_option('', '--build-dir', default='webkit',
                           help='path to main build directory (the parent of '
                                'the Release or Debug directory)')
  option_parser.add_option('', '--target', default='',
      help='test_shell build configuration (Release or Debug)')
  option_parser.add_option('', '--build-type', default='',
      help='build identifier, for custom results and test lists (v8 or kjs)')
  option_parser.add_option('', '--options', default='',
      help='additional options to pass to run_webkit_tests.py')
  option_parser.add_option("", "--platform", default='',
      help=("Platform value passed directly to run_webkit_tests."))
  option_parser.add_option('', '--no-pixel-tests', action='store_true',
                           default=False,
                           help='disable pixel-to-pixel PNG comparisons')
  option_parser.add_option('', '--enable-pageheap', action='store_true',
                           default=False, help='Enable page heap checking')
  option_parser.add_option("", "--batch-size",
                           default=None,
                           help=("Run a the tests in batches (n), after every "
                                 "n tests, the test shell is relaunched."))
  option_parser.add_option("", "--run-part",
                           default=None,
                           help=("Run a specified part (n:l), the nth of lth"
                                 ", of the layout tests"))
  option_parser.add_option("", "--builder-name",
                           default=None,
                           help="The name of the builder running this script.")
  option_parser.add_option("", "--build-number",
                           default=None,
                           help=("The build number of the builder running"
                                 "this script."))
  option_parser.add_option("", "--test-results-server",
                           help=("If specified, upload results json files to "
                                 "this appengine server."))
  options, args = option_parser.parse_args()

  # Disable pageheap checking except on Windows.
  if sys.platform != 'win32':
    options.enable_pageheap = False
  return layout_test(options, args)

if '__main__' == __name__:
  sys.exit(main())
<commit_msg>Modify the way we pass the --results-directory to run_webkit_tests<commit_after>#!/usr/bin/python
# Copyright (c) 2011 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.

"""A wrapper script to run layout tests on the buildbots.

Runs the run_webkit_tests.py script found in webkit/tools/layout_tests above
this script. For a complete list of command-line options, pass '--help' on the
command line.

To pass additional options to run_webkit_tests without having them interpreted
as options for this script, include them in an '--options="..."' argument. In
addition, a list of one or more tests or test directories, specified relative
to the main webkit test directory, may be passed on the command line.
"""

import optparse
import os
import sys

from common import chromium_utils
from slave import slave_utils

def layout_test(options, args):
  """Parse options and call run_webkit_tests.py, using Python from the tree."""
  build_dir = os.path.abspath(options.build_dir)

  # Disable the page heap in case it got left enabled by some previous process.
  try:
    slave_utils.SetPageHeap(build_dir, 'test_shell.exe', False)
  except chromium_utils.PathNotFound:
    # If we don't have gflags.exe, report it but don't worry about it.
    print 'Warning: Couldn\'t disable page heap, if it was already enabled.'

  webkit_tests_dir = chromium_utils.FindUpward(build_dir,
                                              'webkit', 'tools', 'layout_tests')
  run_webkit_tests = os.path.join(webkit_tests_dir, 'run_webkit_tests.py')

  slave_name = slave_utils.SlaveBuildName(build_dir)

  command = [run_webkit_tests,
             '--noshow-results',
             '--verbose',  # Verbose output is enabled to support the dashboard.
             '--full-results-html',  # To make debugging failures easier.
             '--clobber-old-results',  # Clobber test results before each run.
            ]

  if options.results_directory:
    # Prior to the fix in https://bugs.webkit.org/show_bug.cgi?id=58272,
    # run_webkit_tests expects the results directory to be relative to
    # the configuration directory (e.g., src/webkit/Release). The
    # parameter is given to us relative to build_dir, which is where we
    # will run the command from.
    #
    # When 58272 is landed, run_webkit_tests will support absolute file
    # paths as well as paths relative to CWD for non-Chromium ports and
    # paths relative to the configuration dir for Chromium ports. As
    # a transitional fix, we convert to an absolute dir, but once the
    # hack in 58272 is removed, we can use results_dir as-is.
    if not os.path.isabs(options.results_directory):
      options.results_directory = os.path.abspath(
          os.path.join(os.getcwd(), options.results_directory))
    chromium_utils.RemoveDirectory(options.results_directory)
    command.extend(['--results-directory', options.results_directory])

  if options.target:
    command.extend(['--target', options.target])
  if options.platform:
    command.extend(['--platform', options.platform])

  # Build type is no longer used.
  # TODO(pamg): remove it from the buildbot config too.
  #if options.build_type:
  #  command.extend(['--build-type', options.build_type])

  if options.no_pixel_tests:
    command.append('--no-pixel-tests')
  if options.batch_size:
    command.extend(['--batch-size', options.batch_size])
  if options.run_part:
    command.extend(['--run-part', options.run_part])
  if options.builder_name:
    command.extend(['--builder-name', options.builder_name])
  if options.build_number:
    command.extend(['--build-number', options.build_number])
  command.extend(['--master-name', slave_utils.GetActiveMaster()])
  command.extend(['--build-name', slave_name])
  # On Windows, look for the target in an exact location.
  if sys.platform == 'win32':
    command.extend(['--build-directory', build_dir])
  if options.test_results_server:
    command.extend(['--test-results-server', options.test_results_server])

  if options.enable_pageheap:
    # If we're actually requesting pageheap checking, complain if we don't have
    # gflags.exe.
    slave_utils.SetPageHeap(build_dir, 'test_shell.exe', True)
    command.append('--time-out-ms=120000')

  # The list of tests is given as arguments.
  command.extend(options.options.split(' '))
  command.extend(args)

  # Nuke anything that appears to be stale chrome items in the temporary
  # directory from previous test runs (i.e.- from crashes or unittest leaks).
  chromium_utils.RemoveChromeTemporaryFiles()

  # Run the the tests
  result = slave_utils.RunPythonCommandInBuildDir(build_dir, options.target,
                                                  command)

  if options.enable_pageheap:
    slave_utils.SetPageHeap(build_dir, 'test_shell.exe', False)

  return result

def main():
  option_parser = optparse.OptionParser()
  option_parser.add_option('-o', '--results-directory', default='',
                           help='output results directory')
  option_parser.add_option('', '--build-dir', default='webkit',
                           help='path to main build directory (the parent of '
                                'the Release or Debug directory)')
  option_parser.add_option('', '--target', default='',
      help='test_shell build configuration (Release or Debug)')
  option_parser.add_option('', '--build-type', default='',
      help='build identifier, for custom results and test lists (v8 or kjs)')
  option_parser.add_option('', '--options', default='',
      help='additional options to pass to run_webkit_tests.py')
  option_parser.add_option("", "--platform", default='',
      help=("Platform value passed directly to run_webkit_tests."))
  option_parser.add_option('', '--no-pixel-tests', action='store_true',
                           default=False,
                           help='disable pixel-to-pixel PNG comparisons')
  option_parser.add_option('', '--enable-pageheap', action='store_true',
                           default=False, help='Enable page heap checking')
  option_parser.add_option("", "--batch-size",
                           default=None,
                           help=("Run a the tests in batches (n), after every "
                                 "n tests, the test shell is relaunched."))
  option_parser.add_option("", "--run-part",
                           default=None,
                           help=("Run a specified part (n:l), the nth of lth"
                                 ", of the layout tests"))
  option_parser.add_option("", "--builder-name",
                           default=None,
                           help="The name of the builder running this script.")
  option_parser.add_option("", "--build-number",
                           default=None,
                           help=("The build number of the builder running"
                                 "this script."))
  option_parser.add_option("", "--test-results-server",
                           help=("If specified, upload results json files to "
                                 "this appengine server."))
  options, args = option_parser.parse_args()

  # Disable pageheap checking except on Windows.
  if sys.platform != 'win32':
    options.enable_pageheap = False
  return layout_test(options, args)

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


<commit_before># -*- coding: utf-8 -*-
import logging
import os
import pygerduty

from django.db import models
from cabot.cabotapp.alert import AlertPlugin, AlertPluginUserData

logger = logging.getLogger(__name__)

class PagerdutyAlert(AlertPlugin):
    '''
    This plugin is used for alerting to Pagerduty from Cabot.
    The implementation is a hack, because of the current
    pagerduty design. The alerting parameters are configured on
    a per-user basis and not per service. (hipchat, email, phone-no
    etc.)

    Once you install this plugin,
    1. add a user dedicated to pagerduty
    2. configure this user as "fallback duty officer"
    3. configure the service key in this user's profile
    '''
    name = "Pagerduty"
    author = "Mahendra M"

    @property
    def alert_status_list(self):
        if not hasattr(self, '_alert_status_list'):
            self._alert_status_list = _gather_alertable_status()

        return self._alert_status_list

    def send_alert(self, service, users, duty_officers):
        """Implement your send_alert functionality here."""

        if not self._service_alertable(service):
            return

        subdomain = os.environ.get('PAGERDUTY_SUBDOMAIN')
        api_token = os.environ.get('PAGERDUTY_API_TOKEN')

        client = pygerduty.PagerDuty(subdomain, api_token)

        description = 'Service: %s is %s' % (service.name,
                                             service.overall_status)

        incident_key = '%s/%d' % (service.name.lower().replace(' ', '-'),
                                  service.pk)

        users = service.users_to_notify.all()

        service_keys = []
        for user in users:
            for plugin in user.profile.user_data():
                service_key = getattr(plugin, 'service_key', None)

                if service_key:
                    service_keys.append(str(service_key))

        for service_key in service_keys:
            try:
                if service.overall_status == service.PASSING_STATUS:
                    client.resolve_incident(service_key,
                                            incident_key)
                else:
                    client.trigger_incident(service_key,
                                            description,
                                            incident_key=incident_key)
            except Exception, exp:
                logger.exception('Error invoking pagerduty: %s' % str(exp))

    def _service_alertable(self, service):
        """ Evaluate service for alertable status """

        if service.overall_status in self.alert_status_list:
            return True

        if service.overall_status == service.PASSING_STATUS and \
            service.old_overall_status in self.alert_status_list:
            return True

        return False

def _gather_alertable_status():
    alert_status_list = os.environ.get('PAGERDUTY_ALERT_STATUS', 'CRITICAL').split(',')

    return alert_status_list

class PagerdutyAlertUserData(AlertPluginUserData):
    name = "Pagerduty Plugin"
    service_key = models.CharField(max_length=50, blank=True, null=True)
<commit_msg>Reraise exceptions in send_alert() (#3)<commit_after># -*- coding: utf-8 -*-
import logging
import os
import pygerduty

from django.db import models
from cabot.cabotapp.alert import AlertPlugin, AlertPluginUserData

logger = logging.getLogger(__name__)

class PagerdutyAlert(AlertPlugin):
    '''
    This plugin is used for alerting to Pagerduty from Cabot.
    The implementation is a hack, because of the current
    pagerduty design. The alerting parameters are configured on
    a per-user basis and not per service. (hipchat, email, phone-no
    etc.)

    Once you install this plugin,
    1. add a user dedicated to pagerduty
    2. configure this user as "fallback duty officer"
    3. configure the service key in this user's profile
    '''
    name = "Pagerduty"
    author = "Mahendra M"

    @property
    def alert_status_list(self):
        if not hasattr(self, '_alert_status_list'):
            self._alert_status_list = _gather_alertable_status()

        return self._alert_status_list

    def send_alert(self, service, users, duty_officers):
        """Implement your send_alert functionality here."""

        if not self._service_alertable(service):
            return

        subdomain = os.environ.get('PAGERDUTY_SUBDOMAIN')
        api_token = os.environ.get('PAGERDUTY_API_TOKEN')

        client = pygerduty.PagerDuty(subdomain, api_token)

        description = 'Service: %s is %s' % (service.name,
                                             service.overall_status)

        incident_key = '%s/%d' % (service.name.lower().replace(' ', '-'),
                                  service.pk)

        users = service.users_to_notify.all()

        service_keys = []
        for user in users:
            for plugin in user.profile.user_data():
                service_key = getattr(plugin, 'service_key', None)

                if service_key:
                    service_keys.append(str(service_key))

        for service_key in service_keys:
            try:
                if service.overall_status == service.PASSING_STATUS:
                    client.resolve_incident(service_key,
                                            incident_key)
                else:
                    client.trigger_incident(service_key,
                                            description,
                                            incident_key=incident_key)
            except Exception, exp:
                logger.exception('Error invoking pagerduty: %s' % str(exp))
                raise

    def _service_alertable(self, service):
        """ Evaluate service for alertable status """

        if service.overall_status in self.alert_status_list:
            return True

        if service.overall_status == service.PASSING_STATUS and \
            service.old_overall_status in self.alert_status_list:
            return True

        return False

def _gather_alertable_status():
    alert_status_list = os.environ.get('PAGERDUTY_ALERT_STATUS', 'CRITICAL').split(',')

    return alert_status_list

class PagerdutyAlertUserData(AlertPluginUserData):
    name = "Pagerduty Plugin"
    service_key = models.CharField(max_length=50, blank=True, null=True)
<|endoftext|>


<commit_before>from servable import Servable
from graphserver.graphdb import GraphDatabase
import cgi
from graphserver.core import State, WalkOptions
import time
import sys
import graphserver
from graphserver.util import TimeHelpers
from graphserver.ext.gtfs.gtfsdb import GTFSDatabase
import json

class RouteServer(Servable):
    def __init__(self, graphdb_filename, event_dispatch):
        graphdb = GraphDatabase( graphdb_filename )
        self.graph = graphdb.incarnate()
        self.event_dispatch = event_dispatch

    def vertices(self):
        return "\n".join( [vv.label for vv in self.graph.vertices] )
    vertices.mime = "text/plain"

    def path(self, origin, dest, currtime, transfer_penalty=0):

        wo = WalkOptions()
        wo.transfer_penalty=transfer_penalty
        spt = self.graph.shortest_path_tree( origin, dest, State(1,currtime), wo )
        wo.destroy()

        vertices, edges = spt.path( dest )

        ret = []
        for i in range(len(edges)):
            edgetype = edges[i].payload.__class__
            if edgetype in self.event_dispatch:
                ret.append( self.event_dispatch[ edges[i].payload.__class__ ]( vertices[i], edges[i], vertices[i+1] ) )

        spt.destroy()

        return json.dumps(ret)

    def path_retro(self, origin, dest, currtime, transfer_penalty=0):

        wo = WalkOptions()
        wo.transfer_penalty = transfer_penalty
        spt = self.graph.shortest_path_tree_retro( origin, dest, State(1,currtime), wo )
        wo.destroy()

        vertices, edges = spt.path_retro( origin )

        ret = []
        for i in range(len(edges)):
            edgetype = edges[i].payload.__class__
            if edgetype in self.event_dispatch:
                ret.append( self.event_dispatch[ edges[i].payload.__class__ ]( vertices[i], edges[i], vertices[i+1] ) )

        spt.destroy()

        return json.dumps(ret)

    def path_raw(self, origin, dest, currtime):

        wo = WalkOptions()
        spt = self.graph.shortest_path_tree( origin, dest, State(1,currtime), wo )
        wo.destroy()

        vertices, edges = spt.path( dest )

        ret = "\n".join([str(x) for x in vertices]) + "\n\n" + "\n".join([str(x) for x in edges])

        spt.destroy()

        return ret

    def path_raw_retro(self, origin, dest, currtime):

        wo = WalkOptions()
        spt = self.graph.shortest_path_tree_retro( origin, dest, State(1,currtime), wo )
        wo.destroy()

        vertices, edges = spt.path_retro( origin )

        ret = "\n".join([str(x) for x in vertices]) + "\n\n" + "\n".join([str(x) for x in edges])

        spt.destroy()

        return ret

import sys
if __name__ == '__main__':
    usage = "python routeserver.py graphdb_filename gtfsdb_filename"

    if len(sys.argv) < 2:
        print usage
        exit()

    graphdb_filename = sys.argv[1]
    gtfsdb_filename = sys.argv[2]

    gtfsdb = GTFSDatabase( gtfsdb_filename )

    def board_event(vertex1, edge, vertex2):
        event_time = vertex2.payload.time
        trip_id = vertex2.payload.trip_id
        stop_id = vertex1.label

        route_desc = "-".join(list( gtfsdb.execute( "SELECT routes.route_short_name, routes.route_long_name FROM routes, trips WHERE routes.route_id=trips.route_id AND trip_id=?", (trip_id,) ) )[0])
        stop_desc = list( gtfsdb.execute( "SELECT stop_name FROM stops WHERE stop_id = ?", (stop_id,) ) )[0][0]
        lat, lon = list( gtfsdb.execute( "SELECT stop_lat, stop_lon FROM stops WHERE stop_id = ?", (stop_id,) ) )[0]

        what = "Board the %s"%route_desc
        where = stop_desc
        when = str(TimeHelpers.unix_to_localtime( event_time, "America/Los_Angeles" ))
        loc = (lat,lon)
        return (what, where, when, loc)

    def alight_event(vertex1, edge, vertex2):
        event_time = vertex1.payload.time
        stop_id = vertex2.label

        stop_desc = list( gtfsdb.execute( "SELECT stop_name FROM stops WHERE stop_id = ?", (stop_id,) ) )[0][0]
        lat, lon = list( gtfsdb.execute( "SELECT stop_lat, stop_lon FROM stops WHERE stop_id = ?", (stop_id,) ) )[0]

        what = "Alight"
        where = stop_desc
        when = str(TimeHelpers.unix_to_localtime( event_time, "America/Los_Angeles" ))
        loc = (lat,lon)
        return (what, where, when, loc)

    def headwayboard_event(vertex1, edge, vertex2):
        event_time = vertex2.payload.time
        trip_id = vertex2.payload.trip_id
        stop_id = vertex1.label

        route_desc = "-".join(list( gtfsdb.execute( "SELECT routes.route_short_name, routes.route_long_name FROM routes, trips WHERE routes.route_id=trips.route_id AND trip_id=?", (trip_id,) ) )[0])
        stop_desc = list( gtfsdb.execute( "SELECT stop_name FROM stops WHERE stop_id = ?", (stop_id,) ) )[0][0]
        lat, lon = list( gtfsdb.execute( "SELECT stop_lat, stop_lon FROM stops WHERE stop_id = ?", (stop_id,) ) )[0]

        what = "Board the %s"%route_desc
        where = stop_desc
        when = "about %s"%str(TimeHelpers.unix_to_localtime( event_time, "America/Los_Angeles" ))
        loc = (lat,lon)
        return (what, where, when, loc)

    def headwayalight_event(vertex1, edge, vertex2):
        event_time = vertex1.payload.time
        stop_id = vertex2.label

        stop_desc = list( gtfsdb.execute( "SELECT stop_name FROM stops WHERE stop_id = ?", (stop_id,) ) )[0][0]
        lat, lon = list( gtfsdb.execute( "SELECT stop_lat, stop_lon FROM stops WHERE stop_id = ?", (stop_id,) ) )[0]

        what = "Alight"
        where = stop_desc
        when = "about %s"%str(TimeHelpers.unix_to_localtime( event_time, "America/Los_Angeles" ))
        loc = (lat,lon)
        return (what, where, when, loc)

    def street_event(vertex1, edge, vertex2):
        return ("Walk", "", "", None)

    event_dispatch = {graphserver.core.TripBoard:board_event,
                      graphserver.core.Alight:alight_event,
                      graphserver.core.Street:street_event,
                      graphserver.core.HeadwayBoard:headwayboard_event,
                      graphserver.core.HeadwayAlight:headwayalight_event}

    gc = RouteServer(graphdb_filename, event_dispatch)
    gc.run_test_server()<commit_msg>walking speed patched into path and path_retro methods<commit_after>from servable import Servable
from graphserver.graphdb import GraphDatabase
import cgi
from graphserver.core import State, WalkOptions
import time
import sys
import graphserver
from graphserver.util import TimeHelpers
from graphserver.ext.gtfs.gtfsdb import GTFSDatabase
import json

class RouteServer(Servable):
    def __init__(self, graphdb_filename, event_dispatch):
        graphdb = GraphDatabase( graphdb_filename )
        self.graph = graphdb.incarnate()
        self.event_dispatch = event_dispatch

    def vertices(self):
        return "\n".join( [vv.label for vv in self.graph.vertices] )
    vertices.mime = "text/plain"

    def path(self, origin, dest, currtime, transfer_penalty=0, walking_speed=1.0):

        wo = WalkOptions()
        wo.transfer_penalty=transfer_penalty
        wo.walking_speed=walking_speed
        spt = self.graph.shortest_path_tree( origin, dest, State(1,currtime), wo )
        wo.destroy()

        vertices, edges = spt.path( dest )

        ret = []
        for i in range(len(edges)):
            edgetype = edges[i].payload.__class__
            if edgetype in self.event_dispatch:
                ret.append( self.event_dispatch[ edges[i].payload.__class__ ]( vertices[i], edges[i], vertices[i+1] ) )

        spt.destroy()

        return json.dumps(ret)

    def path_retro(self, origin, dest, currtime, transfer_penalty=0, walking_speed=1.0):

        wo = WalkOptions()
        wo.transfer_penalty = transfer_penalty
        wo.walking_speed = walking_speed
        spt = self.graph.shortest_path_tree_retro( origin, dest, State(1,currtime), wo )
        wo.destroy()

        vertices, edges = spt.path_retro( origin )

        ret = []
        for i in range(len(edges)):
            edgetype = edges[i].payload.__class__
            if edgetype in self.event_dispatch:
                ret.append( self.event_dispatch[ edges[i].payload.__class__ ]( vertices[i], edges[i], vertices[i+1] ) )

        spt.destroy()

        return json.dumps(ret)

    def path_raw(self, origin, dest, currtime):

        wo = WalkOptions()
        spt = self.graph.shortest_path_tree( origin, dest, State(1,currtime), wo )
        wo.destroy()

        vertices, edges = spt.path( dest )

        ret = "\n".join([str(x) for x in vertices]) + "\n\n" + "\n".join([str(x) for x in edges])

        spt.destroy()

        return ret

    def path_raw_retro(self, origin, dest, currtime):

        wo = WalkOptions()
        spt = self.graph.shortest_path_tree_retro( origin, dest, State(1,currtime), wo )
        wo.destroy()

        vertices, edges = spt.path_retro( origin )

        ret = "\n".join([str(x) for x in vertices]) + "\n\n" + "\n".join([str(x) for x in edges])

        spt.destroy()

        return ret

import sys
if __name__ == '__main__':
    usage = "python routeserver.py graphdb_filename gtfsdb_filename"

    if len(sys.argv) < 2:
        print usage
        exit()

    graphdb_filename = sys.argv[1]
    gtfsdb_filename = sys.argv[2]

    gtfsdb = GTFSDatabase( gtfsdb_filename )

    def board_event(vertex1, edge, vertex2):
        event_time = vertex2.payload.time
        trip_id = vertex2.payload.trip_id
        stop_id = vertex1.label

        route_desc = "-".join(list( gtfsdb.execute( "SELECT routes.route_short_name, routes.route_long_name FROM routes, trips WHERE routes.route_id=trips.route_id AND trip_id=?", (trip_id,) ) )[0])
        stop_desc = list( gtfsdb.execute( "SELECT stop_name FROM stops WHERE stop_id = ?", (stop_id,) ) )[0][0]
        lat, lon = list( gtfsdb.execute( "SELECT stop_lat, stop_lon FROM stops WHERE stop_id = ?", (stop_id,) ) )[0]

        what = "Board the %s"%route_desc
        where = stop_desc
        when = str(TimeHelpers.unix_to_localtime( event_time, "America/Los_Angeles" ))
        loc = (lat,lon)
        return (what, where, when, loc)

    def alight_event(vertex1, edge, vertex2):
        event_time = vertex1.payload.time
        stop_id = vertex2.label

        stop_desc = list( gtfsdb.execute( "SELECT stop_name FROM stops WHERE stop_id = ?", (stop_id,) ) )[0][0]
        lat, lon = list( gtfsdb.execute( "SELECT stop_lat, stop_lon FROM stops WHERE stop_id = ?", (stop_id,) ) )[0]

        what = "Alight"
        where = stop_desc
        when = str(TimeHelpers.unix_to_localtime( event_time, "America/Los_Angeles" ))
        loc = (lat,lon)
        return (what, where, when, loc)

    def headwayboard_event(vertex1, edge, vertex2):
        event_time = vertex2.payload.time
        trip_id = vertex2.payload.trip_id
        stop_id = vertex1.label

        route_desc = "-".join(list( gtfsdb.execute( "SELECT routes.route_short_name, routes.route_long_name FROM routes, trips WHERE routes.route_id=trips.route_id AND trip_id=?", (trip_id,) ) )[0])
        stop_desc = list( gtfsdb.execute( "SELECT stop_name FROM stops WHERE stop_id = ?", (stop_id,) ) )[0][0]
        lat, lon = list( gtfsdb.execute( "SELECT stop_lat, stop_lon FROM stops WHERE stop_id = ?", (stop_id,) ) )[0]

        what = "Board the %s"%route_desc
        where = stop_desc
        when = "about %s"%str(TimeHelpers.unix_to_localtime( event_time, "America/Los_Angeles" ))
        loc = (lat,lon)
        return (what, where, when, loc)

    def headwayalight_event(vertex1, edge, vertex2):
        event_time = vertex1.payload.time
        stop_id = vertex2.label

        stop_desc = list( gtfsdb.execute( "SELECT stop_name FROM stops WHERE stop_id = ?", (stop_id,) ) )[0][0]
        lat, lon = list( gtfsdb.execute( "SELECT stop_lat, stop_lon FROM stops WHERE stop_id = ?", (stop_id,) ) )[0]

        what = "Alight"
        where = stop_desc
        when = "about %s"%str(TimeHelpers.unix_to_localtime( event_time, "America/Los_Angeles" ))
        loc = (lat,lon)
        return (what, where, when, loc)

    def street_event(vertex1, edge, vertex2):
        return ("Walk", "", "", None)

    event_dispatch = {graphserver.core.TripBoard:board_event,
                      graphserver.core.Alight:alight_event,
                      graphserver.core.Street:street_event,
                      graphserver.core.HeadwayBoard:headwayboard_event,
                      graphserver.core.HeadwayAlight:headwayalight_event}

    gc = RouteServer(graphdb_filename, event_dispatch)
    gc.run_test_server()<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Load a channel id autoencoder model and predict on some train files, then plot it optionally.
"""

from keras.models import load_model
from keras import metrics
import h5py
import numpy as np
import argparse
import matplotlib.pyplot as plt

from get_dataset_info import get_dataset_info
from util.run_cnn import load_zero_center_data, generate_batches_from_hdf5_file, h5_get_number_of_rows

def parse_input():
    parser = argparse.ArgumentParser(description='Predict on channel data')
    parser.add_argument('model_name', type=str)

    args = parser.parse_args()
    params = vars(args)

    return params

params = parse_input()
model_name = params["model_name"]
mode="statistics"
zero_center = False

#for plot mode, number of 32 batches of channel_id arrays should be read through for the plot
how_many_dom_batches = 1000
bins=100

model=load_model(model_name)
dataset_info_dict=get_dataset_info("xyzc_flat")

if mode == "simple":
    #Print some 31-arrays and the prediction from the autoencoder
    how_many_doms=10 #to read from file
    minimum_counts = 5

    test_file = dataset_info_dict["test_file"]

    if zero_center==True:
        xs_mean=load_zero_center_data(((dataset_info_dict["train_file"],),), batchsize=32, n_bins=dataset_info_dict["n_bins"], n_gpu=1)
    else:
        xs_mean = 0

    f = h5py.File(test_file, "r")

    #look for some doms that are not mostly 0
    batch=[]
    i=0
    while len(batch)<=how_many_doms:
        dom=f["x"][i:i+1]
        if dom.sum()>=minimum_counts:
            batch.extend(dom)
        i+=1

    batch=np.array(batch)
    batch_centered=np.subtract(batch, xs_mean)
    pred=np.add(model.predict_on_batch(batch_centered), xs_mean)

    for i in range(len(batch)):
        print("Original")
        print(batch[i])
        print("Prediction")
        print(pred[i])
        print("loss:", ((batch[i]-pred[i])**2).mean())
        print("\n")

elif mode=="plot":
    #make plot of predictions
    #measured counts are almost always 0 or 1
    maximum_counts_to_look_for=1
    skip_zero_counts=False

    train_file=dataset_info_dict["train_file"]
    test_file=dataset_info_dict["test_file"]
    n_bins=dataset_info_dict["n_bins"]
    broken_simulations_mode=dataset_info_dict["broken_simulations_mode"] #def 0
    filesize_factor=dataset_info_dict["filesize_factor"]
    filesize_factor_test=dataset_info_dict["filesize_factor_test"]
    batchsize=dataset_info_dict["batchsize"] #def 32

    print("Total channel ids:", how_many_dom_batches*batchsize*31)

    class_type=(2,"up_down")
    is_autoencoder=True

    train_tuple=[[train_file, int(h5_get_number_of_rows(train_file)*filesize_factor)]]
    #test_tuple=[[test_file, int(h5_get_number_of_rows(test_file)*filesize_factor_test)]]

    if zero_center==True:
        xs_mean = load_zero_center_data(train_files=train_tuple, batchsize=batchsize, n_bins=n_bins, n_gpu=1)
    else:
        xs_mean=0

    generator = generate_batches_from_hdf5_file(test_file, batchsize, n_bins, class_type,
                                    is_autoencoder, dataset_info_dict, broken_simulations_mode=0,
                                    f_size=None, zero_center_image=xs_mean, yield_mc_info=False,
                                    swap_col=None, is_in_test_mode = False)

    #prediction on channel id that measured
    #          0 ,1 ,2 ,3 ...  counts
    pred_on = []

    for measured_counts in range(maximum_counts_to_look_for+1):
        pred_on.append([])

    print_something_after_every = int(how_many_dom_batches/10)
    for i in range(how_many_dom_batches):
        if i%print_something_after_every==0:
            print("Predicting ... ", int(10*i/print_something_after_every), "% done")

        data=next(generator)[0]
        data_real = np.add(data, xs_mean)
        pred=np.add(model.predict_on_batch(data), xs_mean)

        #data_real is still a batch of len batchsize of single doms (dim. e.g. (32,31)), so look at each one:
        for dom_no,data_real_single in enumerate(data_real):
            pred_single=pred[dom_no]
            for measured_counts in range(skip_zero_counts, maximum_counts_to_look_for+1):
                #sort predicitions into list according to original counts
                pred_on[measured_counts].extend(pred_single[data_real_single==measured_counts])

    print("Done, generating plot...")

    plt.title("Channel autoencoder predictions (%)")
    plt.ylabel("Fraction of predicitons")
    plt.xlabel("Predicted counts")
    plt.plot([],[], " ", label="Original counts")

    make_plots_of_counts=list(range(maximum_counts_to_look_for+1))

    ex_list=[]
    #fill with maximum and minimum prediction of every original count number
    for counts_array in pred_on:
        len(counts_array)
        if len(counts_array) != 0:
            ex_list.extend([np.amax(counts_array), np.amin(counts_array)])
    range_of_plot=[np.amin(ex_list),np.amax(ex_list)]

    #relative width of bins as fracton of bin size
    #relative_width=1/len(make_plots_of_counts)
    #bin_size = (range_of_plot[0]-range_of_plot[1]) / bins
    #bin_edges = np.linspace(range_of_plot[0], range_of_plot[1], num=bins+1)

    for c in make_plots_of_counts:
        if len(pred[c]) != 0:
            #offset = bin_size*relative_width*c
            plt.hist( x=pred_on[c], bins=bins, label=str(c), density=True, range=range_of_plot )
    plt.legend()
    plt.show()

elif mode=="statistics":
    #evaluate on test set. Check wheter doms with n hits in total were reconstructed correctly.
    #For this, predictions are rounded to next integer

    train_file=dataset_info_dict["train_file"]
    test_file=dataset_info_dict["test_file"]
    n_bins=dataset_info_dict["n_bins"]
    broken_simulations_mode=dataset_info_dict["broken_simulations_mode"] #def 0
    filesize_factor=dataset_info_dict["filesize_factor"]
    filesize_factor_test=dataset_info_dict["filesize_factor_test"]
    batchsize=dataset_info_dict["batchsize"] #def 32
    #higher for testing
    batchsize=128

    class_type=(2,"up_down")
    is_autoencoder=True

    train_tuple=[[train_file, int(h5_get_number_of_rows(train_file)*filesize_factor)]]
    test_tuple=[[test_file, int(h5_get_number_of_rows(test_file)*filesize_factor_test)]]

    if zero_center==True:
        xs_mean = load_zero_center_data(train_files=train_tuple, batchsize=batchsize, n_bins=n_bins, n_gpu=1)
    else:
        xs_mean=None

    generator = generate_batches_from_hdf5_file(test_file, batchsize, n_bins, class_type,
                                    is_autoencoder, dataset_info_dict, broken_simulations_mode=0,
                                    f_size=None, zero_center_image=xs_mean, yield_mc_info=False,
                                    swap_col=None, is_in_test_mode = False)

    total_number_of_batches = int(test_tuple[0][1]/batchsize)
    total_number_of_batches=10
    print("Filesize:",test_tuple[0][1], "Total number of batches:", total_number_of_batches)

    #a dict with entries: total_counts_per_dom : [[correct_from_batch_0, ...],[total_from_batch_0,...]]
    #e.g.                 0 : [[70,75,...],[96,94,...]]
    counts_dict={}

    for batchno in range(total_number_of_batches):
        data = next(generator)[0]
        print("data shape", data.shape)
        prediction = np.round(model.predict_on_batch(data))

        #shape (batchsize,)
        total_counts_measured_in_dom = np.sum(data, axis=1)
        print("total_counts shape",total_counts_measured_in_dom.shape)

        #Should result in a (batchsize,) array that states wheter the whole dom was predicted correctly
        dom_correct = np.logical_and.reduce(data==prediction, axis=1)
        print("dom_correct shape",dom_correct.shape)

        #which count numbers were measured in all the doms
        counts=np.unique(total_counts_measured_in_dom)

        for count in counts:
            positions = np.where(data==count)
            predicted_correct_there = np.sum(prediction[positions])
            total_doms_with_these_counts = len(positions)

            if count not in counts_dict:
                counts_dict[count]=[[],[]]

            counts_dict[count][0].append(predicted_correct_there)
            counts_dict[count][1].append(total_doms_with_these_counts)

    print(counts_dict)

            <commit_msg>New part<commit_after># -*- coding: utf-8 -*-
"""
Load a channel id autoencoder model and predict on some train files, then plot it optionally.
"""

from keras.models import load_model
from keras import metrics
import h5py
import numpy as np
import argparse
import matplotlib.pyplot as plt

from get_dataset_info import get_dataset_info
from util.run_cnn import load_zero_center_data, generate_batches_from_hdf5_file, h5_get_number_of_rows

def parse_input():
    parser = argparse.ArgumentParser(description='Predict on channel data')
    parser.add_argument('model_name', type=str)

    args = parser.parse_args()
    params = vars(args)

    return params

params = parse_input()
model_name = params["model_name"]
mode="statistics"
zero_center = False

#for plot mode, number of 32 batches of channel_id arrays should be read through for the plot
how_many_dom_batches = 1000
bins=100

model=load_model(model_name)
dataset_info_dict=get_dataset_info("xyzc_flat")

if mode == "simple":
    #Print some 31-arrays and the prediction from the autoencoder
    how_many_doms=10 #to read from file
    minimum_counts = 5

    test_file = dataset_info_dict["test_file"]

    if zero_center==True:
        xs_mean=load_zero_center_data(((dataset_info_dict["train_file"],),), batchsize=32, n_bins=dataset_info_dict["n_bins"], n_gpu=1)
    else:
        xs_mean = 0

    f = h5py.File(test_file, "r")

    #look for some doms that are not mostly 0
    batch=[]
    i=0
    while len(batch)<=how_many_doms:
        dom=f["x"][i:i+1]
        if dom.sum()>=minimum_counts:
            batch.extend(dom)
        i+=1

    batch=np.array(batch)
    batch_centered=np.subtract(batch, xs_mean)
    pred=np.add(model.predict_on_batch(batch_centered), xs_mean)

    for i in range(len(batch)):
        print("Original")
        print(batch[i])
        print("Prediction")
        print(pred[i])
        print("loss:", ((batch[i]-pred[i])**2).mean())
        print("\n")

elif mode=="plot":
    #make plot of predictions
    #measured counts are almost always 0 or 1
    maximum_counts_to_look_for=1
    skip_zero_counts=False

    train_file=dataset_info_dict["train_file"]
    test_file=dataset_info_dict["test_file"]
    n_bins=dataset_info_dict["n_bins"]
    broken_simulations_mode=dataset_info_dict["broken_simulations_mode"] #def 0
    filesize_factor=dataset_info_dict["filesize_factor"]
    filesize_factor_test=dataset_info_dict["filesize_factor_test"]
    batchsize=dataset_info_dict["batchsize"] #def 32

    print("Total channel ids:", how_many_dom_batches*batchsize*31)

    class_type=(2,"up_down")
    is_autoencoder=True

    train_tuple=[[train_file, int(h5_get_number_of_rows(train_file)*filesize_factor)]]
    #test_tuple=[[test_file, int(h5_get_number_of_rows(test_file)*filesize_factor_test)]]

    if zero_center==True:
        xs_mean = load_zero_center_data(train_files=train_tuple, batchsize=batchsize, n_bins=n_bins, n_gpu=1)
    else:
        xs_mean=0

    generator = generate_batches_from_hdf5_file(test_file, batchsize, n_bins, class_type,
                                    is_autoencoder, dataset_info_dict, broken_simulations_mode=0,
                                    f_size=None, zero_center_image=xs_mean, yield_mc_info=False,
                                    swap_col=None, is_in_test_mode = False)

    #prediction on channel id that measured
    #          0 ,1 ,2 ,3 ...  counts
    pred_on = []

    for measured_counts in range(maximum_counts_to_look_for+1):
        pred_on.append([])

    print_something_after_every = int(how_many_dom_batches/10)
    for i in range(how_many_dom_batches):
        if i%print_something_after_every==0:
            print("Predicting ... ", int(10*i/print_something_after_every), "% done")

        data=next(generator)[0]
        data_real = np.add(data, xs_mean)
        pred=np.add(model.predict_on_batch(data), xs_mean)

        #data_real is still a batch of len batchsize of single doms (dim. e.g. (32,31)), so look at each one:
        for dom_no,data_real_single in enumerate(data_real):
            pred_single=pred[dom_no]
            for measured_counts in range(skip_zero_counts, maximum_counts_to_look_for+1):
                #sort predicitions into list according to original counts
                pred_on[measured_counts].extend(pred_single[data_real_single==measured_counts])

    print("Done, generating plot...")

    plt.title("Channel autoencoder predictions (%)")
    plt.ylabel("Fraction of predicitons")
    plt.xlabel("Predicted counts")
    plt.plot([],[], " ", label="Original counts")

    make_plots_of_counts=list(range(maximum_counts_to_look_for+1))

    ex_list=[]
    #fill with maximum and minimum prediction of every original count number
    for counts_array in pred_on:
        len(counts_array)
        if len(counts_array) != 0:
            ex_list.extend([np.amax(counts_array), np.amin(counts_array)])
    range_of_plot=[np.amin(ex_list),np.amax(ex_list)]

    #relative width of bins as fracton of bin size
    #relative_width=1/len(make_plots_of_counts)
    #bin_size = (range_of_plot[0]-range_of_plot[1]) / bins
    #bin_edges = np.linspace(range_of_plot[0], range_of_plot[1], num=bins+1)

    for c in make_plots_of_counts:
        if len(pred[c]) != 0:
            #offset = bin_size*relative_width*c
            plt.hist( x=pred_on[c], bins=bins, label=str(c), density=True, range=range_of_plot )
    plt.legend()
    plt.show()

elif mode=="statistics":
    #evaluate on test set. Check wheter doms with n hits in total were reconstructed correctly.
    #For this, predictions are rounded to next integer

    train_file=dataset_info_dict["train_file"]
    test_file=dataset_info_dict["test_file"]
    n_bins=dataset_info_dict["n_bins"]
    broken_simulations_mode=dataset_info_dict["broken_simulations_mode"] #def 0
    filesize_factor=dataset_info_dict["filesize_factor"]
    filesize_factor_test=dataset_info_dict["filesize_factor_test"]

    #higher for testing
    batchsize=32
    dataset_info_dict["batchsize"]=batchsize #def 32

    class_type=(2,"up_down")
    is_autoencoder=True

    train_tuple=[[train_file, int(h5_get_number_of_rows(train_file)*filesize_factor)]]
    test_tuple=[[test_file, int(h5_get_number_of_rows(test_file)*filesize_factor_test)]]

    if zero_center==True:
        xs_mean = load_zero_center_data(train_files=train_tuple, batchsize=batchsize, n_bins=n_bins, n_gpu=1)
    else:
        xs_mean=None

    generator = generate_batches_from_hdf5_file(test_file, batchsize, n_bins, class_type,
                                    is_autoencoder, dataset_info_dict, broken_simulations_mode=0,
                                    f_size=None, zero_center_image=xs_mean, yield_mc_info=False,
                                    swap_col=None, is_in_test_mode = False)

    total_number_of_batches = int(test_tuple[0][1]/batchsize)
    total_number_of_batches=10
    print("Filesize:",test_tuple[0][1], "Total number of batches:", total_number_of_batches)

    #a dict with entries: total_counts_per_dom : [[correct_from_batch_0, ...],[total_from_batch_0,...]]
    #e.g.                 0 : [[70,75,...],[96,94,...]]
    counts_dict={}

    for batchno in range(total_number_of_batches):
        data = next(generator)[0]
        print("data shape", data.shape)
        prediction = np.round(model.predict_on_batch(data))

        #shape (batchsize,)
        total_counts_measured_in_dom = np.sum(data, axis=1)
        print("total_counts shape",total_counts_measured_in_dom.shape)

        #Should result in a (batchsize,) array that states wheter the whole dom was predicted correctly
        dom_correct = np.logical_and.reduce(data==prediction, axis=1)
        print("dom_correct shape",dom_correct.shape)

        #which count numbers were measured in all the doms
        counts=np.unique(total_counts_measured_in_dom).astype(int)

        for count in counts:
            positions = np.where(data==count)
            predicted_correct_there = np.sum(dom_correct[positions]).astype(int)
            total_doms_with_these_counts = len(positions)

            if count not in counts_dict:
                counts_dict[count]=[[],[]]

            counts_dict[count][0].append(predicted_correct_there)
            counts_dict[count][1].append(total_doms_with_these_counts)

    print(counts_dict)

            <|endoftext|>


<commit_before>from __future__ import print_function

import argparse
import os
import sys

from .config import Config
from .log import Logger
from .util import Environment, camel_case, detect_platform, parse_flags, import_module

def main():
    platform = detect_platform()

    parser = argparse.ArgumentParser(
        epilog='to see the full list of build options, run with --help-options',
        add_help=False,
    )
    # Default options
    group = parser.add_argument_group('wright options')
    group.add_argument('--help-options', action='store_true',
        help='show build options')
    group.add_argument('--config', default='wright.ini', metavar='<file>',
        help='wright configuration (default: wright.ini)')
    group.add_argument('--log', default='wright.log', metavar='<file>',
        help='wright log file (default: wright.log)')
    group.add_argument('--platform', default=platform, metavar='<name>',
        help='target platform (default: {})'.format(platform))
    # Cross compiling options
    group = parser.add_argument_group('compiler options')
    group.add_argument('--cross-execute', default='', metavar='<..>',
        help='cross execute wrapper (default: none)')
    # The rest of the arguments may be environment settings
    parser.add_argument('env', metavar='key=value', nargs='*',
        help='additional environment settings')

    # Parse the arguments that are known to us, so we can extend them from our
    # configuration file.
    args, remaining_args = parser.parse_known_args()

    env = Environment(args.platform)
    env.update(os.environ)
    for item in args.env:
        part = item.split('=', 1)
        if len(part) == 1:
            env[item] = ''
        else:
            env[part[0]] = part[1]
    for key in ('ARFLAGS', 'CFLAGS', 'LDFLAGS'):
        env.merge(parse_flags(os.environ.get(key, ''), origin=key))

    if args.cross_execute:
        env['CROSS_EXECUTE'] = args.cross_execute
    if args.platform:
        env['PLATFORM'] = args.platform
        env['PLATFORM_' + args.platform.upper()] = True

    config = Config(env, args.platform)
    if not config.read(args.config):
        print('unable to parse configuration file {}'.format(args.config))
        return 1

    # Now is a good time to parse the rest of the arguments
    options_parser = argparse.ArgumentParser(parents=[parser])
    options_parser.set_defaults(**args.__dict__)
    config.add_arguments(options_parser)
    args = options_parser.parse_args(remaining_args)

    if args.help_options:
        options_parser.print_help()
        return 0

    # Feed back the build options to our environment
    for key, value in args.__dict__.items():
        env[key.upper()] = value

    log = Logger(args.log)

    stages = {}
    for stage in config.stages():
        if '.' in stage:
            module = stage
        else:
            module = 'wright.stage.{}'.format(stage)

        stages[stage] = import_module(module)
        log.write('loaded stage {}: {}\n'.format(stage, stages[stage].__file__))
        try:
            stages[stage] = getattr(stages[stage], camel_case(stage))
        except AttributeError:
            raise AttributeError('Stage {} has no class {}'.format(
                stage, camel_case(stage)))
        else:
            # Create instance of the stage (once)
            stages[stage] = stages[stage](config, env, log)

    for name, stage in stages.items():
        log.write('executing stage: {}\n'.format(name))
        for check in stage.checks():
            log.write('executing stage: {}, check: {}\n'.format(name, check))
            if not stage.run(check):
                print('wright failed, check {} for more details'.format(
                    args.log))
                return 1

    for key in sorted(env):
        if key.startswith('HAVE_') or key.startswith('WITH_'):
            log.write('env: {}={}\n'.format(key, env[key]))

    return 0

if __name__ == '__main__':
    sys.exit(main())
<commit_msg>Added --cross-compile<commit_after>from __future__ import print_function

import argparse
import os
import sys

from .config import Config
from .log import Logger
from .util import Environment, camel_case, detect_platform, parse_flags, import_module

def main():
    platform = detect_platform()

    parser = argparse.ArgumentParser(
        epilog='to see the full list of build options, run with --help-options',
        add_help=False,
    )
    # Default options
    group = parser.add_argument_group('wright options')
    group.add_argument('--help-options', action='store_true',
        help='show build options')
    group.add_argument('--config', default='wright.ini', metavar='<file>',
        help='wright configuration (default: wright.ini)')
    group.add_argument('--log', default='wright.log', metavar='<file>',
        help='wright log file (default: wright.log)')
    group.add_argument('--platform', default=platform, metavar='<name>',
        help='target platform (default: {})'.format(platform))
    # Cross compiling options
    group = parser.add_argument_group('compiler options')
    group.add_argument('--cross-compile', default='', metavar='<prefix>',
        help='cross compile prefix (default: none)')
    group.add_argument('--cross-execute', default='', metavar='<exec>',
        help='cross execute wrapper (default: none)')
    # The rest of the arguments may be environment settings
    parser.add_argument('env', metavar='key=value', nargs='*',
        help='additional environment settings')

    # Parse the arguments that are known to us, so we can extend them from our
    # configuration file.
    args, remaining_args = parser.parse_known_args()

    env = Environment(args.platform)
    env.update(os.environ)
    for item in args.env:
        part = item.split('=', 1)
        if len(part) == 1:
            env[item] = ''
        else:
            env[part[0]] = part[1]
    for key in ('ARFLAGS', 'CFLAGS', 'LDFLAGS'):
        env.merge(parse_flags(os.environ.get(key, ''), origin=key))

    if args.cross_execute:
        env['CROSS_EXECUTE'] = args.cross_execute
    if args.platform:
        env['PLATFORM'] = args.platform
        env['PLATFORM_' + args.platform.upper()] = True

    config = Config(env, args.platform)
    if not config.read(args.config):
        print('unable to parse configuration file {}'.format(args.config))
        return 1

    # Now is a good time to parse the rest of the arguments
    options_parser = argparse.ArgumentParser(parents=[parser])
    options_parser.set_defaults(**args.__dict__)
    config.add_arguments(options_parser)
    args = options_parser.parse_args(remaining_args)

    if args.help_options:
        options_parser.print_help()
        return 0

    # Feed back the build options to our environment
    for key, value in args.__dict__.items():
        env[key.upper()] = value

    log = Logger(args.log)

    stages = {}
    for stage in config.stages():
        if '.' in stage:
            module = stage
        else:
            module = 'wright.stage.{}'.format(stage)

        stages[stage] = import_module(module)
        log.write('loaded stage {}: {}\n'.format(stage, stages[stage].__file__))
        try:
            stages[stage] = getattr(stages[stage], camel_case(stage))
        except AttributeError:
            raise AttributeError('Stage {} has no class {}'.format(
                stage, camel_case(stage)))
        else:
            # Create instance of the stage (once)
            stages[stage] = stages[stage](config, env, log)

    for name, stage in stages.items():
        log.write('executing stage: {}\n'.format(name))
        for check in stage.checks():
            log.write('executing stage: {}, check: {}\n'.format(name, check))
            if not stage.run(check):
                print('wright failed, check {} for more details'.format(
                    args.log))
                return 1

    for key in sorted(env):
        if key.startswith('HAVE_') or key.startswith('WITH_'):
            log.write('env: {}={}\n'.format(key, env[key]))

    return 0

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


<commit_before>#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you 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.

__author__ = 'Shamal Faily'

from . import ObjectCreationParameters

class ResponseParameters(ObjectCreationParameters.ObjectCreationParameters):
  def __init__(self,respName,respRisk):
    ObjectCreationParameters.ObjectCreationParameters.__init__(self)
    self.theName = respName
    self.theRisk = respRisk

  def name(self): return self.theName
  def risk(self): return self.theRisk
<commit_msg>Remove redundant code<commit_after><|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
# Create your models here.

@python_2_unicode_compatible
class Tag(models.Model):
    name = models.CharField(verbose_name=_('Name'), max_length=25)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = _('Tag')
        verbose_name_plural = _('Tags')

@python_2_unicode_compatible
class Image(models.Model):
    title = models.CharField(verbose_name=_('title'), max_length=25)
    url = models.URLField(verbose_name=_('URL'), unique=True)
    tags = models.ManyToManyField(Tag, verbose_name=_('Tags'))
    publishedAt = models.DateField(verbose_name=_('Published Date'),
                                   auto_now_add=True)
    uuid = models.CharField(verbose_name=_('UUID'), max_length=32, unique=True)
    show = models.BooleanField(verbose_name=_('Show'),
                               help_text=_('Whether to show this image'),
                               default=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = _('Image')
        verbose_name_plural = _('Images')
<commit_msg>索引优化，第二句需要高版本MySQL CREATE INDEX wallpaper_image_title ON wallpaper_image(title); # CREATE INDEX wallpaper_image_title_reverse ON wallpaper_image(REVERSE(title));<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
# Create your models here.

@python_2_unicode_compatible
class Tag(models.Model):
    name = models.CharField(verbose_name=_('Name'), max_length=25)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = _('Tag')
        verbose_name_plural = _('Tags')

@python_2_unicode_compatible
class Image(models.Model):
    title = models.CharField(verbose_name=_('title'), max_length=25, db_index=True)
    url = models.URLField(verbose_name=_('URL'), unique=True)
    tags = models.ManyToManyField(Tag, verbose_name=_('Tags'))
    publishedAt = models.DateField(verbose_name=_('Published Date'),
                                   auto_now_add=True)
    uuid = models.CharField(verbose_name=_('UUID'), max_length=32, unique=True)
    show = models.BooleanField(verbose_name=_('Show'),
                               help_text=_('Whether to show this image'),
                               default=True)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = _('Image')
        verbose_name_plural = _('Images')
<|endoftext|>


<commit_before>#!/usr/bin/env python

from __future__ import print_function
import argparse
import sys
import os, errno, shutil, stat
import subprocess
import six

from .log import Log
from .lockfile import parse_lockfile
from .gitcrate import GitRemote, GitDepSpec
from .gen import gen

def _init(lock):
    if not lock.is_empty():
        lock.log.error('the crate is already initialized')
        return 1

    lock.save(force=True)
    return 0

def _checkout(lock):
    for crate in lock.crates():
        crate.checkout()
        crate.reload_deps()

        mapping = { 'self': os.path.abspath(crate.path) }
        for dep, target in crate.deps():
            mapping[dep] = os.path.abspath(target.path)

    gen(lock)
    return 0

def _gen(lock):
    gen(lock)
    return 0

def _commit(lock):
    for crate in lock.crates():
        crate.update()
    lock.save()
    return 0

def _status(lock):
    r = []

    stati = {}

    for crate in lock.crates():
        assigned = set()

        for dep_name, target in crate.deps():
            assigned.add(dep_name)

            if target not in stati:
                stati[target] = target.status()

            full_name = '{}:{}'.format(crate.name, dep_name)
            r.append((full_name, stati[target], os.path.relpath(target.path)))

        for dep_name in (name for name, spec in crate.dep_specs() if name not in assigned):
            full_name = '{}:{}'.format(crate.name, dep_name)
            r.append((full_name, 'U ', ''))

    if not r:
        return 0

    r.sort(key=lambda r: (r[2], r[1]))

    max_name_len = max(len(name) for name, status, target in r)
    templ = '{{}}      {{:<{}s}}'.format(max_name_len)

    last_target = ''
    for name, status, target in r:
        if target == last_target:
            lock.log.write(templ.format(status, name) + '\n')
        else:
            lock.log.write(templ.format(status, name) + '    {}\n'.format(target))
            last_target = target

    return 0

def _upgrade(lock, depid, target_dir, dir):

    dir = lock.guess_deps_dir(dir)

    remotes = {}
    for crate in lock.crates():
        remotes.setdefault(crate.remote(), set()).add(crate)

    targets = {}

    fetched_crates = set()

    def lock_one(unlocked, locked):
        if not unlocked:
            return locked

        locked = dict(locked)
        unlocked = dict(unlocked)

        c = next(iter(unlocked))
        dep_spec = unlocked[c]
        del unlocked[c]

        for ver in c.versions(dep_spec):
            locked[c] = ver

            new_dep_specs = c.get_dep_specs(ver)
            for dep_name, (remote, ds) in six.iteritems(new_dep_specs):
                tgt = c.get_dep(dep_name)
                if tgt is None:
                    tgt = remotes.get(remote)
                    if tgt is None:
                        name = lock.new_unique_crate_name(remote, dir)
                        tgt = lock.init_crate(remote, ds, name)
                        remotes[remote] = tgt
                    elif len(tgt) == 1:
                        tgt = next(iter(tgt))
                        c.set_dep(dep_name, tgt)
                    else:
                        # Can't associate, skip this dep
                        # XXX write a warning to the log
                        continue
                else:
                    if tgt not in fetched_crates:
                        fetched_crates.add(tgt)
                        tgt.fetch()

                targets[c, dep_name] = tgt
                if tgt in locked:
                    if not tgt.is_compatible_ver(locked[tgt], ds):
                        return
                elif tgt in unlocked:
                    ds = unlocked[tgt].join(ds)
                    if ds is None:
                        return
                    unlocked[tgt] = ds
                else:
                    unlocked[tgt] = ds

            r = lock_one(unlocked, locked)
            if r is not None:
                return r

    self_crate = lock.get_crate('')
    unlocked_crates = { self_crate : self_crate.empty_dep_spec() }
    r = lock_one(unlocked_crates, locked={})

    for (c, dep_name), tgt in six.iteritems(targets):
        c.set_dep(dep_name, tgt)

    for c, ver in six.iteritems(r):
        c.checkout(ver)

    lock.save()

    gen(lock)

def _list_deps(lock):
    r = []
    for crate in lock.crates():
        for dep in crate.deps():
            r.append((crate.name, dep.name))

    r.sort()
    for cname, dname in r:
        print('{}:{}'.format(cname, dname))
    return 0

def _assign(lock, dep, crate, force):
    target_crate = lock.locate_crate(crate)

    toks = dep.split(':', 1)
    if len(toks) != 2:
        toks = '', toks[0]
    cname, dname = toks

    crate = lock.locate_crate(cname)

    if dname not in crate._dep_specs:
        if not force:
            print('error: the dependency {} has no specification in the DEPS file'.format(dep), file=sys.stderr)
            return 1
        else:
            print('warning: the dependency {} has no specification in the DEPS file'.format(dep), file=sys.stderr)

    crate.set_dep(dname, target_crate)
    lock.save()
    gen(lock)
    return 0

def _remove(lock, target):
    crate = lock.locate_crate(target)

    lock.remove(crate)
    lock.save()
    gen(lock)

def _add_git_crate(lock, url, target, branch, quiet):
    remote = GitRemote(url)
    dep_spec = GitDepSpec([branch or 'master'])

    if target is None:
        crate_name = lock.new_unique_crate_name(remote)
        target = crate_name
    else:
        crate_name = lock.crate_name_from_path(target)

    new_crate = lock.init_crate(remote, dep_spec, crate_name)
    new_crate.checkout()

    dep_name = os.path.split(target)[1]
    for crate in lock.crates():
        if dep_name not in crate._deps and dep_name in crate._dep_specs:
            crate.set_dep(dep_name, new_crate)

    lock.save()
    gen(lock)
    return 0

def _add_tar_crate(lock, url, target, subdir, exclude):
    if target is None:
        target = url.replace('\\', '/').rsplit('/', 1)[-1]
        target = target.split('.', 1)[0]
        target = os.path.join(lock.root(), '_deps', target)

    crate_path = os.path.relpath(target, lock.root()).replace('\\', '/')

    new_crate = TarCrate(lock.root(), crate_path, hash=None, url=url, subdir=subdir, exclude=exclude)
    new_crate.checkout()
    lock.add(new_crate)
    lock.save()
    gen(lock)
    return 0

def find_root(dir):
    # The root directory is the one containing the .deps.lock file.
    # If not explicitly specified by --root, try to locate search for
    # the .deps.lock file in the parents of the current directory and choose the
    # farthest one. If there is no such directory, use current directory as the root.

    dir = os.path.abspath(dir)

    parts = []
    while True:
        dir, component = os.path.split(dir)
        if not component:
            break
        parts.append(component)

    while parts:
        if os.path.isfile(os.path.join(dir, '.deps.lock')):
            return dir
        dir = os.path.join(dir, parts.pop())

    return dir

def _main(argv, log):
    ap = argparse.ArgumentParser()
    ap.add_argument('--root')
    sp = ap.add_subparsers()

    p = sp.add_parser('init')
    p.set_defaults(fn=_init)

    for cmd in ('checkout', 'co'):
        p = sp.add_parser(cmd)
        p.set_defaults(fn=_checkout)

    for cmd in ('commit', 'ci'):
        p = sp.add_parser(cmd)
        p.set_defaults(fn=_commit)

    p = sp.add_parser('add-git')
    p.add_argument('--branch', '-b')
    p.add_argument('--quiet', '-q', action='store_true')
    p.add_argument('url')
    p.add_argument('target', nargs='?')
    p.set_defaults(fn=_add_git_crate)

    p = sp.add_parser('add-tar')
    p.add_argument('--subdir')
    p.add_argument('--exclude', '-X', action="append")
    p.add_argument('url')
    p.add_argument('target', nargs='?')
    p.set_defaults(fn=_add_tar_crate)

    for cmd in ('rm', 'remove'):
        p = sp.add_parser(cmd)
        p.add_argument('target', default='.')
        p.set_defaults(fn=_remove)

    p = sp.add_parser('list-deps')
    p.set_defaults(fn=_list_deps)

    p = sp.add_parser('assign')
    p.add_argument('--force', '-f', action='store_true')
    p.add_argument('dep')
    p.add_argument('crate')
    p.set_defaults(fn=_assign)

    for cmd in ('st', 'status'):
        p = sp.add_parser(cmd)
        p.set_defaults(fn=_status)

    p = sp.add_parser('upgrade')
    p.add_argument('--dir')
    p.add_argument('depid', nargs='?')
    p.add_argument('target_dir', nargs='?')
    p.set_defaults(fn=_upgrade)

    p = sp.add_parser('gen')
    p.set_defaults(fn=_gen)

    args = ap.parse_args(argv)

    fn = args.fn
    del args.fn

    root = args.root or find_root('.')
    del args.root

    lock = parse_lockfile(root, log)

    return fn(lock=lock, **vars(args))

def main():
    return _main(sys.argv[1:], Log(sys.stderr))

if __name__ == '__main__':
    sys.exit(main())
<commit_msg>Fix upgrade crash.<commit_after>#!/usr/bin/env python

from __future__ import print_function
import argparse
import sys
import os, errno, shutil, stat
import subprocess
import six

from .log import Log
from .lockfile import parse_lockfile
from .gitcrate import GitRemote, GitDepSpec
from .gen import gen

def _init(lock):
    if not lock.is_empty():
        lock.log.error('the crate is already initialized')
        return 1

    lock.save(force=True)
    return 0

def _checkout(lock):
    for crate in lock.crates():
        crate.checkout()
        crate.reload_deps()

        mapping = { 'self': os.path.abspath(crate.path) }
        for dep, target in crate.deps():
            mapping[dep] = os.path.abspath(target.path)

    gen(lock)
    return 0

def _gen(lock):
    gen(lock)
    return 0

def _commit(lock):
    for crate in lock.crates():
        crate.update()
    lock.save()
    return 0

def _status(lock):
    r = []

    stati = {}

    for crate in lock.crates():
        assigned = set()

        for dep_name, target in crate.deps():
            assigned.add(dep_name)

            if target not in stati:
                stati[target] = target.status()

            full_name = '{}:{}'.format(crate.name, dep_name)
            r.append((full_name, stati[target], os.path.relpath(target.path)))

        for dep_name in (name for name, spec in crate.dep_specs() if name not in assigned):
            full_name = '{}:{}'.format(crate.name, dep_name)
            r.append((full_name, 'U ', ''))

    if not r:
        return 0

    r.sort(key=lambda r: (r[2], r[1]))

    max_name_len = max(len(name) for name, status, target in r)
    templ = '{{}}      {{:<{}s}}'.format(max_name_len)

    last_target = ''
    for name, status, target in r:
        if target == last_target:
            lock.log.write(templ.format(status, name) + '\n')
        else:
            lock.log.write(templ.format(status, name) + '    {}\n'.format(target))
            last_target = target

    return 0

def _upgrade(lock, depid, target_dir, dir):

    dir = lock.guess_deps_dir(dir)

    remotes = {}
    for crate in lock.crates():
        remotes.setdefault(crate.remote(), set()).add(crate)

    targets = {}

    fetched_crates = set()

    def lock_one(unlocked, locked):
        if not unlocked:
            return locked

        locked = dict(locked)
        unlocked = dict(unlocked)

        c = next(iter(unlocked))
        dep_spec = unlocked[c]
        del unlocked[c]

        for ver in c.versions(dep_spec):
            locked[c] = ver

            new_dep_specs = c.get_dep_specs(ver)
            for dep_name, (remote, ds) in six.iteritems(new_dep_specs):
                tgt = c.get_dep(dep_name)
                if tgt is None:
                    tgt = remotes.get(remote)
                    if tgt is None:
                        name = lock.new_unique_crate_name(remote, dir)
                        tgt = lock.init_crate(remote, ds, name)
                        c.set_dep(dep_name, tgt)
                        lock.save()
                        remotes.setdefault(remote, set()).add(tgt)
                    elif len(tgt) == 1:
                        tgt = next(iter(tgt))
                        c.set_dep(dep_name, tgt)
                    else:
                        # Can't associate, skip this dep
                        # XXX write a warning to the log
                        continue
                else:
                    if tgt not in fetched_crates:
                        fetched_crates.add(tgt)
                        tgt.fetch()

                targets[c, dep_name] = tgt
                if tgt in locked:
                    if not tgt.is_compatible_ver(locked[tgt], ds):
                        return
                elif tgt in unlocked:
                    ds = unlocked[tgt].join(ds)
                    if ds is None:
                        return
                    unlocked[tgt] = ds
                else:
                    unlocked[tgt] = ds

            r = lock_one(unlocked, locked)
            if r is not None:
                return r

    self_crate = lock.get_crate('')
    unlocked_crates = { self_crate : self_crate.empty_dep_spec() }
    r = lock_one(unlocked_crates, locked={})

    for (c, dep_name), tgt in six.iteritems(targets):
        c.set_dep(dep_name, tgt)

    for c, ver in six.iteritems(r):
        c.checkout(ver)

    lock.save()

    gen(lock)

def _list_deps(lock):
    r = []
    for crate in lock.crates():
        for dep in crate.deps():
            r.append((crate.name, dep.name))

    r.sort()
    for cname, dname in r:
        print('{}:{}'.format(cname, dname))
    return 0

def _assign(lock, dep, crate, force):
    target_crate = lock.locate_crate(crate)

    toks = dep.split(':', 1)
    if len(toks) != 2:
        toks = '', toks[0]
    cname, dname = toks

    crate = lock.locate_crate(cname)

    if dname not in crate._dep_specs:
        if not force:
            print('error: the dependency {} has no specification in the DEPS file'.format(dep), file=sys.stderr)
            return 1
        else:
            print('warning: the dependency {} has no specification in the DEPS file'.format(dep), file=sys.stderr)

    crate.set_dep(dname, target_crate)
    lock.save()
    gen(lock)
    return 0

def _remove(lock, target):
    crate = lock.locate_crate(target)

    lock.remove(crate)
    lock.save()
    gen(lock)

def _add_git_crate(lock, url, target, branch, quiet):
    remote = GitRemote(url)
    dep_spec = GitDepSpec([branch or 'master'])

    if target is None:
        crate_name = lock.new_unique_crate_name(remote)
        target = crate_name
    else:
        crate_name = lock.crate_name_from_path(target)

    new_crate = lock.init_crate(remote, dep_spec, crate_name)
    new_crate.checkout()

    dep_name = os.path.split(target)[1]
    for crate in lock.crates():
        if dep_name not in crate._deps and dep_name in crate._dep_specs:
            crate.set_dep(dep_name, new_crate)

    lock.save()
    gen(lock)
    return 0

def _add_tar_crate(lock, url, target, subdir, exclude):
    if target is None:
        target = url.replace('\\', '/').rsplit('/', 1)[-1]
        target = target.split('.', 1)[0]
        target = os.path.join(lock.root(), '_deps', target)

    crate_path = os.path.relpath(target, lock.root()).replace('\\', '/')

    new_crate = TarCrate(lock.root(), crate_path, hash=None, url=url, subdir=subdir, exclude=exclude)
    new_crate.checkout()
    lock.add(new_crate)
    lock.save()
    gen(lock)
    return 0

def find_root(dir):
    # The root directory is the one containing the .deps.lock file.
    # If not explicitly specified by --root, try to locate search for
    # the .deps.lock file in the parents of the current directory and choose the
    # farthest one. If there is no such directory, use current directory as the root.

    dir = os.path.abspath(dir)

    parts = []
    while True:
        dir, component = os.path.split(dir)
        if not component:
            break
        parts.append(component)

    while parts:
        if os.path.isfile(os.path.join(dir, '.deps.lock')):
            return dir
        dir = os.path.join(dir, parts.pop())

    return dir

def _main(argv, log):
    ap = argparse.ArgumentParser()
    ap.add_argument('--root')
    sp = ap.add_subparsers()

    p = sp.add_parser('init')
    p.set_defaults(fn=_init)

    for cmd in ('checkout', 'co'):
        p = sp.add_parser(cmd)
        p.set_defaults(fn=_checkout)

    for cmd in ('commit', 'ci'):
        p = sp.add_parser(cmd)
        p.set_defaults(fn=_commit)

    p = sp.add_parser('add-git')
    p.add_argument('--branch', '-b')
    p.add_argument('--quiet', '-q', action='store_true')
    p.add_argument('url')
    p.add_argument('target', nargs='?')
    p.set_defaults(fn=_add_git_crate)

    p = sp.add_parser('add-tar')
    p.add_argument('--subdir')
    p.add_argument('--exclude', '-X', action="append")
    p.add_argument('url')
    p.add_argument('target', nargs='?')
    p.set_defaults(fn=_add_tar_crate)

    for cmd in ('rm', 'remove'):
        p = sp.add_parser(cmd)
        p.add_argument('target', default='.')
        p.set_defaults(fn=_remove)

    p = sp.add_parser('list-deps')
    p.set_defaults(fn=_list_deps)

    p = sp.add_parser('assign')
    p.add_argument('--force', '-f', action='store_true')
    p.add_argument('dep')
    p.add_argument('crate')
    p.set_defaults(fn=_assign)

    for cmd in ('st', 'status'):
        p = sp.add_parser(cmd)
        p.set_defaults(fn=_status)

    p = sp.add_parser('upgrade')
    p.add_argument('--dir')
    p.add_argument('depid', nargs='?')
    p.add_argument('target_dir', nargs='?')
    p.set_defaults(fn=_upgrade)

    p = sp.add_parser('gen')
    p.set_defaults(fn=_gen)

    args = ap.parse_args(argv)

    fn = args.fn
    del args.fn

    root = args.root or find_root('.')
    del args.root

    lock = parse_lockfile(root, log)

    return fn(lock=lock, **vars(args))

def main():
    return _main(sys.argv[1:], Log(sys.stderr))

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