<commit_before>import sublime
import sublime_plugin
import calendar
import datetime
import itertools

NOT_FOUND = sublime.Region(-1, -1)
DONE_SYMBOL = 'X'
NORMAL_SYMBOL = ' '
WEEKEND_SYMBOL = '.'
TODAY_KEY = 'today'
INITIAL_MESSAGE = ("<Put your goal here that you want to achieve everyday. "
                   "Remember, DON'T BREAK THE CHAIN>")

"""A dict(month_name, month_int) with right adjust leading space and
a trailing space. This to be used for regex purposes."""
MONTHS = {m:("{month}: "
            .format(month=calendar.month_name[m].rjust(10, ' ')))
                 for m in range(1, 13)}

def dbtc():
    """output the calendar of current year in text with month
    follow by days on each row"""
    current_yr = datetime.date.today().year
    CALENDAR_WIDTH = 158

    init_msg = "** {0} **".format(INITIAL_MESSAGE).center(CALENDAR_WIDTH)
    year = "{year}\n".format(year=current_yr).rjust(8, ' ')
    txt_builder = []
    txt_builder.append(init_msg)
    txt_builder.append("\n\n")
    txt_builder.append(year)

    cal = calendar.Calendar(calendar.MONDAY)
    for m, month_name in MONTHS.items():
        month = cal.itermonthdays(current_yr, m)
        days = "".join(
            [get_weekday_format(current_yr, m, day)
                for day in month if day != 0]
        )

        txt_builder.append("{month_name}{days}\n".format(month_name=month_name,
                                                         days=days))
    return "".join(txt_builder)

def get_weekday_format(year, month, day, include_weekend=True):
    """return numeric day in string if weekday else day with WEEKEND_SYMBOL"""
    weekend = [calendar.SATURDAY, calendar.SUNDAY]
    d = calendar.weekday(year, month, day)
    # highlight_wkend = False
    # global WEEKEND_SYMBOL

    # WEEKEND_SYMBOL = ' '
    if d in weekend:
        if include_weekend:
            return "{ws}{day}  ".format(ws=WEEKEND_SYMBOL, day=day)
        else:
            return ""

    return " {day}  ".format(day=day)

class DontBreakTheChainCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, dbtc())
        self.view.run_command('highlight_today')

class MarkTodayAsDone(sublime_plugin.TextCommand):
    def run(self, edit):
        regions = self.view.get_regions(TODAY_KEY)

        if regions:
            sel = self.view.sel()
            sel.clear()
            sel.add(regions[0])
            self.view.run_command('mark_as_done')

class HighlightTodayCommand(sublime_plugin.TextCommand):
    def run(self, edit):

        today = datetime.date.today()
        month_name = calendar.month_name[today.month]
        # find line that match month and day in same line
        m_regex = (r"^\s+{month}\:.+[\s\.]{day}[{norm_sym}{done_sym}].*$"
                   .format(month=month_name,
                           day=today.day,
                           norm_sym=NORMAL_SYMBOL,
                           done_sym=DONE_SYMBOL))

        found_region = self.view.find(m_regex, 0)

        if found_region != NOT_FOUND:
            line_r = self.view.line(found_region)
            line = self.view.substr(line_r)
            day_format = get_weekday_format(today.year, today.month, today.day)
            with_symbol = ("{day}{done_sym} "
                           .format(day=day_format.rstrip(' '),
                                   done_sym=DONE_SYMBOL))

            if with_symbol in line:
                day_str = with_symbol
                scope = "today.done.chain"
            else:
                day_str = day_format
                scope = "today.chain"

            start_i = found_region.begin() + line.index(day_str)
            end_i = start_i + len(day_str)
            # highlight region, reversed start/end
            # so that the cursor starts from the beginning
            # regions.add(sublime.Region(end_i, start_i))
            self.view.erase_regions(TODAY_KEY)
            regions = [sublime.Region(end_i, start_i)]
            self.view.add_regions(
                TODAY_KEY,
                regions,
                scope,
                "dot",
                sublime.DRAW_SQUIGGLY_UNDERLINE
            )

class HighlightTodayOnActivated(sublime_plugin.EventListener):
    def on_activated(self, view):
        # only execute when the current view has DBTC syntax
        if view.settings().get('syntax').endswith('Chains.tmLanguage'):
            view.run_command('highlight_today')

def chain_month(view):
    regions = view.sel()

    for r in regions:
        multi_lines_r = view.split_by_newlines(r)

        for subregion in multi_lines_r:
            line_r = view.line(subregion)
            line = view.substr(line_r)

            # using a generator to filter out line that starts with with monthname
            # first hit/find is used
            month_num = next((m for m, name in MONTHS.items() if line.startswith(name)), None)

            # only continue processing for lines
            # that starts with one of the month names
            if month_num:
                in_region = expand_whole_word(view, subregion)
                sentence = view.substr(in_region)
                words = sentence.split()
                print("'"+sentence+"'")

                for w in words:
                    is_weekend = WEEKEND_SYMBOL in w
                    is_done = DONE_SYMBOL in w
                    if is_weekend:
                        w = w.replace(WEEKEND_SYMBOL, '')
                    if is_done:
                        w = w.replace(DONE_SYMBOL, '')

                    if w.isdigit():
                        day_num = int(w)
                        if 1 <= day_num <= 31:
                            day = {"str": w,
                                   "str_norm": w+NORMAL_SYMBOL,
                                   "str_done": w+DONE_SYMBOL,
                                   "is_weekend": is_weekend,
                                   "is_done": is_done,
                                   "date": datetime.date(
                                       datetime.date.today().year,
                                       month_num, day_num
                                   )}

                            yield day, in_region

class MarkAsDoneCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for day, in_region in chain_month(self.view):
            # make sure you cannot mark done ahead of time
            if not day["is_done"] and not is_future(day["date"]):
                search_replace(self.view, edit,
                               search_s=day["str_norm"],
                               replace_s=day["str_done"],
                               in_region=in_region,
                               date=day["date"])

class UnmarkDoneCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for day, in_region in chain_month(self.view):
            if day["is_done"]:
                search_replace(self.view, edit,
                               search_s=day["str_done"],
                               replace_s=day["str_norm"],
                               in_region=in_region,
                               date=day["date"])

class DoneToggleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for day, in_region in chain_month(self.view):
            # ignore if day is not done and in the future.
            # We do not want to toggle this
            if not day["is_done"] and is_future(day["date"]):
                continue

            search_s = day["str_done"] if day["is_done"] else day["str_norm"]
            replace_s = day["str_norm"] if day["is_done"] else day["str_done"]

            search_replace(self.view, edit,
                           search_s,
                           replace_s,
                           in_region,
                           day["date"])

class DontBreakTheChainNewDocCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.new_file()
        regions = view.sel()
        view.set_syntax_file('Packages/Chains'
                             '/Chains.tmLanguage')
        view.run_command("dont_break_the_chain")
        # view.run_command("highlight_today")
        msg_r = view.find(INITIAL_MESSAGE, 0, sublime.LITERAL)
        region = region_cursor_start(msg_r)
        regions.clear()
        regions.add(region)

def search_replace(view, edit, search_s, replace_s, in_region, date):
    """search and replace text within a given region"""
    word_r = get_subregion(view, search_s, in_region)

    if word_r != NOT_FOUND:
        view.replace(edit, word_r, replace_s)
        if date == datetime.date.today():
            view.run_command('highlight_today')
    else:
        print("NOT FOUND!")

def get_subregion(view, word, in_region):
    sentence = view.substr(in_region)

    if word in sentence:
        i = sentence.index(word)
        start_r = in_region.begin() + i
        end_r = start_r + len(word)
        return sublime.Region(start_r, end_r)
    else:
        return NOT_FOUND

def expand_whole_word(view, region):
    """increase point/region until reaches \s, \n
       and DONE_SYMBOL, returns word region"""
    # deal with edge cases where the cursor is inbetween two dates
    if region.empty() and view.substr(region.b) == ' ':
        # peek to the right for any characters
        r = sublime.Region(region.a, region.b+2)
        c = view.substr(r).strip()

        if c:
            return expand_end_of_word(view, r)

        # peek to the left for any characters
        r = sublime.Region(region.a-2, region.b)
        c = view.substr(r).strip()

        if c:
            return expand_beginning_of_word(view, r)

    return expand_end_of_word(view, region)

def expand_beginning_of_word(view, region):
    """decrease the beginning region by 1 until /s and WEEKEND_SYMBOL is hit"""
    # print("expand begin:'" + view.substr(region) + "'",
    #    region, "'" + view.substr(region) + "'")
    beginnings = [' ', WEEKEND_SYMBOL]

    while not any(view.substr(region).startswith(c) for c in beginnings):
        region = add_region_by(region, -1, at_the="beginning")
    # make sure that we end region with NORMAL_SYMBOL,
    # as the space might be replaced with DONE_SYMBOL
    if not view.substr(region).endswith(NORMAL_SYMBOL):
        region = expand_end_of_word(view, region)
    return region

def expand_end_of_word(view, region):
    """increase the end region by 1
       until NORMAL_SYMBOL/WEEKEND_SYMBOL /n is hit"""
    word_r = view.word(region)
    endings = [NORMAL_SYMBOL, DONE_SYMBOL, '\n']

    while not any(view.substr(word_r).endswith(char) for char in endings):
        word_r = add_region_by(word_r, 1, at_the="end")
    return word_r

def add_region_by(region, by_num, at_the):
    """add the given number(positive/neg) to region at the given end"""
    if region.empty():
        return region

    bothends = {"beginning": region.begin(), "end": region.end()}
    end_value = bothends.get(at_the, region.end())

    if region.a == end_value:
        region.a += by_num
    if region.b == end_value:
        region.b += by_num
    return region

def region_cursor_start(region):
    """swap region range so that the end region would be at the beginning
    so cursor starts from the beginning when highlighted"""
    if region.b > region.a:
        region.a, region.b = region.b, region.a
    return region

def is_future(unknown_date):
    return (unknown_date > datetime.date.today())
<commit_msg>	modified:   Chains.py<commit_after>import sublime
import sublime_plugin
import calendar
import datetime
import itertools

NOT_FOUND = sublime.Region(-1, -1)
DONE_SYMBOL = 'X'
NORMAL_SYMBOL = ' '
WEEKEND_SYMBOL = '.'
TODAY_KEY = 'today'
INITIAL_MESSAGE = ("<Put your goal here that you want to achieve everyday. "
                   "Remember, DON'T BREAK THE CHAIN>")

"""A dict(month_name, month_int) with right adjust leading space and
a trailing space. This to be used for regex purposes."""
MONTHS = {m:("{month}: "
            .format(month=calendar.month_name[m].rjust(10, ' ')))
                 for m in range(1, 13)}

def dbtc():
    """output the calendar of current year in text with month
    follow by days on each row"""
    current_yr = datetime.date.today().year
    CALENDAR_WIDTH = 158

    init_msg = "** {0} **".format(INITIAL_MESSAGE).center(CALENDAR_WIDTH)
    year = "{year}\n".format(year=current_yr).rjust(8, ' ')
    txt_builder = []
    txt_builder.append(init_msg)
    txt_builder.append("\n\n")
    txt_builder.append(year)

    cal = calendar.Calendar(calendar.MONDAY)
    for m, month_name in MONTHS.items():
        month = cal.itermonthdays(current_yr, m)
        days = "".join(
            [get_weekday_format(current_yr, m, day)
                for day in month if day != 0]
        )

        txt_builder.append("{month_name}{days}\n".format(month_name=month_name,
                                                         days=days))
    return "".join(txt_builder)

