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

# Licensed under the MIT license
# http://opensource.org/licenses/mit-license.php

# Copyright 2008, Frank Scholz <coherence@beebits.net>

""" transcoder classes to be used in combination with
    a Coherence MediaServer

    using GStreamer pipelines for the actually work
    and feeding the output into a http response
"""

import pygst
pygst.require('0.10')
import gst
import gobject
gobject.threads_init ()

import urllib

from twisted.web import resource, server

from coherence import log

import struct

class FakeTransformer(gst.Element, log.Loggable):
    logCategory = 'faker_datasink'

    _sinkpadtemplate = gst.PadTemplate ("sinkpadtemplate",
                                        gst.PAD_SINK,
                                        gst.PAD_ALWAYS,
                                        gst.caps_new_any())

    _srcpadtemplate =  gst.PadTemplate ("srcpadtemplate",
                                        gst.PAD_SRC,
                                        gst.PAD_ALWAYS,
                                        gst.caps_new_any())

    def __init__(self,destination=None,request=None):
        gst.Element.__init__(self)
        self.sinkpad = gst.Pad(self._sinkpadtemplate, "sink")
        self.srcpad = gst.Pad(self._srcpadtemplate, "src")
        self.add_pad(self.sinkpad)
        self.add_pad(self.srcpad)

        self.sinkpad.set_chain_function(self.chainfunc)

        self.buffer = ''
        self.buffer_size = 0
        self.proxy = False
        self.got_new_segment = False
        self.closed = False

    def get_fake_header(self):
        return struct.pack(">L4s", 32, 'ftyp') + \
            "mp42\x00\x00\x00\x00mp42mp41isomiso2"

    def chainfunc(self, pad, buffer):
        if self.proxy:
            # we are in proxy mode already
            self.srcpad.push(buffer)
            return gst.FLOW_OK

        self.buffer = self.buffer + buffer.data
        if not self.buffer_size:
            try:
                self.buffer_size, a_type = struct.unpack(">L4s", self.buffer[:8])
            except:
                return gst.FLOW_OK

        if len(self.buffer) < self.buffer_size:
            # we need to buffer more
            return gst.FLOW_OK

        buffer = self.buffer[self.buffer_size:]
        fake_header = self.get_fake_header()
        n_buf = gst.Buffer(fake_header + buffer)
        self.proxy = True
        self.srcpad.push(n_buf)

        return gst.FLOW_OK

gobject.type_register(FakeTransformer)

class DataSink(gst.Element, log.Loggable):

    logCategory = 'transcoder_datasink'

    _sinkpadtemplate = gst.PadTemplate ("sinkpadtemplate",
                                        gst.PAD_SINK,
                                        gst.PAD_ALWAYS,
                                        gst.caps_new_any())

    def __init__(self,destination=None,request=None):
        gst.Element.__init__(self)
        self.sinkpad = gst.Pad(self._sinkpadtemplate, "sink")
        self.add_pad(self.sinkpad)

        self.sinkpad.set_chain_function(self.chainfunc)
        self.sinkpad.set_event_function(self.eventfunc)
        self.destination = destination
        self.request = request

        if self.destination != None:
            self.destination = open(self.destination, 'wb')
        self.buffer = ''
        self.data_size = 0
        self.got_new_segment = False
        self.closed = False

    def chainfunc(self, pad, buffer):
        if self.closed == True:
            return gst.FLOW_OK
        if self.destination != None:
            self.destination.write(buffer.data)
        elif self.request != None:
            self.buffer += buffer.data
            if len(self.buffer) > 200000:
                self.request.write(self.buffer)
                self.buffer = ''
        else:
            self.buffer += buffer.data

        self.data_size += buffer.size
        return gst.FLOW_OK

    def eventfunc(self, pad, event):
        if event.type == gst.EVENT_NEWSEGMENT:
            if self.got_new_segment == False:
                self.got_new_segment = True
            else:
                self.closed = True
        elif event.type == gst.EVENT_EOS:
            if self.destination != None:
                self.destination.close()
            elif self.request != None:
                if len(self.buffer) > 0:
                    self.request.write(self.buffer)
                self.request.finish()
        return True

gobject.type_register(DataSink)

class GStreamerPipeline(resource.Resource, log.Loggable):
    logCategory = 'gstreamer'
    addSlash = True

    def __init__(self,pipeline,mimetype):
        self.uri = pipeline
        self.contentType = mimetype
        resource.Resource.__init__(self)

    def start(self,request=None):
        self.info("GStreamerPipeline start %r %r" % (request,self.uri))
        self.pipeline = gst.parse_launch(
                            self.uri)
        sink = DataSink(request=request)
        self.pipeline.add(sink)
        enc = self.pipeline.get_by_name('enc')
        enc.link(sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)

    def getChild(self, name, request):
        self.info('getChild %s, %s' % (name, request))
        return self

    def render_GET(self,request):
        self.info('render GET %r' % (request))
        request.setResponseCode(200)
        if hasattr(self,'contentType'):
            request.setHeader('Content-Type', self.contentType)
        request.write('')

        headers = request.getAllHeaders()
        if('connection' in headers and
           headers['connection'] == 'close'):
            pass

        self.start(request)
        return server.NOT_DONE_YET

    def render_HEAD(self,request):
        self.info('render HEAD %r' % (request))
        request.setResponseCode(200)
        request.setHeader('Content-Type', self.contentType)
        request.write('')

    def requestFinished(self,result):
        self.info("requestFinished %r" % result)
        """ we need to find a way to destroy the pipeline here
        """
        #from twisted.internet import reactor
        #reactor.callLater(0, self.pipeline.set_state, gst.STATE_NULL)
        gobject.idle_add(self.cleanup)

    def on_message(self,bus,message):
        t = message.type
        print "on_message", t
        if t == gst.MESSAGE_ERROR:
            #err, debug = message.parse_error()
            #print "Error: %s" % err, debug
            self.cleanup()
        elif t == gst.MESSAGE_EOS:
            self.cleanup()

    def cleanup(self):
        self.pipeline.set_state(gst.STATE_NULL)

class BaseTranscoder(resource.Resource, log.Loggable):
    logCategory = 'transcoder'
    addSlash = True

    def __init__(self,uri,destination=None):
        self.info('uri %s %r' % (uri, type(uri)))
        if uri[:7] not in ['file://','http://']:
            uri = 'file://' + urllib.quote(uri)   #FIXME
        self.source = uri
        self.destination = destination
        resource.Resource.__init__(self)

    def getChild(self, name, request):
        self.info('getChild %s, %s' % (name, request))
        return self

    def render_GET(self,request):
        self.info('render GET %r' % (request))
        request.setResponseCode(200)
        if hasattr(self,'contentType'):
            request.setHeader('Content-Type', self.contentType)
        request.write('')

        headers = request.getAllHeaders()
        if('connection' in headers and
           headers['connection'] == 'close'):
            pass

        self.start(request)
        return server.NOT_DONE_YET

    def render_HEAD(self,request):
        self.info('render HEAD %r' % (request))
        request.setResponseCode(200)
        request.setHeader('Content-Type', self.contentType)
        request.write('')

    def requestFinished(self,result):
        self.info("requestFinished %r" % result)
        """ we need to find a way to destroy the pipeline here
        """
        #from twisted.internet import reactor
        #reactor.callLater(0, self.pipeline.set_state, gst.STATE_NULL)
        gobject.idle_add(self.cleanup)

    def on_message(self,bus,message):
        t = message.type
        print "on_message", t
        if t == gst.MESSAGE_ERROR:
            #err, debug = message.parse_error()
            #print "Error: %s" % err, debug
            self.cleanup()
        elif t == gst.MESSAGE_EOS:
            self.cleanup()

    def cleanup(self):
        self.pipeline.set_state(gst.STATE_NULL)

class PCMTranscoder(BaseTranscoder):
    contentType = 'audio/L16;rate=44100;channels=2'

    def start(self,request=None):
        self.info("PCMTranscoder start %r %r" % (request,self.source))
        self.pipeline = gst.parse_launch(
            "%s ! decodebin ! audioconvert name=conv" % self.source)

        conv = self.pipeline.get_by_name('conv')
        caps = gst.Caps("audio/x-raw-int,rate=44100,endianness=4321,channels=2,width=16,depth=16,signed=true")
        filter = gst.element_factory_make("capsfilter", "filter")
        filter.set_property("caps", caps)
        self.pipeline.add(filter)
        conv.link(filter)

        sink = DataSink(destination=self.destination,request=request)
        self.pipeline.add(sink)
        filter.link(sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)

class WAVTranscoder(BaseTranscoder):

    contentType = 'audio/x-wav'

    def start(self,request=None):
        self.info("start %r", request)
        self.pipeline = gst.parse_launch(
            "%s ! decodebin ! audioconvert ! wavenc name=enc" % self.source)
        enc = self.pipeline.get_by_name('enc')
        sink = DataSink(destination=self.destination,request=request)
        self.pipeline.add(sink)
        enc.link(sink)
        #bus = self.pipeline.get_bus()
        #bus.connect('message', self.on_message)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)

class MP3Transcoder(BaseTranscoder):

    contentType = 'audio/mpeg'

    def start(self,request=None):
        self.info("start %r", request)
        self.pipeline = gst.parse_launch(
            "%s ! decodebin ! audioconvert ! lame name=enc" % self.source)
        enc = self.pipeline.get_by_name('enc')
        sink = DataSink(destination=self.destination,request=request)
        self.pipeline.add(sink)
        enc.link(sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)

class MP4Transcoder(BaseTranscoder):
    """ Only works if H264 inside Quicktime/MP4 container is input
        Source has to be a valid uri
    """
    contentType = 'video/mp4'

    def start(self,request=None):
        self.info("start %r", request)
        self.pipeline = gst.parse_launch(
            "%s ! qtdemux name=d ! queue ! h264parse ! mp4mux name=mux d. ! queue ! mux." % self.source)
        mux = self.pipeline.get_by_name('mux')
        sink = DataSink(destination=self.destination,request=request)
        self.pipeline.add(sink)
        mux.link(sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)

class JPEGThumbTranscoder(BaseTranscoder):
    """ should create a valid thumbnail according to the DLNA spec
        neither width nor height must exceed 160px
    """
    contentType = 'image/jpeg'

    def start(self,request=None):
        self.info("start %r", request)
        """ what we actually want here is a pipeline that calls
            us when it knows about the size of the original image,
            and allows us now to adjust the caps-filter with the
            calculated values for width and height

            new_width = 160
            new_height = 160
            if original_width > 160:
                new_heigth = int(float(original_height) * (160.0/float(original_width)))
                if new_height > 160:
                    new_width = int(float(new_width) * (160.0/float(new_height)))
            elif original_height > 160:
                new_width = int(float(original_width) * (160.0/float(original_height)))
        """
        self.pipeline = gst.parse_launch(
            "%s ! jpegdec ! videoscale ! video/x-raw-yuv,width=160,height=160 ! jpegenc name=enc" % self.source)
        enc = self.pipeline.get_by_name('enc')
        sink = DataSink(destination=self.destination,request=request)
        self.pipeline.add(sink)
        enc.link(sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)
<commit_msg>typo fix<commit_after># -*- coding: utf-8 -*-

# Licensed under the MIT license
# http://opensource.org/licenses/mit-license.php

# Copyright 2008, Frank Scholz <coherence@beebits.net>

""" transcoder classes to be used in combination with
    a Coherence MediaServer

    using GStreamer pipelines for the actually work
    and feeding the output into a http response
"""

import pygst
pygst.require('0.10')
import gst
import gobject
gobject.threads_init()

import urllib

from twisted.web import resource, server

from coherence import log

import struct

