<commit_before>#!/usr/bin/env python
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Willow Garage, Inc. nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# Author Tully Foote/tfoote@willowgarage.com

"""
Library for detecting the current OS, including detecting specific
Linux distributions.

The APIs of this library are still very coupled with the rosdep
command-line tool.
"""

from __future__ import with_statement

import roslib.exceptions
import roslib.rospack
import roslib.stacks
import os
import sys
import subprocess
import types
import tempfile
import yaml

####### Linux Helper Functions #####
def lsb_get_os():
    """
    Linux: wrapper around lsb_release to get the current OS
    """
    try:
        cmd = ['lsb_release', '-si']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

def lsb_get_codename():
    """
    Linux: wrapper around lsb_release to get the current OS codename
    """
    try:
        cmd = ['lsb_release', '-sc']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

def lsb_get_version():
    """
    Linux: wrapper around lsb_release to get the current OS version
    """
    try:
        cmd = ['lsb_release', '-sr']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

class OSDetectException(roslib.exceptions.ROSLibException): pass

class OSBase:
    def check_presence(self):
        raise OSDetectException("check_presence unimplemented")

    def get_name(self):
        raise OSDetectException("check_presence unimplemented")

    def get_version(self):
        raise OSDetectException("check_presence unimplemented")

###### Debian SPECIALIZATION #########################
class Debian(OSBase):
    """
    Detect Debian OS.
    """
    def check_presence(self):
        if "Debian" == lsb_get_os():
            return True
        return False

    def get_version(self):
        return lsb_get_codename()
    def get_name(self):
        return "debian"

###### END Debian SPECIALIZATION ########################

###### UBUNTU SPECIALIZATION #########################
class Ubuntu(Debian):
    """ This is an implementation of a standard interface for
    interacting with rosdep.  This defines all Ubuntu sepecific
    methods, including detecting the OS/Version number.  As well as
    how to check for and install packages."""
    def check_presence(self):
        if "Ubuntu" == lsb_get_os():
            return True
        return False

    def get_version(self):
        return lsb_get_version()
    def get_name(self):
        return "ubuntu"

###### END UBUNTU SPECIALIZATION ########################

###### Mint SPECIALIZATION #########################
class Mint(OSBase):
    """
    Detect Mint variants of Debian.
    """
    def check_presence(self):
        if "LinuxMint" == lsb_get_os():
            return True
        return False

    def get_version(self):
        return lsb_get_version()

    def get_name(self):
        return "mint"
###### END Mint SPECIALIZATION ########################

###### Fedora SPECIALIZATION #########################
class Fedora(OSBase):
    """
    Detect Fedora OS.
    """
    def check_presence(self):
        try:
            filename = "/etc/redhat-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[0] == "Fedora" and os_list[1] == "release":
                    return True
        except:
            pass
        return False

    def get_version(self):
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Fedora" and os_list[1] == "release":
                    return os_list[2]
        except:
            print "Fedora failed to get version"
            return False

        return False

    def get_name(self):
        return "fedora"

###### END Fedora SPECIALIZATION ########################

###### Rhel SPECIALIZATION #########################
class Rhel(Fedora):
    """
    Detect Redhat OS.
    """
    def check_presence(self):
        try:
            filename = "/etc/redhat-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[2] == "Enterprise":
                    return True
        except:
            pass
        return False

    def get_version(self):
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[2] == "Enterprise":
                    return os_list[6]
        except:
            print "Rhel failed to get version"
            return False

        return False

    def get_name(self):
        return "rhel"

###### END Rhel SPECIALIZATION ########################

###### Macports SPECIALIZATION #########################
def port_detect(p):
    """
    Detect presence of Macports by running "port installed" command.
    """
    cmd = ['port', 'installed', p]
    pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (std_out, std_err) = pop.communicate()

    return (std_out.count("(active)") > 0)

class Macports(OSBase):
    """
    Detect OS X and Macports.
    """
    def check_presence(self):
        filename = "/usr/bin/sw_vers"
        if os.path.exists(filename):
            return True
        return False

    def get_version(self):
        return "macports" # macports is a rolling release and isn't versionsed

    def get_name(self):
        return "macports"

###### END Macports SPECIALIZATION ########################

###### Arch SPECIALIZATION #########################
class Arch(OSBase):
    """
    Detect Arch Linux.
    """

    def check_presence(self):
        filename = "/etc/arch-release"
        if os.path.exists(filename):
            return True
        return False

    def get_version(self):
        return ""
        # arch didn't have a version parsing in cpp version
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Linux" and os_list[1] == "Arch":
                    return os_list[2]
        except:
            print "Arch failed to get version"
            return False

        return False

    def get_name(self):
        return "arch"

###### END Arch SPECIALIZATION ########################

###### Cygwin SPECIALIZATION #########################
class Cygwin(OSBase):
    """
    Detect Cygwin presence on Windows OS.
    """
    def check_presence(self):
        filename = "/usr/bin/cygwin1.dll"
        if os.path.exists(filename):
            return True
        return False

    def get_version(self):
        cmd = ['uname','-r'];
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()

    def get_name(self):
        return "cygwin"

###### END Cygwin SPECIALIZATION ########################

###### Gentoo Sepcialization ###############################
class Gentoo(OSBase):
    """
    Detect Gentoo OS.
    """
    def check_presence(self):
        try:
            filename = "/etc/gentoo-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[0] == "Gentoo" and os_list[1] == "Base":
                    return True
        except:
            pass#print >> sys.stderr, "Gentoo failed to detect OS"
        return False

    def get_version(self):
        try:
            filename = "/etc/gentoo-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Gentoo" and os_list[1] == "Base":
                    return os_list[4]
        except:
            print >> sys.stderr, "Gentoo failed to get version"
            return False

        return False

    def get_name(self):
        return "gentoo"

###### END Gentoo Sepcialization ###############################

#### Override class for debugging and unsupported OSs ###########
class Override(OSBase):
    def __init__(self):
        self._os_name = "uninitialized from ROS_OS_OVERRIDE=name:version"
        self._os_version = "uninitialized from ROS_OS_OVERRIDE=name:version"

    def check_presence(self):
        try:
            (self._os_name, self._os_version) = os.environ["ROS_OS_OVERRIDE"].split(':')
            print >> sys.stderr, "Using environment variable ROS_OS_OVERRIDE=name:version"
            return True
        except:
            return False

    def get_version(self):
        return self._os_version

    def get_name(self):
        return self._os_name

class OSDetect:
    """ This class will iterate over registered classes to lookup the
    active OS and version"""
    def __init__(self, os_list = [Debian(), Ubuntu(), Mint(), Macports(), Arch(), Fedora(), Rhel(), Gentoo(), Cygwin()]):
        self._os_list = [ Override()]
        self._os_list.extend(os_list)

        self._os_class = None
        self._os_name = None
        self._os_version = None

        self.detect_os()

    def add_os(self, class_ref):
        self._os_list.append(class_ref)

        # \TODO look at throwing here
    def detect_os(self):
        for os_class in self._os_list:
            if os_class.check_presence():
                self._os_name = os_class.get_name()
                self._os_version = os_class.get_version()
                self._os_class = os_class
                return True
        attempted_oss = [o.get_name() for o in self._os_list]
        raise OSDetectException("Could not detect OS, tried %s"%attempted_oss)
        return False

    def get_os(self):
        if not self._os_class:
            self.detect_os()
        return self._os_class

    def get_name(self):
        if not self._os_name:
            self.detect_os()
        return self._os_name

    def get_version(self):
        if not self._os_version:
            not self.detect_os()
        return self._os_version

<commit_msg>fixing error message in exception and adding exception on malformed os detect class not from OSBase. similar to work for #2358 <commit_after>#!/usr/bin/env python
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Willow Garage, Inc. nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# Author Tully Foote/tfoote@willowgarage.com

"""
Library for detecting the current OS, including detecting specific
Linux distributions.

The APIs of this library are still very coupled with the rosdep
command-line tool.
"""

from __future__ import with_statement

import roslib.exceptions
import roslib.rospack
import roslib.stacks
import os
import sys
import subprocess
import types
import tempfile
import yaml

####### Linux Helper Functions #####
def lsb_get_os():
    """
    Linux: wrapper around lsb_release to get the current OS
    """
    try:
        cmd = ['lsb_release', '-si']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

def lsb_get_codename():
    """
    Linux: wrapper around lsb_release to get the current OS codename
    """
    try:
        cmd = ['lsb_release', '-sc']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

def lsb_get_version():
    """
    Linux: wrapper around lsb_release to get the current OS version
    """
    try:
        cmd = ['lsb_release', '-sr']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

class OSDetectException(roslib.exceptions.ROSLibException): pass