def get_weekday_format(year, month, day, include_weekend=True):
    """return numeric day in string if weekday else day with WEEKEND_SYMBOL"""
    weekend = [calendar.SATURDAY, calendar.SUNDAY]
    d = calendar.weekday(year, month, day)
    # highlight_wkend = False
    # global WEEKEND_SYMBOL

    # WEEKEND_SYMBOL = ' '
    if d in weekend:
        if include_weekend:
            return "{ws}{day}  ".format(ws=WEEKEND_SYMBOL, day=day)
        else:
            return ""

    return " {day}  ".format(day=day)

class DontBreakTheChainCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, dbtc())
        self.view.run_command('highlight_today')

class MarkTodayAsDone(sublime_plugin.TextCommand):
    def run(self, edit):
        regions = self.view.get_regions(TODAY_KEY)

        if regions:
            sel = self.view.sel()
            sel.clear()
            sel.add(regions[0])
            self.view.run_command('mark_as_done')

class HighlightTodayCommand(sublime_plugin.TextCommand):
    def run(self, edit):

        today = datetime.date.today()
        month_name = calendar.month_name[today.month]
        # find line that match month and day in same line
        m_regex = (r"^\s+{month}\:.+[\s\.]{day}[{norm_sym}{done_sym}].*$"
                   .format(month=month_name,
                           day=today.day,
                           norm_sym=NORMAL_SYMBOL,
                           done_sym=DONE_SYMBOL))

        found_region = self.view.find(m_regex, 0)

        if found_region != NOT_FOUND:
            line_r = self.view.line(found_region)
            line = self.view.substr(line_r)
            day_format = get_weekday_format(today.year, today.month, today.day)
            with_symbol = ("{day}{done_sym} "
                           .format(day=day_format.rstrip(' '),
                                   done_sym=DONE_SYMBOL))

            if with_symbol in line:
                day_str = with_symbol
                scope = "today.done.chain"
            else:
                day_str = day_format
                scope = "today.chain"

            start_i = found_region.begin() + line.index(day_str)
            end_i = start_i + len(day_str)
            # highlight region, reversed start/end
            # so that the cursor starts from the beginning
            # regions.add(sublime.Region(end_i, start_i))
            self.view.erase_regions(TODAY_KEY)
            regions = [sublime.Region(end_i, start_i)]
            self.view.add_regions(
                TODAY_KEY,
                regions,
                scope,
                "dot",
                sublime.DRAW_SQUIGGLY_UNDERLINE
            )

class HighlightTodayOnActivated(sublime_plugin.EventListener):
    def on_activated(self, view):
        # only execute when the current view has DBTC syntax
        if view.settings().get('syntax').endswith('Chains.tmLanguage'):
            view.run_command('highlight_today')

def chain_month(view):
    regions = view.sel()

    for r in regions:
        multi_lines_r = view.split_by_newlines(r)

        for subregion in multi_lines_r:
            line_r = view.line(subregion)
            line = view.substr(line_r)

            # using a generator to filter out line that starts with with monthname
            # first hit/find is used
            month_num = next((m for m, name in MONTHS.items() if line.startswith(name)), None)

            # only continue processing for lines
            # that starts with one of the month names
            if month_num:
                in_region = expand_whole_word(view, subregion)
                sentence = view.substr(in_region)
                words = sentence.split()
                print("'"+sentence+"'")

                for w in words:
                    is_weekend = WEEKEND_SYMBOL in w
                    is_done = DONE_SYMBOL in w
                    if is_weekend:
                        w = w.replace(WEEKEND_SYMBOL, '')
                    if is_done:
                        w = w.replace(DONE_SYMBOL, '')

                    if w.isdigit():
                        day_num = int(w)
                        if 1 <= day_num <= 31:
                            day = {"str": w,
                                   "str_norm": w+NORMAL_SYMBOL,
                                   "str_done": w+DONE_SYMBOL,
                                   "is_weekend": is_weekend,
                                   "is_done": is_done,
                                   "date": datetime.date(
                                       datetime.date.today().year,
                                       month_num, day_num
                                   )}

                            yield day, in_region

class MarkAsDoneCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for day, in_region in chain_month(self.view):
            # make sure you cannot mark done ahead of time
            if not day["is_done"] and not is_future(day["date"]):
                search_replace(self.view, edit,
                               search_s=day["str_norm"],
                               replace_s=day["str_done"],
                               in_region=in_region,
                               date=day["date"])

class UnmarkDoneCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for day, in_region in chain_month(self.view):
            if day["is_done"]:
                search_replace(self.view, edit,
                               search_s=day["str_done"],
                               replace_s=day["str_norm"],
                               in_region=in_region,
                               date=day["date"])

class DoneToggleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for day, in_region in chain_month(self.view):
            # ignore if day is not done and in the future.
            # We do not want to toggle this
            if not day["is_done"] and is_future(day["date"]):
                continue

            search_s = day["str_done"] if day["is_done"] else day["str_norm"]
            replace_s = day["str_norm"] if day["is_done"] else day["str_done"]

            search_replace(self.view, edit,
                           search_s,
                           replace_s,
                           in_region,
                           day["date"])

class ChainsNewDocCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.new_file()
        regions = view.sel()
        view.set_syntax_file('Packages/Chains'
                             '/Chains.tmLanguage')
        view.run_command("dont_break_the_chain")
        # view.run_command("highlight_today")
        msg_r = view.find(INITIAL_MESSAGE, 0, sublime.LITERAL)
        region = region_cursor_start(msg_r)
        regions.clear()
        regions.add(region)

def search_replace(view, edit, search_s, replace_s, in_region, date):
    """search and replace text within a given region"""
    word_r = get_subregion(view, search_s, in_region)

    if word_r != NOT_FOUND:
        view.replace(edit, word_r, replace_s)
        if date == datetime.date.today():
            view.run_command('highlight_today')
    else:
        print("NOT FOUND!")

def get_subregion(view, word, in_region):
    sentence = view.substr(in_region)

    if word in sentence:
        i = sentence.index(word)
        start_r = in_region.begin() + i
        end_r = start_r + len(word)
        return sublime.Region(start_r, end_r)
    else:
        return NOT_FOUND

def expand_whole_word(view, region):
    """increase point/region until reaches \s, \n
       and DONE_SYMBOL, returns word region"""
    # deal with edge cases where the cursor is inbetween two dates
    if region.empty() and view.substr(region.b) == ' ':
        # peek to the right for any characters
        r = sublime.Region(region.a, region.b+2)
        c = view.substr(r).strip()

        if c:
            return expand_end_of_word(view, r)

        # peek to the left for any characters
        r = sublime.Region(region.a-2, region.b)
        c = view.substr(r).strip()

        if c:
            return expand_beginning_of_word(view, r)

    return expand_end_of_word(view, region)

def expand_beginning_of_word(view, region):
    """decrease the beginning region by 1 until /s and WEEKEND_SYMBOL is hit"""
    # print("expand begin:'" + view.substr(region) + "'",
    #    region, "'" + view.substr(region) + "'")
    beginnings = [' ', WEEKEND_SYMBOL]

    while not any(view.substr(region).startswith(c) for c in beginnings):
        region = add_region_by(region, -1, at_the="beginning")
    # make sure that we end region with NORMAL_SYMBOL,
    # as the space might be replaced with DONE_SYMBOL
    if not view.substr(region).endswith(NORMAL_SYMBOL):
        region = expand_end_of_word(view, region)
    return region

def expand_end_of_word(view, region):
    """increase the end region by 1
       until NORMAL_SYMBOL/WEEKEND_SYMBOL /n is hit"""
    word_r = view.word(region)
    endings = [NORMAL_SYMBOL, DONE_SYMBOL, '\n']

    while not any(view.substr(word_r).endswith(char) for char in endings):
        word_r = add_region_by(word_r, 1, at_the="end")
    return word_r

def add_region_by(region, by_num, at_the):
    """add the given number(positive/neg) to region at the given end"""
    if region.empty():
        return region

    bothends = {"beginning": region.begin(), "end": region.end()}
    end_value = bothends.get(at_the, region.end())

    if region.a == end_value:
        region.a += by_num
    if region.b == end_value:
        region.b += by_num
    return region

def region_cursor_start(region):
    """swap region range so that the end region would be at the beginning
    so cursor starts from the beginning when highlighted"""
    if region.b > region.a:
        region.a, region.b = region.b, region.a
    return region

def is_future(unknown_date):
    return (unknown_date > datetime.date.today())
<|endoftext|>


<commit_before><commit_msg>Use get_object_or_404<commit_after><|endoftext|>


<commit_before>""" This script runs an esprit reindex operation, to be used if you have changed the mappings."""

import esprit
from portality.lib import es_data_mapping
from portality.core import app

if __name__ == "__main__":
    if app.config.get("SCRIPTS_READ_ONLY_MODE", False):
        print "System is in READ-ONLY mode, script cannot run"
        exit()

    new_mappings = es_data_mapping.get_mappings(app)

    old = esprit.raw.Connection("localhost", 'doaj')
    new = esprit.raw.Connection("localhost", 'doaj_notes')

    esprit.tasks.reindex(old, new, "doaj_alias", new_mappings.keys(), new_mappings, new_version="1.7.5")

    eq = esprit.tasks.compare_index_counts([old, new], new_mappings.keys())
    print "all equal: ", eq
<commit_msg>tweaks to reindex script<commit_after>""" This script runs an esprit reindex operation, to be used if you have changed the mappings."""

import esprit
from portality.lib import es_data_mapping
from portality.core import app

if __name__ == "__main__":
    # We don't respect read-only mode (for scripts or overall) here
    # This is because this script is supposed to be run while the rest
    # of the system is in READ_ONLY_MODE and SCRIPTS_READ_ONLY_MODE
    # set to True.

    new_mappings = es_data_mapping.get_mappings(app)

    old = esprit.raw.make_connection(None, app.config["ELASTIC_SEARCH_HOST"], None, app.config["ELASTIC_SEARCH_DB"])
    new = esprit.raw.make_connection(None, app.config["ELASTIC_SEARCH_HOST"], None, app.config["ELASTIC_SEARCH_DB"] + '_notes')

    esprit.tasks.reindex(old, new, "doaj_alias", new_mappings.keys(), new_mappings, new_version="1.7.5")

    eq = esprit.tasks.compare_index_counts([old, new], new_mappings.keys())
    print "all equal: ", eq
<|endoftext|>


<commit_before>import json
import logging
import os
import traceback

import boto3

from github import Github

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)

S3_CLIENT = boto3.client("s3")
CB_CLIENT = boto3.client("codebuild")
SSM_CLIENT = boto3.client("ssm")

BUILD_JOB_NAME = os.environ.get("BUILD_JOB_NAME", "")

S3_BUCKET_NAME = os.environ.get("S3_BUCKET_NAME", "")
S3_KEY = os.environ.get("S3_KEY", "")