class FakeTransformer(gst.Element, log.Loggable):
    logCategory = 'faker_datasink'

    _sinkpadtemplate = gst.PadTemplate ("sinkpadtemplate",
                                        gst.PAD_SINK,
                                        gst.PAD_ALWAYS,
                                        gst.caps_new_any())

    _srcpadtemplate =  gst.PadTemplate ("srcpadtemplate",
                                        gst.PAD_SRC,
                                        gst.PAD_ALWAYS,
                                        gst.caps_new_any())

    def __init__(self,destination=None,request=None):
        gst.Element.__init__(self)
        self.sinkpad = gst.Pad(self._sinkpadtemplate, "sink")
        self.srcpad = gst.Pad(self._srcpadtemplate, "src")
        self.add_pad(self.sinkpad)
        self.add_pad(self.srcpad)

        self.sinkpad.set_chain_function(self.chainfunc)

        self.buffer = ''
        self.buffer_size = 0
        self.proxy = False
        self.got_new_segment = False
        self.closed = False

    def get_fake_header(self):
        return struct.pack(">L4s", 32, 'ftyp') + \
            "mp42\x00\x00\x00\x00mp42mp41isomiso2"

    def chainfunc(self, pad, buffer):
        if self.proxy:
            # we are in proxy mode already
            self.srcpad.push(buffer)
            return gst.FLOW_OK

        self.buffer = self.buffer + buffer.data
        if not self.buffer_size:
            try:
                self.buffer_size, a_type = struct.unpack(">L4s", self.buffer[:8])
            except:
                return gst.FLOW_OK

        if len(self.buffer) < self.buffer_size:
            # we need to buffer more
            return gst.FLOW_OK

        buffer = self.buffer[self.buffer_size:]
        fake_header = self.get_fake_header()
        n_buf = gst.Buffer(fake_header + buffer)
        self.proxy = True
        self.srcpad.push(n_buf)

        return gst.FLOW_OK

gobject.type_register(FakeTransformer)

class DataSink(gst.Element, log.Loggable):

    logCategory = 'transcoder_datasink'

    _sinkpadtemplate = gst.PadTemplate ("sinkpadtemplate",
                                        gst.PAD_SINK,
                                        gst.PAD_ALWAYS,
                                        gst.caps_new_any())

    def __init__(self,destination=None,request=None):
        gst.Element.__init__(self)
        self.sinkpad = gst.Pad(self._sinkpadtemplate, "sink")
        self.add_pad(self.sinkpad)

        self.sinkpad.set_chain_function(self.chainfunc)
        self.sinkpad.set_event_function(self.eventfunc)
        self.destination = destination
        self.request = request

        if self.destination != None:
            self.destination = open(self.destination, 'wb')
        self.buffer = ''
        self.data_size = 0
        self.got_new_segment = False
        self.closed = False

    def chainfunc(self, pad, buffer):
        if self.closed == True:
            return gst.FLOW_OK
        if self.destination != None:
            self.destination.write(buffer.data)
        elif self.request != None:
            self.buffer += buffer.data
            if len(self.buffer) > 200000:
                self.request.write(self.buffer)
                self.buffer = ''
        else:
            self.buffer += buffer.data

        self.data_size += buffer.size
        return gst.FLOW_OK

    def eventfunc(self, pad, event):
        if event.type == gst.EVENT_NEWSEGMENT:
            if self.got_new_segment == False:
                self.got_new_segment = True
            else:
                self.closed = True
        elif event.type == gst.EVENT_EOS:
            if self.destination != None:
                self.destination.close()
            elif self.request != None:
                if len(self.buffer) > 0:
                    self.request.write(self.buffer)
                self.request.finish()
        return True

gobject.type_register(DataSink)

class GStreamerPipeline(resource.Resource, log.Loggable):
    logCategory = 'gstreamer'
    addSlash = True

    def __init__(self,pipeline,mimetype):
        self.uri = pipeline
        self.contentType = mimetype
        resource.Resource.__init__(self)

    def start(self,request=None):
        self.info("GStreamerPipeline start %r %r" % (request,self.uri))
        self.pipeline = gst.parse_launch(
                            self.uri)
        sink = DataSink(request=request)
        self.pipeline.add(sink)
        enc = self.pipeline.get_by_name('enc')
        enc.link(sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)

    def getChild(self, name, request):
        self.info('getChild %s, %s' % (name, request))
        return self

    def render_GET(self,request):
        self.info('render GET %r' % (request))
        request.setResponseCode(200)
        if hasattr(self,'contentType'):
            request.setHeader('Content-Type', self.contentType)
        request.write('')

        headers = request.getAllHeaders()
        if('connection' in headers and
           headers['connection'] == 'close'):
            pass

        self.start(request)
        return server.NOT_DONE_YET

    def render_HEAD(self,request):
        self.info('render HEAD %r' % (request))
        request.setResponseCode(200)
        request.setHeader('Content-Type', self.contentType)
        request.write('')

    def requestFinished(self,result):
        self.info("requestFinished %r" % result)
        """ we need to find a way to destroy the pipeline here
        """
        #from twisted.internet import reactor
        #reactor.callLater(0, self.pipeline.set_state, gst.STATE_NULL)
        gobject.idle_add(self.cleanup)

    def on_message(self,bus,message):
        t = message.type
        print "on_message", t
        if t == gst.MESSAGE_ERROR:
            #err, debug = message.parse_error()
            #print "Error: %s" % err, debug
            self.cleanup()
        elif t == gst.MESSAGE_EOS:
            self.cleanup()

    def cleanup(self):
        self.pipeline.set_state(gst.STATE_NULL)

class BaseTranscoder(resource.Resource, log.Loggable):
    logCategory = 'transcoder'
    addSlash = True

    def __init__(self,uri,destination=None):
        self.info('uri %s %r' % (uri, type(uri)))
        if uri[:7] not in ['file://','http://']:
            uri = 'file://' + urllib.quote(uri)   #FIXME
        self.source = uri
        self.destination = destination
        resource.Resource.__init__(self)

    def getChild(self, name, request):
        self.info('getChild %s, %s' % (name, request))
        return self

    def render_GET(self,request):
        self.info('render GET %r' % (request))
        request.setResponseCode(200)
        if hasattr(self,'contentType'):
            request.setHeader('Content-Type', self.contentType)
        request.write('')

        headers = request.getAllHeaders()
        if('connection' in headers and
           headers['connection'] == 'close'):
            pass

        self.start(request)
        return server.NOT_DONE_YET

    def render_HEAD(self,request):
        self.info('render HEAD %r' % (request))
        request.setResponseCode(200)
        request.setHeader('Content-Type', self.contentType)
        request.write('')

    def requestFinished(self,result):
        self.info("requestFinished %r" % result)
        """ we need to find a way to destroy the pipeline here
        """
        #from twisted.internet import reactor
        #reactor.callLater(0, self.pipeline.set_state, gst.STATE_NULL)
        gobject.idle_add(self.cleanup)

    def on_message(self,bus,message):
        t = message.type
        print "on_message", t
        if t == gst.MESSAGE_ERROR:
            #err, debug = message.parse_error()
            #print "Error: %s" % err, debug
            self.cleanup()
        elif t == gst.MESSAGE_EOS:
            self.cleanup()

    def cleanup(self):
        self.pipeline.set_state(gst.STATE_NULL)

class PCMTranscoder(BaseTranscoder):
    contentType = 'audio/L16;rate=44100;channels=2'

    def start(self,request=None):
        self.info("PCMTranscoder start %r %r" % (request,self.source))
        self.pipeline = gst.parse_launch(
            "%s ! decodebin ! audioconvert name=conv" % self.source)

        conv = self.pipeline.get_by_name('conv')
        caps = gst.Caps("audio/x-raw-int,rate=44100,endianness=4321,channels=2,width=16,depth=16,signed=true")
        filter = gst.element_factory_make("capsfilter", "filter")
        filter.set_property("caps", caps)
        self.pipeline.add(filter)
        conv.link(filter)

        sink = DataSink(destination=self.destination,request=request)
        self.pipeline.add(sink)
        filter.link(sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)

class WAVTranscoder(BaseTranscoder):

    contentType = 'audio/x-wav'

    def start(self,request=None):
        self.info("start %r", request)
        self.pipeline = gst.parse_launch(
            "%s ! decodebin ! audioconvert ! wavenc name=enc" % self.source)
        enc = self.pipeline.get_by_name('enc')
        sink = DataSink(destination=self.destination,request=request)
        self.pipeline.add(sink)
        enc.link(sink)
        #bus = self.pipeline.get_bus()
        #bus.connect('message', self.on_message)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)

class MP3Transcoder(BaseTranscoder):

    contentType = 'audio/mpeg'

    def start(self,request=None):
        self.info("start %r", request)
        self.pipeline = gst.parse_launch(
            "%s ! decodebin ! audioconvert ! lame name=enc" % self.source)
        enc = self.pipeline.get_by_name('enc')
        sink = DataSink(destination=self.destination,request=request)
        self.pipeline.add(sink)
        enc.link(sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)

class MP4Transcoder(BaseTranscoder):
    """ Only works if H264 inside Quicktime/MP4 container is input
        Source has to be a valid uri
    """
    contentType = 'video/mp4'

    def start(self,request=None):
        self.info("start %r", request)
        self.pipeline = gst.parse_launch(
            "%s ! qtdemux name=d ! queue ! h264parse ! mp4mux name=mux d. ! queue ! mux." % self.source)
        mux = self.pipeline.get_by_name('mux')
        sink = DataSink(destination=self.destination,request=request)
        self.pipeline.add(sink)
        mux.link(sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)