class OSBase:
    def check_presence(self):
        raise OSDetectException("check_presence unimplemented")

    def get_name(self):
        raise OSDetectException("get_name unimplemented")

    def get_version(self):
        raise OSDetectException("get_version unimplemented")

###### Debian SPECIALIZATION #########################
class Debian(OSBase):
    """
    Detect Debian OS.
    """
    def check_presence(self):
        if "Debian" == lsb_get_os():
            return True
        return False

    def get_version(self):
        return lsb_get_codename()
    def get_name(self):
        return "debian"

###### END Debian SPECIALIZATION ########################

###### UBUNTU SPECIALIZATION #########################
class Ubuntu(Debian):
    """ This is an implementation of a standard interface for
    interacting with rosdep.  This defines all Ubuntu sepecific
    methods, including detecting the OS/Version number.  As well as
    how to check for and install packages."""
    def check_presence(self):
        if "Ubuntu" == lsb_get_os():
            return True
        return False

    def get_version(self):
        return lsb_get_version()
    def get_name(self):
        return "ubuntu"

###### END UBUNTU SPECIALIZATION ########################

###### Mint SPECIALIZATION #########################
class Mint(OSBase):
    """
    Detect Mint variants of Debian.
    """
    def check_presence(self):
        if "LinuxMint" == lsb_get_os():
            return True
        return False

    def get_version(self):
        return lsb_get_version()

    def get_name(self):
        return "mint"
###### END Mint SPECIALIZATION ########################

###### Fedora SPECIALIZATION #########################
class Fedora(OSBase):
    """
    Detect Fedora OS.
    """
    def check_presence(self):
        try:
            filename = "/etc/redhat-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[0] == "Fedora" and os_list[1] == "release":
                    return True
        except:
            pass
        return False

    def get_version(self):
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Fedora" and os_list[1] == "release":
                    return os_list[2]
        except:
            print "Fedora failed to get version"
            return False

        return False

    def get_name(self):
        return "fedora"

###### END Fedora SPECIALIZATION ########################

###### Rhel SPECIALIZATION #########################
class Rhel(Fedora):
    """
    Detect Redhat OS.
    """
    def check_presence(self):
        try:
            filename = "/etc/redhat-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[2] == "Enterprise":
                    return True
        except:
            pass
        return False

    def get_version(self):
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[2] == "Enterprise":
                    return os_list[6]
        except:
            print "Rhel failed to get version"
            return False

        return False

    def get_name(self):
        return "rhel"

###### END Rhel SPECIALIZATION ########################

###### Macports SPECIALIZATION #########################
def port_detect(p):
    """
    Detect presence of Macports by running "port installed" command.
    """
    cmd = ['port', 'installed', p]
    pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (std_out, std_err) = pop.communicate()

    return (std_out.count("(active)") > 0)

class Macports(OSBase):
    """
    Detect OS X and Macports.
    """
    def check_presence(self):
        filename = "/usr/bin/sw_vers"
        if os.path.exists(filename):
            return True
        return False

    def get_version(self):
        return "macports" # macports is a rolling release and isn't versionsed

    def get_name(self):
        return "macports"

###### END Macports SPECIALIZATION ########################

###### Arch SPECIALIZATION #########################
class Arch(OSBase):
    """
    Detect Arch Linux.
    """

    def check_presence(self):
        filename = "/etc/arch-release"
        if os.path.exists(filename):
            return True
        return False

    def get_version(self):
        return ""
        # arch didn't have a version parsing in cpp version
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Linux" and os_list[1] == "Arch":
                    return os_list[2]
        except:
            print "Arch failed to get version"
            return False

        return False

    def get_name(self):
        return "arch"

###### END Arch SPECIALIZATION ########################

###### Cygwin SPECIALIZATION #########################
class Cygwin(OSBase):
    """
    Detect Cygwin presence on Windows OS.
    """
    def check_presence(self):
        filename = "/usr/bin/cygwin1.dll"
        if os.path.exists(filename):
            return True
        return False

    def get_version(self):
        cmd = ['uname','-r'];
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()

    def get_name(self):
        return "cygwin"

###### END Cygwin SPECIALIZATION ########################

###### Gentoo Sepcialization ###############################
class Gentoo(OSBase):
    """
    Detect Gentoo OS.
    """
    def check_presence(self):
        try:
            filename = "/etc/gentoo-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[0] == "Gentoo" and os_list[1] == "Base":
                    return True
        except:
            pass#print >> sys.stderr, "Gentoo failed to detect OS"
        return False

    def get_version(self):
        try:
            filename = "/etc/gentoo-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Gentoo" and os_list[1] == "Base":
                    return os_list[4]
        except:
            print >> sys.stderr, "Gentoo failed to get version"
            return False

        return False

    def get_name(self):
        return "gentoo"

###### END Gentoo Sepcialization ###############################

#### Override class for debugging and unsupported OSs ###########
class Override(OSBase):
    def __init__(self):
        self._os_name = "uninitialized from ROS_OS_OVERRIDE=name:version"
        self._os_version = "uninitialized from ROS_OS_OVERRIDE=name:version"

    def check_presence(self):
        try:
            (self._os_name, self._os_version) = os.environ["ROS_OS_OVERRIDE"].split(':')
            print >> sys.stderr, "Using environment variable ROS_OS_OVERRIDE=name:version"
            return True
        except:
            return False

    def get_version(self):
        return self._os_version

    def get_name(self):
        return self._os_name

class OSDetect:
    """ This class will iterate over registered classes to lookup the
    active OS and version"""
    def __init__(self, os_list = [Debian(), Ubuntu(), Mint(), Macports(), Arch(), Fedora(), Rhel(), Gentoo(), Cygwin()]):
        self._os_list = [ Override()]
        self._os_list.extend(os_list)
        for o in self._os_list:
            if not isinstance(o, OSBase):
                raise OSDetectException("Class [%s] not derived from OSBase"%o.__class__.__name__)

        self._os_class = None
        self._os_name = None
        self._os_version = None

        self.detect_os()

    def add_os(self, class_ref):
        self._os_list.append(class_ref)

        # \TODO look at throwing here
    def detect_os(self):
        for os_class in self._os_list:
            if os_class.check_presence():
                self._os_name = os_class.get_name()
                self._os_version = os_class.get_version()
                self._os_class = os_class
                return True
        attempted_oss = [o.get_name() for o in self._os_list]
        raise OSDetectException("Could not detect OS, tried %s"%attempted_oss)
        return False

    def get_os(self):
        if not self._os_class:
            self.detect_os()
        return self._os_class

    def get_name(self):
        if not self._os_name:
            self.detect_os()
        return self._os_name

    def get_version(self):
        if not self._os_version:
            not self.detect_os()
        return self._os_version

<|endoftext|>


<commit_before>from typing import List, Union
Num = Union[int, float]

from manimlib.imports import *

class GangAnimation(GraphScene):

    CONFIG = {
        "x_min": 0,
        "x_max": 15,
        "y_min": 0,
        "y_max": 4,
        "y_axis_height": 3,
        "x_axis_label": "Time",
        "y_axis_label": "Core",
    }

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.colors = []

    def construct(self):

        self.colors = ["#67EB34", "#F269FF"]

        self.setup_axes(animate=True)
        self.play(
            ApplyMethod(self.x_axis_label_mob.set_color, "#000000"),
            ApplyMethod(self.y_axis_label_mob.set_color, "#000000")
        )
        self.play(ApplyMethod(self.y_axis_label_mob.shift, 1.5*LEFT))

        tasks = [[[5, 3], [7.5, 2], [15, 1]], [[6, 2], [12, 1]]]

        for task, color in zip(tasks, self.colors):
            main_rec = None
            for width, height in task:
                rec = self.create_coords_rec(width, height, color)
                rec.move_to(self.coords_to_point(width/2, height/2))
                if main_rec is not None:
                    self.play(Transform(main_rec, rec))
                else:
                    main_rec = rec
                    self.bring_to_back(main_rec)
                    self.play(Write(rec))
                self.wait(1)
            self.play(FadeOut(main_rec))

        # main_rec = self.show_first_gangs()
        #
        # task_2 = [6, 2]
        # task_2_rec = self.create_coords_rec(task_2[0], task_2[1], self.colors[1])
        # task_2_rec.move_to(self.coords_to_point(task_2[0]/2, 3 + task_2[1]/2))
        # self.bring_to_back(task_2_rec)
        # self.play(ShowCreation(task_2_rec))

    def show_first_gangs(self) -> Rectangle:
        gang_tasks = [[5, 3], [7.5, 2], [15, 1]]

        original_rec = None
        main_rec = None
        for width, height in gang_tasks:
            rec = self.create_coords_rec(width, height, self.colors[0])
            rec.move_to(self.coords_to_point(width/2, height/2))
            self.bring_to_back(rec)
            if main_rec is not None:
                self.play(Transform(main_rec, rec))
            else:
                original_rec = rec.deepcopy()
                self.remove(original_rec)
                main_rec = rec
                self.play(Write(rec))

            self.wait(2)

        self.play(Transform(main_rec, original_rec))
        return main_rec

    def create_coords_rec(self, width: Num, height: Num, color: str = "#000000") -> Rectangle:
        point = self.coords_to_point(width, height)
        point -= self.coords_to_point(0, 0)
        rec = Rectangle(width=point[0], height=point[1], color=color)
        rec.set_fill(color, opacity=1.)
        return rec