GITHUB_REPO_NAME = os.environ.get("GITHUB_REPO_NAME", "")
MAINTAINERS = json.loads(os.environ.get("PROJECT_MAINTAINERS", ""))
TEST_TAGS = json.loads(os.environ.get("TEST_TAGS", ""))

def get_token():
    param = os.environ.get("GITHUB_TOKEN", "")
    resp = SSM_CLIENT.get_parameters(Names=[param], WithDecryption=True)
    return resp["Parameters"][0]["Value"]

def get_history():
    if not S3_BUCKET_NAME or not S3_KEY:
        raise ValueError("Environment variables for s3 bucket not set")
    try:
        data = S3_CLIENT.get_object(Bucket=S3_BUCKET_NAME, Key=S3_KEY)["Body"]
    except S3_CLIENT.exceptions.NoSuchKey:
        return []
    return json.loads(data.read())

def set_history(history):
    history = list(set(history))
    body = bytes(json.dumps(history).encode("utf8"))
    S3_CLIENT.put_object(Bucket=S3_BUCKET_NAME, Key=S3_KEY, Body=body)

def comment_is_command(comment):
    for l in comment.split("\n"):
        for t in TEST_TAGS:
            if l.startswith(t):
                return True
    return False

def get_pending_builds(history):
    comments = {}
    repo = Github(get_token()).get_repo(GITHUB_REPO_NAME)
    prs = repo.get_pulls(state="open")
    for pr in prs:
        if pr.base.ref != "master":
            continue
        for comment in pr.get_issue_comments():
            if comment.id in history:
                continue
            if not comment_is_command(comment.body):
                continue
            if comment.user.login not in MAINTAINERS:
                LOG.warning(f"{comment.user.login} is not listed as a maintainer")
                continue
            if pr.number in comments:
                comments[pr.number]["comment_ids"].add(comment.id)
                continue
            comments[pr.number] = {
                "comment_ids": {comment.id},
                "pr_repo_name": pr.head.repo.full_name.split("/")[1],
                "pr_github_org": pr.head.repo.full_name.split("/")[0],
                "pr_branch": pr.head.ref,
            }
    return comments

def handler(_, __):
    history = get_history()
    builds = get_pending_builds(history)
    for pr_number, pr_details in builds.items():
        try:
            LOG.info(
                f"starting build for pr #{pr_number} due to comments "
                f"{pr_details['comment_ids']}"
            )
            CB_CLIENT.start_build(
                projectName=BUILD_JOB_NAME,
                environmentVariablesOverride=[
                    {"name": "PR_NUMBER", "value": str(pr_number), "type": "PLAINTEXT"},
                    {
                        "name": "PR_REPO_NAME",
                        "value": pr_details["pr_repo_name"],
                        "type": "PLAINTEXT",
                    },
                    {
                        "name": "PR_GITHUB_ORG",
                        "value": pr_details["pr_github_org"],
                        "type": "PLAINTEXT",
                    },
                    {
                        "name": "PR_BRANCH",
                        "value": pr_details["pr_branch"],
                        "type": "PLAINTEXT",
                    },
                ],
            )
            history += list(pr_details["comment_ids"])
        except Exception:
            LOG.error("failed to start build")
            traceback.print_exc()
    set_history(history)
<commit_msg>flake8 fixes.<commit_after>import json
import logging
import os
import traceback

import boto3

from github import Github

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)

S3_CLIENT = boto3.client("s3")
CB_CLIENT = boto3.client("codebuild")
SSM_CLIENT = boto3.client("ssm")

BUILD_JOB_NAME = os.environ.get("BUILD_JOB_NAME", "")

S3_BUCKET_NAME = os.environ.get("S3_BUCKET_NAME", "")
S3_KEY = os.environ.get("S3_KEY", "")

GITHUB_REPO_NAME = os.environ.get("GITHUB_REPO_NAME", "")
MAINTAINERS = json.loads(os.environ.get("PROJECT_MAINTAINERS", ""))
TEST_TAGS = json.loads(os.environ.get("TEST_TAGS", ""))

def get_token():
    param = os.environ.get("GITHUB_TOKEN", "")
    resp = SSM_CLIENT.get_parameters(Names=[param], WithDecryption=True)
    return resp["Parameters"][0]["Value"]

def get_history():
    if not S3_BUCKET_NAME or not S3_KEY:
        raise ValueError("Environment variables for s3 bucket not set")
    try:
        data = S3_CLIENT.get_object(Bucket=S3_BUCKET_NAME, Key=S3_KEY)["Body"]
    except S3_CLIENT.exceptions.NoSuchKey:
        return []
    return json.loads(data.read())

def set_history(history):
    history = list(set(history))
    body = bytes(json.dumps(history).encode("utf8"))
    S3_CLIENT.put_object(Bucket=S3_BUCKET_NAME, Key=S3_KEY, Body=body)

def comment_is_command(comment):
    for _l in comment.split("\n"):
        for _t in TEST_TAGS:
            if _l.startswith(_t):
                return True
    return False

def get_pending_builds(history):
    comments = {}
    repo = Github(get_token()).get_repo(GITHUB_REPO_NAME)
    prs = repo.get_pulls(state="open")
    for pr in prs:
        if pr.base.ref != "master":
            continue
        for comment in pr.get_issue_comments():
            if comment.id in history:
                continue
            if not comment_is_command(comment.body):
                continue
            if comment.user.login not in MAINTAINERS:
                LOG.warning(f"{comment.user.login} is not listed as a maintainer")
                continue
            if pr.number in comments:
                comments[pr.number]["comment_ids"].add(comment.id)
                continue
            comments[pr.number] = {
                "comment_ids": {comment.id},
                "pr_repo_name": pr.head.repo.full_name.split("/")[1],
                "pr_github_org": pr.head.repo.full_name.split("/")[0],
                "pr_branch": pr.head.ref,
            }
    return comments

def handler(_, __):
    history = get_history()
    builds = get_pending_builds(history)
    for pr_number, pr_details in builds.items():
        try:
            LOG.info(
                f"starting build for pr #{pr_number} due to comments "
                f"{pr_details['comment_ids']}"
            )
            CB_CLIENT.start_build(
                projectName=BUILD_JOB_NAME,
                environmentVariablesOverride=[
                    {"name": "PR_NUMBER", "value": str(pr_number), "type": "PLAINTEXT"},
                    {
                        "name": "PR_REPO_NAME",
                        "value": pr_details["pr_repo_name"],
                        "type": "PLAINTEXT",
                    },
                    {
                        "name": "PR_GITHUB_ORG",
                        "value": pr_details["pr_github_org"],
                        "type": "PLAINTEXT",
                    },
                    {
                        "name": "PR_BRANCH",
                        "value": pr_details["pr_branch"],
                        "type": "PLAINTEXT",
                    },
                ],
            )
            history += list(pr_details["comment_ids"])
        except Exception:
            LOG.error("failed to start build")
            traceback.print_exc()
    set_history(history)
<|endoftext|>


<commit_before>#!/usr/bin/python

# This tutorial is for Python 3.X
#
# Python is a Scripted Language - Operates at runtime, line by line until it reaches an error
# As opposed to compiled languages where they are compiled before being executed.
#
# Execute with python3 python.py
# Executing without the 3 will use python2.7x
#
###

# Commenting

# Single Line Comment

'''
Multi-Line Comment
'''

'''

# Print and Strings

print('Hello World')
print("Double Quotes")
print('Concat'+'enation')
print('hello','there') # Adds a space, purpose is for combining strings and integers
print('I am',5)

# Escape characters and using ' within a string

print('I\'m 5') # Using \ to escape a character
print("I'm 5") # Using Double quotes if you want to use single quote in the string
print('He said "Hi"') # Using single quotes if you want to use double quotes in the string

# Math

print(1+3) # Addition
print(1-3) # Subtraction
print(1*3) # Multiplication
print(1/3) # Division

print(1.5/3.6) # Float
print(4**2) # Exponents

# Variables

exVar = 100 # Integer
exVar = '100' # String
print(exVar)

exVar = 100
opVar = exVar / 5.3
print(opVar)

_100ma = 5 # Moving Average Variable that begins with a number, prefaced with an underscore
print(_100ma)

# While Loop

condition = 1
while condition < 10: # The : is a standard to allow for tabbing on the next line, doesn't seem to work in subl.
	print(condition)
	condition += 1
	# Alternate: condition = condition+1

while True:	# Infinite loop
	print('infinity')

# For Loops

exampleList = [1,6,7,3,6,9,0]
for x in exampleList:
	print(x)

for x in range (1,11):
	print(X)

'''
# If statements

x = 2
y = 7
z = 10

if x > y:
	print(x,'is greater than',y)
if x < y:
	print(x,'is less than',y)

if x == y: # == is a comparison operator
	print(x,'is the same as',y)

if x == 2:
	print(x,'equals the string 2')

if x <= y:
	print(x,'is less than or equal to',y)

if x >= y:
	print(x,'is greater than or equal to',y)

if z > y > x:
	print(z,'is greater than',y,'which is greater than',x)

<commit_msg>Updated Notes<commit_after>#!/usr/bin/python

# This tutorial is for Python 3.X
#
# Python is a Scripted Language which operates at runtime, line by line until it reaches an error or completes its operations
# This is as opposed to compiled languages where they are compiled into a binary application.
#
# Commands should be executed with the python3 command
# Executing without the trailing 3 will use python 2.x
#
###

# Commenting

# Single Line Comment

'''
Multi-Line Comment
'''

# Print and Strings

print('Hello World') # Single Qoutes
print("Double Quotes")
print('Concat'+'enation') # Merging two strings
print('hello','there') # Adds a space, purpose is for combining strings and integers on the same line

# Escape characters and using ' within a string

print('I am',5) # Print both the string and integer with a space inbetween
print('I\'m 5') # Using \ to escape a character and allow for the ' to be printed to std out
print("I'm 5") # Using Double quotes if you want to use single quote in the string
print('He said "Hi"') # Using single quotes if you want to use double quotes in the string

# Math

print(1+3) # Addition
print(1-3) # Subtraction
print(1*3) # Multiplication
print(1/3) # Division
print(1.5/3.6) # Float
print(4**2) # Exponents

# Variables

exVar = 100 # Integer
exVar = '100' # String
print(exVar)

exVar = 100
opVar = exVar / 5.3
print(opVar)

_100ma = 5 # Moving Average Variable that begins with a number, prefaced with an underscore
print(_100ma)

# While Loop

condition = 1
while condition < 10: # The : is a standard to allow for tabbing on the next line
	print(condition)
	condition += 1
	# Alternate: condition = condition+1

# Infinite loop

while True:
	print('infinity')

# For Loops

exampleList = [1,6,7,3,6,9,0]
for x in exampleList:
	print(x)

for x in range (1,11):
	print(X)

# If statements

x = 2
y = 7
z = 10

if x > y:
	print(x,'is greater than',y)
if x < y:
	print(x,'is less than',y)

if x == y: # == is a comparison operator
	print(x,'is the same as',y)

if x == 2:
	print(x,'equals the string 2')

if x <= y:
	print(x,'is less than or equal to',y)

if x >= y:
	print(x,'is greater than or equal to',y)

if z > y > x:
	print(z,'is greater than',y,'which is greater than',x)

<|endoftext|>


<commit_before>#! /usr/bin/env python
from __future__ import with_statement

# sfi -- slice-based facility interface