class JPEGThumbTranscoder(BaseTranscoder):
    """ should create a valid thumbnail according to the DLNA spec
        neither width nor height must exceed 160px
    """
    contentType = 'image/jpeg'

    def start(self,request=None):
        self.info("start %r", request)
        """ what we actually want here is a pipeline that calls
            us when it knows about the size of the original image,
            and allows us now to adjust the caps-filter with the
            calculated values for width and height

            new_width = 160
            new_height = 160
            if original_width > 160:
                new_heigth = int(float(original_height) * (160.0/float(original_width)))
                if new_height > 160:
                    new_width = int(float(new_width) * (160.0/float(new_height)))
            elif original_height > 160:
                new_width = int(float(original_width) * (160.0/float(original_height)))
        """
        self.pipeline = gst.parse_launch(
            "%s ! jpegdec ! videoscale ! video/x-raw-yuv,width=160,height=160 ! jpegenc name=enc" % self.source)
        enc = self.pipeline.get_by_name('enc')
        sink = DataSink(destination=self.destination,request=request)
        self.pipeline.add(sink)
        enc.link(sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        d = request.notifyFinish()
        d.addBoth(self.requestFinished)
<|endoftext|>


<commit_before># Copyright 2020 The cert-manager Authors.
#
# 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.

load("@io_bazel_rules_docker//container:container.bzl", "container_pull")

def define_base_images():
    # Use 'static' distroless image for all builds by default.
    # To get the latest-amd64 digest for gcr.io/distroless/static, assuming
    # that $GOPATH/bin is in your $PATH, run:
    # go install github.com/google/go-containerregistry/cmd/crane@latest && crane digest gcr.io/distroless/static:latest-amd64
    container_pull(
        name = "static_base",
        registry = "gcr.io",
        repository = "distroless/static",
        digest = "sha256:aadea1b1f16af043a34491eec481d0132479382096ea34f608087b4bef3634be"
    )
    # Use 'dynamic' distroless image for modified cert-manager deployments that
    # are dynamically linked. (This is not the default and you probably don't
    # need this.)
    # To get the latest-amd64 digest for gcr.io/distroless/base,
    # assuming that $GOPATH/bin is in your $PATH, run:
    # go install github.com/google/go-containerregistry/cmd/crane@latest && crane digest gcr.io/distroless/base:latest-amd64
    container_pull(
        name = "dynamic_base",
        registry = "gcr.io",
        repository = "distroless/base",
        digest = "sha256:ba7a315f86771332e76fa9c3d423ecfdbb8265879c6f1c264d6fff7d4fa460a4"
    )
<commit_msg>bump distroless base images to latest versions<commit_after># Copyright 2020 The cert-manager Authors.
#
# 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.

load("@io_bazel_rules_docker//container:container.bzl", "container_pull")

def define_base_images():
    # Use 'static' distroless image for all builds by default.
    # To get the latest-amd64 digest for gcr.io/distroless/static, assuming
    # that $GOPATH/bin is in your $PATH, run:
    # go install github.com/google/go-containerregistry/cmd/crane@latest && crane digest gcr.io/distroless/static:latest-amd64
    container_pull(
        name = "static_base",
        registry = "gcr.io",
        repository = "distroless/static",
        digest = "sha256:a5635fa9dda1cf81666d8c288130bf3519bdeab1b7ed717db496a73d25d1b35c"
    )
    # Use 'dynamic' distroless image for modified cert-manager deployments that
    # are dynamically linked. (This is not the default and you probably don't
    # need this.)
    # To get the latest-amd64 digest for gcr.io/distroless/base,
    # assuming that $GOPATH/bin is in your $PATH, run:
    # go install github.com/google/go-containerregistry/cmd/crane@latest && crane digest gcr.io/distroless/base:latest-amd64
    container_pull(
        name = "dynamic_base",
        registry = "gcr.io",
        repository = "distroless/base",
        digest = "sha256:1a80a34cb3d7c4326191047976e4161741aef22c351932b55e32b72ce8827c27"
    )
<|endoftext|>


<commit_before>import sys
import weakref
from _pydev_imps import _pydev_thread
from _pydevd_bundle.pydevd_constants import STATE_RUN, PYTHON_SUSPEND, dict_iter_items
from _pydevd_bundle.pydevd_frame import PyDBFrame

#=======================================================================================================================
# PyDBAdditionalThreadInfo
#=======================================================================================================================
# IFDEF CYTHON
# cdef class PyDBAdditionalThreadInfo:
# ELSE
class PyDBAdditionalThreadInfo:
# ENDIF

    # IFDEF CYTHON
    # cdef public int pydev_state;
    # cdef public object pydev_step_stop; # Actually, it's a frame or None
    # cdef public int pydev_step_cmd;
    # cdef public bint pydev_notify_kill;
    # cdef public object pydev_smart_step_stop; # Actually, it's a frame or None
    # cdef public bint pydev_django_resolve_frame;
    # cdef public bint is_tracing;
    # cdef public tuple conditional_breakpoint_exception;
    # cdef public str pydev_message;
    # cdef public int suspend_type;
    # cdef public int pydev_next_line;
    # cdef public str pydev_func_name;
    # ELSE
    __slots__ = [
        'pydev_state',
        'pydev_step_stop',
        'pydev_step_cmd',
        'pydev_notify_kill',
        'pydev_smart_step_stop',
        'pydev_django_resolve_frame',
        'is_tracing',
        'conditional_breakpoint_exception',
        'pydev_message',
        'suspend_type',
        'pydev_next_line',
        'pydev_func_name'
    ]
    # ENDIF

    def __init__(self):
        self.pydev_state = STATE_RUN
        self.pydev_step_stop = None
        self.pydev_step_cmd = -1 # Something as CMD_STEP_INTO, CMD_STEP_OVER, etc.
        self.pydev_notify_kill = False
        self.pydev_smart_step_stop = None
        self.pydev_django_resolve_frame = False
        self.is_tracing = False
        self.conditional_breakpoint_exception = None
        self.pydev_message = ''
        self.suspend_type = PYTHON_SUSPEND
        self.pydev_next_line = None
        self.pydev_func_name = None

    def iter_frames(self, t):
        #sys._current_frames(): dictionary with thread id -> topmost frame
        current_frames = sys._current_frames()
        v = current_frames.get(t.ident)
        if v is not None:
            return [v]
        return []

    # IFDEF CYTHON
    # def create_db_frame(self, *args, **kwargs):
    #     raise AssertionError('This method should not be called on cython (PyDbFrame should be used directly).')
    # ELSE
    # just create the db frame directly
    create_db_frame = PyDBFrame
    # ENDIF

    def __str__(self):
        return 'State:%s Stop:%s Cmd: %s Kill:%s' % (
            self.pydev_state, self.pydev_step_stop, self.pydev_step_cmd, self.pydev_notify_kill)

#=======================================================================================================================
# Note that the Cython version has only the contents above
#=======================================================================================================================

# IFDEF CYTHON
# ELSE

#=======================================================================================================================
# PyDBAdditionalThreadInfoWithoutCurrentFramesSupport
#=======================================================================================================================
class PyDBAdditionalThreadInfoWithoutCurrentFramesSupport(PyDBAdditionalThreadInfo):

    def __init__(self):
        PyDBAdditionalThreadInfo.__init__(self)
        #That's where the last frame entered is kept. That's needed so that we're able to
        #trace contexts that were previously untraced and are currently active. So, the bad thing
        #is that the frame may be kept alive longer than it would if we go up on the frame stack,
        #and is only disposed when some other frame is removed.
        #A better way would be if we could get the topmost frame for each thread, but that's
        #not possible (until python 2.5 -- which is the PyDBAdditionalThreadInfo version)
        #Or if the user compiled threadframe (from http://www.majid.info/mylos/stories/2004/06/10/threadframe.html)

        #NOT RLock!! (could deadlock if it was)
        self.lock = _pydev_thread.allocate_lock()
        self._acquire_lock = self.lock.acquire
        self._release_lock = self.lock.release

        #collection with the refs
        d = {}
        self.pydev_existing_frames = d
        try:
            self._iter_frames = d.iterkeys
        except AttributeError:
            self._iter_frames = d.keys

    def _OnDbFrameCollected(self, ref):
        '''
            Callback to be called when a given reference is garbage-collected.
        '''
        self._acquire_lock()
        try:
            del self.pydev_existing_frames[ref]
        finally:
            self._release_lock()

    def _AddDbFrame(self, db_frame):
        self._acquire_lock()
        try:
            #create the db frame with a callback to remove it from the dict when it's garbage-collected
            #(could be a set, but that's not available on all versions we want to target).
            r = weakref.ref(db_frame, self._OnDbFrameCollected)
            self.pydev_existing_frames[r] = r
        finally:
            self._release_lock()

    def create_db_frame(self, args):
        #the frame must be cached as a weak-ref (we return the actual db frame -- which will be kept
        #alive until its trace_dispatch method is not referenced anymore).
        #that's a large workaround because:
        #1. we can't have weak-references to python frame object
        #2. only from 2.5 onwards we have _current_frames support from the interpreter
        db_frame = PyDBFrame(args)
        db_frame.frame = args[-1]
        self._AddDbFrame(db_frame)
        return db_frame

    def iter_frames(self, t):
        #We cannot use yield (because of the lock)
        self._acquire_lock()
        try:
            ret = []

            for weak_db_frame in self._iter_frames():
                try:
                    ret.append(weak_db_frame().frame)
                except AttributeError:
                    pass  # ok, garbage-collected already
            return ret
        finally:
            self._release_lock()

    def __str__(self):
        return 'State:%s Stop:%s Cmd: %s Kill:%s Frames:%s' % (
            self.pydev_state, self.pydev_step_stop, self.pydev_step_cmd, self.pydev_notify_kill, len(self.iter_frames(None)))

#=======================================================================================================================
# NOW, WE HAVE TO DEFINE WHICH THREAD INFO TO USE
# (whether we have to keep references to the frames or not)
# from version 2.5 onwards, we can use sys._current_frames to get a dict with the threads
# and frames, but to support other versions, we can't rely on that.
#=======================================================================================================================
if not hasattr(sys, '_current_frames'):
    try:
        import threadframe  #@UnresolvedImport
        sys._current_frames = threadframe.dict
        assert sys._current_frames is threadframe.dict  #Just check if it was correctly set
    except:
        #If all fails, let's use the support without frames
        PyDBAdditionalThreadInfo = PyDBAdditionalThreadInfoWithoutCurrentFramesSupport

# ENDIF
<commit_msg>Fix intinite recursion with Jython<commit_after>import sys
import weakref
from _pydev_imps import _pydev_thread
from _pydevd_bundle.pydevd_constants import STATE_RUN, PYTHON_SUSPEND, dict_iter_items
from _pydevd_bundle.pydevd_frame import PyDBFrame

#=======================================================================================================================
# PyDBAdditionalThreadInfo
#=======================================================================================================================
# IFDEF CYTHON
# cdef class PyDBAdditionalThreadInfo:
# ELSE
class PyDBAdditionalThreadInfo:
# ENDIF

    # IFDEF CYTHON
    # cdef public int pydev_state;
    # cdef public object pydev_step_stop; # Actually, it's a frame or None
    # cdef public int pydev_step_cmd;
    # cdef public bint pydev_notify_kill;
    # cdef public object pydev_smart_step_stop; # Actually, it's a frame or None
    # cdef public bint pydev_django_resolve_frame;
    # cdef public bint is_tracing;
    # cdef public tuple conditional_breakpoint_exception;
    # cdef public str pydev_message;
    # cdef public int suspend_type;
    # cdef public int pydev_next_line;
    # cdef public str pydev_func_name;
    # ELSE
    __slots__ = [
        'pydev_state',
        'pydev_step_stop',
        'pydev_step_cmd',
        'pydev_notify_kill',
        'pydev_smart_step_stop',
        'pydev_django_resolve_frame',
        'is_tracing',
        'conditional_breakpoint_exception',
        'pydev_message',
        'suspend_type',
        'pydev_next_line',
        'pydev_func_name'
    ]
    # ENDIF

    def __init__(self):
        self.pydev_state = STATE_RUN
        self.pydev_step_stop = None
        self.pydev_step_cmd = -1 # Something as CMD_STEP_INTO, CMD_STEP_OVER, etc.
        self.pydev_notify_kill = False
        self.pydev_smart_step_stop = None
        self.pydev_django_resolve_frame = False
        self.is_tracing = False
        self.conditional_breakpoint_exception = None
        self.pydev_message = ''
        self.suspend_type = PYTHON_SUSPEND
        self.pydev_next_line = None
        self.pydev_func_name = None

    def iter_frames(self, t):
        #sys._current_frames(): dictionary with thread id -> topmost frame
        current_frames = sys._current_frames()
        v = current_frames.get(t.ident)
        if v is not None:
            return [v]
        return []

    # IFDEF CYTHON
    # def create_db_frame(self, *args, **kwargs):
    #     raise AssertionError('This method should not be called on cython (PyDbFrame should be used directly).')
    # ELSE
    # just create the db frame directly
    create_db_frame = PyDBFrame
    # ENDIF

    def __str__(self):
        return 'State:%s Stop:%s Cmd: %s Kill:%s' % (
            self.pydev_state, self.pydev_step_stop, self.pydev_step_cmd, self.pydev_notify_kill)

#=======================================================================================================================
# Note that the Cython version has only the contents above
#=======================================================================================================================

# IFDEF CYTHON
# ELSE

PyDBAdditionalThreadInfoOriginal = PyDBAdditionalThreadInfo
#=======================================================================================================================
# PyDBAdditionalThreadInfoWithoutCurrentFramesSupport
#=======================================================================================================================
class PyDBAdditionalThreadInfoWithoutCurrentFramesSupport(PyDBAdditionalThreadInfoOriginal):

    def __init__(self):
        PyDBAdditionalThreadInfoOriginal.__init__(self)
        #That's where the last frame entered is kept. That's needed so that we're able to
        #trace contexts that were previously untraced and are currently active. So, the bad thing
        #is that the frame may be kept alive longer than it would if we go up on the frame stack,
        #and is only disposed when some other frame is removed.
        #A better way would be if we could get the topmost frame for each thread, but that's
        #not possible (until python 2.5 -- which is the PyDBAdditionalThreadInfo version)
        #Or if the user compiled threadframe (from http://www.majid.info/mylos/stories/2004/06/10/threadframe.html)

        #NOT RLock!! (could deadlock if it was)
        self.lock = _pydev_thread.allocate_lock()
        self._acquire_lock = self.lock.acquire
        self._release_lock = self.lock.release

        #collection with the refs
        d = {}
        self.pydev_existing_frames = d
        try:
            self._iter_frames = d.iterkeys
        except AttributeError:
            self._iter_frames = d.keys

    def _OnDbFrameCollected(self, ref):
        '''
            Callback to be called when a given reference is garbage-collected.
        '''
        self._acquire_lock()
        try:
            del self.pydev_existing_frames[ref]
        finally:
            self._release_lock()

    def _AddDbFrame(self, db_frame):
        self._acquire_lock()
        try:
            #create the db frame with a callback to remove it from the dict when it's garbage-collected
            #(could be a set, but that's not available on all versions we want to target).
            r = weakref.ref(db_frame, self._OnDbFrameCollected)
            self.pydev_existing_frames[r] = r
        finally:
            self._release_lock()

    def create_db_frame(self, args):
        #the frame must be cached as a weak-ref (we return the actual db frame -- which will be kept
        #alive until its trace_dispatch method is not referenced anymore).
        #that's a large workaround because:
        #1. we can't have weak-references to python frame object
        #2. only from 2.5 onwards we have _current_frames support from the interpreter
        db_frame = PyDBFrame(args)
        db_frame.frame = args[-1]
        self._AddDbFrame(db_frame)
        return db_frame

    def iter_frames(self, t):
        #We cannot use yield (because of the lock)
        self._acquire_lock()
        try:
            ret = []

            for weak_db_frame in self._iter_frames():
                try:
                    ret.append(weak_db_frame().frame)
                except AttributeError:
                    pass  # ok, garbage-collected already
            return ret
        finally:
            self._release_lock()

    def __str__(self):
        return 'State:%s Stop:%s Cmd: %s Kill:%s Frames:%s' % (
            self.pydev_state, self.pydev_step_stop, self.pydev_step_cmd, self.pydev_notify_kill, len(self.iter_frames(None)))

#=======================================================================================================================
# NOW, WE HAVE TO DEFINE WHICH THREAD INFO TO USE
# (whether we have to keep references to the frames or not)
# from version 2.5 onwards, we can use sys._current_frames to get a dict with the threads
# and frames, but to support other versions, we can't rely on that.
#=======================================================================================================================
if not hasattr(sys, '_current_frames'):
    try:
        import threadframe  #@UnresolvedImport
        sys._current_frames = threadframe.dict
        assert sys._current_frames is threadframe.dict  #Just check if it was correctly set
    except:
        #If all fails, let's use the support without frames
        PyDBAdditionalThreadInfo = PyDBAdditionalThreadInfoWithoutCurrentFramesSupport

# ENDIF
<|endoftext|>


<commit_before>import bpy
import math

#-------------- Create Text Files --------------------------
bpy.ops.text.new()
bpy.data.texts[-1].name = "StartServer"

#-------------- Add empty Controller ------------------------
bpy.ops.object.add(type='EMPTY')
bpy.context.active_object.name = "Controller"
bpy.data.objects["Controller"].location = (0.0, 0.0, 0.0)

bpy.ops.logic.sensor_add(type="DELAY", name="StartServer")
bpy.ops.logic.controller_add(type="PYTHON", name="Sever")
bpy.ops.logic.actuator_add(type="MOTION", name="RotateRight")
bpy.ops.logic.actuator_add(type="MOTION", name="RotateLeft")
bpy.ops.logic.actuator_add(type="MOTION", name="RotateUp")
bpy.ops.logic.actuator_add(type="MOTION", name="RotateDown")

bpy.data.objects["Controller"].game.sensors["StartServer"].use_repeat = True
c = bpy.data.objects['Controller'].game.controllers['Sever']
c.text = bpy.data.texts['StartServer']

bpy.data.objects['Controller'].game.sensors['StartServer'].link(c)
bpy.data.objects['Controller'].game.actuators['RotateRight'].link(c)
bpy.data.objects['Controller'].game.actuators['RotateLeft'].link(c)
bpy.data.objects['Controller'].game.actuators['RotateUp'].link(c)
bpy.data.objects['Controller'].game.actuators['RotateDown'].link(c)

#------------- Add Camera ----------------
bpy.ops.object.add(type='CAMERA')
bpy.context.active_object.name = "ControllerView"
bpy.data.objects["ControllerView"].location = (0.0, -5.0, 0.0)
bpy.data.objects["ControllerView"].rotation_euler = (math.radians(90),0.0,0.0)
bpy.data.objects["ControllerView"].parent = bpy.data.objects["Controller"]
con = bpy.data.objects["ControllerView"].constraints.new('DAMPED_TRACK')
con.name = "ViewLock"
con.target = bpy.data.objects["Controller"]
con.track_axis = 'TRACK_NEGATIVE_Z'

bpy.context.scene.camera = bpy.data.objects["ControllerView"]
bpy.context.scene.render.engine = 'BLENDER_GAME'

bpy.ops.logic.actuator_add(type="MOTION", name="ZoomOut")
bpy.ops.logic.actuator_add(type="MOTION", name="ZoomIn")
bpy.data.objects["ControllerView"].game.actuators["ZoomIn"].offset_location = (0.0,0.0,0.01)
bpy.data.objects["ControllerView"].game.actuators["ZoomOut"].offset_location = (0.0,0.0,-0.01)<commit_msg>Rewrote script a blender plug in<commit_after>bl_info = {
    "name": "Blender Web Conttroller",
    "category": "Game Engine",
}

import bpy
import math

class BlenderWebController_pl(bpy.types.Panel):
    bl_idname = "game.panel.webcontroller"
    bl_label = "Web Controller"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "render"

    def draw(self, context):
        layout = self.layout

        obj = context.object
        row = layout.row()
        row.operator('game.webcontroller')

class BlenderWebController_op(bpy.types.Operator):
    """Launches a website to contoller a new BGE camera"""      # blender will use this as a tooltip for menu items and buttons.
    bl_idname = "game.webcontroller"        # unique identifier for buttons and menu items to reference.
    bl_label = "Setup Web Controller"         # display name in the interface.
    bl_options = {'REGISTER', 'UNDO' }
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"

    #when to show this add on
    @classmethod
    def poll(self, context):
        return True

    def execute(self, context):
        bpy.context.scene.render.engine = 'BLENDER_GAME'
        #-------------- Create Text Files --------------------------
        bpy.ops.text.new()
        bpy.data.texts[-1].name = "StartServer"

        #-------------- Add empty Controller ------------------------
        bpy.ops.object.add(type='EMPTY')
        bpy.context.active_object.name = "Controller"
        bpy.data.objects["Controller"].location = (0.0, 0.0, 0.0)

        bpy.ops.logic.sensor_add(       type="DELAY",  name="StartServer")
        bpy.ops.logic.controller_add(   type="PYTHON", name="Sever")
        bpy.ops.logic.actuator_add(     type="MOTION", name="RotateRight")
        bpy.ops.logic.actuator_add(     type="MOTION", name="RotateLeft")
        bpy.ops.logic.actuator_add(     type="MOTION", name="RotateUp")
        bpy.ops.logic.actuator_add(     type="MOTION", name="RotateDown")
        bpy.ops.logic.actuator_add(     type="MOTION", name="ZRotateLeft")
        bpy.ops.logic.actuator_add(     type="MOTION", name="ZRotateRight")

        #------------- Add Camera ----------------
        bpy.ops.object.add(type='CAMERA')
        bpy.context.active_object.name = "ControllerView"
        bpy.data.objects["ControllerView"].location = (0.0, -5.0, 0.0)
        bpy.data.objects["ControllerView"].rotation_euler = (math.radians(90),0.0,0.0)
        bpy.data.objects["ControllerView"].parent = bpy.data.objects["Controller"]
        bpy.ops.logic.actuator_add(type="MOTION", name="ZoomOut")
        bpy.ops.logic.actuator_add(type="MOTION", name="ZoomIn")
        con = bpy.data.objects["ControllerView"].constraints.new('DAMPED_TRACK')
        con.name = "ViewLock"
        con.target = bpy.data.objects["Controller"]
        con.track_axis = 'TRACK_NEGATIVE_Z'
        bpy.context.scene.camera = bpy.data.objects["ControllerView"]

        #------------------ Add Logic Block Info -----------------------
        bpy.data.objects["Controller"].game.sensors["StartServer"].use_repeat           = True
        bpy.data.objects['Controller'].game.controllers['Sever'].use_priority           = True
        bpy.data.objects['Controller'].game.controllers['Sever'].text                   = bpy.data.texts['StartServer']
        bpy.data.objects["Controller"].game.actuators["RotateRight"].offset_rotation    = (0.0,     0.0,   math.radians(-1))
        bpy.data.objects["Controller"].game.actuators["RotateLeft"].offset_rotation     = (0.0,     0.0,    math.radians(1))
        bpy.data.objects["Controller"].game.actuators["RotateUp"].offset_rotation       = (math.radians(1),     0.0,    0.0)
        bpy.data.objects["Controller"].game.actuators["RotateDown"].offset_rotation     = (math.radians(-1),    0.0,    0.0)
        bpy.data.objects["Controller"].game.actuators["ZRotateLeft"].offset_rotation    = (0.0,     math.radians(1),    0.0)
        bpy.data.objects["Controller"].game.actuators["ZRotateRight"].offset_rotation   = (0.0,     math.radians(-1),   0.0)
        bpy.data.objects["ControllerView"].game.actuators["ZoomIn"].offset_location     = (0.0,     0.0,                0.01)
        bpy.data.objects["ControllerView"].game.actuators["ZoomOut"].offset_location    = (0.0,     0.0,                -0.01)

        c = bpy.data.objects['Controller'].game.controllers['Sever']
        bpy.data.objects['Controller'].game.sensors['StartServer'].link(c)
        bpy.data.objects['Controller'].game.actuators['RotateRight'].link(c)
        bpy.data.objects['Controller'].game.actuators['RotateLeft'].link(c)
        bpy.data.objects['Controller'].game.actuators['RotateUp'].link(c)
        bpy.data.objects['Controller'].game.actuators['RotateDown'].link(c)
        bpy.data.objects['Controller'].game.actuators['ZRotateLeft'].link(c)
        bpy.data.objects['Controller'].game.actuators['ZRotateRight'].link(c)
        bpy.data.objects["ControllerView"].game.actuators["ZoomIn"].link(c)
        bpy.data.objects["ControllerView"].game.actuators["ZoomOut"].link(c)

        return {'FINISHED'}

def register():
    bpy.utils.register_class(BlenderWebController_op)
    bpy.utils.register_class(BlenderWebController_pl)

def unregister():
    bpy.utils.unregister_class(BlenderWebController_op)
    bpy.utils.unregister_class(BlenderWebController_pl)

# This allows you to run the script directly from blenders text editor
# to test the addon without having to install it.
if __name__ == "__main__":
    register()<|endoftext|>


<commit_before>import zipfile

PACKAGE_FILENAME = 'google-calendar-gadget.gg'

FILES = [
  'addevent.js',
  'addevent.xml',
  'auth.js',
  'cache.js',
  'calendar.js',
  'calendarevent.js',
  'constants.js',
  'errorMessage.js',
  'eventdetails.js',
  'eventdetails.xml',
  'events.js',
  'gadget.gmanifest',
  'geocode.js',
  'icon_large.png',
  'icon_small.png',
  'json_sans_eval.js',
  'main.js',
  'main.xml',
  'quickadd.js',
  'quickadd.xml',
  'ui_agenda.js',
  'ui_calendar.js',
  'ui_dayview.js',
  'ui_main.js',
  'ui_main_options.js',
  'ui_main_resize.js',
  'utils.js',
  'en/strings.xml',
  'images/action_default.png',
  'images/action_down.png',
  'images/action_hover.png',
  'images/agenda_fullday_next.png',
  'images/agenda_fullday_prev.png',
  'images/agenda_header.png',
  'images/agenda_nav_down.png',
  'images/agenda_nav_up.png',
  'images/base_bottom.png',
  'images/base_bottomleft.png',
  'images/base_bottomright.png',
  'images/base_left.png',
  'images/base_right.png',
  'images/base_top.png',
  'images/base_topleft.png',
  'images/base_topright.png',
  'images/blue_bottom_left.png',
  'images/blue_bottom_middle.png',
  'images/blue_bottom_right.png',
  'images/captcha_loading.jpg',
  'images/checkbox_checked.png',
  'images/checkbox_default.png',
  'images/checkbox_focus.png',
  'images/closeBtn.png',
  'images/dayview_bottom_bg.png',
  'images/dayview_bottom_left.png',
  'images/dayview_bottom_right.png',
  'images/dayview_header_left.png',
  'images/dayview_header_right.png',
  'images/dayview_nav_next.png',
  'images/dayview_nav_prev.png',
  'images/day_selected.png',
  'images/design_left.png',
  'images/design_right.png',
  'images/detailsview_bg.png',
  'images/detailsview_content.png',
  'images/details_corner_bl.png',
  'images/details_corner_br.png',
  'images/details_corner_tl.png',
  'images/details_corner_tr.png',
  'images/error_center.png',
  'images/error_left.png',
  'images/error_right.png',
  'images/event_add_bg.png',
  'images/footer_bg.png',
  'images/footer_cal_out.png',
  'images/footer_cal_over.png',
  'images/footer_left.png',
  'images/footer_right.png',
  'images/google_logo.png',
  'images/loginIcon.png',
  'images/month_nav_bg.png',
  'images/month_nav_next.png',
  'images/month_nav_next_hover.png',
  'images/month_nav_prev.png',
  'images/month_nav_prev_hover.png',
  'images/quick_add_bg.png',
  'images/rsvp_maybe.png',
  'images/rsvp_no.png',
  'images/rsvp_yes.png',
  'images/scroll-bar.gif',
  'images/scroll-down-clicked.gif',
  'images/scroll-down-over.gif',
  'images/scroll-down.gif',
  'images/scroll-track-clicked.gif',
  'images/scroll-track.gif',
  'images/scroll-up-clicked.gif',
  'images/scroll-up-over.gif',
  'images/scroll-up.gif',
  'images/scroll.png',
  'images/weekday_bg.png',
  'images/weekday_left.png',
  'images/weekday_right.png',
]

package = zipfile.ZipFile(PACKAGE_FILENAME, 'w',
    compression=zipfile.ZIP_DEFLATED)
for file in FILES:
  print file
  package.write(file)
package.close()

print
print PACKAGE_FILENAME
<commit_msg>+ added resize indicator to build_package.py<commit_after>import zipfile

PACKAGE_FILENAME = 'google-calendar-gadget.gg'

FILES = [
  'addevent.js',
  'addevent.xml',
  'auth.js',
  'cache.js',
  'calendar.js',
  'calendarevent.js',
  'constants.js',
  'errorMessage.js',
  'eventdetails.js',
  'eventdetails.xml',
  'events.js',
  'gadget.gmanifest',
  'geocode.js',
  'icon_large.png',
  'icon_small.png',
  'json_sans_eval.js',
  'main.js',
  'main.xml',
  'quickadd.js',
  'quickadd.xml',
  'ui_agenda.js',
  'ui_calendar.js',
  'ui_dayview.js',
  'ui_main.js',
  'ui_main_options.js',
  'ui_main_resize.js',
  'utils.js',
  'en/strings.xml',
  'images/action_default.png',
  'images/action_down.png',
  'images/action_hover.png',
  'images/agenda_fullday_next.png',
  'images/agenda_fullday_prev.png',
  'images/agenda_header.png',
  'images/agenda_nav_down.png',
  'images/agenda_nav_up.png',
  'images/base_bottom.png',
  'images/base_bottomleft.png',
  'images/base_bottomright.png',
  'images/base_left.png',
  'images/base_right.png',
  'images/base_top.png',
  'images/base_topleft.png',
  'images/base_topright.png',
  'images/blue_bottom_left.png',
  'images/blue_bottom_middle.png',
  'images/blue_bottom_right.png',
  'images/captcha_loading.jpg',
  'images/checkbox_checked.png',
  'images/checkbox_default.png',
  'images/checkbox_focus.png',
  'images/closeBtn.png',
  'images/dayview_bottom_bg.png',
  'images/dayview_bottom_left.png',
  'images/dayview_bottom_right.png',
  'images/dayview_header_left.png',
  'images/dayview_header_right.png',
  'images/dayview_nav_next.png',
  'images/dayview_nav_prev.png',
  'images/day_selected.png',
  'images/design_left.png',
  'images/design_right.png',
  'images/detailsview_bg.png',
  'images/detailsview_content.png',
  'images/details_corner_bl.png',
  'images/details_corner_br.png',
  'images/details_corner_tl.png',
  'images/details_corner_tr.png',
  'images/error_center.png',
  'images/error_left.png',
  'images/error_right.png',
  'images/event_add_bg.png',
  'images/footer_bg.png',
  'images/footer_cal_out.png',
  'images/footer_cal_over.png',
  'images/footer_left.png',
  'images/footer_right.png',
  'images/google_logo.png',
  'images/loginIcon.png',
  'images/month_nav_bg.png',
  'images/month_nav_next.png',
  'images/month_nav_next_hover.png',
  'images/month_nav_prev.png',
  'images/month_nav_prev_hover.png',
  'images/quick_add_bg.png',
  'images/resize_indicator.png',
  'images/rsvp_maybe.png',
  'images/rsvp_no.png',
  'images/rsvp_yes.png',
  'images/scroll-bar.gif',
  'images/scroll-down-clicked.gif',
  'images/scroll-down-over.gif',
  'images/scroll-down.gif',
  'images/scroll-track-clicked.gif',
  'images/scroll-track.gif',
  'images/scroll-up-clicked.gif',
  'images/scroll-up-over.gif',
  'images/scroll-up.gif',
  'images/scroll.png',
  'images/weekday_bg.png',
  'images/weekday_left.png',
  'images/weekday_right.png',
]

package = zipfile.ZipFile(PACKAGE_FILENAME, 'w',
    compression=zipfile.ZIP_DEFLATED)
for file in FILES:
  print file
  package.write(file)
package.close()

print
print PACKAGE_FILENAME
<|endoftext|>


<commit_before>import python_jwt

# GOOD

token1 = "1"
python_jwt.process_jwt(token1)
python_jwt.verify_jwt(token1, "key", "HS256")

# BAD

# no call to verify_jwt
token2 = "123"
python_jwt.process_jwt(token2)
<commit_msg>Improve test<commit_after>import python_jwt

# GOOD

def good1(token):
    python_jwt.process_jwt(token)
    python_jwt.verify_jwt(token, "key", "HS256")

# BAD

def bad(token):
    python_jwt.process_jwt(token)
<|endoftext|>


<commit_before>import csv
import numpy as np
from os import sep, listdir, mkdir
from os.path import join, isfile, isdir, basename, dirname

from constants import PARAMS_ID, OUTPUT_DIR, CSV_DATA_TYPES, PARAMS_ID_INV, ODE_SYSTEMS

def write_bifurc_data(bifurcation_search, x0, x0_stab, x1, x1_stab, x2, x2_stab, bifurc_id, filedir, filename):
    csv_header = [bifurc_id, 'x0_x', 'x0_y', 'x0_z', 'x0_stab', 'x1_x', 'x1_y', 'x1_z', 'x1_stab', 'x2_x', 'x2_y',
                  'x2_z', 'x2_stab']
    filepath = filedir + sep + filename
    with open(filepath, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        writer.writerow(csv_header)
        for idx in xrange(len(bifurcation_search)):
            line = [bifurcation_search[idx]] + list(x0[idx,:]) + list(x0_stab[idx]) + list(x1[idx,:]) + \
                   list(x1_stab[idx]) + list(x2[idx,:]) + list(x2_stab[idx])
            writer.writerow(line)
    return filepath

def write_params(params, system, filedir, filename):
    filepath = filedir + sep + filename
    with open(filepath, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        for idx in xrange(len(PARAMS_ID)):
            if params[idx] is None:
                params[idx] = 'None'
            writer.writerow([PARAMS_ID[idx], params[idx]])
        # any extra non-dynamics params
        writer.writerow(['system', system])
    return filepath

def read_bifurc_data(filedir, filename):
    def str_to_data(elem):
        if elem == 'True':
            return True
        elif elem == 'False':
            return False
        else:
            return elem
    with open(filedir + sep + filename, 'rb') as csvfile:
        datareader = csv.reader(csvfile, delimiter=',', quotechar='|')
        nn = sum(1 for row in datareader) - 1
        csvfile.seek(0)
        header = datareader.next()
        data_dict = {key: np.zeros((nn, 1), dtype=CSV_DATA_TYPES[key]) for key in header}
        for idx_row, row in enumerate(datareader):
            for idx_col, elem in enumerate(row):
                data_dict[header[idx_col]][idx_row] = str_to_data(elem)
    return data_dict

def read_params(filedir, filename):
    with open(filedir + sep + filename, 'rb') as csvfile:
        datareader = csv.reader(csvfile, delimiter=',', quotechar='|')
        num_params = sum(1 for row in datareader)
        csvfile.seek(0)
        params = [0.0]*num_params
        for idx, pair in enumerate(datareader):
            if idx < num_params - 1:
                assert pair[0] == PARAMS_ID[idx]
                if pair[1] != 'None':
                    params[idx] = float(pair[1])
                else:
                    params[idx] = None
            else:
                assert pair[0] == 'system'
                params[idx] = pair[1]
    return params

def write_fpt_and_params(fpt, params, system, filedir=OUTPUT_DIR, filename="fpt", filename_mod=""):
    if filename_mod != "":
        filename_params = filename + "_" + filename_mod + "_params.csv"
        filename_fpt = filename + "_" + filename_mod + "_data.txt"
    else:
        filename_params = filename + "_params.csv"
        filename_fpt = filename + "_data.txt"
    write_params(params, system, filedir, filename_params)
    with open(filedir + sep + filename_fpt, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        for idx in xrange(len(fpt)):
            writer.writerow([str(fpt[idx])])
    return filedir + sep + filename_fpt

def write_varying_mean_sd_fpt_and_params(fpt_mean, fpt_sd, param_vary_name, param_vary_set, params, system, filedir=OUTPUT_DIR, filename="fpt_stats", filename_mod=""):
    if filename_mod != "":
        filename_params = filename + "_" + filename_mod + "_mean_sd_varying_%s_params.csv" % param_vary_name
        filename_fpt = filename + "_" + filename_mod + "_mean_sd_varying_%s.txt" % param_vary_name
    else:
        filename_params = filename + "_mean_sd_varying_%s_params.csv" % param_vary_name
        filename_fpt = filename + "_mean_sd_varying_%s.txt" % param_vary_name
    params[PARAMS_ID_INV[param_vary_name]] = None
    write_params(params, system, filedir, filename_params)
    with open(filedir + sep + filename_fpt, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        writer.writerow([param_vary_name, "fpt_mean", "fpt_sd"])
        for idx in xrange(len(fpt_mean)):
            datarow = [str(param_vary_set[idx]), str(fpt_mean[idx]), str(fpt_sd[idx])]
            writer.writerow(datarow)
    return filedir + sep + filename_fpt, filedir + sep + filename_params

def read_fpt_and_params(filedir, filename_data=None, filename_params=None):
    onlyfiles = [f for f in listdir(filedir) if isfile(join(filedir, f))]
    datafiles = [f for f in onlyfiles if "data" == f[-8:-4]]
    paramfiles = [f for f in onlyfiles if "params" == f[-10:-4]]
    if filename_data is None:
        assert len(datafiles) == 1
        filename_data = basename(datafiles[0])
    if filename_params is None:
        assert len(paramfiles) == 1
        filename_params = basename(paramfiles[0])

    params_with_system = read_params(filedir, filename_params)
    assert params_with_system[-1] in ODE_SYSTEMS
    params = params_with_system[:-1]
    system = params_with_system[-1]
    with open(filedir + sep + filename_data, 'rb') as csvfile:
        datareader = csv.reader(csvfile, delimiter=',', quotechar='|')
        nn = sum(1 for row in datareader)
        csvfile.seek(0)
        fp_times = [0.0]*nn
        for idx, fpt in enumerate(datareader):
            fp_times[idx] = float(fpt[0])
    return fp_times, params, system

def read_varying_mean_sd_fpt(datafile):
    with open(datafile, 'rb') as csvfile:
        datareader = csv.reader(csvfile, delimiter=',', quotechar='|')
        param_vary_name = next(datareader)[0]
        nn = sum(1 for row in datareader)
        csvfile.seek(0)
        next(datareader)  # skip header line
        param_vary_list = [0.0] * nn
        fpt_means = [0.0] * nn
        fpt_sd = [0.0] * nn
        for idx, fpt in enumerate(datareader):
            param_vary_list[idx] = float(fpt[0])
            fpt_means[idx] = float(fpt[1])
            fpt_sd[idx] = float(fpt[2])
    return fpt_means, fpt_sd, param_vary_name, param_vary_list

def read_varying_mean_sd_fpt_and_params(datafile, paramfile):
    params = read_params(dirname(paramfile), basename(paramfile))
    mean_fpt_varying, sd_fpt_varying, param_to_vary, param_set = read_varying_mean_sd_fpt(datafile)
    return mean_fpt_varying, sd_fpt_varying, param_to_vary, param_set, params[:-1], params[-1]

def collect_fpt_and_params(filedir):
    # NOTE: assumes folder structure 8s N files of ..._data and N files of ..._params which ALL correspond
    onlydirs = [f for f in listdir(filedir) if isfile(join(filedir, f))]
    datafiles = [f for f in onlyfiles if "data" == f[-8:-4]]
    paramfiles = [f for f in onlyfiles if "params" == f[-10:-4]]
    assert len(datafiles) == len(paramfiles)

    params_0 = read_params(filedir, basename(paramfiles[0]))
    for pf in paramfiles:
        params = read_params(filedir, basename(pf))
        assert params == params_0

    fpt_collected = []
    for idx, df in enumerate(datafiles):
        fp_times, params, system = read_fpt_and_params(filedir, df, basename(paramfiles[0]))
        fpt_collected += fp_times

    collected_dirname = "collected_%d" % len(fpt_collected)
    collected_dir = filedir + sep + collected_dirname
    mkdir(collected_dir)
    write_fpt_and_params(fpt_collected, params_0[:-1], params_0[-1], filedir=collected_dir, filename="fpt", filename_mod=collected_dirname)
    return collected_dir

def collect_fpt_mean_stats_and_params(filedir, dirbase="means"):
    # NOTE: assumes folder structure is filedir -> means1, means2, means3 ... collection of dirs
    #       each of these dirs contains an output folder with a params file and a data file

    def data_and_param_files_from_fptdir(fptdir):
        # input: location of filedir/means1 for example
        outputdir = fptdir + sep + "output"
        outputdirfiles = listdir(outputdir)
        assert len(outputdirfiles) == 2
        for f in outputdirfiles:
            if f[-10:-4] == "params":
                paramfile = f
            else:
                datafile = f
        return outputdir + sep + datafile, outputdir + sep + paramfile

    onlydirs = [f for f in listdir(filedir) if isdir(join(filedir, f))]
    dirstocheck = [filedir + sep + fptdir for fptdir in onlydirs if basename(fptdir)[:len(dirbase)] == dirbase]
    dirstocheck.sort(key=lambda f: int(filter(str.isdigit, f)))  # assumes dirbase+suffix (e.g.'means7') has only 1 int

    _, pf = data_and_param_files_from_fptdir(dirstocheck[0])
    print pf
    print dirname(pf), basename(pf)
    params_0 = read_params(dirname(pf), basename(pf))

    fpt_means_collected = []
    fpt_sd_collected = []
    param_vary_collected = []
    for idx, fptdir in enumerate(dirstocheck):
        df, pf = data_and_param_files_from_fptdir(fptdir)
        params = read_params(dirname(pf), basename(pf))
        assert params == params_0
        fpt_means, fpt_sd, param_vary_name, param_vary_list = read_varying_mean_sd_fpt(df)
        fpt_means_collected += fpt_means
        fpt_sd_collected += fpt_sd
        param_vary_collected += param_vary_list

    coll_dirname = "collected_stats_%d" % len(fpt_means_collected)
    collected_dir = filedir + sep + coll_dirname
    mkdir(collected_dir)
    coll_df, coll_pf = write_varying_mean_sd_fpt_and_params(fpt_means_collected, fpt_sd_collected, param_vary_name, param_vary_collected,
                                                            params_0[:-1], params_0[-1], filedir=collected_dir, filename_mod="collected")
    return coll_df, coll_pf

def write_matrix_data_and_idx_vals(arr, row_vals, col_vals, dataname, rowname, colname, output_dir=OUTPUT_DIR):
    datapath = output_dir + sep + dataname + ".txt"
    rowpath = output_dir + sep + dataname + '_' + rowname + ".txt"
    colpath = output_dir + sep + dataname + '_' + colname + ".txt"
    np.savetxt(datapath, np.array(arr), delimiter=",")
    np.savetxt(rowpath, np.array(row_vals), delimiter=",")
    np.savetxt(colpath, np.array(col_vals), delimiter=",")
    return datapath, rowpath, colpath

def read_matrix_data_and_idx_vals(datapath, rowpath, colpath):
    arr = np.loadtxt(datapath, delimiter=",", dtype=float)
    row = np.loadtxt(rowpath, delimiter=",", dtype=float)
    col = np.loadtxt(datapath, delimiter=",", dtype=float)
    return arr, row, col
<commit_msg>minor<commit_after>import csv
import numpy as np
from os import sep, listdir, mkdir
from os.path import join, isfile, isdir, basename, dirname

from constants import PARAMS_ID, OUTPUT_DIR, CSV_DATA_TYPES, PARAMS_ID_INV, ODE_SYSTEMS

def write_bifurc_data(bifurcation_search, x0, x0_stab, x1, x1_stab, x2, x2_stab, bifurc_id, filedir, filename):
    csv_header = [bifurc_id, 'x0_x', 'x0_y', 'x0_z', 'x0_stab', 'x1_x', 'x1_y', 'x1_z', 'x1_stab', 'x2_x', 'x2_y',
                  'x2_z', 'x2_stab']
    filepath = filedir + sep + filename
    with open(filepath, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        writer.writerow(csv_header)
        for idx in xrange(len(bifurcation_search)):
            line = [bifurcation_search[idx]] + list(x0[idx,:]) + list(x0_stab[idx]) + list(x1[idx,:]) + \
                   list(x1_stab[idx]) + list(x2[idx,:]) + list(x2_stab[idx])
            writer.writerow(line)
    return filepath

def write_params(params, system, filedir, filename):
    filepath = filedir + sep + filename
    with open(filepath, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        for idx in xrange(len(PARAMS_ID)):
            if params[idx] is None:
                params[idx] = 'None'
            writer.writerow([PARAMS_ID[idx], params[idx]])
        # any extra non-dynamics params
        writer.writerow(['system', system])
    return filepath

def read_bifurc_data(filedir, filename):
    def str_to_data(elem):
        if elem == 'True':
            return True
        elif elem == 'False':
            return False
        else:
            return elem
    with open(filedir + sep + filename, 'rb') as csvfile:
        datareader = csv.reader(csvfile, delimiter=',', quotechar='|')
        nn = sum(1 for row in datareader) - 1
        csvfile.seek(0)
        header = datareader.next()
        data_dict = {key: np.zeros((nn, 1), dtype=CSV_DATA_TYPES[key]) for key in header}
        for idx_row, row in enumerate(datareader):
            for idx_col, elem in enumerate(row):
                data_dict[header[idx_col]][idx_row] = str_to_data(elem)
    return data_dict

def read_params(filedir, filename):
    with open(filedir + sep + filename, 'rb') as csvfile:
        datareader = csv.reader(csvfile, delimiter=',', quotechar='|')
        num_params = sum(1 for row in datareader)
        csvfile.seek(0)
        params = [0.0]*num_params
        for idx, pair in enumerate(datareader):
            if idx < num_params - 1:
                assert pair[0] == PARAMS_ID[idx]
                if pair[1] != 'None':
                    params[idx] = float(pair[1])
                else:
                    params[idx] = None
            else:
                assert pair[0] == 'system'
                params[idx] = pair[1]
    return params

def write_fpt_and_params(fpt, params, system, filedir=OUTPUT_DIR, filename="fpt", filename_mod=""):
    if filename_mod != "":
        filename_params = filename + "_" + filename_mod + "_params.csv"
        filename_fpt = filename + "_" + filename_mod + "_data.txt"
    else:
        filename_params = filename + "_params.csv"
        filename_fpt = filename + "_data.txt"
    write_params(params, system, filedir, filename_params)
    with open(filedir + sep + filename_fpt, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        for idx in xrange(len(fpt)):
            writer.writerow([str(fpt[idx])])
    return filedir + sep + filename_fpt

def write_varying_mean_sd_fpt_and_params(fpt_mean, fpt_sd, param_vary_name, param_vary_set, params, system, filedir=OUTPUT_DIR, filename="fpt_stats", filename_mod=""):
    if filename_mod != "":
        filename_params = filename + "_" + filename_mod + "_mean_sd_varying_%s_params.csv" % param_vary_name
        filename_fpt = filename + "_" + filename_mod + "_mean_sd_varying_%s.txt" % param_vary_name
    else:
        filename_params = filename + "_mean_sd_varying_%s_params.csv" % param_vary_name
        filename_fpt = filename + "_mean_sd_varying_%s.txt" % param_vary_name
    params[PARAMS_ID_INV[param_vary_name]] = None
    write_params(params, system, filedir, filename_params)
    with open(filedir + sep + filename_fpt, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        writer.writerow([param_vary_name, "fpt_mean", "fpt_sd"])
        for idx in xrange(len(fpt_mean)):
            datarow = [str(param_vary_set[idx]), str(fpt_mean[idx]), str(fpt_sd[idx])]
            writer.writerow(datarow)
    return filedir + sep + filename_fpt, filedir + sep + filename_params

def read_fpt_and_params(filedir, filename_data=None, filename_params=None):
    onlyfiles = [f for f in listdir(filedir) if isfile(join(filedir, f))]
    datafiles = [f for f in onlyfiles if "data" == f[-8:-4]]
    paramfiles = [f for f in onlyfiles if "params" == f[-10:-4]]
    if filename_data is None:
        assert len(datafiles) == 1
        filename_data = basename(datafiles[0])
    if filename_params is None:
        assert len(paramfiles) == 1
        filename_params = basename(paramfiles[0])

    params_with_system = read_params(filedir, filename_params)
    assert params_with_system[-1] in ODE_SYSTEMS
    params = params_with_system[:-1]
    system = params_with_system[-1]
    with open(filedir + sep + filename_data, 'rb') as csvfile:
        datareader = csv.reader(csvfile, delimiter=',', quotechar='|')
        nn = sum(1 for row in datareader)
        csvfile.seek(0)
        fp_times = [0.0]*nn
        for idx, fpt in enumerate(datareader):
            fp_times[idx] = float(fpt[0])
    return fp_times, params, system

def read_varying_mean_sd_fpt(datafile):
    with open(datafile, 'rb') as csvfile:
        datareader = csv.reader(csvfile, delimiter=',', quotechar='|')
        param_vary_name = next(datareader)[0]
        nn = sum(1 for row in datareader)
        csvfile.seek(0)
        next(datareader)  # skip header line
        param_vary_list = [0.0] * nn
        fpt_means = [0.0] * nn
        fpt_sd = [0.0] * nn
        for idx, fpt in enumerate(datareader):
            param_vary_list[idx] = float(fpt[0])
            fpt_means[idx] = float(fpt[1])
            fpt_sd[idx] = float(fpt[2])
    return fpt_means, fpt_sd, param_vary_name, param_vary_list

def read_varying_mean_sd_fpt_and_params(datafile, paramfile):
    params = read_params(dirname(paramfile), basename(paramfile))
    mean_fpt_varying, sd_fpt_varying, param_to_vary, param_set = read_varying_mean_sd_fpt(datafile)
    return mean_fpt_varying, sd_fpt_varying, param_to_vary, param_set, params[:-1], params[-1]

def collect_fpt_and_params(filedir):
    # NOTE: assumes folder structure 8s N files of ..._data and N files of ..._params which ALL correspond
    onlydirs = [f for f in listdir(filedir) if isfile(join(filedir, f))]
    datafiles = [f for f in onlyfiles if "data" == f[-8:-4]]
    paramfiles = [f for f in onlyfiles if "params" == f[-10:-4]]
    assert len(datafiles) == len(paramfiles)

    params_0 = read_params(filedir, basename(paramfiles[0]))
    for pf in paramfiles:
        params = read_params(filedir, basename(pf))
        assert params == params_0

    fpt_collected = []
    for idx, df in enumerate(datafiles):
        fp_times, params, system = read_fpt_and_params(filedir, df, basename(paramfiles[0]))
        fpt_collected += fp_times

    collected_dirname = "collected_%d" % len(fpt_collected)
    collected_dir = filedir + sep + collected_dirname
    mkdir(collected_dir)
    write_fpt_and_params(fpt_collected, params_0[:-1], params_0[-1], filedir=collected_dir, filename="fpt", filename_mod=collected_dirname)
    return collected_dir

def collect_fpt_mean_stats_and_params(filedir, dirbase="means"):
    # NOTE: assumes folder structure is filedir -> means1, means2, means3 ... collection of dirs
    #       each of these dirs contains an output folder with a params file and a data file

    def data_and_param_files_from_fptdir(fptdir):
        # input: location of filedir/means1 for example
        outputdir = fptdir + sep + "output"
        outputdirfiles = listdir(outputdir)
        assert len(outputdirfiles) == 2
        for f in outputdirfiles:
            if f[-10:-4] == "params":
                paramfile = f
            else:
                datafile = f
        return outputdir + sep + datafile, outputdir + sep + paramfile

    onlydirs = [f for f in listdir(filedir) if isdir(join(filedir, f))]
    dirstocheck = [filedir + sep + fptdir for fptdir in onlydirs if basename(fptdir)[:len(dirbase)] == dirbase]
    dirstocheck.sort(key=lambda f: int(filter(str.isdigit, f)))  # assumes dirbase+suffix (e.g.'means7') has only 1 int

    _, pf = data_and_param_files_from_fptdir(dirstocheck[0])
    print pf
    print dirname(pf), basename(pf)
    params_0 = read_params(dirname(pf), basename(pf))

    fpt_means_collected = []
    fpt_sd_collected = []
    param_vary_collected = []
    for idx, fptdir in enumerate(dirstocheck):
        df, pf = data_and_param_files_from_fptdir(fptdir)
        params = read_params(dirname(pf), basename(pf))
        assert params == params_0
        fpt_means, fpt_sd, param_vary_name, param_vary_list = read_varying_mean_sd_fpt(df)
        fpt_means_collected += fpt_means
        fpt_sd_collected += fpt_sd
        param_vary_collected += param_vary_list

    coll_dirname = "collected_stats_%d" % len(fpt_means_collected)
    collected_dir = filedir + sep + coll_dirname
    mkdir(collected_dir)
    coll_df, coll_pf = write_varying_mean_sd_fpt_and_params(fpt_means_collected, fpt_sd_collected, param_vary_name, param_vary_collected,
                                                            params_0[:-1], params_0[-1], filedir=collected_dir, filename_mod="collected")
    return coll_df, coll_pf

def write_matrix_data_and_idx_vals(arr, row_vals, col_vals, dataname, rowname, colname, output_dir=OUTPUT_DIR):
    datapath = output_dir + sep + dataname + ".txt"
    rowpath = output_dir + sep + dataname + '_' + rowname + ".txt"
    colpath = output_dir + sep + dataname + '_' + colname + ".txt"
    np.savetxt(datapath, np.array(arr), delimiter=",")
    np.savetxt(rowpath, np.array(row_vals), delimiter=",")
    np.savetxt(colpath, np.array(col_vals), delimiter=",")
    return datapath, rowpath, colpath

def read_matrix_data_and_idx_vals(datapath, rowpath, colpath):
    arr = np.loadtxt(datapath, delimiter=",", dtype=float)
    row = np.loadtxt(rowpath, delimiter=",", dtype=float)
    col = np.loadtxt(colpath, delimiter=",", dtype=float)
    return arr, row, col
<|endoftext|>


<commit_before>from django.db import models

from common.util.generator import get_random_id

class Student(models.Model):
    # ToDo: Make username unique
    username = models.CharField(max_length=7)
    magic_id = models.CharField(max_length=8)
    child = models.BooleanField()

    def __str__(self):
        return self.username

    def save(self, *args, **kwargs):
        if len(self.magic_id) == 0:
            self.magic_id = get_random_id()
        super(Student, self).save(*args, **kwargs)

    def get_new_student_popup(self):
        message = "Email has been sent to %s@ic.ac.uk. Go activate your account." % self.username
        return {'message': message, 'state': 'success'}

    def get_existing_student_popup(self):
        message = "Account already exists. Activation email re-sent to %s@ic.ac.uk." % self.username
        return {'message': message, 'state': 'warning'}
<commit_msg>Enforce unique user names in the database model<commit_after>from django.db import models

from common.util.generator import get_random_id

class Student(models.Model):
    username = models.CharField(max_length=7,unique=True)
    magic_id = models.CharField(max_length=8)
    child = models.BooleanField()

    def __str__(self):
        return self.username

    def save(self, *args, **kwargs):
        if len(self.magic_id) == 0:
            self.magic_id = get_random_id()
        super(Student, self).save(*args, **kwargs)

    def get_new_student_popup(self):
        message = "Email has been sent to %s@ic.ac.uk. Go activate your account." % self.username
        return {'message': message, 'state': 'success'}

    def get_existing_student_popup(self):
        message = "Account already exists. Activation email re-sent to %s@ic.ac.uk." % self.username
        return {'message': message, 'state': 'warning'}
<|endoftext|>


<commit_before>"""Ghost Buster. Static site generator for Ghost.

Usage:
  buster.py setup [--gh-repo=<repo-url>] [--dir=<path>]
  buster.py generate [--domain=<local-address>] [--dir=<path>] [--github-id=<github-id>]
  buster.py preview [--dir=<path>]
  buster.py deploy [--dir=<path>]
  buster.py add-domain <domain-name> [--dir=<path>]
  buster.py (-h | --help)
  buster.py --version

Options:
  -h --help                 Show this screen.
  --version                 Show version.
  --dir=<path>              Absolute path of directory to store static pages.
  --domain=<local-address>  Address of local ghost installation [default: localhost:2368].
  --github-id=<github-id>   Your Github ID for http://github-id.github.io URL
  --gh-repo=<repo-url>      URL of your gh-pages repository.
"""

import os
import re
import sys
import fnmatch
import shutil
import SocketServer
import SimpleHTTPServer
from docopt import docopt
from time import gmtime, strftime
from git import Repo
from pyquery import PyQuery

def main():
    arguments = docopt(__doc__, version='0.1.3')
    if arguments['--dir'] is not None:
        static_path = arguments['--dir']
    else:
        static_path = os.path.join(os.getcwd(), 'static')

    if arguments['--github-id'] is not None:
        github_url = "{}.github.io".format(arguments['--github-id'])
    else:
        github_url = None

    if arguments['generate']:
        command = ("wget "
                   "--recursive "             # follow links to download entire site
                   "--convert-links "         # make links relative
                   "--page-requisites "       # grab everything: css / inlined images
                   "--no-parent "             # don't go to parent level
                   "--directory-prefix {1} "  # download contents to static/ folder
                   "--no-host-directories "   # don't create domain named folder
                   "--restrict-file-name=unix "  # don't escape query string
                   "{0}").format(arguments['--domain'], static_path)
        os.system(command)

        # copy sitemap files since Ghost 0.5.7
        # from https://github.com/joshgerdes/buster/blob/f28bb10fc9522b8b1b1a74d8b74865562d9d5f9e/buster/buster.py
        base_command = "wget --convert-links --page-requisites --no-parent --directory-prefix {1} --no-host-directories --restrict-file-name=unix {0}/{2}"
        command = base_command.format(arguments['--domain'], static_path, "sitemap.xsl")
        os.system(command)
        command = base_command.format(arguments['--domain'], static_path, "sitemap.xml")
        os.system(command)
        command = base_command.format(arguments['--domain'], static_path, "sitemap-pages.xml")
        os.system(command)
        command = base_command.format(arguments['--domain'], static_path, "sitemap-posts.xml")
        os.system(command)
        command = base_command.format(arguments['--domain'], static_path, "sitemap-authors.xml")
        os.system(command)
        command = base_command.format(arguments['--domain'], static_path, "sitemap-tags.xml")
        os.system(command)

        # remove query string since Ghost 0.4
        file_regex = re.compile(r'.*?(\?.*)')
        for root, dirs, filenames in os.walk(static_path):
            for filename in filenames:
                if file_regex.match(filename):
                    newname = re.sub(r'\?.*', '', filename)
                    print "Rename", filename, "=>", newname
                    os.rename(os.path.join(root, filename), os.path.join(root, newname))

        # remove superfluous "index.html" from relative hyperlinks found in text
        abs_url_regex = re.compile(r'^(?:[a-z]+:)?//', flags=re.IGNORECASE)

        def fixLinks(text, parser):
            d = PyQuery(bytes(bytearray(text, encoding='utf-8')), parser=parser)
            for element in d('a'):
                e = PyQuery(element)
                href = e.attr('href')
                if not abs_url_regex.search(href):
                    new_href = re.sub(r'rss/index\.html$', 'rss/index.rss', href)
                    new_href = re.sub(r'/index\.html$', '/', new_href)
                    e.attr('href', new_href)
                    print "\t", href, "=>", new_href
            if parser == 'html':
                return "<!DOCTYPE html>\n" + pqd.html(method='html').encode('utf8') + ""
            return "<!DOCTYPE html>\n" + pqd.__unicode__().encode('utf8') + ""

        # fix links in all html files
        for root, dirs, filenames in os.walk(static_path):
            for filename in fnmatch.filter(filenames, "*.html"):
                filepath = os.path.join(root, filename)
                parser = 'html'
                if root.endswith("/rss"):  # rename rss index.html to index.rss
                    parser = 'xml'
                    newfilepath = os.path.join(root, os.path.splitext(filename)[0] + ".rss")
                    os.rename(filepath, newfilepath)
                    filepath = newfilepath
                with open(filepath) as f:
                    filetext = f.read().decode('utf8')
                print "fixing links in ", filepath
                newtext = fixLinks(filetext, parser)
                with open(filepath, 'w') as f:
                    f.write(newtext)

        def trans_local_domain_to_github_pages(text):
            modified_text = text.replace('localhost:2368', github_url)
            return modified_text

        def remove_v_tag_in_css_and_html(text):
            modified_text = re.sub(r"%3Fv=[\d|\w]+\.css", "", text)
            modified_text = re.sub(r".js%3Fv=[\d|\w]+", ".js", modified_text)
            return modified_text

        for root, dirs, filenames in os.walk(static_path):
            for filename in filenames:
                if filename.endswith(('.html', '.xml', '.xsl', '.rss')):
                    filepath = os.path.join(root, filename)
                    with open(filepath) as f:
                        filetext = f.read()
                    print "fixing local domain in ", filepath
                    newtext = trans_local_domain_to_github_pages(filetext)
                    newtext = remove_v_tag_in_css_and_html(newtext)
                    with open(filepath, 'w') as f:
                        f.write(newtext)

    elif arguments['preview']:
        os.chdir(static_path)

        Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        httpd = SocketServer.TCPServer(("", 9000), Handler)

        print "Serving at port 9000"
        # gracefully handle interrupt here
        httpd.serve_forever()

    elif arguments['setup']:
        if arguments['--gh-repo']:
            repo_url = arguments['--gh-repo']
        else:
            repo_url = raw_input("Enter the Github repository URL:\n").strip()

        # Create a fresh new static files directory
        if os.path.isdir(static_path):
            confirm = raw_input("This will destroy everything inside static/."
                                " Are you sure you want to continue? (y/N)").strip()
            if confirm != 'y' and confirm != 'Y':
                sys.exit(0)
            shutil.rmtree(static_path)

        # User/Organization page -> master branch
        # Project page -> gh-pages branch
        branch = 'gh-pages'
        regex = re.compile(".*[\w-]+\.github\.(?:io|com).*")
        if regex.match(repo_url):
            branch = 'master'

        # Prepare git repository
        repo = Repo.init(static_path)
        git = repo.git

        if branch == 'gh-pages':
            git.checkout(b='gh-pages')
        repo.create_remote('origin', repo_url)

        # Add README
        file_path = os.path.join(static_path, 'README.md')
        with open(file_path, 'w') as f:
            f.write('# Blog\nPowered by [Ghost](http://ghost.org) and [Buster](https://github.com/axitkhurana/buster/).\n')

        print "All set! You can generate and deploy now."

    elif arguments['deploy']:
        repo = Repo(static_path)
        repo.git.add('.')

        current_time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
        repo.index.commit('Blog update at {}'.format(current_time))

        origin = repo.remotes.origin
        repo.git.execute(['git', 'push', '-u', origin.name,
                         repo.active_branch.name])
        print "Good job! Deployed to Github Pages."

    elif arguments['add-domain']:
        repo = Repo(static_path)
        custom_domain = arguments['<domain-name>']

        file_path = os.path.join(static_path, 'CNAME')
        with open(file_path, 'w') as f:
            f.write(custom_domain + '\n')

        print "Added CNAME file to repo. Use `deploy` to deploy"

    else:
        print __doc__

if __name__ == '__main__':
    main()
<commit_msg>removes version tags from woff/ttf file and removes '.html' from css/png/jpg<commit_after>"""Ghost Buster. Static site generator for Ghost.

Usage:
  buster.py setup [--gh-repo=<repo-url>] [--dir=<path>]
  buster.py generate [--domain=<local-address>] [--dir=<path>] [--github-id=<github-id>]
  buster.py preview [--dir=<path>]
  buster.py deploy [--dir=<path>]
  buster.py add-domain <domain-name> [--dir=<path>]
  buster.py (-h | --help)
  buster.py --version

Options:
  -h --help                 Show this screen.
  --version                 Show version.
  --dir=<path>              Absolute path of directory to store static pages.
  --domain=<local-address>  Address of local ghost installation [default: localhost:2368].
  --github-id=<github-id>   Your Github ID for http://github-id.github.io URL
  --gh-repo=<repo-url>      URL of your gh-pages repository.
"""

import os
import re
import sys
import fnmatch
import shutil
import SocketServer
import SimpleHTTPServer
from docopt import docopt
from time import gmtime, strftime
from git import Repo
from pyquery import PyQuery

def main():
    arguments = docopt(__doc__, version='0.1.3')
    if arguments['--dir'] is not None:
        static_path = arguments['--dir']
    else:
        static_path = os.path.join(os.getcwd(), 'static')

    if arguments['--github-id'] is not None:
        github_url = "{}.github.io".format(arguments['--github-id'])
    else:
        github_url = None

    if arguments['generate']:
        command = ("wget "
                   "--recursive "             # follow links to download entire site
                   "--convert-links "         # make links relative
                   "--page-requisites "       # grab everything: css / inlined images
                   "--no-parent "             # don't go to parent level
                   "--directory-prefix {1} "  # download contents to static/ folder
                   "--no-host-directories "   # don't create domain named folder
                   "--restrict-file-name=unix "  # don't escape query string
                   "{0}").format(arguments['--domain'], static_path)
        os.system(command)

        # copy sitemap files since Ghost 0.5.7
        # from https://github.com/joshgerdes/buster/blob/f28bb10fc9522b8b1b1a74d8b74865562d9d5f9e/buster/buster.py
        base_command = "wget --convert-links --page-requisites --no-parent --directory-prefix {1} --no-host-directories --restrict-file-name=unix {0}/{2}"
        command = base_command.format(arguments['--domain'], static_path, "sitemap.xsl")
        os.system(command)
        command = base_command.format(arguments['--domain'], static_path, "sitemap.xml")
        os.system(command)
        command = base_command.format(arguments['--domain'], static_path, "sitemap-pages.xml")
        os.system(command)
        command = base_command.format(arguments['--domain'], static_path, "sitemap-posts.xml")
        os.system(command)
        command = base_command.format(arguments['--domain'], static_path, "sitemap-authors.xml")
        os.system(command)
        command = base_command.format(arguments['--domain'], static_path, "sitemap-tags.xml")
        os.system(command)

        # remove query string since Ghost 0.4
        file_regex = re.compile(r'.*?(\?.*)')
        for root, dirs, filenames in os.walk(static_path):
            for filename in filenames:
                if file_regex.match(filename):
                    newname = re.sub(r'\?.*', '', filename)
                    print "Rename", filename, "=>", newname
                    os.rename(os.path.join(root, filename), os.path.join(root, newname))

        # remove superfluous "index.html" from relative hyperlinks found in text
        abs_url_regex = re.compile(r'^(?:[a-z]+:)?//', flags=re.IGNORECASE)

        def fixLinks(text, parser):
            d = PyQuery(bytes(bytearray(text, encoding='utf-8')), parser=parser)
            for element in d('a'):
                e = PyQuery(element)
                href = e.attr('href')
                if not abs_url_regex.search(href):
                    new_href = re.sub(r'rss/index\.html$', 'rss/index.rss', href)
                    new_href = re.sub(r'/index\.html$', '/', new_href)
                    e.attr('href', new_href)
                    print "\t", href, "=>", new_href
            if parser == 'html':
                return "<!DOCTYPE html>\n" + d.html(method='html').encode('utf8') + ""
            return "<!DOCTYPE html>\n" + d.__unicode__().encode('utf8') + ""

        # fix links in all html files
        for root, dirs, filenames in os.walk(static_path):
            for filename in fnmatch.filter(filenames, "*.html"):
                filepath = os.path.join(root, filename)
                parser = 'html'
                if root.endswith("/rss"):  # rename rss index.html to index.rss
                    parser = 'xml'
                    newfilepath = os.path.join(root, os.path.splitext(filename)[0] + ".rss")
                    os.rename(filepath, newfilepath)
                    filepath = newfilepath
                with open(filepath) as f:
                    filetext = f.read().decode('utf8')
                print "fixing links in ", filepath
                newtext = fixLinks(filetext, parser)
                with open(filepath, 'w') as f:
                    f.write(newtext)

        def trans_local_domain_to_github_pages(text):
            modified_text = text.replace('localhost:2368', github_url)
            return modified_text

        def remove_v_tag_in_css_and_html(text):
            modified_text = re.sub(r"%3Fv=[\d|\w]+\.css", "", text)
            modified_text = re.sub(r".js%3Fv=[\d|\w]+", ".js", modified_text)
            modified_text = re.sub(r".woff%3Fv=[\d|\w]+", ".woff", modified_text)
            modified_text = re.sub(r".ttf%3Fv=[\d|\w]+", ".ttf", modified_text)

            modified_text = re.sub(r"css\.html", "css", modified_text)
            modified_text = re.sub(r"png\.html", "png", modified_text)
            modified_text = re.sub(r"jpg\.html", "jpg", modified_text)
            return modified_text

        for root, dirs, filenames in os.walk(static_path):
            for filename in filenames:
                if filename.endswith(('.html', '.xml', '.css', '.xsl', '.rss')):
                    filepath = os.path.join(root, filename)
                    with open(filepath) as f:
                        filetext = f.read()
                    print "fixing local domain in ", filepath
                    newtext = trans_local_domain_to_github_pages(filetext)
                    newtext = remove_v_tag_in_css_and_html(newtext)
                    with open(filepath, 'w') as f:
                        f.write(newtext)

    elif arguments['preview']:
        os.chdir(static_path)

        Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        httpd = SocketServer.TCPServer(("", 9000), Handler)

        print "Serving at port 9000"
        # gracefully handle interrupt here
        httpd.serve_forever()

    elif arguments['setup']:
        if arguments['--gh-repo']:
            repo_url = arguments['--gh-repo']
        else:
            repo_url = raw_input("Enter the Github repository URL:\n").strip()

        # Create a fresh new static files directory
        if os.path.isdir(static_path):
            confirm = raw_input("This will destroy everything inside static/."
                                " Are you sure you want to continue? (y/N)").strip()
            if confirm != 'y' and confirm != 'Y':
                sys.exit(0)
            shutil.rmtree(static_path)

        # User/Organization page -> master branch
        # Project page -> gh-pages branch
        branch = 'gh-pages'
        regex = re.compile(".*[\w-]+\.github\.(?:io|com).*")
        if regex.match(repo_url):
            branch = 'master'

        # Prepare git repository
        repo = Repo.init(static_path)
        git = repo.git

        if branch == 'gh-pages':
            git.checkout(b='gh-pages')
        repo.create_remote('origin', repo_url)

        # Add README
        file_path = os.path.join(static_path, 'README.md')
        with open(file_path, 'w') as f:
            f.write('# Blog\nPowered by [Ghost](http://ghost.org) and [Buster](https://github.com/axitkhurana/buster/).\n')

        print "All set! You can generate and deploy now."

    elif arguments['deploy']:
        repo = Repo(static_path)
        repo.git.add('.')

        current_time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
        repo.index.commit('Blog update at {}'.format(current_time))

        origin = repo.remotes.origin
        repo.git.execute(['git', 'push', '-u', origin.name,
                         repo.active_branch.name])
        print "Good job! Deployed to Github Pages."

    elif arguments['add-domain']:
        repo = Repo(static_path)
        custom_domain = arguments['<domain-name>']

        file_path = os.path.join(static_path, 'CNAME')
        with open(file_path, 'w') as f:
            f.write(custom_domain + '\n')

        print "Added CNAME file to repo. Use `deploy` to deploy"

    else:
        print __doc__

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