if __name__ == "__main__":
    module_name = os.path.basename(__file__)
    os.system(f"manim -p -m -c white {module_name} {sys.argv[1]}")
<commit_msg>Add proper rectangles generation<commit_after>from typing import List, Union

Num = Union[int, float]

from manimlib.imports import *

class GangAnimation(GraphScene):
    CONFIG = {
        "x_min": 0,
        "x_max": 15,
        "y_min": 0,
        "y_max": 4,
        "y_axis_height": 3,
        "x_axis_label": "Time",
        "y_axis_label": "Core",
    }

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.colors = []

    def construct(self):

        self.colors = ["#67EB34", "#F269FF"]

        self.setup_axes(animate=True)
        self.play(
            ApplyMethod(self.x_axis_label_mob.set_color, "#000000"),
            ApplyMethod(self.y_axis_label_mob.set_color, "#000000")
        )
        self.play(ApplyMethod(self.y_axis_label_mob.shift, 1.5*LEFT))

        tasks = [[[5, 3], [7.5, 2], [15, 1]], [[6, 2], [12, 1]]]
        self.show_possible_executions(tasks)
        self.show_schedules(tasks)

        self.wait(5)

    def show_possible_executions(self, tasks: List[List[List[int]]]):
        for task, color in zip(tasks, self.colors):
            main_rec = None
            for width, height in task:
                rec = self.create_coords_rec(width, height, color)
                rec.move_to(self.coords_to_point(width/2, height/2))
                if main_rec is not None:
                    self.play(Transform(main_rec, rec))
                else:
                    main_rec = rec
                    self.bring_to_back(main_rec)
                    self.play(Write(rec))
                self.wait(1)
            self.play(FadeOut(main_rec))

    def show_schedules(self, tasks: List[List[List[int]]]):
        core = 0
        recs = []
        for task, color in zip(tasks, self.colors):
            rec = self.create_coords_rec(task[0][0], task[0][1], color)
            rec.move_to(self.coords_to_point(task[0][0]/2, task[0][1]/2 + core))
            recs.append(rec)
            core += task[0][1]
            self.bring_to_back(rec)
            self.play(Write(rec))
            self.wait(1)

        # Create possible task sets
        task_sets = self.get_schedules(tasks)
        print(task_sets
              )

        new_rec = [
            self.create_coords_rec(tasks[0][1][0], tasks[0][1][1], self.colors[0]),
            self.create_coords_rec(tasks[1][0][0], tasks[1][0][1], self.colors[1]),
        ]
        new_rec[0].move_to(self.coords_to_point(tasks[0][1][0]/2, tasks[0][1][1]/2))
        new_rec[1].move_to(self.coords_to_point(tasks[1][0][0]/2, tasks[1][0][1]/2 + tasks[0][1][1]))
        self.play(Transform(recs[0], new_rec[0]), Transform(recs[1], new_rec[1]))

    def create_coords_rec(self, width: Num, height: Num, color: str = "#000000",
                          color_stroke: str = "#000000") -> Rectangle:
        point = self.coords_to_point(width, height)
        point -= self.coords_to_point(0, 0)
        rec = Rectangle(width=point[0], height=point[1], color=color)
        rec.set_stroke(color_stroke, opacity=1.)
        rec.set_fill(color, opacity=1.)
        return rec

    def get_schedules(self, tasks: List[List[List[int]]], task: int = 0, sched: List[List[int]] = None, cores = 0) -> List[List[List[int]]]:
        if sched is None:
            sched = []
        if cores > 4:
            return []
        elif task == len(tasks):
            return [sched]

        all_schedules = []
        for i, opts in enumerate(tasks[task]):
            all_schedules += self.get_schedules(tasks, task + 1, sched + [[task, i]], cores + opts[1])

        return all_schedules

if __name__ == "__main__":
    module_name = os.path.basename(__file__)
    os.system(f"manim --leave_progress_bars -p -m -c white {module_name} {sys.argv[1]}")
<|endoftext|>


<commit_before>'''
Author: Michael Asquith, Aaron Gruneklee
Created: 2014.12.12
Last Modified: 2014.12.23

The brain of the robot. Has access to the robot's I/O, interpreter and
instructions
'''

from interpreter import Interpreter
from robotio import RobotIO
from codeblock import CodeBlock
from maze import Maze

class RobotController():
    """studentProgram is the robot's instructions"""

    def __init__(self, studentProgram):
        self.program = studentProgram
        self.maze = Maze()
        self.robotio = RobotIO(self.maze)

    def executeProgram(self):
        """Executes the robot's program"""
        code = open('user_file',r)
        inter = Interpreter(self.robotio)
        for line in self.program:
            inter.interpret(code)

    def getRobotLocationAndFacing():
        return self.robotio.getLocationAndFacing()

    def getMaze():
        return self.maze.getMaze()
<commit_msg>Removed reference to studentProgram<commit_after>'''
Author: Michael Asquith, Aaron Gruneklee
Created: 2014.12.12
Last Modified: 2014.12.23

The brain of the robot. Has access to the robot's I/O, interpreter and
instructions
'''

from interpreter import Interpreter
from robotio import RobotIO
from codeblock import CodeBlock
from maze import Maze

class RobotController():
    """studentProgram is the robot's instructions"""

    def __init__(self):
        self.maze = Maze()
        self.robotio = RobotIO(self.maze)

    def executeProgram(self):
        """Executes the robot's program"""
        code = open('user_file',r)
        inter = Interpreter(self.robotio)
        for line in self.program:
            inter.interpret(code)

    def getRobotLocationAndFacing():
        return self.robotio.getLocationAndFacing()

    def getMaze():
        return self.maze.getMaze()
<|endoftext|>


<commit_before>#! usr/bin/python
'''
Class to handle database connections and queries for
Dropbox Mirror Bot
'''

import sqlite3

from utils import generate_thing_ids

class Database(object):

    def __init__(self, database=":memory:"):
        self._database = database
        c = self.cursor()
        query = '''CREATE TABLE IF NOT EXISTS dropbox_submissions (
                   processed_id INTEGER PRIMARY KEY ASC,
                   submission_id VARCHAR(10) UNIQUE)'''
        c.execute(query)
        self.conn.commit()

    @property
    def conn(self):
        if not hasattr(self, '_connection'):
            self._connection = sqlite3.connect(self._database)
        return self._connection

    def cursor(self):
        return self.conn.cursor()

    def close(self):
        self.conn.close()

    def is_processed(self, submission_id):
        '''Return true if the submission has already been processed'''

        c = self.cursor()
        query = '''SELECT submission_id FROM dropbox_submissions
                   WHERE submission_id = (?)'''

        c.execute(query, (submission_id,))
        if c.fetchone():
            return True
        return False

    def mark_as_processed(self, submission_id):
        c = self.cursor()
        query = '''INSERT INTO dropbox_submissions (submission_id)
                   VALUES (?)'''
        c.execute(query , (submission_id,))
        self.conn.commit()

if __name__ == '__main__':
    pass
# Add some random thing_ids for testing
#
#    db = Database('processed.db')
#    for thing_id in generate_thing_ids(10):
#        db.mark_as_processed(thing_id)
<commit_msg>Removed unneccessary imports<commit_after>#! usr/bin/python
'''
Class to handle database connections and queries for
Dropbox Mirror Bot
'''

import sqlite3

class Database(object):

    def __init__(self, database=":memory:"):
        self._database = database
        c = self.cursor()
        query = '''CREATE TABLE IF NOT EXISTS dropbox_submissions (
                   processed_id INTEGER PRIMARY KEY ASC,
                   submission_id VARCHAR(10) UNIQUE)'''
        c.execute(query)
        self.conn.commit()

    @property
    def conn(self):
        if not hasattr(self, '_connection'):
            self._connection = sqlite3.connect(self._database)
        return self._connection

    def cursor(self):
        return self.conn.cursor()

    def close(self):
        self.conn.close()

    def is_processed(self, submission_id):
        '''Return true if the submission has already been processed'''

        c = self.cursor()
        query = '''SELECT submission_id FROM dropbox_submissions
                   WHERE submission_id = (?)'''

        c.execute(query, (submission_id,))
        if c.fetchone():
            return True
        return False

    def mark_as_processed(self, submission_id):
        c = self.cursor()
        query = '''INSERT INTO dropbox_submissions (submission_id)
                   VALUES (?)'''
        c.execute(query , (submission_id,))
        self.conn.commit()