import sys
import os, os.path
from optparse import OptionParser
from util.cert import Keypair, Certificate
from util.credential import Credential
from util.geniclient import GeniClient

sfi_dir = os.path.expanduser("~/.sfi/")
sm_chan = None
reg_chan = None
user = None
authority = None
verbose = False

#
# Establish Connection to SliceMgr and Registry Servers
#
def set_servers(options):
   global sm_chan
   global reg_chan
   global user
   global authority

   # Set SliceMgr URL
   if (options.sm is not None):
      sm_url = options.sm
   elif ("SFI_SM" in os.environ):
      sm_url = os.environ["SFI_SM"]
   else:
      print "No Known Slice Manager"
      sys.exit(1)

   # Set Registry URL
   if (options.registry is not None):
      reg_url = options.registry
   elif ("SFI_REGISTRY" in os.environ):
      reg_url = os.environ["SFI_REGISTRY"]
   else:
      print "No Known Registry Server"
      sys.exit(1)

   if options.verbose :
      print "Contacting Slice Manager at:", sm_url
      print "Contacting Registry at:", registry_url

   # Set user HRN
   if (options.user is not None):
      user = options.user
   elif ("SFI_USER" in os.environ):
      user = os.environ["SFI_USER"]
   else:
      print "No Known User Name"
      sys.exit(1)

   # Set authority HRN
   if (options.auth is not None):
      authority = options.auth
   elif ("SFI_AUTH" in os.environ):
      authority = os.environ["SFI_AUTH"]
   else:
      authority = None

   # Get key and certificate
   key_file = get_key_file()
   cert_file = get_cert_file(key_file)

   # Establish connection to server(s)
   # SliceMgr and Registry may be available on the same server
   if (sm_url == registry_url):
      sm_chan = GeniClient(sm_url, key_file, cert_file)
      reg_chan = sm_chan
   else:
      sm_chan = GeniClient(sm_url, key_file, cert_file)
      reg_chan = GeniClient(registry_url, key_file, cert_file)
   return

#
# Get various credential and spec files
#
# Establishes limiting conventions
#   - conflates MAs and SAs
#   - assumes last token in slice name is unique
#
# Bootstraps credentials
#   - bootstrap user credential from self-signed certificate
#   - bootstrap authority credential from user credential
#   - bootstrap slice credential from user credential
#

def get_leaf(name):
   parts = name.split(".")
   return parts[-1]

def get_key_file():
   file = os.path.join(sfi_dir, get_leaf(user) + ".pkey")
   if (os.path.isfile(file)):
      return file
   else:
      print "Key file", file, "does not exist"
      sys.exit(-1)
   return

def get_cert_file(key_file):
   global verbose

   file = os.path.join(sfi_dir, get_leaf(user) + ".cert")
   if (os.path.isfile(file)):
      return file
   else:
      k = Keypair(filename = key_file)
      cert = Certificate(subject=user)
      cert.set_pubkey(k)
      cert.set_issuer(k, user)
      cert.sign()
      if verbose :
         print "Writing self-signed certificate to", file
      cert.save_to_file(file)
      return file

def get_user_cred():
   global user

   file = os.path.join(sfi_dir, get_leaf(user) + ".cred")
   if (os.path.isfile(file)):
      user_cred = Credential(filename=file)
      return user_cred
   else:
      # bootstrap user credential
      user_cred = get_credential(None, "user", user)
      if user_cred:
         user_cred.save_to_file(file, save_parents=True)
         if verbose:
            print "Writing user credential to", file
         return user_cred
      else:
         print "Failed to get user credential"
         sys.exit(-1)

def get_auth_cred():
   global authority

   file = os.path.join(sfi_dir, "authority.cred")
   if (os.path.isfile(file)):
      auth_cred = Credential(filename=file)
      return auth_cred
   else:
      # bootstrap authority credential from user credential
      user_cred = get_user_cred()
      auth_cred = get_credential(user_cred, "sa", authority)
      if auth_cred:
         auth_cred.save_to_file(file, save_parents=True)
         if verbose:
            print "Writing authority credential to", file
         return auth_cred
      else:
         print "Failed to get authority credential"
         sys.exit(-1)

def get_slice_cred(name):
   file = os.path.join(sfi_dir, "slice_" + get_leaf(name) + ".cred")
   if (os.path.isfile(file)):
      slice_cred = Credential(filename=file)
      return slice_cred
   else:
      # bootstrap slice credential from user credential
      user_cred = get_user_cred()
      slice_cred = get_credential(user_cred, "slice", name)
      if slice_cred:
         slice_cred.save_to_file(file, save_parents=True)
         if verbose:
            print "Writing slice credential to", file
         return slice_cred
      else:
         print "Failed to get slice credential"
         sys.exit(-1)

def get_rspec_file(rspec):
   if (os.path.isabs(rspec)):
      file = rspec
   else:
      file = os.path.join(sfi_dir, rspec)
   if (os.path.isfile(file)):
      return file
   else:
      print "No such rspec file"
      sys.exit(1)

def get_record_file(record):
   if (os.path.isabs(record)):
      file = record
   else:
      file = os.path.join(sfi_dir, record)
   if (os.path.isfile(file)):
      return file
   else:
      print "No such registry record file"
      sys.exit(1)

#
# Generate sub-command parser
#
def create_cmd_parser(command):
   cmdargs = {"list": "name",
              "show": "name",
              "remove": "name",
              "add": "name record",
              "update": "name record",
              "nodes": "[name]",
              "slices": "",
              "resources": "name",
              "create": "name rspec",
              "delete": "name",
              "reset": "name",
              "start": "name",
              "stop": "name"
             }
   if command not in cmdargs:
      print "Invalid command\n"
      print "Commands:list,show,remove,add,update,nodes,slices,resources,create,delete,start,stop,reset"
      sys.exit(2)

   parser = OptionParser(usage="sfi [sfi_options] %s [options] %s" \
      % (command, cmdargs[command]))
   if command in ("nodes", "resources"):
      parser.add_option("-f", "--format", dest="format",type="choice",
           help="output format (dns|ip|hrn|rspec)",default="rspec",
           choices=("dns","ip","hrn","rspec"))
   elif command in ("list", "show", "remove"):
      parser.add_option("-t", "--type", dest="type",type="choice",
           help="type filter (user|slice|sa|ma|node|aggregate)",
           choices=("user","slice","sa","ma","node","aggregate", "all"),
           default="all")
   return parser

#
# Main: parse arguments and dispatch to command
#
def main():
   global sm_chan
   global reg_chan
   global verbose

   # Generate command line parser
   parser = OptionParser(usage="sfi [options] command [command_options] [command_args]",
        description="Commands: list,show,remove,add,update,nodes,slices,resources,create,delete,start,stop,reset")
   parser.add_option("-r", "--registry", dest="registry",
        help="root registry", metavar="URL", default=None)
   parser.add_option("-s", "--slicemgr", dest="sm",
        help="slice manager", metavar="URL", default=None)
   parser.add_option("-d", "--dir", dest="dir",
        help="working directory", metavar="PATH", default = sfi_dir)
   parser.add_option("-u", "--user", dest="user",
        help="user name", metavar="USER_HRN", default=None)
   parser.add_option("-a", "--auth", dest="auth",
        help="authority name", metavar="AUTH_HRN", default=None)
   parser.add_option("-v", "--verbose",
        action="store_true", dest="verbose", default=False,
        help="verbose mode")
   parser.disable_interspersed_args()
   (options, args) = parser.parse_args()
   command = args[0]
   (cmd_opts, cmd_args) = create_cmd_parser(command).parse_args(args[1:])
   verbose = options.verbose
   if verbose :
      print options.registry, options.sm, options.dir, options.verbose,\
         options.user, options.auth
      print command
      if command in ("nodes", "resources"):
         print cmd_opts.format
      elif command in ("list","show","remove"):
         print cmd_opts.type
      print cmd_args

   set_servers(options)

   # Dispatch to selected command
   try:
      globals()[command](cmd_opts, cmd_args)
   except KeyError:
      print "Command not found:", command
      sys.exit(1)
   return

#
# Following functions implement the commands
#
# Registry-related commands
#

# list entires in named authority registry
def list(opts, args):
   global reg_chan
   user_cred = get_user_cred()
   result = reg_chan.list(user_cred, args[0])
   display_record(opts.type, results)
   return

# show named registry record
def show(opts, args):
   global reg_chan
   user_cred = get_user_cred()
   result = reg_chan.resolve(user_cred, args[0])
   display_record(opts.type, results)
   return

# removed named registry record
#   - have to first retrieve the record to be removed
def remove(opts, args):
   global reg_chan
   auth_cred = get_auth_cred()
   results = reg_chan.resolve(auth_cred, args[0])
   record = filter_record(opts.type, results)
   return reg_chan.remove(auth_cred, record)

# add named registry record
def add(opts, args):
   global reg_chan
   auth_cred = get_auth_cred()
   rec_file = get_record_file(args[1])
   with open(rec_file) as f:
      record = f.read()
   return reg_chan.register(auth_cred, record)

# update named registry entry
def update(opts, args):
   global reg_chan
   user_cred = get_user_cred()
   rec_file = get_record_file(args[1])
   with open(rec_file) as f:
      record = f.read()
   return reg_chan.update(user_cred, record)

#
# Slice-related commands
#

# list available nodes
def nodes(opts, args):
   global sm_chan
   user_cred = get_user_cred()
   if (len(args) == 0):
      context = None
   else:
      context = args[0]
   result = sm_chan.list_nodes(user_cred, context)
   display_rspec(opts.format, result)
   return

# list instantiated slices
def slices(opts, args):
   global sm_chan
   user_cred = get_user_cred()
   result = sm_chan.list_slices(user_cred)
   display_rspec(opts.format, results)
   return

# show rspec for named slice
def resources(opts, args):
   global sm_chan
   slice_cred = get_slice_cred(args[0])
   print "resources:", opts.format, args[0], sm_chan
   result = sm_chan.get_resources(slice_cred, args[0])
   display_rspec(opts.format, result)
   return

# created named slice with given rspec
def create(opts, args):
   global sm_chan
   slice_cred = get_slice_cred(args[0])
   rspec_file = get_rspec_file(args[1])
   with open(rspec_file) as f:
      rspec = f.read()
   return sm_chan.instantiate(slice_cred, rspec)

# delete named slice
def delete(opts, args):
   global sm_chan
   slice_cred = get_slice_cred(args[0])
   return sm_chan.delete_slice(slice_cred)

# start named slice
def start(opts, args):
   global sm_chan
   slice_cred = get_slice_cred(args[0])
   return sm_chan.start_slice(slice_cred)

# stop named slice
def stop(opts, args):
   global sm_chan
   slice_cred = get_slice_cred(args[0])
   return sm_chan.stop_slice(slice_cred)

# reset named slice
def reset(opts, args):
   global sm_chan
   slice_cred = get_slice_cred(args[0])
   return sm_chan.reset_slice(slice_cred)

#
#
# Display and Filter RSpecs and Records
#
#

def display_rspec(format, rspec):
   print "display rspec"
   return

def display_record(type, record):
   rec = filter_record(type, record)
   print "display record"
   return

def filter_record(type, record):
   print "filter record"
   return

if __name__=="__main__":
   main()
<commit_msg>resolved imports<commit_after>#! /usr/bin/env python
from __future__ import with_statement

