コード例 #1
0
ファイル: deploy.py プロジェクト: ecalifornica/harold
    def get_deploy_hours(self, irc, sender, channel):
        "Get the deploy hours for this salon."
        salon = yield self.salons.by_channel(channel)
        if not salon:
            irc.send_message(channel, "This channel isn't a salon.")
            return

        start = fmt_time(salon.deploy_hours_start)
        end = fmt_time(salon.deploy_hours_end)
        tz = salon.tz
        irc.send_message(
            salon.channel, """deploy hours are %s-%s, %s (%s)""" %
            (start, end, tz, utc_offset(tz)))
コード例 #2
0
ファイル: deploy.py プロジェクト: ecalifornica/harold
    def set_deploy_hours(self, irc, sender, channel, *args):
        def usage():
            irc.send_message(
                channel,
                "*USAGE:* set_deploy_hours 0900 1700 America/Los_Angeles")
            irc.send_message(
                channel,
                "List of timezone names: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List"
            )

        if not args or len(args) != 3:
            usage()
        start, end, tz_name = args

        salon = yield self.salons.by_channel(channel)
        if not salon:
            irc.send_message(channel, "This channel isn't a salon.")
            return

        try:
            tz = pytz.timezone(tz_name)
        except pytz.UnknownTimeZoneError:
            irc.send_message(channel,
                             "*Error:* Unknown timezone {}".format(tz_name))
            usage()
            return

        try:
            start_time = parse_time(start)
            end_time = parse_time(end)
            if start_time > end_time:
                raise ValueError
        except ValueError:
            irc.send_message(
                channel,
                "*Error:* Invalid time range {} {}".format(start, end))
            usage()
            return

        date = datetime.datetime.now(tz=tz).date()
        deploy_start = tz.localize(datetime.datetime.combine(date, start_time))
        deploy_end = tz.localize(datetime.datetime.combine(date, end_time))
        deploy_timerange = (deploy_start, deploy_end)

        blackout_tz = self.config.default_tz
        blackout_date = datetime.datetime.now(tz=blackout_tz).date()
        blackout_start = blackout_tz.localize(
            datetime.datetime.combine(blackout_date,
                                      self.config.blackout_hours_start))
        blackout_end = blackout_tz.localize(
            datetime.datetime.combine(blackout_date,
                                      self.config.blackout_hours_end))

        # User-defined deploy hours can only exist in a single day in their
        # given timezone, but may cross day boundaries in the blackout
        # timezone. This necessitates us checking for overlap for both today
        # and yesterday.
        # Example: Deploy hours of 0100-0200 EST, blackout hours of 2200-2300 PST.
        for i in (0, 1):
            delta = datetime.timedelta(days=i)
            blackout_timerange = (blackout_start - delta, blackout_end - delta)

            if timerange_overlap(deploy_timerange, blackout_timerange):
                irc.send_message(
                    channel,
                    "ERROR: Requested deploy hours overlap with blackout window."
                )
                irc.send_message(
                    channel, "Blackout hours are {} to {}, {} ({})".format(
                        fmt_time(blackout_start.astimezone(tz)),
                        fmt_time(blackout_end.astimezone(tz)),
                        tz,
                        utc_offset(tz),
                    ))
                return

        yield salon.set_deploy_hours(irc, start_time, end_time, tz)
        irc.send_message(
            salon.channel, """deploy hours set to %s-%s, %s (%s)""" %
            (start, end, tz_name, utc_offset(salon.tz)))