if __name__ == '__main__':
    pass
# Add some random thing_ids for testing
#
#    db = Database('processed.db')
#    for thing_id in generate_thing_ids(10):
#        db.mark_as_processed(thing_id)
<|endoftext|>


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

from functools import wraps
from tweepy import TweepError, OAuthHandler as TwitterOAuthHandler, API as TwitterAPI
from httplib import BadStatusLine
from ssl import SSLError
from socket import error as socket_error, gaierror
from flask.ext.oauth import OAuth, OAuthException  # OAuth 1.0a
from lastuser_core.registry import LoginProvider, LoginInitError, LoginCallbackError

__all__ = ['TwitterProvider']

def twitter_exception_handler(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except (OAuthException, BadStatusLine, AttributeError, socket_error, gaierror) as e:
            raise LoginCallbackError(e)
    return decorated_function

class TwitterProvider(LoginProvider):
    at_username = True

    def __init__(self, name, title, key, secret, access_key, access_secret, at_login=True, priority=True, icon=None):
        self.name = name
        self.title = title
        self.at_login = at_login
        self.priority = priority
        self.icon = icon
        self.consumer_key = key
        self.consumer_secret = secret
        self.access_key = access_key
        self.access_secret = access_secret
        oauth = OAuth()
        twitter = oauth.remote_app('twitter',
            base_url='https://api.twitter.com/1/',
            request_token_url='https://api.twitter.com/oauth/request_token',
            access_token_url='https://api.twitter.com/oauth/access_token',
            authorize_url='https://api.twitter.com/oauth/authenticate',
            consumer_key=key,
            consumer_secret=secret,
            )

        twitter.tokengetter(lambda token=None: None)  # We have no use for tokengetter

        self.callback = twitter_exception_handler(twitter.authorized_handler(self.unwrapped_callback))
        self.twitter = twitter

    def do(self, callback_url):
        try:
            return self.twitter.authorize(callback=callback_url)
        except (OAuthException, BadStatusLine, SSLError, gaierror), e:
            raise LoginInitError(e)

    def unwrapped_callback(self, resp):
        if resp is None:
            raise LoginCallbackError("You denied the request to login")

        # Try to read more from the user's Twitter profile
        auth = TwitterOAuthHandler(self.consumer_key, self.consumer_secret)
        if self.access_key is not None and self.access_secret is not None:
            auth.set_access_token(self.access_key, self.access_secret)
        else:
            auth.set_access_token(resp['oauth_token'], resp['oauth_token_secret'])
        api = TwitterAPI(auth)
        try:
            twinfo = api.lookup_users(user_ids=[resp['user_id']])[0]
            fullname = twinfo.name
            avatar_url = twinfo.profile_image_url_https.replace("_normal.", "_bigger.")
        except TweepError:
            fullname = None
            avatar_url = None

        return {'userid': resp['user_id'],
                'username': resp['screen_name'],
                'fullname': fullname,
                'avatar_url': avatar_url,
                'oauth_token': resp['oauth_token'],
                'oauth_token_secret': resp['oauth_token_secret'],
                'oauth_token_type': None,  # Twitter doesn't have token types
                }
<commit_msg>Handle socket errors in Twitter init.<commit_after># -*- coding: utf-8 -*-

from functools import wraps
from tweepy import TweepError, OAuthHandler as TwitterOAuthHandler, API as TwitterAPI
from httplib import BadStatusLine
from ssl import SSLError
from socket import error as socket_error, gaierror
from flask.ext.oauth import OAuth, OAuthException  # OAuth 1.0a
from lastuser_core.registry import LoginProvider, LoginInitError, LoginCallbackError

__all__ = ['TwitterProvider']

def twitter_exception_handler(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except (OAuthException, BadStatusLine, AttributeError, socket_error, gaierror) as e:
            raise LoginCallbackError(e)
    return decorated_function

class TwitterProvider(LoginProvider):
    at_username = True

    def __init__(self, name, title, key, secret, access_key, access_secret, at_login=True, priority=True, icon=None):
        self.name = name
        self.title = title
        self.at_login = at_login
        self.priority = priority
        self.icon = icon
        self.consumer_key = key
        self.consumer_secret = secret
        self.access_key = access_key
        self.access_secret = access_secret
        oauth = OAuth()
        twitter = oauth.remote_app('twitter',
            base_url='https://api.twitter.com/1/',
            request_token_url='https://api.twitter.com/oauth/request_token',
            access_token_url='https://api.twitter.com/oauth/access_token',
            authorize_url='https://api.twitter.com/oauth/authenticate',
            consumer_key=key,
            consumer_secret=secret,
            )

        twitter.tokengetter(lambda token=None: None)  # We have no use for tokengetter

        self.callback = twitter_exception_handler(twitter.authorized_handler(self.unwrapped_callback))
        self.twitter = twitter

    def do(self, callback_url):
        try:
            return self.twitter.authorize(callback=callback_url)
        except (OAuthException, BadStatusLine, SSLError, socket_error, gaierror), e:
            raise LoginInitError(e)

    def unwrapped_callback(self, resp):
        if resp is None:
            raise LoginCallbackError("You denied the request to login")

        # Try to read more from the user's Twitter profile
        auth = TwitterOAuthHandler(self.consumer_key, self.consumer_secret)
        if self.access_key is not None and self.access_secret is not None:
            auth.set_access_token(self.access_key, self.access_secret)
        else:
            auth.set_access_token(resp['oauth_token'], resp['oauth_token_secret'])
        api = TwitterAPI(auth)
        try:
            twinfo = api.lookup_users(user_ids=[resp['user_id']])[0]
            fullname = twinfo.name
            avatar_url = twinfo.profile_image_url_https.replace("_normal.", "_bigger.")
        except TweepError:
            fullname = None
            avatar_url = None

        return {'userid': resp['user_id'],
                'username': resp['screen_name'],
                'fullname': fullname,
                'avatar_url': avatar_url,
                'oauth_token': resp['oauth_token'],
                'oauth_token_secret': resp['oauth_token_secret'],
                'oauth_token_type': None,  # Twitter doesn't have token types
                }
<|endoftext|>


<commit_before># ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2020, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

import os
from operator import itemgetter
from pathlib import Path
from shutil import copytree

class SaveFinalCheckpoint(object):
    """
    Experiment mixin to save a copy of the final checkpoint from `logdir` upon
    `stop_experiment`.

    :param config:

        - copy_checkpoint_dir: (optional) name of directory to save a copy of the final
                               checkpoint. The string may contain:
                                    * {name} - name of experiment; taken from config
                                    * {wandb_project} - taken from wandb_args
                                    * {trainable_id} - inferred from logdir

    Example
    ```
    config["name"] = "my_experiment"
    config["logdir"] = ".../ImagenetTrainable_...-45xrhk_mc2"
    config["wandb_args"] = dict(..., project="my_project")
    config["copy_checkpoint_dir"] = "/mnt/.../{wandb_project}/{name}/{trainable_id}/"

    ... 200 epochs of training

    >> checkpoint saved to "/mnt/.../my_project/my_experiment/45xrhk_mc2/checkpoint_200"
    ```
    """

    def setup_experiment(self, config):
        """Setup experiment and get desired path for copied checkpoint."""
        super().setup_experiment(config)

        assert self.logdir
        assert "copy_checkpoint_dir" in config

        # Set default formatting.
        self.copy_checkpoint_dir = config["copy_checkpoint_dir"]
        self.name = config.get("name", "unknown_name")
        self.wandb_project = "unknown_project"
        self.trainable_id = "unknown_id"

        # Retrieve project name if any - this may or may not be used.
        if "{wandb_project}" in self.copy_checkpoint_dir:
            wandb_args = config.get("wandb_args", {})
            self.wandb_project = wandb_args.get("project", "unknown_project")

        # Retrieve run_id - this may or may not be used.
        if "{trainable_id}" in self.copy_checkpoint_dir:
            self.trainable_id = self.logdir.split("-")[-1]  # e.g. 45xrhk_mc2

        # Set up to directory that will contain the copied checkpoint.
        self.setup_copy_dir()

    def setup_copy_dir(self):
        base_path = self.copy_checkpoint_dir.split("{")[0]
        assert os.path.exists(base_path), (
            "Can't copy to directory ({base_path}) which does not exists.")

        copy_ckpt_path = self.copy_checkpoint_dir.replace("{name}", self.name)
        copy_ckpt_path = copy_ckpt_path.replace("{wandb_project}", self.wandb_project)
        copy_ckpt_path = copy_ckpt_path.replace("{trainable_id}", self.trainable_id)
        self.copy_ckpt_path = Path(copy_ckpt_path)

        # Create the save directory and parents as needed.
        self.logger.info(f"Making dir for final checkpoint: {str(self.copy_ckpt_path)}")
        self.copy_ckpt_path.mkdir(exist_ok=True, parents=True)

    def stop_experiment(self):
        """
        Copy over last checkpoint to specified directory.
        """
        super().stop_experiment()
        if self.rank != 0:
            return

        # Find the latest checkpoint.
        log_path = Path(self.logdir)
        checkpoint_paths = []
        for path in log_path.iterdir():
            if "checkpoint" in path.name:
                num = int(path.name.split("_")[1])
                checkpoint_paths.append((num, path))

        checkpoint_paths = sorted(checkpoint_paths, key=itemgetter(0))
        latest_checkpoint = checkpoint_paths[-1][1]  # path of latest

        # Copy over checkpoint.
        try:
            copy_latest_checkpoint = self.copy_ckpt_path / latest_checkpoint.name
            copytree(src=latest_checkpoint, dst=copy_latest_checkpoint)
            self.logger.info(
                f"Copied over checkpoint from "
                f"{str(latest_checkpoint)} to {str(copy_latest_checkpoint)}."
            )
        except OSError:
            self.logger.warning(
                f"Unable to copy checkpoint from "
                f"{str(latest_checkpoint)} to {str(copy_latest_checkpoint)}."
            )

    @classmethod
    def get_execution_order(cls):
        eo = super().get_execution_order()
        eo["setup_experiment"].append("SaveFinalCheckpoint: Resolve path to copy dir.")
        eo["setup_copy_dir"].append("SaveFinalCheckpoint: Make copy dir if needed.")
        eo["stop_experiment"].append(
            "SaveFinalCheckpoint: Copy final checkpoint to specified directory."
        )
        return eo
<commit_msg>Make `copy_checkpoint_dir` non optional in docstring.<commit_after># ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2020, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero Public License for more details.
#
# You should have received a copy of the GNU Affero Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------

import os
from operator import itemgetter
from pathlib import Path
from shutil import copytree

class SaveFinalCheckpoint(object):
    """
    Experiment mixin to save a copy of the final checkpoint from `logdir` upon
    `stop_experiment`.

    :param config:

        - copy_checkpoint_dir: name of directory to save a copy of the final
                               checkpoint. The string may contain:
                                    * {name} - name of experiment; taken from config
                                    * {wandb_project} - taken from wandb_args
                                    * {trainable_id} - inferred from logdir

    Example
    ```
    config["name"] = "my_experiment"
    config["logdir"] = ".../ImagenetTrainable_...-45xrhk_mc2"
    config["wandb_args"] = dict(..., project="my_project")
    config["copy_checkpoint_dir"] = "/mnt/.../{wandb_project}/{name}/{trainable_id}/"

    ... 200 epochs of training

    >> checkpoint saved to "/mnt/.../my_project/my_experiment/45xrhk_mc2/checkpoint_200"
    ```
    """

    def setup_experiment(self, config):
        """Setup experiment and get desired path for copied checkpoint."""
        super().setup_experiment(config)

        assert self.logdir
        assert "copy_checkpoint_dir" in config

        # Set default formatting.
        self.copy_checkpoint_dir = config["copy_checkpoint_dir"]
        self.name = config.get("name", "unknown_name")
        self.wandb_project = "unknown_project"
        self.trainable_id = "unknown_id"

        # Retrieve project name if any - this may or may not be used.
        if "{wandb_project}" in self.copy_checkpoint_dir:
            wandb_args = config.get("wandb_args", {})
            self.wandb_project = wandb_args.get("project", "unknown_project")

        # Retrieve run_id - this may or may not be used.
        if "{trainable_id}" in self.copy_checkpoint_dir:
            self.trainable_id = self.logdir.split("-")[-1]  # e.g. 45xrhk_mc2

        # Set up to directory that will contain the copied checkpoint.
        self.setup_copy_dir()

    def setup_copy_dir(self):
        base_path = self.copy_checkpoint_dir.split("{")[0]
        assert os.path.exists(base_path), (
            "Can't copy to directory ({base_path}) which does not exists.")

        copy_ckpt_path = self.copy_checkpoint_dir.replace("{name}", self.name)
        copy_ckpt_path = copy_ckpt_path.replace("{wandb_project}", self.wandb_project)
        copy_ckpt_path = copy_ckpt_path.replace("{trainable_id}", self.trainable_id)
        self.copy_ckpt_path = Path(copy_ckpt_path)

        # Create the save directory and parents as needed.
        self.logger.info(f"Making dir for final checkpoint: {str(self.copy_ckpt_path)}")
        self.copy_ckpt_path.mkdir(exist_ok=True, parents=True)

    def stop_experiment(self):
        """
        Copy over last checkpoint to specified directory.
        """
        super().stop_experiment()
        if self.rank != 0:
            return

        # Find the latest checkpoint.
        log_path = Path(self.logdir)
        checkpoint_paths = []
        for path in log_path.iterdir():
            if "checkpoint" in path.name:
                num = int(path.name.split("_")[1])
                checkpoint_paths.append((num, path))

        checkpoint_paths = sorted(checkpoint_paths, key=itemgetter(0))
        latest_checkpoint = checkpoint_paths[-1][1]  # path of latest

        # Copy over checkpoint.
        try:
            copy_latest_checkpoint = self.copy_ckpt_path / latest_checkpoint.name
            copytree(src=latest_checkpoint, dst=copy_latest_checkpoint)
            self.logger.info(
                f"Copied over checkpoint from "
                f"{str(latest_checkpoint)} to {str(copy_latest_checkpoint)}."
            )
        except OSError:
            self.logger.warning(
                f"Unable to copy checkpoint from "
                f"{str(latest_checkpoint)} to {str(copy_latest_checkpoint)}."
            )

    @classmethod
    def get_execution_order(cls):
        eo = super().get_execution_order()
        eo["setup_experiment"].append("SaveFinalCheckpoint: Resolve path to copy dir.")
        eo["setup_copy_dir"].append("SaveFinalCheckpoint: Make copy dir if needed.")
        eo["stop_experiment"].append(
            "SaveFinalCheckpoint: Copy final checkpoint to specified directory."
        )
        return eo
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2019, Frappe Technologies and contributors
# For license information, please see license.txt

from __future__ import unicode_literals

import frappe, json
from frappe.model.document import Document
from frappe.utils import now_datetime, get_datetime
from datetime import datetime
from croniter import croniter
from frappe.utils.background_jobs import enqueue, get_jobs

class ScheduledJobType(Document):
	def autoname(self):
		self.name = '.'.join(self.method.split('.')[-2:])

	def validate(self):
		if self.frequency != 'All':
			# force logging for all events other than continuous ones (ALL)
			self.create_log = 1

	def enqueue(self):
		# enqueue event if last execution is done
		if self.is_event_due():
			self.update_last_execution()
			if frappe.flags.enqueued_jobs:
				frappe.flags.enqueued_jobs.append(self.method)

			if frappe.flags.execute_job:
				self.execute()
			else:
				if not self.is_job_in_queue():
					enqueue('frappe.core.doctype.scheduled_job_type.scheduled_job_type.run_scheduled_job',
						queue = self.get_queue_name(), job_type=self.method)
					return True

		return False

	def is_event_due(self, current_time = None):
		'''Return true if event is due based on time lapsed since last execution'''
		# save last execution in expected execution time as per cron
		self.last_execution = self.get_next_execution()

		# if the next scheduled event is before NOW, then its due!
		return self.last_execution <= (current_time or now_datetime())

	def is_job_in_queue(self):
		queued_jobs = get_jobs(site=frappe.local.site, key='job_type')[frappe.local.site]
		return self.method in queued_jobs

	def get_next_execution(self):
		CRON_MAP = {
			"Yearly": "0 0 1 1 *",
			"Annual": "0 0 1 1 *",
			"Monthly": "0 0 1 * *",
			"Monthly Long": "0 0 1 * *",
			"Weekly": "0 0 * * 0",
			"Weekly Long": "0 0 * * 0",
			"Daily": "0 0 * * *",
			"Daily Long": "0 0 * * *",
			"Hourly": "0 * * * *",
			"Hourly Long": "0 * * * *",
			"All": "0/" + str((frappe.get_conf().scheduler_interval or 240) // 60) + " * * * *",
		}

		if not self.cron_format:
			self.cron_format = CRON_MAP[self.frequency]

		return croniter(self.cron_format,
			get_datetime(self.last_execution)).get_next(datetime)

	def execute(self):
		self.scheduler_log = None
		try:
			self.log_status('Start')
			frappe.get_attr(self.method)()
			frappe.db.commit()
			self.log_status('Complete')
		except Exception:
			frappe.db.rollback()
			self.log_status('Failed')

	def log_status(self, status):
		# log file
		frappe.logger(__name__).info('Scheduled Job {0}: {1} for {2}'.format(status, self.method, frappe.local.site))
		self.update_scheduler_log(status)

	def update_scheduler_log(self, status):
		if not self.create_log:
			return
		if not self.scheduler_log:
			self.scheduler_log = frappe.get_doc(dict(doctype = 'Scheduled Job Log', scheduled_job_type=self.name)).insert(ignore_permissions=True)
		self.scheduler_log.db_set('status', status)
		if status == 'Failed':
			self.scheduler_log.db_set('details', frappe.get_traceback())
		frappe.db.commit()

	def update_last_execution(self):
		self.db_set('last_execution', self.last_execution, update_modified=False)
		frappe.db.commit()

	def get_queue_name(self):
		return 'long' if ('Long' in self.frequency) else 'default'

@frappe.whitelist()
def execute_event(doc):
	frappe.only_for('System Manager')
	doc = json.loads(doc)
	frappe.get_doc('Scheduled Job Type', doc.get('name')).enqueue()

def run_scheduled_job(job_type):
	'''This is a wrapper function that runs a hooks.scheduler_events method'''
	try:
		frappe.get_doc('Scheduled Job Type', dict(method=job_type)).execute()
	except Exception:
		print(frappe.get_traceback())

def sync_jobs():
	frappe.reload_doc('core', 'doctype', 'scheduled_job_type')
	all_events = []
	scheduler_events = frappe.get_hooks("scheduler_events")
	insert_events(all_events, scheduler_events)
	clear_events(all_events, scheduler_events)

def insert_events(all_events, scheduler_events):
	for event_type in scheduler_events:
		events = scheduler_events.get(event_type)
		if isinstance(events, dict):
			insert_cron_event(events, all_events)
		else:
			# hourly, daily etc
			insert_event_list(events, event_type, all_events)

def insert_cron_event(events, all_events):
	for cron_format in events:
		for event in events.get(cron_format):
			all_events.append(event)
			insert_single_event('Cron', event, cron_format)

def insert_event_list(events, event_type, all_events):
	for event in events:
		all_events.append(event)
		frequency = event_type.replace('_', ' ').title()
		insert_single_event(frequency, event)

def insert_single_event(frequency, event, cron_format = None):
	if not frappe.db.exists('Scheduled Job Type', dict(method=event)):
		frappe.get_doc(dict(
			doctype = 'Scheduled Job Type',
			method = event,
			cron_format = cron_format,
			frequency = frequency
		)).insert()

def clear_events(all_events, scheduler_events):
	for event in frappe.get_all('Scheduled Job Type', ('name', 'method')):
		if event.method not in all_events:
			frappe.delete_doc('Scheduled Job Type', event.name)
<commit_msg>fix: scheduler log cold start<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2019, Frappe Technologies and contributors
# For license information, please see license.txt

from __future__ import unicode_literals

import frappe, json
from frappe.model.document import Document
from frappe.utils import now_datetime, get_datetime
from datetime import datetime
from croniter import croniter
from frappe.utils.background_jobs import enqueue, get_jobs

class ScheduledJobType(Document):
	def autoname(self):
		self.name = '.'.join(self.method.split('.')[-2:])

	def validate(self):
		if self.frequency != 'All':
			# force logging for all events other than continuous ones (ALL)
			self.create_log = 1

	def enqueue(self):
		# enqueue event if last execution is done
		if self.is_event_due():
			if frappe.flags.enqueued_jobs:
				frappe.flags.enqueued_jobs.append(self.method)

			if frappe.flags.execute_job:
				self.execute()
			else:
				if not self.is_job_in_queue():
					enqueue('frappe.core.doctype.scheduled_job_type.scheduled_job_type.run_scheduled_job',
						queue = self.get_queue_name(), job_type=self.method)
					return True

		return False

	def is_event_due(self, current_time = None):
		'''Return true if event is due based on time lapsed since last execution'''
		# if the next scheduled event is before NOW, then its due!
		return self.get_next_execution() <= (current_time or now_datetime())

	def is_job_in_queue(self):
		queued_jobs = get_jobs(site=frappe.local.site, key='job_type')[frappe.local.site]
		return self.method in queued_jobs

	def get_next_execution(self):
		CRON_MAP = {
			"Yearly": "0 0 1 1 *",
			"Annual": "0 0 1 1 *",
			"Monthly": "0 0 1 * *",
			"Monthly Long": "0 0 1 * *",
			"Weekly": "0 0 * * 0",
			"Weekly Long": "0 0 * * 0",
			"Daily": "0 0 * * *",
			"Daily Long": "0 0 * * *",
			"Hourly": "0 * * * *",
			"Hourly Long": "0 * * * *",
			"All": "0/" + str((frappe.get_conf().scheduler_interval or 240) // 60) + " * * * *",
		}

		if not self.cron_format:
			self.cron_format = CRON_MAP[self.frequency]

		return croniter(self.cron_format,
			get_datetime(self.last_execution or datetime(2000, 1, 1))).get_next(datetime)

	def execute(self):
		self.scheduler_log = None
		try:
			self.log_status('Start')
			frappe.get_attr(self.method)()
			frappe.db.commit()
			self.log_status('Complete')
		except Exception:
			frappe.db.rollback()
			self.log_status('Failed')

	def log_status(self, status):
		# log file
		frappe.logger(__name__).info('Scheduled Job {0}: {1} for {2}'.format(status, self.method, frappe.local.site))
		self.update_scheduler_log(status)

	def update_scheduler_log(self, status):
		if not self.create_log:
			return
		if not self.scheduler_log:
			self.scheduler_log = frappe.get_doc(dict(doctype = 'Scheduled Job Log', scheduled_job_type=self.name)).insert(ignore_permissions=True)
		self.scheduler_log.db_set('status', status)
		if status == 'Failed':
			self.scheduler_log.db_set('details', frappe.get_traceback())
		if status == 'Start':
			self.db_set('last_execution', now_datetime(), update_modified=False)
		frappe.db.commit()

	def get_queue_name(self):
		return 'long' if ('Long' in self.frequency) else 'default'

	def on_trash(self):
		frappe.db.sql('delete from `tabScheduled Job Log` where scheduled_job_type=%s', self.name)

@frappe.whitelist()
def execute_event(doc):
	frappe.only_for('System Manager')
	doc = json.loads(doc)
	frappe.get_doc('Scheduled Job Type', doc.get('name')).enqueue()

def run_scheduled_job(job_type):
	'''This is a wrapper function that runs a hooks.scheduler_events method'''
	try:
		frappe.get_doc('Scheduled Job Type', dict(method=job_type)).execute()
	except Exception:
		print(frappe.get_traceback())

def sync_jobs():
	frappe.reload_doc('core', 'doctype', 'scheduled_job_type')
	all_events = []
	scheduler_events = frappe.get_hooks("scheduler_events")
	insert_events(all_events, scheduler_events)
	clear_events(all_events, scheduler_events)

def insert_events(all_events, scheduler_events):
	for event_type in scheduler_events:
		events = scheduler_events.get(event_type)
		if isinstance(events, dict):
			insert_cron_event(events, all_events)
		else:
			# hourly, daily etc
			insert_event_list(events, event_type, all_events)

def insert_cron_event(events, all_events):
	for cron_format in events:
		for event in events.get(cron_format):
			all_events.append(event)
			insert_single_event('Cron', event, cron_format)

def insert_event_list(events, event_type, all_events):
	for event in events:
		all_events.append(event)
		frequency = event_type.replace('_', ' ').title()
		insert_single_event(frequency, event)

def insert_single_event(frequency, event, cron_format = None):
	if not frappe.db.exists('Scheduled Job Type', dict(method=event)):
		frappe.get_doc(dict(
			doctype = 'Scheduled Job Type',
			method = event,
			cron_format = cron_format,
			frequency = frequency
		)).insert()

def clear_events(all_events, scheduler_events):
	for event in frappe.get_all('Scheduled Job Type', ('name', 'method')):
		if event.method not in all_events:
			frappe.delete_doc('Scheduled Job Type', event.name)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Willow Garage, Inc. nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# Author Tully Foote/tfoote@willowgarage.com

#"""
#Library and command-line tool for calculating rosdeps.
#"""

from __future__ import with_statement

import roslib.exceptions
import roslib.rospack
import roslib.stacks
import os
import sys
import subprocess
import types
import tempfile
import yaml

####### Linux Helper Functions #####
def lsb_get_os():
    try:
        cmd = ['lsb_release', '-si']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

def lsb_get_codename():
    try:
        cmd = ['lsb_release', '-sc']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

def lsb_get_version():
    try:
        cmd = ['lsb_release', '-sr']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

class OSDetectException(roslib.exceptions.ROSLibException): pass

class OSBase:
    def check_presence(self):
        raise OSDetectException("check_presence unimplemented")

    def get_name(self):
        raise OSDetectException("check_presence unimplemented")

    def get_version(self):
        raise OSDetectException("check_presence unimplemented")

###### Debian SPECIALIZATION #########################
class Debian(OSBase):
    def check_presence(self):
        if "Debian" == lsb_get_os():
            return True
        return False

    def get_version(self):
        return lsb_get_codename()
    def get_name(self):
        return "debian"

###### END Debian SPECIALIZATION ########################

###### UBUNTU SPECIALIZATION #########################
class Ubuntu(Debian):
    """ This is an implementation of a standard interface for
    interacting with rosdep.  This defines all Ubuntu sepecific
    methods, including detecting the OS/Version number.  As well as
    how to check for and install packages."""
    def check_presence(self):
        if "Ubuntu" == lsb_get_os():
            return True
        return False

    def get_version(self):
        return lsb_get_version()
    def get_name(self):
        return "ubuntu"

###### END UBUNTU SPECIALIZATION ########################

###### Mint SPECIALIZATION #########################
class Mint(Debian):
    def check_presence(self):
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[0] == "Linux" and os_list[1] == "Mint":
                    return True
        except:
            print "Mint failed to detect OS"
        return False

    #get_version inherited from debian

    def get_name(self):
        return "mint"
###### END Mint SPECIALIZATION ########################

###### Fedora SPECIALIZATION #########################
class Fedora(OSBase):
    def check_presence(self):
        try:
            filename = "/etc/redhat_release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[0] == "Fedora" and os_list[1] == "release":
                    return True
        except:
            print "Fedora failed to detect OS"
        return False

    def get_version(self):
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Fedora" and os_list[1] == "release":
                    return os_list[2]
        except:
            print "Fedora failed to get version"
            return False

        return False

    def get_name(self):
        return "fedora"

###### END Fedora SPECIALIZATION ########################

###### Rhel SPECIALIZATION #########################
class Rhel(Fedora):
    def check_presence(self):
        try:
            filename = "/etc/redhat_release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[2] == "Enterprise":
                    return True
        except:
            print "Rhel failed to detect OS"
        return False

    def get_version(self):
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[2] == "Enterprise":
                    return os_list[6]
        except:
            print "Rhel failed to get version"
            return False

        return False

    def get_name(self):
        return "rhel"

###### END Rhel SPECIALIZATION ########################

###### Macports SPECIALIZATION #########################
def port_detect(p):
    cmd = ['port', 'installed', p]
    pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (std_out, std_err) = pop.communicate()

    return (std_out.count("(active)") > 0)

class Macports(OSBase):
    def check_presence(self):
        filename = "/usr/bin/sw_vers"
        if os.path.exists(filename):
            return True
        return False

    def get_version(self):
        return "macports" # macports is a rolling release and isn't versionsed

    def get_name(self):
        return "macports"

###### END Macports SPECIALIZATION ########################

###### Arch SPECIALIZATION #########################
class Arch(OSBase):

    def check_presence(self):
        filename = "/etc/arch-release"
        if os.path.exists(filename):
            return True
        return False

    def get_version(self):
        return ""
        # arch didn't have a version parsing in cpp version
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Linux" and os_list[1] == "Arch":
                    return os_list[2]
        except:
            print "Arch failed to get version"
            return False

        return False

    def get_name(self):
        return "arch"

###### END Arch SPECIALIZATION ########################

###### Gentoo Sepcialization ###############################
class Gentoo(OSBase):
    def check_presence(self):
        try:
            filename = "/etc/gentoo-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[0] == "Gentoo" and os_list[1] == "Base":
                    return True
        except:
            pass#print >> sys.stderr, "Gentoo failed to detect OS"
        return False

    def get_version(self):
        try:
            filename = "/etc/gentoo-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Gentoo" and os_list[1] == "Base":
                    return os_list[4]
        except:
            print >> sys.stderr, "Gentoo failed to get version"
            return False

        return False

    def get_name(self):
        return "gentoo"

###### END Gentoo Sepcialization ###############################

#### Override class for debugging and unsupported OSs ###########
class Override(OSBase):
    def __init__(self):
        self._os_name = "uninitialized from ROS_OS_OVERRIDE=name:version"
        self._os_version = "uninitialized from ROS_OS_OVERRIDE=name:version"

    def check_presence(self):
        try:
            (self._os_name, self._os_version) = os.environ["ROS_OS_OVERRIDE"].split(':')
            print >> sys.stderr, "Using environment variable ROS_OS_OVERRIDE=name:version"
            return True
        except:
            return False

    def get_version(self):
        return self._os_version

    def get_name(self):
        return self._os_name

class OSDetect:
    """ This class will iterate over registered classes to lookup the
    active OS and Version of that OS for lookup in rosdep.yaml"""
    def __init__(self, os_list = [Debian(), Ubuntu(), Mint(), Macports(), Arch(), Fedora(), Rhel()]):
        self._os_list = [ Override()]
        self._os_list.extend(os_list)

        self.detect_os()

    def add_os(self, class_ref):
        self._os_list.append(class_ref)

        # \TODO look at throwing here
    def detect_os(self):
        for os_class in self._os_list:
            if os_class.check_presence():
                self._os_name = os_class.get_name()
                self._os_version = os_class.get_version()
                self._os_class = os_class
                return True
        return False

    def get_os(self):
        if not self._os_class:
            if not self.detect_os():
                raise OSDetectException("No OS detected")
        else:
            return self._os_class

    def get_name(self):
        if not self._os_name:
            os_class = self.get_os()
            if os_class:
                return os_class.get_name()
            else:
                return False
        return self._os_name

    def get_version(self):
        if not self._os_version:
            if self.os_class:
                self._os_version = self.os_class.get_version()
        return self._os_version

<commit_msg>Patch from Daniel Stonier re Gentoo support<commit_after>#!/usr/bin/env python
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Willow Garage, Inc. nor the names of its
#       contributors may be used to endorse or promote products derived from
#       this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# Author Tully Foote/tfoote@willowgarage.com

#"""
#Library and command-line tool for calculating rosdeps.
#"""

from __future__ import with_statement

import roslib.exceptions
import roslib.rospack
import roslib.stacks
import os
import sys
import subprocess
import types
import tempfile
import yaml

####### Linux Helper Functions #####
def lsb_get_os():
    try:
        cmd = ['lsb_release', '-si']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

def lsb_get_codename():
    try:
        cmd = ['lsb_release', '-sc']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

def lsb_get_version():
    try:
        cmd = ['lsb_release', '-sr']
        pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (std_out, std_err) = pop.communicate()
        return std_out.strip()
    except:
        return None

class OSDetectException(roslib.exceptions.ROSLibException): pass

class OSBase:
    def check_presence(self):
        raise OSDetectException("check_presence unimplemented")

    def get_name(self):
        raise OSDetectException("check_presence unimplemented")

    def get_version(self):
        raise OSDetectException("check_presence unimplemented")

###### Debian SPECIALIZATION #########################
class Debian(OSBase):
    def check_presence(self):
        if "Debian" == lsb_get_os():
            return True
        return False

    def get_version(self):
        return lsb_get_codename()
    def get_name(self):
        return "debian"

###### END Debian SPECIALIZATION ########################

###### UBUNTU SPECIALIZATION #########################
class Ubuntu(Debian):
    """ This is an implementation of a standard interface for
    interacting with rosdep.  This defines all Ubuntu sepecific
    methods, including detecting the OS/Version number.  As well as
    how to check for and install packages."""
    def check_presence(self):
        if "Ubuntu" == lsb_get_os():
            return True
        return False

    def get_version(self):
        return lsb_get_version()
    def get_name(self):
        return "ubuntu"

###### END UBUNTU SPECIALIZATION ########################

###### Mint SPECIALIZATION #########################
class Mint(Debian):
    def check_presence(self):
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[0] == "Linux" and os_list[1] == "Mint":
                    return True
        except:
            print "Mint failed to detect OS"
        return False

    #get_version inherited from debian

    def get_name(self):
        return "mint"
###### END Mint SPECIALIZATION ########################

###### Fedora SPECIALIZATION #########################
class Fedora(OSBase):
    def check_presence(self):
        try:
            filename = "/etc/redhat_release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[0] == "Fedora" and os_list[1] == "release":
                    return True
        except:
            print "Fedora failed to detect OS"
        return False

    def get_version(self):
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Fedora" and os_list[1] == "release":
                    return os_list[2]
        except:
            print "Fedora failed to get version"
            return False

        return False

    def get_name(self):
        return "fedora"

###### END Fedora SPECIALIZATION ########################

###### Rhel SPECIALIZATION #########################
class Rhel(Fedora):
    def check_presence(self):
        try:
            filename = "/etc/redhat_release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[2] == "Enterprise":
                    return True
        except:
            print "Rhel failed to detect OS"
        return False

    def get_version(self):
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[2] == "Enterprise":
                    return os_list[6]
        except:
            print "Rhel failed to get version"
            return False

        return False

    def get_name(self):
        return "rhel"

###### END Rhel SPECIALIZATION ########################

###### Macports SPECIALIZATION #########################
def port_detect(p):
    cmd = ['port', 'installed', p]
    pop = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (std_out, std_err) = pop.communicate()

    return (std_out.count("(active)") > 0)

class Macports(OSBase):
    def check_presence(self):
        filename = "/usr/bin/sw_vers"
        if os.path.exists(filename):
            return True
        return False

    def get_version(self):
        return "macports" # macports is a rolling release and isn't versionsed

    def get_name(self):
        return "macports"

###### END Macports SPECIALIZATION ########################

###### Arch SPECIALIZATION #########################
class Arch(OSBase):

    def check_presence(self):
        filename = "/etc/arch-release"
        if os.path.exists(filename):
            return True
        return False

    def get_version(self):
        return ""
        # arch didn't have a version parsing in cpp version
        try:
            filename = "/etc/issue"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Linux" and os_list[1] == "Arch":
                    return os_list[2]
        except:
            print "Arch failed to get version"
            return False

        return False

    def get_name(self):
        return "arch"

###### END Arch SPECIALIZATION ########################

###### Gentoo Sepcialization ###############################
class Gentoo(OSBase):
    def check_presence(self):
        try:
            filename = "/etc/gentoo-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list and os_list[0] == "Gentoo" and os_list[1] == "Base":
                    return True
        except:
            pass#print >> sys.stderr, "Gentoo failed to detect OS"
        return False

    def get_version(self):
        try:
            filename = "/etc/gentoo-release"
            if os.path.exists(filename):
                with open(filename, 'r') as fh:
                    os_list = fh.read().split()
                if os_list[0] == "Gentoo" and os_list[1] == "Base":
                    return os_list[4]
        except:
            print >> sys.stderr, "Gentoo failed to get version"
            return False

        return False

    def get_name(self):
        return "gentoo"

###### END Gentoo Sepcialization ###############################

#### Override class for debugging and unsupported OSs ###########
class Override(OSBase):
    def __init__(self):
        self._os_name = "uninitialized from ROS_OS_OVERRIDE=name:version"
        self._os_version = "uninitialized from ROS_OS_OVERRIDE=name:version"

    def check_presence(self):
        try:
            (self._os_name, self._os_version) = os.environ["ROS_OS_OVERRIDE"].split(':')
            print >> sys.stderr, "Using environment variable ROS_OS_OVERRIDE=name:version"
            return True
        except:
            return False

    def get_version(self):
        return self._os_version

    def get_name(self):
        return self._os_name

class OSDetect:
    """ This class will iterate over registered classes to lookup the
    active OS and Version of that OS for lookup in rosdep.yaml"""
    def __init__(self, os_list = [Debian(), Ubuntu(), Mint(), Macports(), Arch(), Fedora(), Rhel(), Gentoo()]):
        self._os_list = [ Override()]
        self._os_list.extend(os_list)

        self.detect_os()

    def add_os(self, class_ref):
        self._os_list.append(class_ref)

        # \TODO look at throwing here
    def detect_os(self):
        for os_class in self._os_list:
            if os_class.check_presence():
                self._os_name = os_class.get_name()
                self._os_version = os_class.get_version()
                self._os_class = os_class
                return True
        return False

    def get_os(self):
        if not self._os_class:
            if not self.detect_os():
                raise OSDetectException("No OS detected")
        else:
            return self._os_class

    def get_name(self):
        if not self._os_name:
            os_class = self.get_os()
            if os_class:
                return os_class.get_name()
            else:
                return False
        return self._os_name

    def get_version(self):
        if not self._os_version:
            if self.os_class:
                self._os_version = self.os_class.get_version()
        return self._os_version

<|endoftext|>


<commit_before># -*- coding=utf8 -*-

import datetime, re
from pymongo import MongoClient, ASCENDING
from author import Author

class DBConnection:
    def __init__(self, addr='localhost', port=27017, db_name='zhihutu', collection='author'):
        self.client = MongoClient(addr, port)
        self.db = self.client[db_name]
        self.collection = self.db[collection]

    def save_author(self, author: Author):
        """
        Saved pattern:
        {
            name: "Str",  // Doesn't unique!
            url_token: "Str",
            avatar_url_template: "Str",
            gender: 0/1,
            answer_pictures: ["str", "str", ...],
            user_type: "People",
            update_time: datetime.datetime.utcnow(),
        }
        """
        MIN_VOTE_UP_COUNT = 50 # 低于此赞数的回答不保存
        author_to_save = {
            "name": author.name,
            "url_token": author.url_token,
            "avatar_url_template": author.avatar_url_template,
            "gender": author.gender,
            "user_type": author.user_type,
            "update_time": datetime.datetime.utcnow(),
            # "answer_pictures": []
        }

        pic_set = set()
        for answer in author.answers:
            if answer['voteup_count'] < MIN_VOTE_UP_COUNT:
                continue
            self._get_pic_list(answer['content'], pic_set)

        author_to_save['answer_pictures'] = list(pic_set)

        self.collection.delete_many({'url_token': author.url_token})
        self.collection.insert_one(author_to_save)

        return author.url_token

    def _get_pic_list(self, content, pic_set: set):
        for pic in re.findall(r"data-original=\"([\w./\-:]+)", content):
            pic_set.add(pic.replace('_b.', '_r.'))
        for pic in re.findall(r"data-actualsrc=\"([\w./\-:]+)", content):
            pic_set.add(pic.replace('_b.', '_r.'))

    def save_temp(self, url_token):
        temp_author = {
            'url_token': url_token,
            'name': 'Loading...'
        }
        return self.collection.insert_one(temp_author)

    def loading_number(self):
        return len(self.collection.find({'name': 'Loading...'}))

    def restore_url_token(self):
        result = set()
        for item in self.collection.find({}, {'url_token': 1, '_id': 0}):
            print(item)
            result.add(item['url_token'])
        return result

    def find_one(self, url_token):
        return self.collection.find_one({'url_token': url_token})

    def delete_many(self, url_token):
        result = self.collection.delete_many({'url_token': url_token})
        return result.deleted_count == 1
<commit_msg>获取find结果的长度<commit_after># -*- coding=utf8 -*-

import datetime, re
from pymongo import MongoClient, ASCENDING
from author import Author

class DBConnection:
    def __init__(self, addr='localhost', port=27017, db_name='zhihutu', collection='author'):
        self.client = MongoClient(addr, port)
        self.db = self.client[db_name]
        self.collection = self.db[collection]

    def save_author(self, author: Author):
        """
        Saved pattern:
        {
            name: "Str",  // Doesn't unique!
            url_token: "Str",
            avatar_url_template: "Str",
            gender: 0/1,
            answer_pictures: ["str", "str", ...],
            user_type: "People",
            update_time: datetime.datetime.utcnow(),
        }
        """
        MIN_VOTE_UP_COUNT = 50 # 低于此赞数的回答不保存
        author_to_save = {
            "name": author.name,
            "url_token": author.url_token,
            "avatar_url_template": author.avatar_url_template,
            "gender": author.gender,
            "user_type": author.user_type,
            "update_time": datetime.datetime.utcnow(),
            # "answer_pictures": []
        }

        pic_set = set()
        for answer in author.answers:
            if answer['voteup_count'] < MIN_VOTE_UP_COUNT:
                continue
            self._get_pic_list(answer['content'], pic_set)

        author_to_save['answer_pictures'] = list(pic_set)

        self.collection.delete_many({'url_token': author.url_token})
        self.collection.insert_one(author_to_save)

        return author.url_token

    def _get_pic_list(self, content, pic_set: set):
        for pic in re.findall(r"data-original=\"([\w./\-:]+)", content):
            pic_set.add(pic.replace('_b.', '_r.'))
        for pic in re.findall(r"data-actualsrc=\"([\w./\-:]+)", content):
            pic_set.add(pic.replace('_b.', '_r.'))

    def save_temp(self, url_token):
        temp_author = {
            'url_token': url_token,
            'name': 'Loading...'
        }
        return self.collection.insert_one(temp_author)

    def loading_number(self):
        return self.collection.find({'name': 'Loading...'}).count()

    def restore_url_token(self):
        result = set()
        for item in self.collection.find({}, {'url_token': 1, '_id': 0}):
            print(item)
            result.add(item['url_token'])
        return result

    def find_one(self, url_token):
        return self.collection.find_one({'url_token': url_token})

    def delete_many(self, url_token):
        result = self.collection.delete_many({'url_token': url_token})
        return result.deleted_count == 1
<|endoftext|>