# sfi -- slice-based facility interface

import sys
import os, os.path
from optparse import OptionParser
from util.cert import Keypair, Certificate
from util.credential import Credential
from util.geniclient import GeniClient

sfi_dir = os.path.expanduser("~/.sfi/")
slicemgr = None
registry = None
user = None
authority = None
verbose = False

#
# Establish Connection to SliceMgr and Registry Servers
#
def set_servers(options):
   global slicemgr
   global registry
   global user
   global authority

   # Set SliceMgr URL
   if (options.sm is not None):
      sm_url = options.sm
   elif ("SFI_SM" in os.environ):
      sm_url = os.environ["SFI_SM"]
   else:
      print "No Known Slice Manager"
      sys.exit(1)

   # Set Registry URL
   if (options.registry is not None):
      reg_url = options.registry
   elif ("SFI_REGISTRY" in os.environ):
      reg_url = os.environ["SFI_REGISTRY"]
   else:
      print "No Known Registry Server"
      sys.exit(1)

   if options.verbose :
      print "Contacting Slice Manager at:", sm_url
      print "Contacting Registry at:", registry_url

   # Set user HRN
   if (options.user is not None):
      user = options.user
   elif ("SFI_USER" in os.environ):
      user = os.environ["SFI_USER"]
   else:
      print "No Known User Name"
      sys.exit(1)

   # Set authority HRN
   if (options.auth is not None):
      authority = options.auth
   elif ("SFI_AUTH" in os.environ):
      authority = os.environ["SFI_AUTH"]
   else:
      authority = None

   # Get key and certificate
   key_file = get_key_file()
   cert_file = get_cert_file(key_file)

   # Establish connection to server(s)
   # SliceMgr and Registry may be available on the same server
   if (sm_url == registry_url):
      slicemgr = GeniClient(sm_url, key_file, cert_file)
      registry = slicemgr
   else:
      slicemgr = GeniClient(sm_url, key_file, cert_file)
      registry = GeniClient(registry_url, key_file, cert_file)
   return

#
# Get various credential and spec files
#
# Establishes limiting conventions
#   - conflates MAs and SAs
#   - assumes last token in slice name is unique
#
# Bootstraps credentials
#   - bootstrap user credential from self-signed certificate
#   - bootstrap authority credential from user credential
#   - bootstrap slice credential from user credential
#

def get_leaf(name):
   parts = name.split(".")
   return parts[-1]

def get_key_file():
   file = os.path.join(sfi_dir, get_leaf(user) + ".pkey")
   if (os.path.isfile(file)):
      return file
   else:
      print "Key file", file, "does not exist"
      sys.exit(-1)
   return

def get_cert_file(key_file):
   global verbose

   file = os.path.join(sfi_dir, get_leaf(user) + ".cert")
   if (os.path.isfile(file)):
      return file
   else:
      k = Keypair(filename = key_file)
      cert = Certificate(subject=user)
      cert.set_pubkey(k)
      cert.set_issuer(k, user)
      cert.sign()
      if verbose :
         print "Writing self-signed certificate to", file
      cert.save_to_file(file)
      return file

def get_user_cred():
   global user

   file = os.path.join(sfi_dir, get_leaf(user) + ".cred")
   if (os.path.isfile(file)):
      user_cred = Credential(filename=file)
      return user_cred
   else:
      # bootstrap user credential
      user_cred = get_credential(None, "user", user)
      if user_cred:
         user_cred.save_to_file(file, save_parents=True)
         if verbose:
            print "Writing user credential to", file
         return user_cred
      else:
         print "Failed to get user credential"
         sys.exit(-1)

def get_auth_cred():
   global authority

   file = os.path.join(sfi_dir, "authority.cred")
   if (os.path.isfile(file)):
      auth_cred = Credential(filename=file)
      return auth_cred
   else:
      # bootstrap authority credential from user credential
      user_cred = get_user_cred()
      auth_cred = get_credential(user_cred, "sa", authority)
      if auth_cred:
         auth_cred.save_to_file(file, save_parents=True)
         if verbose:
            print "Writing authority credential to", file
         return auth_cred
      else:
         print "Failed to get authority credential"
         sys.exit(-1)

def get_slice_cred(name):
   file = os.path.join(sfi_dir, "slice_" + get_leaf(name) + ".cred")
   if (os.path.isfile(file)):
      slice_cred = Credential(filename=file)
      return slice_cred
   else:
      # bootstrap slice credential from user credential
      user_cred = get_user_cred()
      slice_cred = get_credential(user_cred, "slice", name)
      if slice_cred:
         slice_cred.save_to_file(file, save_parents=True)
         if verbose:
            print "Writing slice credential to", file
         return slice_cred
      else:
         print "Failed to get slice credential"
         sys.exit(-1)

def get_rspec_file(rspec):
   if (os.path.isabs(rspec)):
      file = rspec
   else:
      file = os.path.join(sfi_dir, rspec)
   if (os.path.isfile(file)):
      return file
   else:
      print "No such rspec file"
      sys.exit(1)

def get_record_file(record):
   if (os.path.isabs(record)):
      file = record
   else:
      file = os.path.join(sfi_dir, record)
   if (os.path.isfile(file)):
      return file
   else:
      print "No such registry record file"
      sys.exit(1)

#
# Generate sub-command parser
#
def create_cmd_parser(command):
   cmdargs = {"list": "name",
              "show": "name",
              "remove": "name",
              "add": "name record",
              "update": "name record",
              "nodes": "[name]",
              "slices": "",
              "resources": "name",
              "create": "name rspec",
              "delete": "name",
              "reset": "name",
              "start": "name",
              "stop": "name"
             }
   if command not in cmdargs:
      print "Invalid command\n"
      print "Commands:list,show,remove,add,update,nodes,slices,resources,create,delete,start,stop,reset"
      sys.exit(2)

   parser = OptionParser(usage="sfi [sfi_options] %s [options] %s" \
      % (command, cmdargs[command]))
   if command in ("nodes", "resources"):
      parser.add_option("-f", "--format", dest="format",type="choice",
           help="output format (dns|ip|hrn|rspec)",default="rspec",
           choices=("dns","ip","hrn","rspec"))
   elif command in ("list", "show", "remove"):
      parser.add_option("-t", "--type", dest="type",type="choice",
           help="type filter (user|slice|sa|ma|node|aggregate)",
           choices=("user","slice","sa","ma","node","aggregate", "all"),
           default="all")
   return parser

#
# Main: parse arguments and dispatch to command
#
def main():
   global verbose

   # Generate command line parser
   parser = OptionParser(usage="sfi [options] command [command_options] [command_args]",
        description="Commands: list,show,remove,add,update,nodes,slices,resources,create,delete,start,stop,reset")
   parser.add_option("-r", "--registry", dest="registry",
        help="root registry", metavar="URL", default=None)
   parser.add_option("-s", "--slicemgr", dest="sm",
        help="slice manager", metavar="URL", default=None)
   parser.add_option("-d", "--dir", dest="dir",
        help="working directory", metavar="PATH", default = sfi_dir)
   parser.add_option("-u", "--user", dest="user",
        help="user name", metavar="HRN", default=None)
   parser.add_option("-a", "--auth", dest="auth",
        help="authority name", metavar="HRN", default=None)
   parser.add_option("-v", "--verbose",
        action="store_true", dest="verbose", default=False,
        help="verbose mode")
   parser.disable_interspersed_args()
   (options, args) = parser.parse_args()
   command = args[0]
   (cmd_opts, cmd_args) = create_cmd_parser(command).parse_args(args[1:])
   verbose = options.verbose
   if verbose :
      print options.registry, options.sm, options.dir, options.verbose,\
         options.user, options.auth
      print command
      if command in ("nodes", "resources"):
         print cmd_opts.format
      elif command in ("list","show","remove"):
         print cmd_opts.type
      print cmd_args

   set_servers(options)

   # Dispatch to selected command
   try:
      globals()[command](cmd_opts, cmd_args)
   except KeyError:
      print "Command not found:", command
      sys.exit(1)
   return

#
# Following functions implement the commands
#
# Registry-related commands
#

# list entires in named authority registry
def list(opts, args):
   global registry
   user_cred = get_user_cred()
   result = registry.list(user_cred, args[0])
   display_record(opts.type, results)
   return

# show named registry record
def show(opts, args):
   global registry
   user_cred = get_user_cred()
   result = reg_chan.resolve(user_cred, args[0])
   display_record(opts.type, results)
   return

# removed named registry record
#   - have to first retrieve the record to be removed
def remove(opts, args):
   global registry
   auth_cred = get_auth_cred()
   results = registry.resolve(auth_cred, args[0])
   record = filter_record(opts.type, results)
   return registry.remove(auth_cred, record)

# add named registry record
def add(opts, args):
   global registry
   auth_cred = get_auth_cred()
   rec_file = get_record_file(args[1])
   with open(rec_file) as f:
      record = f.read()
   return registry.register(auth_cred, record)

# update named registry entry
def update(opts, args):
   global registry
   user_cred = get_user_cred()
   rec_file = get_record_file(args[1])
   with open(rec_file) as f:
      record = f.read()
   return registry.update(user_cred, record)

#
# Slice-related commands
#

# list available nodes
def nodes(opts, args):
   global slicemgr
   user_cred = get_user_cred()
   if (len(args) == 0):
      context = None
   else:
      context = args[0]
   result = slicemgr.list_nodes(user_cred, context)
   display_rspec(opts.format, result)
   return

# list instantiated slices
def slices(opts, args):
   global slicemgr
   user_cred = get_user_cred()
   result = slicemgr.list_slices(user_cred)
   display_rspec(opts.format, results)
   return

# show rspec for named slice
def resources(opts, args):
   global slicemgr
   slice_cred = get_slice_cred(args[0])
   result = slicemgr.get_resources(slice_cred, args[0])
   display_rspec(opts.format, result)
   return

# created named slice with given rspec
def create(opts, args):
   global slicemgr
   slice_cred = get_slice_cred(args[0])
   rspec_file = get_rspec_file(args[1])
   with open(rspec_file) as f:
      rspec = f.read()
   return slicemgr.instantiate(slice_cred, rspec)

# delete named slice
def delete(opts, args):
   global slicemgr
   slice_cred = get_slice_cred(args[0])
   return slicemgr.delete_slice(slice_cred)

# start named slice
def start(opts, args):
   global slicemgr
   slice_cred = get_slice_cred(args[0])
   return slicemgr.start_slice(slice_cred)

# stop named slice
def stop(opts, args):
   global slicemgr
   slice_cred = get_slice_cred(args[0])
   return slicemgr.stop_slice(slice_cred)

# reset named slice
def reset(opts, args):
   global slicemgr
   slice_cred = get_slice_cred(args[0])
   return slicemgr.reset_slice(slice_cred)

#
#
# Display and Filter RSpecs and Records
#   - to be replace by EMF-generated routines
#
#

def display_rspec(format, rspec):
   print "display rspec"
   return

def display_record(type, record):
   rec = filter_record(type, record)
   print "display record"
   return

def filter_record(type, record):
   print "filter record"
   return

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


<commit_before>import math

from bge import logic, constraints, types
from mathutils import Vector, Matrix

from .attack_manager import AttackSensor, MeleeAttackManager, MouseRangeAttackManager

class Character(types.KX_GameObject):
	MAX_HP = 10

	MAX_SPEED = 0.10
	MAX_AIR_SPEED = MAX_SPEED * 0.75
	ACCELERATION = 0.01
	DECELERATION = ACCELERATION * 10
	FRICTION = ACCELERATION

	GRAVITY = 9.8 * 5

	MESH = "Sinbad"

	ANIMATIONS = {
				"move": [('RunBase', 1, 20), ('RunTop', 1, 20)],
				"idle": [('IdleBase', 1, 220), ('IdleTop', 1, 300)],
				"jump_start": [('JumpStart', 1, 5)],
				"jump_loop": [('JumpLoop', 1, 30)],
				"dead": [('Dance', 1, 71)],
				}

	def __init__(self, obj):
		types.KX_GameObject.__init__(obj)

		self._speed_h = Vector.Fill(2)
		self._flags = set()

		self._library = ""

		self.hp = self.MAX_HP

		l = [i for i in self.childrenRecursive if isinstance(i, types.BL_ArmatureObject)]
		if l:
			self._armature = l[0]
		else:
			self._armature = None

		# Don't play animations if this is true
		self.animation_lock = False

		# Enable double jump
		self._phy_char = constraints.getCharacter(self)
		self._phy_char.maxJumps = 2

		self.gravity = self.GRAVITY

		self.running = False

		self = self

	@property
	def is_dead(self):
		return "DEAD" in self._flags

	@property
	def gravity(self):
		return self._phy_char.gravity

	@gravity.setter
	def gravity(self, value):
		self._phy_char.gravity = value

	@property
	def airborne(self):
		return not self._phy_char.onGround

	@property
	def armature(self):
		return self._armature if self._armature else self

	@classmethod
	def spawn(cls, position=None, orientation=None):
		name = cls.MESH

		library = "//../characters/" + name + ".blend"
		logic.LibLoad(library, "Scene", load_actions=True)

		scene = logic.getCurrentScene()
		adder = logic.getCurrentController().owner
		if position:
			adder.worldPosition = position
		if orientation:
			adder.worldOrientation = orientation

		obj = scene.addObject(name, adder)

		char = cls(obj)
		char._library = library
		return char

	def free(self):
		if self._library:
			logic.LibFree(self._library)

	def rotate(self, rotation):
		if not "DEAD" in self._flags:
			self.applyRotation(Vector((0, 0, rotation)))

	def animate(self, animation, speed=1.0):
		if self.animation_lock:
			return

		if animation not in self.ANIMATIONS:
			print("WARNING: %s does not have animation %s" % (self.__class__.__name__, animation))
			return

		for layer, v in enumerate(self.ANIMATIONS[animation]):
			anim, start, end = v
			if anim == "*":
				continue
			ob = self.armature

			ob.playAction(anim, start, end, play_mode=logic.KX_ACTION_MODE_LOOP, layer=layer, blendin=3, layer_weight=0.5, speed=speed)

	def stop_animation(self, layer):
		if self._armature:
			self._armature.stopAction(layer)
		else:
			self.stopAction(layer)

	def update(self):
		if self.is_dead:
			self._apply_movement(Vector((0, 0, 0)))
			self.animate('dead')
			return

		if self.hp <= 0:
			self._flags.add("DEAD")
			return

		if self.airborne:
			self.animate('jump_loop')
			self.stop_animation(1)
		elif self._speed_h.length_squared < 0.0001:
			self.animate('idle')
		else:
			self.animate('move', 2.0 if self.running else 1.0)

	def move(self, direction):
		'''Moves the player horizontally'''

		if self.is_dead:
			return

		# Determine the direction the player is moving
		momentum = self._speed_h.copy()
		momentum.normalize()

		if direction.length_squared != 0:

			# Normalize direction
			direction.normalize()

			# Accelerate player if not going fast enough
			if momentum.length_squared == 0 or momentum.angle(direction) < math.pi:
				self._speed_h += self.ACCELERATION * direction
			else:
				self._speed_h += self.DECELERATION * direction

			if not self.airborne:
				max_speed = self.MAX_SPEED * (2.0 if self.running else 1.0)
			else:
				max_speed = self.MAX_AIR_SPEED

			# print(player['speed_h'])
			if self._speed_h.length_squared > max_speed ** 2:
				self._speed_h = max_speed * direction
		else:  # Friction
			friction = min(self._speed_h.length, self.FRICTION) * momentum
			self._speed_h = self._speed_h - friction

		movement = self._speed_h.to_3d()
		self._apply_movement(movement)

		# Face the player in the direction they are moving
		if movement.length_squared > 0.0001:
			x = movement.cross(Vector((0, 0, 1)))
			x.normalize()

			y = movement.normalized()

			z = x.cross(y)
			z.normalize()

			ori = (
					(x[0], y[0], z[0]),
					(x[1], y[1], z[1]),
					(x[2], y[2], z[2])
				)

			self.localOrientation = ori

	def jump(self, double=False):
		if self.is_dead:
			return

		if not self.airborne and self._phy_char.jumpCount == 0:
			self.animate('jump_start')

		self._phy_char.jump()

	def _apply_movement(self, vec):
		'''Applies a movement vector to the in game player object'''

		# Scale the speed so FPS changes don't mess with movement
		fps = logic.getAverageFrameRate()
		fps_scale = 60 / fps if fps != 0 else 60

		self._phy_char.walkDirection = vec * fps_scale

class Meatsack(Character):
	MESH = "Cosbad"

	MELEE_ATTACK = [
			('SliceVertical', 1, 16),
		]

	def __init__(self, gameobj):
		super().__init__(gameobj)

		attack_sensors = [AttackSensor(i, self) for i in self.childrenRecursive if i.name.startswith('AttackSensor')]
		self.attack_manager = MeleeAttackManager(self, attack_sensors, self.MELEE_ATTACK, 5)

	def update(self):
		self.attack_manager.update()
		super().update()

	def attack(self):
		self.attack_manager.attack()

class UllurCharacter(Character):
	LEFT_MELEE_ATTACKS = [
			('Attack1', 1, 4),
			('Attack2', 1, 4),
		]

	RIGHT_MELEE_ATTACKS = [
			('SliceHorizontal', 1, 16),
		]

	def __init__(self, gameobj):
		super().__init__(gameobj)
		attack_sensors = [AttackSensor(i, self) for i in self.childrenRecursive if i.name.startswith('AttackSensor')]
		self.left_attack_manager = MeleeAttackManager(self, attack_sensors, self.LEFT_MELEE_ATTACKS, 5)
		#self.right_attack_manager = MeleeAttackManager(self, attack_sensors, self.RIGHT_MELEE_ATTACKS)
		self.right_attack_manager = MouseRangeAttackManager(self, "Projectile", 1, 100, 10, 0.5)
		self.collectables = []

	def update(self):
		self.left_attack_manager.update()
		self.right_attack_manager.update()
		super().update()

	def attack(self, mode):

		if mode == "LEFT":
			self.stop_animation(1)
			self.left_attack_manager.attack()
		else:
			self.right_attack_manager.attack()

	def add_collectable(self, collectable):
		if collectable not in self.collectables:
			self.collectables.append(collectable)
<commit_msg>The speed multiplier for running is now controlled by a RUN_MULTIPLIER variable.<commit_after>import math

from bge import logic, constraints, types
from mathutils import Vector, Matrix

from .attack_manager import AttackSensor, MeleeAttackManager, MouseRangeAttackManager

class Character(types.KX_GameObject):
	MAX_HP = 10

	MAX_SPEED = 0.10
	MAX_AIR_SPEED = MAX_SPEED * 0.75
	ACCELERATION = 0.01
	DECELERATION = ACCELERATION * 10
	FRICTION = ACCELERATION

	RUN_MULTIPLIER = 2.0

	GRAVITY = 9.8 * 5

	MESH = "Sinbad"

	ANIMATIONS = {
				"move": [('RunBase', 1, 20), ('RunTop', 1, 20)],
				"idle": [('IdleBase', 1, 220), ('IdleTop', 1, 300)],
				"jump_start": [('JumpStart', 1, 5)],
				"jump_loop": [('JumpLoop', 1, 30)],
				"dead": [('Dance', 1, 71)],
				}

	def __init__(self, obj):
		types.KX_GameObject.__init__(obj)

		self._speed_h = Vector.Fill(2)
		self._flags = set()

		self._library = ""

		self.hp = self.MAX_HP

		l = [i for i in self.childrenRecursive if isinstance(i, types.BL_ArmatureObject)]
		if l:
			self._armature = l[0]
		else:
			self._armature = None

		# Don't play animations if this is true
		self.animation_lock = False

		# Enable double jump
		self._phy_char = constraints.getCharacter(self)
		self._phy_char.maxJumps = 2

		self.gravity = self.GRAVITY

		self.running = False

		self = self

	@property
	def is_dead(self):
		return "DEAD" in self._flags

	@property
	def gravity(self):
		return self._phy_char.gravity

	@gravity.setter
	def gravity(self, value):
		self._phy_char.gravity = value

	@property
	def airborne(self):
		return not self._phy_char.onGround

	@property
	def armature(self):
		return self._armature if self._armature else self

	@classmethod
	def spawn(cls, position=None, orientation=None):
		name = cls.MESH

		library = "//../characters/" + name + ".blend"
		logic.LibLoad(library, "Scene", load_actions=True)

		scene = logic.getCurrentScene()
		adder = logic.getCurrentController().owner
		if position:
			adder.worldPosition = position
		if orientation:
			adder.worldOrientation = orientation

		obj = scene.addObject(name, adder)

		char = cls(obj)
		char._library = library
		return char

	def free(self):
		if self._library:
			logic.LibFree(self._library)

	def rotate(self, rotation):
		if not "DEAD" in self._flags:
			self.applyRotation(Vector((0, 0, rotation)))

	def animate(self, animation, speed=1.0):
		if self.animation_lock:
			return

		if animation not in self.ANIMATIONS:
			print("WARNING: %s does not have animation %s" % (self.__class__.__name__, animation))
			return

		for layer, v in enumerate(self.ANIMATIONS[animation]):
			anim, start, end = v
			if anim == "*":
				continue
			ob = self.armature

			ob.playAction(anim, start, end, play_mode=logic.KX_ACTION_MODE_LOOP, layer=layer, blendin=3, layer_weight=0.5, speed=speed)

	def stop_animation(self, layer):
		if self._armature:
			self._armature.stopAction(layer)
		else:
			self.stopAction(layer)

	def update(self):
		if self.is_dead:
			self._apply_movement(Vector((0, 0, 0)))
			self.animate('dead')
			return

		if self.hp <= 0:
			self._flags.add("DEAD")
			return

		if self.airborne:
			self.animate('jump_loop')
			self.stop_animation(1)
		elif self._speed_h.length_squared < 0.0001:
			self.animate('idle')
		else:
			self.animate('move', self.RUN_MULTIPLIER if self.running else 1.0)

	def move(self, direction):
		'''Moves the player horizontally'''

		if self.is_dead:
			return

		# Determine the direction the player is moving
		momentum = self._speed_h.copy()
		momentum.normalize()

		if direction.length_squared != 0:

			# Normalize direction
			direction.normalize()

			# Accelerate player if not going fast enough
			if momentum.length_squared == 0 or momentum.angle(direction) < math.pi:
				self._speed_h += self.ACCELERATION * direction
			else:
				self._speed_h += self.DECELERATION * direction

			if not self.airborne:
				max_speed = self.MAX_SPEED * (self.RUN_MULTIPLIER if self.running else 1.0)
			else:
				max_speed = self.MAX_AIR_SPEED

			# print(player['speed_h'])
			if self._speed_h.length_squared > max_speed ** 2:
				self._speed_h = max_speed * direction
		else:  # Friction
			friction = min(self._speed_h.length, self.FRICTION) * momentum
			self._speed_h = self._speed_h - friction

		movement = self._speed_h.to_3d()
		self._apply_movement(movement)

		# Face the player in the direction they are moving
		if movement.length_squared > 0.0001:
			x = movement.cross(Vector((0, 0, 1)))
			x.normalize()

			y = movement.normalized()

			z = x.cross(y)
			z.normalize()

			ori = (
					(x[0], y[0], z[0]),
					(x[1], y[1], z[1]),
					(x[2], y[2], z[2])
				)

			self.localOrientation = ori

	def jump(self, double=False):
		if self.is_dead:
			return

		if not self.airborne and self._phy_char.jumpCount == 0:
			self.animate('jump_start')

		self._phy_char.jump()

	def _apply_movement(self, vec):
		'''Applies a movement vector to the in game player object'''

		# Scale the speed so FPS changes don't mess with movement
		fps = logic.getAverageFrameRate()
		fps_scale = 60 / fps if fps != 0 else 60

		self._phy_char.walkDirection = vec * fps_scale

class Meatsack(Character):
	MESH = "Cosbad"

	MELEE_ATTACK = [
			('SliceVertical', 1, 16),
		]

	def __init__(self, gameobj):
		super().__init__(gameobj)

		attack_sensors = [AttackSensor(i, self) for i in self.childrenRecursive if i.name.startswith('AttackSensor')]
		self.attack_manager = MeleeAttackManager(self, attack_sensors, self.MELEE_ATTACK, 5)

	def update(self):
		self.attack_manager.update()
		super().update()

	def attack(self):
		self.attack_manager.attack()

class UllurCharacter(Character):
	LEFT_MELEE_ATTACKS = [
			('Attack1', 1, 4),
			('Attack2', 1, 4),
		]

	RIGHT_MELEE_ATTACKS = [
			('SliceHorizontal', 1, 16),
		]

	def __init__(self, gameobj):
		super().__init__(gameobj)
		attack_sensors = [AttackSensor(i, self) for i in self.childrenRecursive if i.name.startswith('AttackSensor')]
		self.left_attack_manager = MeleeAttackManager(self, attack_sensors, self.LEFT_MELEE_ATTACKS, 5)
		#self.right_attack_manager = MeleeAttackManager(self, attack_sensors, self.RIGHT_MELEE_ATTACKS)
		self.right_attack_manager = MouseRangeAttackManager(self, "Projectile", 1, 100, 10, 0.5)
		self.collectables = []

	def update(self):
		self.left_attack_manager.update()
		self.right_attack_manager.update()
		super().update()

	def attack(self, mode):

		if mode == "LEFT":
			self.stop_animation(1)
			self.left_attack_manager.attack()
		else:
			self.right_attack_manager.attack()

	def add_collectable(self, collectable):
		if collectable not in self.collectables:
			self.collectables.append(collectable)
<|endoftext|>


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

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

import json
import time

from mock import patch

from nailgun.settings import settings

import nailgun
import nailgun.rpc as rpc
from nailgun.errors import errors
from nailgun.task.manager import DeploymentTaskManager
from nailgun.task.fake import FAKE_THREADS
from nailgun.test.base import BaseHandlers
from nailgun.test.base import reverse
from nailgun.test.base import fake_tasks
from nailgun.api.models import Cluster, Task, ClusterChanges

class TestClusterChanges(BaseHandlers):

    def tearDown(self):
        self._wait_for_threads()
        super(TestClusterChanges, self).tearDown()

    def test_cluster_creation_adds_pending_changes(self):
        cluster = self.env.create_cluster(api=True)
        attributes_changes = self.db.query(ClusterChanges).filter_by(
            name="attributes"
        ).all()
        self.assertEquals(len(attributes_changes), 1)
        networks_changes = self.db.query(ClusterChanges).filter_by(
            name="networks"
        ).all()
        self.assertEquals(len(networks_changes), 1)
        all_changes = self.db.query(ClusterChanges).all()
        self.assertEquals(len(all_changes), 2)

    def test_node_volumes_modification_adds_pending_changes(self):
        cluster = self.env.create_cluster(api=True)
        node = self.env.create_node(
            api=True,
            cluster_id=cluster["id"]
        )
        node_db = self.env.nodes[0]
        node_disks_changes = self.db.query(ClusterChanges).filter_by(
            name="disks",
            node_id=node_db.id
        ).all()
        self.assertEquals(len(node_disks_changes), 1)
        resp = self.app.get(
            reverse(
                'ClusterHandler',
                kwargs={'cluster_id': cluster['id']}),
            headers=self.default_headers
        )
        response = json.loads(resp.body)
        self.assertIn(
            ["disks", node_db.id, node_db.name],
            response["changes"]
        )

    def test_node_volumes_clears_after_deletion_from_cluster(self):
        cluster = self.env.create_cluster(api=True)
        node = self.env.create_node(
            api=True,
            cluster_id=cluster["id"]
        )
        node_db = self.env.nodes[0]
        node_disks_changes = self.db.query(ClusterChanges).filter_by(
            name="disks",
            node_id=node_db.id
        ).all()
        self.assertEquals(len(node_disks_changes), 1)
        self.app.put(
            reverse('NodeCollectionHandler'),
            json.dumps([{"id": node_db.id, "cluster_id": None}]),
            headers=self.default_headers
        )
        self.env.refresh_clusters()
        node_disks_changes = self.db.query(ClusterChanges).filter_by(
            name="disks",
            node_id=node_db.id
        ).all()
        self.assertEquals(len(node_disks_changes), 0)

    def test_attributes_changing_adds_pending_changes(self):
        cluster = self.env.create_cluster(api=True)
        cluster_db = self.env.clusters[0]
        cluster_db.clear_pending_changes()
        all_changes = self.db.query(ClusterChanges).all()
        self.assertEquals(len(all_changes), 0)
        resp = self.app.put(
            reverse(
                'ClusterAttributesHandler',
                kwargs={'cluster_id': cluster['id']}),
            json.dumps({
                'editable': {
                    "foo": "bar"
                }
            }),
            headers=self.default_headers
        )
        pending_changes = self.db.query(ClusterChanges).filter_by(
            name="attributes"
        ).all()
        self.assertEquals(len(pending_changes), 1)

    def test_default_attributes_adds_pending_changes(self):
        cluster = self.env.create_cluster(api=True)
        cluster_db = self.env.clusters[0]
        cluster_db.clear_pending_changes()
        all_changes = self.db.query(ClusterChanges).all()
        self.assertEquals(len(all_changes), 0)
        resp = self.app.put(
            reverse(
                'ClusterAttributesDefaultsHandler',
                kwargs={'cluster_id': cluster['id']}),
            headers=self.default_headers
        )
        pending_changes = self.db.query(ClusterChanges).filter_by(
            name="attributes"
        ).all()
        self.assertEquals(len(pending_changes), 1)

    def test_network_changing_adds_pending_changes(self):
        cluster = self.env.create_cluster(api=True)
        cluster_db = self.env.clusters[0]
        cluster_db.clear_pending_changes()
        all_changes = self.db.query(ClusterChanges).all()
        self.assertEquals(len(all_changes), 0)
        resp = self.app.get(
            reverse(
                'NetworkConfigurationHandler',
                kwargs={'cluster_id': cluster['id']}),
            headers=self.default_headers
        )
        net_id = json.loads(resp.body)['networks'][0]["id"]
        resp = self.app.put(
            reverse(
                'NetworkConfigurationHandler',
                kwargs={'cluster_id': cluster['id']}),
            json.dumps({'networks': [{
                "id": net_id, "access": "restricted"}
            ]}),
            headers=self.default_headers
        )
        pending_changes = self.db.query(ClusterChanges).filter_by(
            name="networks"
        ).all()
        self.assertEquals(len(pending_changes), 1)

    @fake_tasks()
    def test_successful_deployment_drops_all_changes(self):
        cluster = self.env.create_cluster(api=True)
        node = self.env.create_node(cluster_id=cluster["id"])
        supertask = self.env.launch_deployment()
        self.env.wait_ready(supertask, 60)
        cluster_db = self.db.query(Cluster).get(cluster["id"])
        self.assertEquals(list(cluster_db.changes), [])

    @fake_tasks()
    def test_failed_deployment_does_nothing_with_changes(self):
        cluster = self.env.create_cluster(api=True)
        node = self.env.create_node(
            cluster_id=cluster["id"],
            status="error",
            error_type="provision"
        )
        supertask = self.env.launch_deployment()
        self.env.wait_error(supertask, 60)
        attributes_changes = self.db.query(ClusterChanges).filter_by(
            name="attributes"
        ).all()
        self.assertEquals(len(attributes_changes), 1)
        networks_changes = self.db.query(ClusterChanges).filter_by(
            name="networks"
        ).all()
        self.assertEquals(len(networks_changes), 1)
        all_changes = self.db.query(ClusterChanges).all()
        self.assertEquals(len(all_changes), 2)
<commit_msg>Fix: python unit test for cluster changes prop<commit_after># -*- coding: utf-8 -*-

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

import json
import time

from mock import patch

from nailgun.settings import settings

import nailgun
import nailgun.rpc as rpc
from nailgun.errors import errors
from nailgun.task.manager import DeploymentTaskManager
from nailgun.task.fake import FAKE_THREADS
from nailgun.test.base import BaseHandlers
from nailgun.test.base import reverse
from nailgun.test.base import fake_tasks
from nailgun.api.models import Cluster, Task, ClusterChanges

class TestClusterChanges(BaseHandlers):

    def tearDown(self):
        self._wait_for_threads()
        super(TestClusterChanges, self).tearDown()

    def test_cluster_creation_adds_pending_changes(self):
        cluster = self.env.create_cluster(api=True)
        attributes_changes = self.db.query(ClusterChanges).filter_by(
            name="attributes"
        ).all()
        self.assertEquals(len(attributes_changes), 1)
        networks_changes = self.db.query(ClusterChanges).filter_by(
            name="networks"
        ).all()
        self.assertEquals(len(networks_changes), 1)
        all_changes = self.db.query(ClusterChanges).all()
        self.assertEquals(len(all_changes), 2)

    def test_node_volumes_modification_adds_pending_changes(self):
        cluster = self.env.create_cluster(api=True)
        node = self.env.create_node(
            api=True,
            cluster_id=cluster["id"]
        )
        node_db = self.env.nodes[0]
        node_disks_changes = self.db.query(ClusterChanges).filter_by(
            name="disks",
            node_id=node_db.id
        ).all()
        self.assertEquals(len(node_disks_changes), 1)
        resp = self.app.get(
            reverse(
                'ClusterHandler',
                kwargs={'cluster_id': cluster['id']}),
            headers=self.default_headers
        )
        response = json.loads(resp.body)
        self.assertIn(
            ["disks", node_db.id],
            response["changes"]
        )

    def test_node_volumes_clears_after_deletion_from_cluster(self):
        cluster = self.env.create_cluster(api=True)
        node = self.env.create_node(
            api=True,
            cluster_id=cluster["id"]
        )
        node_db = self.env.nodes[0]
        node_disks_changes = self.db.query(ClusterChanges).filter_by(
            name="disks",
            node_id=node_db.id
        ).all()
        self.assertEquals(len(node_disks_changes), 1)
        self.app.put(
            reverse('NodeCollectionHandler'),
            json.dumps([{"id": node_db.id, "cluster_id": None}]),
            headers=self.default_headers
        )
        self.env.refresh_clusters()
        node_disks_changes = self.db.query(ClusterChanges).filter_by(
            name="disks",
            node_id=node_db.id
        ).all()
        self.assertEquals(len(node_disks_changes), 0)

    def test_attributes_changing_adds_pending_changes(self):
        cluster = self.env.create_cluster(api=True)
        cluster_db = self.env.clusters[0]
        cluster_db.clear_pending_changes()
        all_changes = self.db.query(ClusterChanges).all()
        self.assertEquals(len(all_changes), 0)
        resp = self.app.put(
            reverse(
                'ClusterAttributesHandler',
                kwargs={'cluster_id': cluster['id']}),
            json.dumps({
                'editable': {
                    "foo": "bar"
                }
            }),
            headers=self.default_headers
        )
        pending_changes = self.db.query(ClusterChanges).filter_by(
            name="attributes"
        ).all()
        self.assertEquals(len(pending_changes), 1)

    def test_default_attributes_adds_pending_changes(self):
        cluster = self.env.create_cluster(api=True)
        cluster_db = self.env.clusters[0]
        cluster_db.clear_pending_changes()
        all_changes = self.db.query(ClusterChanges).all()
        self.assertEquals(len(all_changes), 0)
        resp = self.app.put(
            reverse(
                'ClusterAttributesDefaultsHandler',
                kwargs={'cluster_id': cluster['id']}),
            headers=self.default_headers
        )
        pending_changes = self.db.query(ClusterChanges).filter_by(
            name="attributes"
        ).all()
        self.assertEquals(len(pending_changes), 1)

    def test_network_changing_adds_pending_changes(self):
        cluster = self.env.create_cluster(api=True)
        cluster_db = self.env.clusters[0]
        cluster_db.clear_pending_changes()
        all_changes = self.db.query(ClusterChanges).all()
        self.assertEquals(len(all_changes), 0)
        resp = self.app.get(
            reverse(
                'NetworkConfigurationHandler',
                kwargs={'cluster_id': cluster['id']}),
            headers=self.default_headers
        )
        net_id = json.loads(resp.body)['networks'][0]["id"]
        resp = self.app.put(
            reverse(
                'NetworkConfigurationHandler',
                kwargs={'cluster_id': cluster['id']}),
            json.dumps({'networks': [{
                "id": net_id, "access": "restricted"}
            ]}),
            headers=self.default_headers
        )
        pending_changes = self.db.query(ClusterChanges).filter_by(
            name="networks"
        ).all()
        self.assertEquals(len(pending_changes), 1)

    @fake_tasks()
    def test_successful_deployment_drops_all_changes(self):
        cluster = self.env.create_cluster(api=True)
        node = self.env.create_node(cluster_id=cluster["id"])
        supertask = self.env.launch_deployment()
        self.env.wait_ready(supertask, 60)
        cluster_db = self.db.query(Cluster).get(cluster["id"])
        self.assertEquals(list(cluster_db.changes), [])

    @fake_tasks()
    def test_failed_deployment_does_nothing_with_changes(self):
        cluster = self.env.create_cluster(api=True)
        node = self.env.create_node(
            cluster_id=cluster["id"],
            status="error",
            error_type="provision"
        )
        supertask = self.env.launch_deployment()
        self.env.wait_error(supertask, 60)
        attributes_changes = self.db.query(ClusterChanges).filter_by(
            name="attributes"
        ).all()
        self.assertEquals(len(attributes_changes), 1)
        networks_changes = self.db.query(ClusterChanges).filter_by(
            name="networks"
        ).all()
        self.assertEquals(len(networks_changes), 1)
        all_changes = self.db.query(ClusterChanges).all()
        self.assertEquals(len(all_changes), 2)
<|endoftext|>


<commit_before>import os
import unittest
from random import randint

import swish

class SwishClientTestCase(unittest.TestCase):
    def setUp(self):
        current_folder = os.path.dirname(os.path.abspath(__file__))
        cert_file_path = os.path.join(current_folder, "cert.pem")
        key_file_path = os.path.join(current_folder, "key.pem")
        cert = (cert_file_path, key_file_path)
        verify = os.path.join(current_folder, "swish.pem")
        self.client = swish.SwishClient(
            environment=swish.Environment.Test,
            merchant_swish_number='1231181189',
            cert=cert,
            verify=verify
        )

    def test_client(self):
        self.assertEqual(self.client.environment.base_url, swish.Environment.Test.base_url)
        self.assertEqual(self.client.merchant_swish_number, '1231181189')

    def test_create_payment_ecommerce(self):
        payer_alias = '467%i' % randint(1000000, 9999999)
        payment = self.client.create_payment(
            payee_payment_reference='0123456789',
            callback_url='https://example.com/api/swishcb/paymentrequests',
            payer_alias=payer_alias,
            amount=100,
            currency='SEK',
            message='Kingston USB Flash Drive 8 GB'
        )
        self.assertIsNotNone(payment.id)

    def test_create_payment_mcommerce(self):
        payment = self.client.create_payment(
            payee_payment_reference='0123456789',
            callback_url='https://example.com/api/swishcb/paymentrequests',
            amount=100,
            currency='SEK',
            message='Kingston USB Flash Drive 8 GB'
        )
        self.assertIsNotNone(payment.id)
        self.assertIsNotNone(payment.request_token)

    def test_create_payment_error(self):
        with self.assertRaises(swish.SwishError):
            self.client.create_payment(
                payee_payment_reference='0123456789',
                callback_url='https://example.com/api/swishcb/paymentrequests',
                amount=100,
                currency='SEK',
                message='BE18'
            )

    def test_get_payment(self):
        payment_request = self.client.create_payment(
            payee_payment_reference='0123456789',
            callback_url='https://example.com/api/swishcb/paymentrequests',
            amount=100,
            currency='SEK',
            message='Kingston USB Flash Drive 8 GB'
        )
        payment = self.client.get_payment(payment_request.id)
        self.assertEqual(payment.payee_payment_reference, '0123456789')
        self.assertEqual(payment.callback_url, 'https://example.com/api/swishcb/paymentrequests')
        self.assertEqual(payment.amount, 100)
        self.assertEqual(payment.currency, 'SEK')
        self.assertEqual(payment.message, 'Kingston USB Flash Drive 8 GB')

    def test_create_refund(self):
        self.fail("Not implemented")

    def test_get_refund(self):
        self.fail("Not implemented")
<commit_msg>Added tests for Refunds<commit_after>import os
import unittest
from random import randint
import time

import swish

class SwishClientTestCase(unittest.TestCase):
    def setUp(self):
        current_folder = os.path.dirname(os.path.abspath(__file__))
        cert_file_path = os.path.join(current_folder, "cert.pem")
        key_file_path = os.path.join(current_folder, "key.pem")
        cert = (cert_file_path, key_file_path)
        verify = os.path.join(current_folder, "swish.pem")
        self.client = swish.SwishClient(
            environment=swish.Environment.Test,
            merchant_swish_number='1231181189',
            cert=cert,
            verify=verify
        )

    def test_client(self):
        self.assertEqual(self.client.environment.base_url, swish.Environment.Test.base_url)
        self.assertEqual(self.client.merchant_swish_number, '1231181189')

    def test_create_payment_ecommerce(self):
        payer_alias = '467%i' % randint(1000000, 9999999)
        payment = self.client.create_payment(
            payee_payment_reference='0123456789',
            callback_url='https://example.com/api/swishcb/paymentrequests',
            payer_alias=payer_alias,
            amount=100,
            currency='SEK',
            message='Kingston USB Flash Drive 8 GB'
        )
        self.assertIsNotNone(payment.id)
        self.assertIsNotNone(payment.location)

    def test_create_payment_mcommerce(self):
        payment = self.client.create_payment(
            payee_payment_reference='0123456789',
            callback_url='https://example.com/api/swishcb/paymentrequests',
            amount=100,
            currency='SEK',
            message='Kingston USB Flash Drive 8 GB'
        )
        self.assertIsNotNone(payment.id)
        self.assertIsNotNone(payment.location)
        self.assertIsNotNone(payment.request_token)

    def test_create_payment_error(self):
        with self.assertRaises(swish.SwishError):
            self.client.create_payment(
                payee_payment_reference='0123456789',
                callback_url='https://example.com/api/swishcb/paymentrequests',
                amount=100,
                currency='SEK',
                message='BE18'
            )

    def test_get_payment(self):
        payment_request = self.client.create_payment(
            payee_payment_reference='0123456789',
            callback_url='https://example.com/api/swishcb/paymentrequests',
            amount=100,
            currency='SEK',
            message='Kingston USB Flash Drive 8 GB'
        )
        payment = self.client.get_payment(payment_request.id)
        self.assertEqual(payment.payee_payment_reference, '0123456789')
        self.assertEqual(payment.callback_url, 'https://example.com/api/swishcb/paymentrequests')
        self.assertEqual(payment.amount, 100)
        self.assertEqual(payment.currency, 'SEK')
        self.assertEqual(payment.message, 'Kingston USB Flash Drive 8 GB')

    def test_create_refund(self):
        payment = self.client.create_payment(
            payee_payment_reference='0123456789',
            callback_url='https://example.com/api/swishcb/paymentrequests',
            amount=100,
            currency='SEK',
            message='Kingston USB Flash Drive 8 GB'
        )
        time.sleep(5)
        refund = self.client.create_refund(
            original_payment_reference=payment.id,
            amount=100,
            currency='SEK',
            callback_url='https://example.com/api/swishcb/refunds',
            payer_payment_reference='0123456789',
            message='Refund for Kingston USB Flash Drive 8 GB'
        )
        self.assertIsNotNone(refund.id)
        self.assertIsNotNone(refund.location)

    def test_get_refund(self):
        payment = self.client.create_payment(
            payee_payment_reference='0123456789',
            callback_url='https://example.com/api/swishcb/paymentrequests',
            amount=100,
            currency='SEK',
            message='Kingston USB Flash Drive 8 GB'
        )
        time.sleep(5)
        refund_request = self.client.create_refund(
            original_payment_reference=payment.id,
            amount=100,
            currency='SEK',
            callback_url='https://example.com/api/swishcb/refunds',
            payer_payment_reference='0123456789',
            message='Refund for Kingston USB Flash Drive 8 GB'
        )
        refund = self.client.get_refund(refund_request.id)
        self.assertEqual(refund.original_payment_reference, payment.id)
        self.assertEqual(refund.callback_url, 'https://example.com/api/swishcb/refunds')
        self.assertEqual(refund.amount, 100)
        self.assertEqual(refund.currency, 'SEK')
        self.assertEqual(refund.message, 'Refund for Kingston USB Flash Drive 8 GB')
<|endoftext|>