<commit_before>standard_ban = "Only scrubs play Standard."

modern_ban = "**Modern** includes cards from **Eighth Edition** to today.\n" + "The following cards are banned in Modern:\n" + "Ancestral Vision\n" + "Ancient Den\n" + "Birthing Pod\n" + "Blazing Shoal\n" + "Bloodbraid Elf\n" + "Chrome Mox\n" + "Cloudpost\n" + "Dark Depths\n" + "Deathrite Shaman\n" + "Dig Through Time\n" + "Dread Return\n" + "Glimpse of Nature\n" + "Great Furnace\n" + "Green Sun's Zenith\n" + "Hypergenesis\n" + "Jace, the Mind Sculptor\n" + "Mental Misstep\n" + "Ponder\n" + "Preordain\n" + "Punishing Fire\n" + "Rite of Flame\n" + "Seat of the Synod\n" + "Second Sunrise\n" + "Seething Song\n" + "Sensei's Divining Top\n" + "Stoneforge Mystic\n" + "Skullclamp\n" + "Sword of the Meek\n" + "Treasure Cruise\n" + "Tree of Tales\n" + "Umezawa's Jitte\n" + "Vault of Whispers\n"

legacy_ban = "All **ante**, **dexterity**, and **Conspiracy** cards are banned in Legacy.\n" + "The following cards are banned in Legacy:\n" + "Ancestral Recall\n" + "Balance\n" + "Bazaar of Baghdad\n" + "Black Lotus\n" + "Channel\n" + "Demonic Consultation\n" + "Demonic Tutor\n" + "Dig Through Time\n" + "Earthcraft\n" + "Fastbond\n" + "Flash\n" + "Frantic Search\n" + "Goblin Recruiter\n" + "Gush\n" + "Hermit Druid\n" + "Imperial Seal\n" + "Library of Alexandria\n" + "Mana Crypt\n" + "Mana Drain\n" + "Mana Vault\n" + "Memory Jar\n" + "Mental Misstep\n" + "Mind Twist\n" + "Mind's Desire\n" + "Mishra's Workshop\n" + "Mox Emerald\n" + "Mox Jet\n" + "Mox Pearl\n" + "Mox Ruby\n" + "Mox Sapphire\n" + "Mystical Tutor\n" + "Necropotence\n" + "Oath of Druids\n" + "Shahrazad\n" + "Skullclamp\n" + "Sol Ring\n" + "Strip Mine\n" + "Survival of the Fittest\n" + "Time Vault\n" + "Time Walk\n" + "Timetwister\n" + "Tinker\n" + "Tolarian Academy\n" + "Treasure Cruise\n" + "Vampiric Tutor\n" + "Wheel of Fortune\n" + "Windfall\n" + "Yawgmoth's Bargain\n" + "Yawgmoth's Will\n"

vintage_ban = "All **ante**, **dexterity**, and **Conspiracy** cards are banned in Vintage.\n" + "The following cards are restricted in Vintage:\n" + "Ancestral Recall\n" + "Balance\n" + "Black Lotus\n" + "Brainstorm\n" + "Chalice of the Void\n" + "Channel\n" + "Demonic Consultation\n" + "Demonic Tutor\n" + "Dig Through Time\n" + "Fastbond\n" +  "Flash\n" + "Imperial Seal\n" +  "Library of Alexandria\n" + "Lion's Eye Diamond\n" + "Lotus Petal\n" + "Mana Crypt\n" + "Mana Vault\n" + "Memory Jar\n" + "Merchant Scroll\n" + "Mind's Desire\n" + "Mox Emerald\n" + "Mox Jet\n" + "Mox Pearl\n" + "Mox Ruby\n" + "Mox Sapphire\n" + "Mystical Tutor\n" + "Necropotence\n" + "Ponder\n" + "Sol Ring\n" + "Strip Mine\n" + "Time Vault\n" + "Time Walk\n" + "Timetwister\n" + "Tinker\n" + "Tolarian Academy\n" + "Treasure Cruise\n" + "Trinisphere\n" + "Vampiric Tutor\n" + "Wheel of Fortune\n" + "Windfall\n" + "Yawgmoth's Bargain\n" + "Yawgmoth's Will\n"

edh_ban = "All **ante**, **dexterity**, and **Conspiracy** cards are banned in Commander.\n" + "The following cards are banned in Commander:\n" + "Ancestral Recall\n" + "Balance\n" + "Biorhythm\n" + "Black Lotus\n" + "Braids, Cabal Minion\n" + "Coalition Victory\n" + "Channel\n" + "Emrakul, the Aeons Torn\n" + "Erayo, Soratami Ascendant\n" + "Fastbond\n" + "Gifts Ungiven\n" + "Griselbrand\n" + "Karakas\n" + "Library of Alexandria\n" + "Limited Resources\n" + "Mox Emerald\n" + "Mox Jet\n" + "Mox Pearl\n" + "Mox Ruby\n" + "Mox Sapphire\n" + "Painter's Servant\n" + "Panoptic Mirror\n" + "Primeval Titan\n" + "Protean Hulk\n" + "Recurring Nightmare\n" + "Rofellos, Llanowar Emissary\n" + "Shahrazad\n" + "Sundering Titan\n" + "Sway of the Stars\n" + "Sylvan Primordial\n" + "Time Vault\n" + "Time Walk\n" + "Tinker\n" + "Tolarian Academy\n" + "Trade Secrets\n" + "Upheaval\n" + "Worldfire\n" + "Yawgmoth's Bargain\n"
<commit_msg>Update banlists.py<commit_after>standard_ban = "Only scrubs play Standard."

modern_ban = "**Modern** includes cards from **Eighth Edition** to today.\n" + "The following cards are banned in Modern:\n" + "Ancient Den\n" + "Birthing Pod\n" + "Blazing Shoal\n" + "Bloodbraid Elf\n" + "Chrome Mox\n" + "Cloudpost\n" + "Dark Depths\n" + "Deathrite Shaman\n" + "Dig Through Time\n" + "Dread Return\n" + "Eye of Ugin\n" + "Glimpse of Nature\n" + "Great Furnace\n" + "Green Sun's Zenith\n" + "Hypergenesis\n" + "Jace, the Mind Sculptor\n" + "Mental Misstep\n" + "Ponder\n" + "Preordain\n" + "Punishing Fire\n" + "Rite of Flame\n" + "Seat of the Synod\n" + "Second Sunrise\n" + "Seething Song\n" + "Sensei's Divining Top\n" +"Skullclamp\n" + "Splinter Twin\n" + "Stoneforge Mystic\n" + "Summer Bloom\n" + "Treasure Cruise\n" + "Tree of Tales\n" + "Umezawa's Jitte\n" + "Vault of Whispers\n"

legacy_ban = "All **ante**, **dexterity**, and **Conspiracy** cards are banned in Legacy.\n" + "The following cards are banned in Legacy:\n" + "Ancestral Recall\n" + "Balance\n" + "Bazaar of Baghdad\n" + "Black Lotus\n" + "Channel\n" + "Demonic Consultation\n" + "Demonic Tutor\n" + "Dig Through Time\n" + "Earthcraft\n" + "Fastbond\n" + "Flash\n" + "Frantic Search\n" + "Goblin Recruiter\n" + "Gush\n" + "Hermit Druid\n" + "Imperial Seal\n" + "Library of Alexandria\n" + "Mana Crypt\n" + "Mana Drain\n" + "Mana Vault\n" + "Memory Jar\n" + "Mental Misstep\n" + "Mind Twist\n" + "Mind's Desire\n" + "Mishra's Workshop\n" + "Mox Emerald\n" + "Mox Jet\n" + "Mox Pearl\n" + "Mox Ruby\n" + "Mox Sapphire\n" + "Mystical Tutor\n" + "Necropotence\n" + "Oath of Druids\n" + "Shahrazad\n" + "Skullclamp\n" + "Sol Ring\n" + "Strip Mine\n" + "Survival of the Fittest\n" + "Time Vault\n" + "Time Walk\n" + "Timetwister\n" + "Tinker\n" + "Tolarian Academy\n" + "Treasure Cruise\n" + "Vampiric Tutor\n" + "Wheel of Fortune\n" + "Windfall\n" + "Yawgmoth's Bargain\n" + "Yawgmoth's Will\n"

vintage_ban = "All **ante**, **dexterity**, and **Conspiracy** cards are banned in Vintage.\n" + "The following cards are restricted in Vintage:\n" + "Ancestral Recall\n" + "Balance\n" + "Black Lotus\n" + "Brainstorm\n" + "Chalice of the Void\n" + "Channel\n" + "Demonic Consultation\n" + "Demonic Tutor\n" + "Dig Through Time\n" + "Fastbond\n" +  "Flash\n" + "Imperial Seal\n" +  "Library of Alexandria\n" + "Lion's Eye Diamond\n" + "Lodestone Golem\n" + "Lotus Petal\n" + "Mana Crypt\n" + "Mana Vault\n" + "Memory Jar\n" + "Merchant Scroll\n" + "Mind's Desire\n" + "Mox Emerald\n" + "Mox Jet\n" + "Mox Pearl\n" + "Mox Ruby\n" + "Mox Sapphire\n" + "Mystical Tutor\n" + "Necropotence\n" + "Ponder\n" + "Sol Ring\n" + "Strip Mine\n" + "Time Vault\n" + "Time Walk\n" + "Timetwister\n" + "Tinker\n" + "Tolarian Academy\n" + "Treasure Cruise\n" + "Trinisphere\n" + "Vampiric Tutor\n" + "Wheel of Fortune\n" + "Windfall\n" + "Yawgmoth's Bargain\n" + "Yawgmoth's Will\n"

edh_ban = "All **ante**, **dexterity**, and **Conspiracy** cards are banned in Commander.\n" + "The following cards are banned in Commander:\n" + "Ancestral Recall\n" + "Balance\n" + "Biorhythm\n" + "Black Lotus\n" + "Braids, Cabal Minion\n" + "Coalition Victory\n" + "Channel\n" + "Emrakul, the Aeons Torn\n" + "Erayo, Soratami Ascendant\n" + "Fastbond\n" + "Gifts Ungiven\n" + "Griselbrand\n" + "Karakas\n" + "Library of Alexandria\n" + "Limited Resources\n" + "Mox Emerald\n" + "Mox Jet\n" + "Mox Pearl\n" + "Mox Ruby\n" + "Mox Sapphire\n" + "Painter's Servant\n" + "Panoptic Mirror\n" + "Primeval Titan\n" + "Protean Hulk\n" + "Recurring Nightmare\n" + "Rofellos, Llanowar Emissary\n" + "Shahrazad\n" + "Sundering Titan\n" + "Sway of the Stars\n" + "Sylvan Primordial\n" + "Time Vault\n" + "Time Walk\n" + "Tinker\n" + "Tolarian Academy\n" + "Trade Secrets\n" + "Upheaval\n" + "Worldfire\n" + "Yawgmoth's Bargain\n"
<|endoftext|>


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

import argparse
import code
import sys
import threading
import time
import ssl
import gzip
import zlib

import six
from six.moves.urllib.parse import urlparse

import websocket

try:
    import readline
except ImportError:
    pass

def get_encoding():
    encoding = getattr(sys.stdin, "encoding", "")
    if not encoding:
        return "utf-8"
    else:
        return encoding.lower()

OPCODE_DATA = (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY)
ENCODING = get_encoding()

class VAction(argparse.Action):

    def __call__(self, parser, args, values, option_string=None):
        if values is None:
            values = "1"
        try:
            values = int(values)
        except ValueError:
            values = values.count("v") + 1
        setattr(args, self.dest, values)

def parse_args():
    parser = argparse.ArgumentParser(description="WebSocket Simple Dump Tool")
    parser.add_argument("url", metavar="ws_url",
                        help="websocket url. ex. ws://echo.websocket.org/")
    parser.add_argument("-p", "--proxy",
                        help="proxy url. ex. http://127.0.0.1:8080")
    parser.add_argument("-v", "--verbose", default=0, nargs='?', action=VAction,
                        dest="verbose",
                        help="set verbose mode. If set to 1, show opcode. "
                        "If set to 2, enable to trace  websocket module")
    parser.add_argument("-n", "--nocert", action='store_true',
                        help="Ignore invalid SSL cert")
    parser.add_argument("-r", "--raw", action="store_true",
                        help="raw output")
    parser.add_argument("-s", "--subprotocols", nargs='*',
                        help="Set subprotocols")
    parser.add_argument("-o", "--origin",
                        help="Set origin")
    parser.add_argument("--eof-wait", default=0, type=int,
                        help="wait time(second) after 'EOF' received.")
    parser.add_argument("-t", "--text",
                        help="Send initial text")
    parser.add_argument("--timings", action="store_true",
                        help="Print timings in seconds")
    parser.add_argument("--headers",
                        help="Set custom headers. Use ',' as separator")

    return parser.parse_args()

class RawInput:

    def raw_input(self, prompt):
        if six.PY3:
            line = input(prompt)
        else:
            line = raw_input(prompt)

        if ENCODING and ENCODING != "utf-8" and not isinstance(line, six.text_type):
            line = line.decode(ENCODING).encode("utf-8")
        elif isinstance(line, six.text_type):
            line = line.encode("utf-8")

        return line

class InteractiveConsole(RawInput, code.InteractiveConsole):

    def write(self, data):
        sys.stdout.write("\033[2K\033[E")
        # sys.stdout.write("\n")
        sys.stdout.write("\033[34m< " + data + "\033[39m")
        sys.stdout.write("\n> ")
        sys.stdout.flush()

    def read(self):
        return self.raw_input("> ")

class NonInteractive(RawInput):

    def write(self, data):
        sys.stdout.write(data)
        sys.stdout.write("\n")
        sys.stdout.flush()

    def read(self):
        return self.raw_input("")

def main():
    start_time = time.time()
    args = parse_args()
    if args.verbose > 1:
        websocket.enableTrace(True)
    options = {}
    if args.proxy:
        p = urlparse(args.proxy)
        options["http_proxy_host"] = p.hostname
        options["http_proxy_port"] = p.port
    if args.origin:
        options["origin"] = args.origin
    if args.subprotocols:
        options["subprotocols"] = args.subprotocols
    opts = {}
    if args.nocert:
        opts = {"cert_reqs": ssl.CERT_NONE, "check_hostname": False}
    if args.headers:
        options['header'] = map(str.strip, args.headers.split(','))
    ws = websocket.create_connection(args.url, sslopt=opts, **options)
    if args.raw:
        console = NonInteractive()
    else:
        console = InteractiveConsole()
        print("Press Ctrl+C to quit")

    def recv():
        try:
            frame = ws.recv_frame()
        except websocket.WebSocketException:
            return websocket.ABNF.OPCODE_CLOSE, None
        if not frame:
            raise websocket.WebSocketException("Not a valid frame %s" % frame)
        elif frame.opcode in OPCODE_DATA:
            return frame.opcode, frame.data
        elif frame.opcode == websocket.ABNF.OPCODE_CLOSE:
            ws.send_close()
            return frame.opcode, None
        elif frame.opcode == websocket.ABNF.OPCODE_PING:
            ws.pong(frame.data)
            return frame.opcode, frame.data

        return frame.opcode, frame.data

    def recv_ws():
        while True:
            opcode, data = recv()
            msg = None
            if six.PY3 and opcode == websocket.ABNF.OPCODE_TEXT and isinstance(data, bytes):
                data = str(data, "utf-8")
            if isinstance(data, bytes) and len(data)>2 and data[:2] == b'\037\213':  # gzip magick
                try:
                    data = "[gzip] " + str(gzip.decompress(data), "utf-8")
                except:
                    pass
            elif isinstance(data, bytes):
                try:
                    decomp = zlib.decompressobj(
                            -zlib.MAX_WBITS
                    )
                    data = decomp.decompress(data)
                    data = "[zlib] " + str(data + decomp.flush(), "utf-8")
                except:
                    pass

            if isinstance(data, bytes):
                data = repr(data)

            if args.verbose:
                msg = "%s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data)
            else:
                msg = data

            if msg is not None:
                if args.timings:
                    console.write(str(time.time() - start_time) + ": " + msg)
                else:
                    console.write(msg)

            if opcode == websocket.ABNF.OPCODE_CLOSE:
                break

    thread = threading.Thread(target=recv_ws)
    thread.daemon = True
    thread.start()

    if args.text:
        ws.send(args.text)

    while True:
        try:
            message = console.read()
            ws.send(message)
        except KeyboardInterrupt:
            return
        except EOFError:
            time.sleep(args.eof_wait)
            return

if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print(e)
<commit_msg>Simplify zlib.decompress call<commit_after>#!/usr/bin/env python

import argparse
import code
import sys
import threading
import time
import ssl
import gzip
import zlib

import six
from six.moves.urllib.parse import urlparse

import websocket

try:
    import readline
except ImportError:
    pass

def get_encoding():
    encoding = getattr(sys.stdin, "encoding", "")
    if not encoding:
        return "utf-8"
    else:
        return encoding.lower()

OPCODE_DATA = (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY)
ENCODING = get_encoding()

class VAction(argparse.Action):

    def __call__(self, parser, args, values, option_string=None):
        if values is None:
            values = "1"
        try:
            values = int(values)
        except ValueError:
            values = values.count("v") + 1
        setattr(args, self.dest, values)

def parse_args():
    parser = argparse.ArgumentParser(description="WebSocket Simple Dump Tool")
    parser.add_argument("url", metavar="ws_url",
                        help="websocket url. ex. ws://echo.websocket.org/")
    parser.add_argument("-p", "--proxy",
                        help="proxy url. ex. http://127.0.0.1:8080")
    parser.add_argument("-v", "--verbose", default=0, nargs='?', action=VAction,
                        dest="verbose",
                        help="set verbose mode. If set to 1, show opcode. "
                        "If set to 2, enable to trace  websocket module")
    parser.add_argument("-n", "--nocert", action='store_true',
                        help="Ignore invalid SSL cert")
    parser.add_argument("-r", "--raw", action="store_true",
                        help="raw output")
    parser.add_argument("-s", "--subprotocols", nargs='*',
                        help="Set subprotocols")
    parser.add_argument("-o", "--origin",
                        help="Set origin")
    parser.add_argument("--eof-wait", default=0, type=int,
                        help="wait time(second) after 'EOF' received.")
    parser.add_argument("-t", "--text",
                        help="Send initial text")
    parser.add_argument("--timings", action="store_true",
                        help="Print timings in seconds")
    parser.add_argument("--headers",
                        help="Set custom headers. Use ',' as separator")

    return parser.parse_args()

class RawInput:

    def raw_input(self, prompt):
        if six.PY3:
            line = input(prompt)
        else:
            line = raw_input(prompt)

        if ENCODING and ENCODING != "utf-8" and not isinstance(line, six.text_type):
            line = line.decode(ENCODING).encode("utf-8")
        elif isinstance(line, six.text_type):
            line = line.encode("utf-8")

        return line

class InteractiveConsole(RawInput, code.InteractiveConsole):

    def write(self, data):
        sys.stdout.write("\033[2K\033[E")
        # sys.stdout.write("\n")
        sys.stdout.write("\033[34m< " + data + "\033[39m")
        sys.stdout.write("\n> ")
        sys.stdout.flush()

    def read(self):
        return self.raw_input("> ")

class NonInteractive(RawInput):

    def write(self, data):
        sys.stdout.write(data)
        sys.stdout.write("\n")
        sys.stdout.flush()

    def read(self):
        return self.raw_input("")

def main():
    start_time = time.time()
    args = parse_args()
    if args.verbose > 1:
        websocket.enableTrace(True)
    options = {}
    if args.proxy:
        p = urlparse(args.proxy)
        options["http_proxy_host"] = p.hostname
        options["http_proxy_port"] = p.port
    if args.origin:
        options["origin"] = args.origin
    if args.subprotocols:
        options["subprotocols"] = args.subprotocols
    opts = {}
    if args.nocert:
        opts = {"cert_reqs": ssl.CERT_NONE, "check_hostname": False}
    if args.headers:
        options['header'] = map(str.strip, args.headers.split(','))
    ws = websocket.create_connection(args.url, sslopt=opts, **options)
    if args.raw:
        console = NonInteractive()
    else:
        console = InteractiveConsole()
        print("Press Ctrl+C to quit")

    def recv():
        try:
            frame = ws.recv_frame()
        except websocket.WebSocketException:
            return websocket.ABNF.OPCODE_CLOSE, None
        if not frame:
            raise websocket.WebSocketException("Not a valid frame %s" % frame)
        elif frame.opcode in OPCODE_DATA:
            return frame.opcode, frame.data
        elif frame.opcode == websocket.ABNF.OPCODE_CLOSE:
            ws.send_close()
            return frame.opcode, None
        elif frame.opcode == websocket.ABNF.OPCODE_PING:
            ws.pong(frame.data)
            return frame.opcode, frame.data

        return frame.opcode, frame.data

    def recv_ws():
        while True:
            opcode, data = recv()
            msg = None
            if six.PY3 and opcode == websocket.ABNF.OPCODE_TEXT and isinstance(data, bytes):
                data = str(data, "utf-8")
            if isinstance(data, bytes) and len(data)>2 and data[:2] == b'\037\213':  # gzip magick
                try:
                    data = "[gzip] " + str(gzip.decompress(data), "utf-8")
                except:
                    pass
            elif isinstance(data, bytes):
                try:
                    data = "[zlib] " + str(zlib.decompress(data, -zlib.MAX_WBITS), "utf-8")
                except:
                    pass

            if isinstance(data, bytes):
                data = repr(data)

            if args.verbose:
                msg = "%s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data)
            else:
                msg = data

            if msg is not None:
                if args.timings:
                    console.write(str(time.time() - start_time) + ": " + msg)
                else:
                    console.write(msg)

            if opcode == websocket.ABNF.OPCODE_CLOSE:
                break

    thread = threading.Thread(target=recv_ws)
    thread.daemon = True
    thread.start()

    if args.text:
        ws.send(args.text)

    while True:
        try:
            message = console.read()
            ws.send(message)
        except KeyboardInterrupt:
            return
        except EOFError:
            time.sleep(args.eof_wait)
            return

if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print(e)
<|endoftext|>


<commit_before>'''
Created on Sep 4, 2012

@author: dstrauss
'''

import numpy as np

D = {'solverType':'splitField', 'flavor':'TE', 'numRuns':3, 'expt':'spfTest'}

def getMyVars(parseNumber, D):
    '''routine to return the parameters to test at the current iteration.'''
#    rhos, bkgLocal = np.meshgrid(np.logspace(-4,0,20), range(100))
#    rhos = rhos.flatten()
#    bkgLocal = bkgLocal.flatten()
#
#
#
#    D['bkgSig'] = rhos[parseNumber]
#    D['bkgNo'] =  bkgLocal[parseNumber] + 100
    D['numProcs'] = 16

    if parseNumber == 0:
        D['bkgSig'] = 0.005
        D['bkgNo'] = 100
    elif parseNumber == 1:
        D['bkgSig'] = 0.05
        D['bkgNo'] = 100
    elif parseNumber == 2:
        # D['bkgSig'] = .5
        D['bkgNo'] = 100

    return D<commit_msg>make them close, but not the same.<commit_after>'''
Created on Sep 4, 2012

@author: dstrauss
'''

import numpy as np

D = {'solverType':'splitField', 'flavor':'TE', 'numRuns':3, 'expt':'spfTest'}

def getMyVars(parseNumber, D):
    '''routine to return the parameters to test at the current iteration.'''
#    rhos, bkgLocal = np.meshgrid(np.logspace(-4,0,20), range(100))
#    rhos = rhos.flatten()
#    bkgLocal = bkgLocal.flatten()
#
#
#
#    D['bkgSig'] = rhos[parseNumber]
#    D['bkgNo'] =  bkgLocal[parseNumber] + 100
    D['numProcs'] = 16

    if parseNumber == 0:
        D['bkgSig'] = 0.005
        D['bkgNo'] = 100
    elif parseNumber == 1:
        D['bkgSig'] = 0.0048
        D['bkgNo'] = 100
    elif parseNumber == 2:
        # D['bkgSig'] = .5
        D['bkgNo'] = 100

    return D<|endoftext|>


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

from optparse import OptionParser
import os,sys

class installationTool():

	def cmdLine(self):
		usage = "%prog [options]"

		parser = OptionParser(usage=usage)
		parser.add_option("-a", "--runall",
							action="store_true",
							dest="runall_flag",
							default=False,
							help="Run reference doc generation process")
		parser.add_option("-r", "--runreference",
							action="store_true",
							dest="runreference_flag",
							default=False,
							help="Run reference doc generation process")
		parser.add_option("-g", "--runguide",
							action="store_true",
							dest="runguide_flag",
							default=False,
							help="Run user guide generation process")
		parser.add_option("-d", "--runasciidoc",
							action="store_true",
							dest="runasciidoc_flag",
							default=False,
							help="Run the asciidoc conversion process")
		parser.add_option("-p", "--runpdf",
							action="store_true",
							dest="runpdf_flag",
							default=False,
							help="Run the pdf conversion process")
		parser.add_option("-i", "--images",
							action="store_true",
							dest="images_flag",
							default=False,
							help="Generate the Gaffer UI images")
		parser.add_option("-v", "--verbose",
							type='int',
							dest="verbose_flag",
							default=0,
							help="Enable verbose output from cmds")

		(opts, args) = parser.parse_args()

		#check to see if we have been passed any flags. if no flags, this tmp will stay False
		tmp = False
		for opt, value in opts.__dict__.items():
			if opt != 'verbose_flag':
				if value:
					tmp = True

		print tmp
		self.opts = {}

		if tmp == False or opts.runall_flag:
			#we have no flags, so we want to treat that the same as --all=True
			#turn everything on
			for opt, value in opts.__dict__.items():
				if opt == 'verbose_flag':
					self.opts[opt] = value
				else:
					self.opts[opt] = True

		else:
			for opt, value in opts.__dict__.items():
				self.opts[opt] = value

		self.runCommands()

	def getPythonInstructions(self,pyfile):
		'''
		for use with the gaffer screengrab app.
		returns a string which can be passed to screengrab app,
		by reading the contents of a .py file
		'''

		result = ''
		handle = open(pyfile, 'r')
		contents = handle.readlines()
		handle.close()
		for line in contents:
			#get rid of newlines
			line = line.rstrip('\n')
			#strip any comments from end of line
			line = line.split('#')[0]
			line = line.strip()

			result += '%s;' % (line)

		return result

	def logit(self,message):
		'''
		simple formatted output.
		only enabled in verbose mode
		'''
		if self.opts['verbose_flag']:
			print '\033[92m%s:\033[0m %s' % (self.messageHeader,message)

	def runCommands(self):

		gp_cmd = 'gaffer python'
		ad_cmd = 'asciidoc'
		sg_cmd = 'gaffer screengrab'
		pdf_cmd = 'prince'
		if self.opts['verbose_flag'] > 1: #extra verbose
			ad_cmd += ' -v'
			pdf_cmd += ' -v'

		build_root = os.getcwd()

		if self.opts['runreference_flag']:
			#build the node reference
			os.chdir( os.path.join (build_root, 'GafferNodeReference' ))
			self.messageHeader = 'GafferNodeReference'

			## extract the info from the node metadata
			self.logit('Generating node descriptions data...')
			os.system( gp_cmd + ' extractoMatic.py' )

			if self.opts['runasciidoc_flag']:
				## run asciidoc then convert the result into a pdf
				self.logit('Processing Asciidoc source...')
				os.system( ad_cmd + ' GafferNodeReference.txt' )
			if self.opts['runpdf_flag']:
				self.logit('Postconverting html to pdf...')
				os.system( pdf_cmd + ' GafferNodeReference.html GafferNodeReference.pdf' )

			self.logit('Done.')

		if self.opts['runguide_flag']:
			#build the user guide
			os.chdir( os.path.join( build_root, 'GafferUserGuide' ))
			self.messageHeader = 'GafferUserGuide'

			##generate licenses content in asciidoc format
			self.logit('Generating license data...')
			os.system( gp_cmd + ' appendicies_licensesGenerator.py' )

			if self.opts['images_flag']:
				##generate images from reference scripts
				self.logit('Generating Gaffer UI images...')
				source_dir = os.path.join( build_root, 'GafferUserGuide', 'images', 'autoGenerated_source' )
				target_dir = os.path.join( build_root, 'GafferUserGuide', 'images', 'autoGenerated_target' )
				for source in os.listdir( source_dir ):
					if source.endswith('.gfr'):
						self.logit('Processing [ %s ]' % ( source ) )
						target_img = source[:-4] + ".png"
						sg_cmd_full = "%s -script %s -image %s" % \
										(sg_cmd, os.path.join( source_dir, source ), os.path.join( target_dir, target_img ) )

						pythonfile = os.path.join( source_dir, source[:-4] + '.py' )
						if os.path.exists( pythonfile ):
							#there is a corresponding post launch python file
							sg_cmd_full = '%s -cmd "%s"' % ( sg_cmd_full, self.getPythonInstructions(pythonfile))

						self.logit( sg_cmd_full )
						os.system( sg_cmd_full )

			if self.opts['runasciidoc_flag']:
				## run asciidoc then convert the result into a pdf
				self.logit('Processing Asciidoc source...')
				os.system( ad_cmd + " GafferUserGuide.txt" )

			if self.opts['runpdf_flag']:
				self.logit('Postconverting html to pdf...')
				os.system( pdf_cmd + " GafferUserGuide.html GafferUserGuide.pdf" )

			self.logit('Done.')

if __name__ == "__main__":
	# run the code
	doIt=installationTool()
	doIt.cmdLine()
<commit_msg>clean up<commit_after>#!/usr/bin/env python

from optparse import OptionParser
import os,sys

class installationTool():

	def cmdLine(self):
		usage = "%prog [options]"

		parser = OptionParser(usage=usage)
		parser.add_option("-a", "--runall",
							action="store_true",
							dest="runall_flag",
							default=False,
							help="Run reference doc generation process")
		parser.add_option("-r", "--runreference",
							action="store_true",
							dest="runreference_flag",
							default=False,
							help="Run reference doc generation process")
		parser.add_option("-g", "--runguide",
							action="store_true",
							dest="runguide_flag",
							default=False,
							help="Run user guide generation process")
		parser.add_option("-d", "--runasciidoc",
							action="store_true",
							dest="runasciidoc_flag",
							default=False,
							help="Run the asciidoc conversion process")
		parser.add_option("-p", "--runpdf",
							action="store_true",
							dest="runpdf_flag",
							default=False,
							help="Run the pdf conversion process")
		parser.add_option("-i", "--images",
							action="store_true",
							dest="images_flag",
							default=False,
							help="Generate the Gaffer UI images")
		parser.add_option("-v", "--verbose",
							type='int',
							dest="verbose_flag",
							default=0,
							help="Enable verbose output from cmds")

		(opts, args) = parser.parse_args()

		#check to see if we have been passed any flags. if no flags, this tmp will stay False
		tmp = False
		for opt, value in opts.__dict__.items():
			if opt != 'verbose_flag':
				if value:
					tmp = True

		self.opts = {}

		if tmp == False or opts.runall_flag:
			#we have no flags, so we want to treat that the same as --all=True
			#turn everything on
			for opt, value in opts.__dict__.items():
				if opt == 'verbose_flag':
					self.opts[opt] = value
				else:
					self.opts[opt] = True

		else:
			for opt, value in opts.__dict__.items():
				self.opts[opt] = value

		self.runCommands()

	def getPythonInstructions(self,pyfile):
		'''
		for use with the gaffer screengrab app.
		returns a string which can be passed to screengrab app,
		by reading the contents of a .py file
		'''

		result = ''
		handle = open(pyfile, 'r')
		contents = handle.readlines()
		handle.close()
		for line in contents:
			#get rid of newlines
			line = line.rstrip('\n')
			#strip any comments from end of line
			line = line.split('#')[0]
			line = line.strip()

			result += '%s;' % (line)

		return result

	def logit(self,message):
		'''
		simple formatted output.
		only enabled in verbose mode
		'''
		if self.opts['verbose_flag']:
			print '\033[92m%s:\033[0m %s' % (self.messageHeader,message)

	def runCommands(self):

		gp_cmd = 'gaffer python'
		ad_cmd = 'asciidoc'
		sg_cmd = 'gaffer screengrab'
		pdf_cmd = 'prince'
		if self.opts['verbose_flag'] > 1: #extra verbose
			ad_cmd += ' -v'
			pdf_cmd += ' -v'

		build_root = os.getcwd()

		if self.opts['runreference_flag']:
			#build the node reference
			os.chdir( os.path.join (build_root, 'GafferNodeReference' ))
			self.messageHeader = 'GafferNodeReference'

			## extract the info from the node metadata
			self.logit('Generating node descriptions data...')
			os.system( gp_cmd + ' extractoMatic.py' )

			if self.opts['runasciidoc_flag']:
				## run asciidoc then convert the result into a pdf
				self.logit('Processing Asciidoc source...')
				os.system( ad_cmd + ' GafferNodeReference.txt' )
			if self.opts['runpdf_flag']:
				self.logit('Postconverting html to pdf...')
				os.system( pdf_cmd + ' GafferNodeReference.html GafferNodeReference.pdf' )

			self.logit('Done.')

		if self.opts['runguide_flag']:
			#build the user guide
			os.chdir( os.path.join( build_root, 'GafferUserGuide' ))
			self.messageHeader = 'GafferUserGuide'

			##generate licenses content in asciidoc format
			self.logit('Generating license data...')
			os.system( gp_cmd + ' appendicies_licensesGenerator.py' )

			if self.opts['images_flag']:
				##generate images from reference scripts
				self.logit('Generating Gaffer UI images...')
				source_dir = os.path.join( build_root, 'GafferUserGuide', 'images', 'autoGenerated_source' )
				target_dir = os.path.join( build_root, 'GafferUserGuide', 'images', 'autoGenerated_target' )
				for source in os.listdir( source_dir ):
					if source.endswith('.gfr'):
						self.logit('Processing [ %s ]' % ( source ) )
						target_img = source[:-4] + ".png"
						sg_cmd_full = "%s -script %s -image %s" % \
										(sg_cmd, os.path.join( source_dir, source ), os.path.join( target_dir, target_img ) )

						pythonfile = os.path.join( source_dir, source[:-4] + '.py' )
						if os.path.exists( pythonfile ):
							#there is a corresponding post launch python file
							sg_cmd_full = '%s -cmd "%s"' % ( sg_cmd_full, self.getPythonInstructions(pythonfile))

						self.logit( sg_cmd_full )
						os.system( sg_cmd_full )

			if self.opts['runasciidoc_flag']:
				## run asciidoc then convert the result into a pdf
				self.logit('Processing Asciidoc source...')
				os.system( ad_cmd + " GafferUserGuide.txt" )

			if self.opts['runpdf_flag']:
				self.logit('Postconverting html to pdf...')
				os.system( pdf_cmd + " GafferUserGuide.html GafferUserGuide.pdf" )

			self.logit('Done.')

if __name__ == "__main__":
	# run the code
	doIt=installationTool()
	doIt.cmdLine()
<|endoftext|>


<commit_before>from contextlib import nested
from deployer.host import Host, HostContext
from deployer.exceptions import ExecCommandFailed
from deployer.utils import isclass, esc1
from functools import wraps

__all__ = ('HostContainer', 'HostsContainer', )

class HostsContainer(object):
    """
    Facade to the host instances.
    if you have a role, name 'www' inside the service webserver, you can do:

    - webserver.hosts.run(...)
    - webserver.hosts.www.run(...)
    - webserver.hosts[0].run(...)
    - webserver.hosts.www[0].run(...)
    - webserver.hosts.filter('www')[0].run(...)

    The host container also keeps track of HostStatus. So, if we fork a new
    thread, and the HostStatus object gets modified in either thread. Clone
    this HostsContainer first.
    """
    def __init__(self, hosts, pty=None, logger=None, is_sandbox=False):
        # the hosts parameter is a dictionary, mapping roles to <Host> instances, or lists
        # of <Host>-instances.
        # e.g. hosts = { 'www': [ <host1>, <host2> ], 'queue': <host3> }
        self._logger = logger
        self._pty = pty
        self._sandbox = is_sandbox

        # Create
        self._hosts = { }

        def get(h):
            # Create host instance if class was given. Otherwise return
            # instance.
            if isclass(h):
                assert issubclass(h, Host)
                return h()
            else:
                assert isinstance(h, Host)
                return h

        for k, v in hosts.items():
            # A value should be a list of Host classes.
            assert isinstance(v, set)
            self._hosts[k] = { get(h) for h in v }

        # Validate hosts. No two host with the same slug can occur in a
        # same role within a container.
        for k, v in hosts.items():
            slugs = { } # Slug -> Host class
            for h in v:
                if h.slug in slugs and h != slugs[h.slug]:
                    raise Exception('Duplicate host slug %s found in HostsContainer.' % h.slug)
                else:
                    slugs[h.slug] = h

    @property
    def _all(self):
        return [ h for v in self._hosts.values() for h in v ]

    @classmethod
    def from_definition(cls, hosts_class, **kw):
        """
        Create a ``HostContainer`` from a Hosts class.
        """
        hosts = { }
        for k in dir(hosts_class):
            v = getattr(hosts_class, k)

            if isinstance(v, HostsContainer):
                # This happens when we define Hosts inline in an action, to be
                # initialized, e.g. by initialize_node.
                hosts[k] = v.get_hosts()
            elif isclass(v) and issubclass(v, Host):
                hosts[k] = { v }
            elif isinstance(v, (set, tuple)):
                for h in v:
                    assert issubclass(h, Host)
                hosts[k] = { h for h in v }
            elif not k.startswith('_'):
                raise TypeError('Invalid attribute in host definition %s: %r=%r' % (hosts_class, k, v))

        return cls(hosts, **kw)

    def get_hosts(self):
        """
        Return a set of :class:`Host` classes that appear in this container.
        Each ``Host`` class will abviously appear only once in the set, even
        when it appears in several roles.
        """
        return { h.__class__ for l in self._hosts.values() for h in l }

    def get_hosts_as_dict(self):
        """
        Return a dictionary which maps all the roles to the set of
        :class:`Host` classes for each role.
        """
        return { k: { h.__class__ for h in l } for k,l in self._hosts.items() }

    def __repr__(self):
        return ('<%s\n' % self.__class__.__name__ +
                ''.join('   %s: [%s]\n' % (r, ','.join(h.slug for h in self.filter(r))) for r in self.roles) +
                '>')

    def __eq__(self, other):
        """
        Return ``True`` when the roles/hosts are the same.
        """
        # We can't do this: host instances are created during initialisation.
        raise NotImplementedError('There is no valid definition for HostsContainer equality.')

    def _new(self, hosts):
        return HostsContainer(hosts, self._pty, self._logger, self._sandbox)

    def _new_1(self, host):
        return HostContainer({ 'host': {host} }, self._pty, self._logger, self._sandbox)

    def __len__(self):
        """
        Returns the amount of :class:`Host` instances in this container. If a
        host appears in several roles, each appearance will be taken in
        account.
        """
        return sum(len(v) for v in self._hosts.values())

    def __nonzero__(self):
        return len(self) > 0

    @property
    def roles(self):
        return sorted(self._hosts.keys())

    def __contains__(self, host):
        """
        Return ``True`` when this host appears in this host container.
        """
        raise Exception('No valid implementation for this...') # XXX

#        assert isinstance(host, (Host, HostContainer))
#
#        if isinstance(host, HostContainer):
#            host = host._host
#        return host.__class__ in self.get_hosts()

    def filter(self, *roles):
        """
        Returns a new HostsContainer instance, containing only the hosts
        matching this filter. The hosts are passed by reference, so if you'd
        call `cd()` on the returned container, it will also effect the hosts in
        this object.

        Examples:

        ::

            hosts.filter('role1', 'role2')
        """
        assert all(isinstance(r, basestring) for r in roles), TypeError('Unknown host filter %r' % roles)

        return self._new({ r: self._hosts.get(r, set()) for r in roles })

    def __getitem__(self, index):
        """
        For backwards-compatibility:

        We only allow the [0] index operation, because the hosts that exist in
        a HostsContainer don't have an order defined. [0] will work, but a random
        host is returned.

        :returns: :class:`HostContainer`;
        """
        if index != 0:
            raise Exception('Only [0] index operation is allowed on HostsContainer instance.')

        hosts = list(self._all)

        if len(hosts) == 0:
            raise IndexError

        return self._new_1(hosts[index])

    def __iter__(self):
        for h in self._all:
            yield self._new_1(h)

    def expand_path(self, path):
        return [ h.expand_path(path) for h in self._all ]

    @wraps(Host.run)
    def run(self, *a, **kw):
        """
        Call ``run`` with this parameters on every host.

        :returns: An array of all the results.
        """
        # First create a list of callables
        def closure(host):
            def call(pty):
                assert pty # TODO: this fails???
                kw2 = dict(**kw)
                if not 'sandbox' in kw2:
                    kw2['sandbox'] = self._sandbox
                kw2['logger'] = self._logger
                return host.run(pty, *a, **kw2)
            return call

        callables = map(closure, self._all)

        # When addressing multiple hosts and auxiliary ptys are available,
        # do a parallel run.
        if len(callables) > 1 and self._pty.auxiliary_ptys_are_available:
            # Run in auxiliary ptys, wait for them all to finish,
            # and return result.
            print 'Forking to %i pseudo terminals...' % len(callables)

            # Wait for the forks to finish
            fork_result = self._pty.run_in_auxiliary_ptys(callables)
            fork_result.join()
            result = fork_result.result

            # Return result.
            print ''.join(result) # (Print it once more in the main terminal, not really sure whether we should do that.)
            return result

        # Otherwise, run all serially.
        else:
            return [ c(self._pty) for c in callables ]

    @wraps(Host.sudo)
    def sudo(self, *args, **kwargs):
        """
        Call ``sudo`` with this parameters on every host.
        """
        kwargs['use_sudo'] = True
        return HostsContainer.run(self, *args, **kwargs)
                    # NOTE: here we use HostsContainer instead of self, to be
                    #       sure that we don't call te overriden method in
                    #       HostContainer.

    @wraps(HostContext.prefix)
    def prefix(self, *a, **kw):
        """
        Call 'prefix' with this parameters on every host.
        """
        return nested(* [ h.host_context.prefix(*a, **kw) for h in self._all ])

    @wraps(HostContext.cd)
    def cd(self, *a, **kw):
        """
        Call 'cd' with this parameters on every host.
        """
        return nested(* [ h.host_context.cd(*a, **kw) for h in self._all ])

    @wraps(HostContext.env)
    def env(self, *a, **kw):
        """
        Call 'env' with this parameters on every host.
        """
        return nested(* [ h.host_context.env(*a, **kw) for h in self._all ])

    def getcwd(self):
        """ Call getcwd() for every host """
        return [ h._host.getcwd() for h in self ]

    #
    # Commands
    # (these don't need sandboxing.)
    #
    def exists(self, filename, use_sudo=True):
        """
        Returns ``True`` when this file exists on the hosts.
        """
        def on_host(container):
            return container._host.exists(filename, use_sudo=use_sudo)

        return map(on_host, self)

    def has_command(self, command, use_sudo=False):
        """
        Test whether this command can be found in the bash shell, by executing a 'which'
        """
        def on_host(container):
            try:
                container.run("which '%s'" % esc1(command), use_sudo=use_sudo,
                                interactive=False, sandbox=False)
                return True
            except ExecCommandFailed:
                return False

        return map(on_host, self)

    @property
    def hostname(self): # TODO: Deprecate!!!
        with self.cd('/'):
            return self.run('hostname', sandbox=False).strip()

    @property
    def is_64_bit(self): # TODO: deprecate!!!
        with self.cd('/'):
            return 'x86_64' in self._run_silent('uname -m', sandbox=False)

class HostContainer(HostsContainer):
    """
    Similar to hostsContainer, but wraps only around exactly one host.
    """
    @property
    def _host(self):
        """
        This host container has only one host.
        """
        return self._all[0]

    @property
    def slug(self):
        return self._host.slug

    @wraps(Host.get_file)
    def get_file(self, *args,**kwargs):
        if len(self) == 1:
            kwargs['logger'] = self._logger
            kwargs['sandbox'] = self._sandbox

            return self._host.get_file(*args, **kwargs)
        else:
            raise AttributeError

    @wraps(Host.put_file)
    def put_file(self, *args,**kwargs):
        if len(self) == 1:
            kwargs['logger'] = self._logger
            kwargs['sandbox'] = self._sandbox

            return self._host.put_file(*args, **kwargs)
        else:
            raise AttributeError

    @wraps(Host.open)
    def open(self, *args,**kwargs):
        if len(self) == 1:
            kwargs['logger'] = self._logger
            kwargs['sandbox'] = self._sandbox

            return self._host.open(*args, **kwargs)
        else:
            raise AttributeError

    @wraps(HostsContainer.run)
    def run(self, *a, **kw):
        return HostsContainer.run(self, *a, **kw)[0]

    @wraps(HostsContainer.sudo)
    def sudo(self, *a, **kw):
        return HostsContainer.sudo(self, *a, **kw)[0]

    @wraps(HostsContainer.getcwd)
    def getcwd(self):
        return HostsContainer.getcwd(self)[0]

    def start_interactive_shell(self, command=None, initial_input=None):
        if not self._sandbox:
            return self._host.start_interactive_shell(self._pty, command=command, initial_input=initial_input)
        else:
            print 'Interactive shell is not available in sandbox mode.'

    def __getattr__(self, name):
        """
        Proxy to the Host object. Following commands can be
        accessed when this hostcontainer contains exactly one host.
        """
        return getattr(self._host, name)

    @wraps(HostsContainer.expand_path)
    def expand_path(self, *a, **kw):
        return HostsContainer.expand_path(self, *a, **kw)[0]

    @wraps(HostsContainer.exists)
    def exists(self, *a, **kw):
        return HostsContainer.exists(self, *a, **kw)[0]

    @wraps(HostsContainer.has_command)
    def has_command(self, *a, **kw):
        return HostsContainer.has_command(self, *a, **kw)[0]
<commit_msg>Cleanup in HostContainer.<commit_after>from contextlib import nested
from deployer.host import Host, HostContext
from deployer.exceptions import ExecCommandFailed
from deployer.utils import isclass, esc1
from functools import wraps

__all__ = ('HostContainer', 'HostsContainer', )

class HostsContainer(object):
    """
    Facade to the host instances.
    if you have a role, name 'www' inside the service webserver, you can do:

    - webserver.hosts.run(...)
    - webserver.hosts.www.run(...)
    - webserver.hosts[0].run(...)
    - webserver.hosts.www[0].run(...)
    - webserver.hosts.filter('www')[0].run(...)

    The host container also keeps track of HostStatus. So, if we fork a new
    thread, and the HostStatus object gets modified in either thread. Clone
    this HostsContainer first.
    """
    def __init__(self, hosts, pty=None, logger=None, is_sandbox=False):
        # the hosts parameter is a dictionary, mapping roles to <Host> instances, or lists
        # of <Host>-instances.
        # e.g. hosts = { 'www': [ <host1>, <host2> ], 'queue': <host3> }
        self._logger = logger
        self._pty = pty
        self._sandbox = is_sandbox

        # Create
        self._hosts = { }

        def get(h):
            # Create host instance if class was given. Otherwise return
            # instance.
            if isclass(h):
                assert issubclass(h, Host)
                return h()
            else:
                assert isinstance(h, Host)
                return h

        for k, v in hosts.items():
            # A value should be a list of Host classes.
            assert isinstance(v, set)
            self._hosts[k] = { get(h) for h in v }

        # Validate hosts. No two host with the same slug can occur in a
        # same role within a container.
        for k, v in hosts.items():
            slugs = { } # Slug -> Host class
            for h in v:
                if h.slug in slugs and h != slugs[h.slug]:
                    raise Exception('Duplicate host slug %s found in HostsContainer.' % h.slug)
                else:
                    slugs[h.slug] = h

    @property
    def _all(self):
        return [ h for v in self._hosts.values() for h in v ]

    @classmethod
    def from_definition(cls, hosts_class, **kw):
        """
        Create a ``HostContainer`` from a Hosts class.
        """
        hosts = { }
        for k in dir(hosts_class):
            v = getattr(hosts_class, k)

            if isinstance(v, HostsContainer):
                # This happens when we define Hosts inline in an action, to be
                # initialized, e.g. by initialize_node.
                hosts[k] = v.get_hosts()
            elif isclass(v) and issubclass(v, Host):
                hosts[k] = { v }
            elif isinstance(v, (set, tuple)):
                for h in v:
                    assert issubclass(h, Host)
                hosts[k] = { h for h in v }
            elif not k.startswith('_'):
                raise TypeError('Invalid attribute in host definition %s: %r=%r' % (hosts_class, k, v))

        return cls(hosts, **kw)

    def get_hosts(self):
        """
        Return a set of :class:`Host` classes that appear in this container.
        Each ``Host`` class will abviously appear only once in the set, even
        when it appears in several roles.
        """
        return { h.__class__ for l in self._hosts.values() for h in l }

    def get_hosts_as_dict(self):
        """
        Return a dictionary which maps all the roles to the set of
        :class:`Host` classes for each role.
        """
        return { k: { h.__class__ for h in l } for k,l in self._hosts.items() }

    def __repr__(self):
        return ('<%s\n' % self.__class__.__name__ +
                ''.join('   %s: [%s]\n' % (r, ','.join(h.slug for h in self.filter(r))) for r in self.roles) +
                '>')

    def __eq__(self, other):
        """
        Return ``True`` when the roles/hosts are the same.
        """
        # We can't do this: host instances are created during initialisation.
        raise NotImplementedError('There is no valid definition for HostsContainer equality.')

    def _new(self, hosts):
        return HostsContainer(hosts, self._pty, self._logger, self._sandbox)

    def _new_1(self, host):
        return HostContainer({ 'host': {host} }, self._pty, self._logger, self._sandbox)

    def __len__(self):
        """
        Returns the amount of :class:`Host` instances in this container. If a
        host appears in several roles, each appearance will be taken in
        account.
        """
        return sum(len(v) for v in self._hosts.values())

    def __nonzero__(self):
        return len(self) > 0

    @property
    def roles(self):
        return sorted(self._hosts.keys())

    def __contains__(self, host):
        """
        Return ``True`` when this host appears in this host container.
        """
        raise Exception('No valid implementation for this...') # XXX

#        assert isinstance(host, (Host, HostContainer))
#
#        if isinstance(host, HostContainer):
#            host = host._host
#        return host.__class__ in self.get_hosts()

    def filter(self, *roles):
        """
        Returns a new HostsContainer instance, containing only the hosts
        matching this filter. The hosts are passed by reference, so if you'd
        call `cd()` on the returned container, it will also effect the hosts in
        this object.

        Examples:

        ::

            hosts.filter('role1', 'role2')
        """
        assert all(isinstance(r, basestring) for r in roles), TypeError('Unknown host filter %r' % roles)

        return self._new({ r: self._hosts.get(r, set()) for r in roles })

    def __getitem__(self, index):
        """
        For backwards-compatibility:

        We only allow the [0] index operation, because the hosts that exist in
        a HostsContainer don't have an order defined. [0] will work, but a random
        host is returned.

        :returns: :class:`HostContainer`;
        """
        if index != 0:
            raise Exception('Only [0] index operation is allowed on HostsContainer instance.')

        hosts = list(self._all)

        if len(hosts) == 0:
            raise IndexError

        return self._new_1(hosts[index])

    def __iter__(self):
        for h in self._all:
            yield self._new_1(h)

    def expand_path(self, path):
        return [ h.expand_path(path) for h in self._all ]

    @wraps(Host.run)
    def run(self, *a, **kw):
        """
        Call ``run`` with this parameters on every host.

        :returns: An array of all the results.
        """
        # First create a list of callables
        def closure(host):
            def call(pty):
                assert pty # TODO: this fails???
                kw2 = dict(**kw)
                if not 'sandbox' in kw2:
                    kw2['sandbox'] = self._sandbox
                kw2['logger'] = self._logger
                return host.run(pty, *a, **kw2)
            return call

        callables = map(closure, self._all)

        # When addressing multiple hosts and auxiliary ptys are available,
        # do a parallel run.
        if len(callables) > 1 and self._pty.auxiliary_ptys_are_available:
            # Run in auxiliary ptys, wait for them all to finish,
            # and return result.
            print 'Forking to %i pseudo terminals...' % len(callables)

            # Wait for the forks to finish
            fork_result = self._pty.run_in_auxiliary_ptys(callables)
            fork_result.join()
            result = fork_result.result

            # Return result.
            print ''.join(result) # (Print it once more in the main terminal, not really sure whether we should do that.)
            return result

        # Otherwise, run all serially.
        else:
            return [ c(self._pty) for c in callables ]

    @wraps(Host.sudo)
    def sudo(self, *args, **kwargs):
        """
        Call ``sudo`` with this parameters on every host.
        """
        kwargs['use_sudo'] = True
        return HostsContainer.run(self, *args, **kwargs)
                    # NOTE: here we use HostsContainer instead of self, to be
                    #       sure that we don't call te overriden method in
                    #       HostContainer.

    @wraps(HostContext.prefix)
    def prefix(self, *a, **kw):
        """
        Call 'prefix' with this parameters on every host.
        """
        return nested(* [ h.host_context.prefix(*a, **kw) for h in self._all ])

    @wraps(HostContext.cd)
    def cd(self, *a, **kw):
        """
        Call 'cd' with this parameters on every host.
        """
        return nested(* [ h.host_context.cd(*a, **kw) for h in self._all ])

    @wraps(HostContext.env)
    def env(self, *a, **kw):
        """
        Call 'env' with this parameters on every host.
        """
        return nested(* [ h.host_context.env(*a, **kw) for h in self._all ])

    def getcwd(self):
        """ Call getcwd() for every host """
        return [ h._host.getcwd() for h in self ]

    #
    # Commands
    # (these don't need sandboxing.)
    #
    def exists(self, filename, use_sudo=True):
        """
        Returns ``True`` when this file exists on the hosts.
        """
        def on_host(container):
            return container._host.exists(filename, use_sudo=use_sudo)

        return map(on_host, self)

    def has_command(self, command, use_sudo=False):
        """
        Test whether this command can be found in the bash shell, by executing a 'which'
        """
        def on_host(container):
            try:
                container.run("which '%s'" % esc1(command), use_sudo=use_sudo,
                                interactive=False, sandbox=False)
                return True
            except ExecCommandFailed:
                return False

        return map(on_host, self)

    @property
    def hostname(self): # TODO: Deprecate!!!
        with self.cd('/'):
            return self.run('hostname', sandbox=False).strip()

    @property
    def is_64_bit(self): # TODO: deprecate!!!
        with self.cd('/'):
            return 'x86_64' in self._run_silent('uname -m', sandbox=False)

class HostContainer(HostsContainer):
    """
    Similar to hostsContainer, but wraps only around exactly one host.
    """
    @property
    def _host(self):
        """
        This host container has only one host.
        """
        assert len(self) == 1
        return self._all[0]

    @property
    def slug(self):
        return self._host.slug

    @wraps(Host.get_file)
    def get_file(self, *args,**kwargs):
        if len(self) == 1:
            kwargs['logger'] = self._logger
            kwargs['sandbox'] = self._sandbox

            return self._host.get_file(*args, **kwargs)
        else:
            raise AttributeError

    @wraps(Host.put_file)
    def put_file(self, *args, **kwargs):
        kwargs['logger'] = self._logger
        kwargs['sandbox'] = self._sandbox

        return self._host.put_file(*args, **kwargs)

    @wraps(Host.open)
    def open(self, *args, **kwargs):
        kwargs['logger'] = self._logger
        kwargs['sandbox'] = self._sandbox

        return self._host.open(*args, **kwargs)

    @wraps(HostsContainer.run)
    def run(self, *a, **kw):
        return HostsContainer.run(self, *a, **kw)[0]

    @wraps(HostsContainer.sudo)
    def sudo(self, *a, **kw):
        return HostsContainer.sudo(self, *a, **kw)[0]

    @wraps(HostsContainer.getcwd)
    def getcwd(self):
        return HostsContainer.getcwd(self)[0]

    def start_interactive_shell(self, command=None, initial_input=None):
        if not self._sandbox:
            return self._host.start_interactive_shell(self._pty, command=command, initial_input=initial_input)
        else:
            print 'Interactive shell is not available in sandbox mode.'

    def __getattr__(self, name):
        """
        Proxy to the Host object. Following commands can be
        accessed when this hostcontainer contains exactly one host.
        """
        return getattr(self._host, name)

    @wraps(HostsContainer.expand_path)
    def expand_path(self, *a, **kw):
        return HostsContainer.expand_path(self, *a, **kw)[0]

    @wraps(HostsContainer.exists)
    def exists(self, *a, **kw):
        return HostsContainer.exists(self, *a, **kw)[0]

    @wraps(HostsContainer.has_command)
    def has_command(self, *a, **kw):
        return HostsContainer.has_command(self, *a, **kw)[0]
<|endoftext|>


<commit_before># Copyright 2017 Google Inc. All rights reserved.
#
# 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.
"""Commit sha for bazelbuild/rules_go."""

version = "d475f375b041daca853610baa32823d361535fba"
<commit_msg>Project import generated by Copybara. (#631)<commit_after># Copyright 2017 Google Inc. All rights reserved.
#
# 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.
"""Commit sha for bazelbuild/rules_go."""

version = "0303b3a69695e35940b09ddbf7a444bcc7fbefd4"
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# coding: utf-8
import argparse
import urllib.request
import urllib.parse
import urllib.error
import socket
import ssl
import dns.resolver
import dns.exception

# Configuration
dns_records_list = {"gelbooru.com": ['5.178.68.100'],
                    "e621.net": ['162.159.243.197', '162.159.244.197'],
                    "sukebei.nyaa.se": ['69.165.95.242'],
                    "2chru.net": ['162.159.251.219', '198.41.249.219']}

http_list = {'http://gelbooru.com/':
                 {'status': 200, 'lookfor': 'Hentai and Anime Imageboard', 'ip': '5.178.68.100'},
             'http://gelbooru.com/index.php?page=post&s=view&id=1989610':
                 {'status': 200, 'lookfor': 'Gelbooru- Image View', 'ip': '208.100.25.82'},
             'http://sukebei.nyaa.se/':
                 {'status': 200, 'lookfor': 'A BitTorrent community', 'ip': '69.165.95.242'},
             'http://sukebei.nyaa.se/?page=view&tid=395631':
                 {'status': 200, 'lookfor': 'A BitTorrent community', 'ip': '69.165.95.242'},
            }

https_list = {'https://2chru.net/', 'https://e621.net/'}

proxy_addr = 'proxy.antizapret.prostovpn.org:3128'
google_dns = '8.8.4.4'
antizapret_dns = '107.150.11.192'

# End configuration

try:
    import tkinter as tk
    import threading
    import queue
    tkusable = True

    class ThreadSafeConsole(tk.Text):
        def __init__(self, master, **options):
            tk.Text.__init__(self, master, **options)
            self.queue = queue.Queue()
            self.update_me()
        def write(self, line):
            self.queue.put(line)
        def clear(self):
            self.queue.put(None)
        def update_me(self):
            try:
                while 1:
                    line = self.queue.get_nowait()
                    if line is None:
                        self.delete(1.0, tk.END)
                    else:
                        self.insert(tk.END, str(line))
                    self.see(tk.END)
                    self.update_idletasks()
            except queue.Empty:
                pass
            self.after(100, self.update_me)

except ImportError:
    tkusable = False

    class ThreadSafeConsole():
        pass

def print(*args, **kwargs):
    if tkusable:
        for arg in args:
            text.write(str(arg))
        text.write("\n")
    else:
        __builtins__.print(*args, **kwargs)

def _get_a_record(site, dnsserver=None):
    resolver = dns.resolver.Resolver()
    resolver.timeout = 5
    resolver.lifetime = 5

    if dnsserver:
        resolver.nameservers = [dnsserver]

    result = []
    while len(resolver.nameservers):
        try:
            for item in resolver.query(site).rrset.items:
                result.append(item.to_text())
            return result

        except dns.exception.Timeout:
            resolver.nameservers.remove(resolver.nameservers[0])

    # If all the requests failed
    return ""

def _get_a_records(sitelist, dnsserver=None):
    result = []
    for site in sitelist:
        try:
            for item in _get_a_record(site, dnsserver):
                result.append(item)
        except dns.resolver.NXDOMAIN:
            print("[!] Невозможно получить DNS-запись для домена {} (NXDOMAIN). Результаты могут быть неточными.".format(site))
        except dns.exception.DNSException:
            return ""

    return sorted(result)

def _get_url(url, proxy=None, ip=None):
    if ip:
        parsed_url = list(urllib.parse.urlsplit(url))
        host = parsed_url[1]
        parsed_url[1] = str(ip)
        newurl = urllib.parse.urlunsplit(parsed_url)
        req = urllib.request.Request(newurl)
        req.add_header('Host', host)
    else:
        req = urllib.request.Request(url)

    if proxy:
        req.set_proxy(proxy, 'http')

    req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0')

    try:
        opened = urllib.request.urlopen(req, timeout=15, cadefault=True)
        output = opened.readall()
    except (urllib.error.URLError, socket.error, socket.timeout): # we do not expect ssl.CertificateError here
        return (0, '')
    return (opened.status, str(output))

def test_dns():
    sites = dns_records_list
    sites_list = list(sites.keys())

    print("[O] Тестируем DNS")
    print("[O] Получаем эталонные DNS с сервера")
    try:
        remote_dns = urllib.request.urlopen("http://blockcheck.antizapret.prostovpn.org/getdns.php",
            timeout=10).read()
        remote_dns = remote_dns.decode('utf-8').split()
        print("\tЭталонные адреса:\t\t", str(remote_dns))
    except:
        remote_dns = None
        print("[☠] Не удалось получить DNS с сервера, результаты могут быть неточными")

    resolved_default_dns = _get_a_records(sites_list)
    print("\tАдреса через системный DNS:\t", str(resolved_default_dns))
    resolved_google_dns = _get_a_records(sites_list, google_dns)
    if resolved_google_dns:
        print("\tАдреса через Google DNS:\t", str(resolved_google_dns))
    else:
        print("\tНе удалось подключиться к Google DNS")
    resolved_az_dns = _get_a_records(sites_list, antizapret_dns)
    if resolved_az_dns:
        print("\tАдреса через DNS AntiZapret:\t", str(resolved_az_dns))
    else:
        print("\tНе удалось подключиться к DNS AntiZapret")

    if not resolved_google_dns or not resolved_default_dns:
        return 4

    if (remote_dns):
        # Если получили IP с сервера, используем их
        dns_records = remote_dns
    else:
        dns_records = sorted([item for sublist in sites.values() for item in sublist])

    if resolved_default_dns == resolved_google_dns:
        if set(resolved_az_dns) == {antizapret_dns}:
            print("[✓] DNS записи не подменяются")
            print("[✓] DNS не перенаправляется")
            return 0

        if resolved_default_dns == dns_records:
            print("[✓] DNS записи не подменяются")
            print("[☠] DNS перенаправляется")
            return 1
        else:
            print("[☠] DNS записи подменяются")
            print("[☠] DNS перенаправляется")
            return 2

    else:
        if resolved_google_dns == dns_records:
            print("[☠] DNS записи подменяются")
            print("[✓] DNS не перенаправляется")
            return 3

    print("[?] Способ блокировки DNS определить не удалось")
    return 5

def test_http_access(by_ip=False):
    sites = http_list
    proxy = proxy_addr

    print("[O] Тестируем HTTP")

    siteresults = []
    for site in sites:
        print("\tОткрываем ", site)
        result = _get_url(site, ip=sites[site].get('ip') if by_ip else None)
        if result[0] == sites[site]['status'] and result[1].find(sites[site]['lookfor']) != -1:
            print("[✓] Сайт открывается")
            siteresults.append(True)
        else:
            print("[☠] Сайт не открывается")
            siteresults.append(False)

    siteresults_proxy = []
    for site in sites:
        print("\tОткрываем через прокси ", site)
        result_proxy = _get_url(site, proxy)
        if result_proxy[0] == sites[site]['status'] and result_proxy[1].find(sites[site]['lookfor']) != -1:
            print("[✓] Сайт открывается")
            siteresults_proxy.append(True)
        else:
            print("[☠] Сайт не открывается")
            siteresults_proxy.append(False)

    if all(siteresults):
        # No blocks
        return 0
    elif any(siteresults) and all(siteresults_proxy):
        # IP-DPI
        return 1
    elif any(siteresults) and any(siteresults_proxy):
        # Full-DPI
        return 2
    else:
        # IP
        return 3

def test_https_cert():
    sites = https_list

    print("[O] Тестируем HTTPS")

    siteresults = []
    for site in sites:
        print("\tОткрываем ", site)
        try:
            result = _get_url(site, None)
            if result[0] < 200:
                print("[☠] Сайт не открывается")
            else:
                print("[✓] Сайт открывается")
                siteresults.append(True)
        except ssl.CertificateError:
            print("[☠] Сертификат подменяется")
            siteresults.append(False)

    if all(siteresults):
        # No blocks
        return 0
    elif any(siteresults):
        # Blocked
        return 1
    else:
        # Unknown result
        return 2

def main():
    dns = test_dns()
    print()
    if dns == 0:
        http = test_http_access(False)
    else:
        http = test_http_access(True)
    print()
    https = test_https_cert()
    print()
    print("[!] Результат:")
    if dns == 4:
        print("[⚠] Ваш провайдер блокирует чужие DNS-серверы.\n",
              "Вам следует использовать шифрованный канал до DNS-серверов, например, через VPN, Tor или " + \
              "HTTPS/Socks прокси.")
    elif dns == 3:
        print("[⚠] Ваш провайдер подменяет DNS-записи, но не перенаправляет чужие DNS-серверы на свой.\n",
              "Вам поможет смена DNS, например, на Яндекс.DNS 77.88.8.8 или Google DNS 8.8.8.8 и 8.8.4.4.")
    elif dns == 2:
        print("[⚠] Ваш провайдер подменяет DNS-записи и перенаправляет чужие DNS-серверы на свой.\n",
              "Вам следует использовать шифрованный канал до DNS-серверов, например, через VPN, Tor или " + \
              "HTTPS/Socks прокси.")
    elif dns == 1:
        print("[⚠] Ваш провайдер перенаправляет чужие DNS-серверы на свой, но не подменяет DNS-записи.\n",
              "Это несколько странно и часто встречается в мобильных сетях.\n",
              "Вам следует использовать шифрованный канал до DNS-серверов, например, через VPN, Tor или " + \
              "HTTPS/Socks прокси.")

    if https == 1:
        print("[⚠] Ваш провайдер лезет в HTTPS.")

    if http == 3:
        print("[⚠] Ваш провайдер блокирует по IP-адресу.\n",
              "Используйте любой способ обхода блокировок.")
    elif http == 2:
        print("[⚠] У вашего провайдера \"полный\" DPI. Он отслеживает ссылки даже внутри прокси, поэтому вам следует " + \
              "использовать любое шифрованное соединение, например, VPN или Tor.")
    elif http == 1:
        print("[⚠] У вашего провайдера \"обычный\" DPI.\n",
              "Вам поможет HTTPS/Socks прокси, VPN или Tor.")
    elif http == 0:
        print("[☺] Ваш провайдер не блокирует сайты.")

    _get_url('http://blockcheck.antizapret.prostovpn.org/index.php?dns=' + str(dns) + '&http=' + str(http) + '&https=' + str(https))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Определитель типа блокировки сайтов у провайдера.')
    parser.add_argument('--console', action='store_true', help='Консольный режим. Отключает Tkinter GUI.')
    args = parser.parse_args()
    if args.console:
        tkusable = False

    if tkusable:
        root = tk.Tk()
        root.title("BlockCheck")
        text = ThreadSafeConsole(root)
        text.pack()
        threading.Thread(target=main).start()
        root.mainloop()
    else:
        try:
            main()
        except (KeyboardInterrupt, SystemExit):
            quit(1)
<commit_msg>Fix HTTPS certificate check. Closes #30.<commit_after>#!/usr/bin/env python3
# coding: utf-8
import argparse
import urllib.request
import urllib.parse
import urllib.error
import socket
import ssl
import dns.resolver
import dns.exception

# Configuration
dns_records_list = {"gelbooru.com": ['5.178.68.100'],
                    "e621.net": ['162.159.243.197', '162.159.244.197'],
                    "sukebei.nyaa.se": ['69.165.95.242'],
                    "2chru.net": ['162.159.251.219', '198.41.249.219']}

http_list = {'http://gelbooru.com/':
                 {'status': 200, 'lookfor': 'Hentai and Anime Imageboard', 'ip': '5.178.68.100'},
             'http://gelbooru.com/index.php?page=post&s=view&id=1989610':
                 {'status': 200, 'lookfor': 'Gelbooru- Image View', 'ip': '208.100.25.82'},
             'http://sukebei.nyaa.se/':
                 {'status': 200, 'lookfor': 'A BitTorrent community', 'ip': '69.165.95.242'},
             'http://sukebei.nyaa.se/?page=view&tid=395631':
                 {'status': 200, 'lookfor': 'A BitTorrent community', 'ip': '69.165.95.242'},
            }

https_list = {'https://2chru.net/', 'https://e621.net/'}

proxy_addr = 'proxy.antizapret.prostovpn.org:3128'
google_dns = '8.8.4.4'
antizapret_dns = '107.150.11.192'

# End configuration

try:
    import tkinter as tk
    import threading
    import queue
    tkusable = True

    class ThreadSafeConsole(tk.Text):
        def __init__(self, master, **options):
            tk.Text.__init__(self, master, **options)
            self.queue = queue.Queue()
            self.update_me()
        def write(self, line):
            self.queue.put(line)
        def clear(self):
            self.queue.put(None)
        def update_me(self):
            try:
                while 1:
                    line = self.queue.get_nowait()
                    if line is None:
                        self.delete(1.0, tk.END)
                    else:
                        self.insert(tk.END, str(line))
                    self.see(tk.END)
                    self.update_idletasks()
            except queue.Empty:
                pass
            self.after(100, self.update_me)

except ImportError:
    tkusable = False

    class ThreadSafeConsole():
        pass

def print(*args, **kwargs):
    if tkusable:
        for arg in args:
            text.write(str(arg))
        text.write("\n")
    else:
        __builtins__.print(*args, **kwargs)

def _get_a_record(site, dnsserver=None):
    resolver = dns.resolver.Resolver()
    resolver.timeout = 5
    resolver.lifetime = 5

    if dnsserver:
        resolver.nameservers = [dnsserver]

    result = []
    while len(resolver.nameservers):
        try:
            for item in resolver.query(site).rrset.items:
                result.append(item.to_text())
            return result

        except dns.exception.Timeout:
            resolver.nameservers.remove(resolver.nameservers[0])

    # If all the requests failed
    return ""

def _get_a_records(sitelist, dnsserver=None):
    result = []
    for site in sitelist:
        try:
            for item in _get_a_record(site, dnsserver):
                result.append(item)
        except dns.resolver.NXDOMAIN:
            print("[!] Невозможно получить DNS-запись для домена {} (NXDOMAIN). Результаты могут быть неточными.".format(site))
        except dns.exception.DNSException:
            return ""

    return sorted(result)

def _get_url(url, proxy=None, ip=None):
    if ip:
        parsed_url = list(urllib.parse.urlsplit(url))
        host = parsed_url[1]
        parsed_url[1] = str(ip)
        newurl = urllib.parse.urlunsplit(parsed_url)
        req = urllib.request.Request(newurl)
        req.add_header('Host', host)
    else:
        req = urllib.request.Request(url)

    if proxy:
        req.set_proxy(proxy, 'http')

    req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0')

    try:
        opened = urllib.request.urlopen(req, timeout=15, cadefault=True)
        output = opened.readall()
    except (urllib.error.URLError, socket.error, socket.timeout) as e:
        if 'CERTIFICATE_VERIFY_FAILED' in str(e):
            return (-1, '')
        return (0, '')
    return (opened.status, str(output))

def test_dns():
    sites = dns_records_list
    sites_list = list(sites.keys())

    print("[O] Тестируем DNS")
    print("[O] Получаем эталонные DNS с сервера")
    try:
        remote_dns = urllib.request.urlopen("http://blockcheck.antizapret.prostovpn.org/getdns.php",
            timeout=10).read()
        remote_dns = remote_dns.decode('utf-8').split()
        print("\tЭталонные адреса:\t\t", str(remote_dns))
    except:
        remote_dns = None
        print("[☠] Не удалось получить DNS с сервера, результаты могут быть неточными")

    resolved_default_dns = _get_a_records(sites_list)
    print("\tАдреса через системный DNS:\t", str(resolved_default_dns))
    resolved_google_dns = _get_a_records(sites_list, google_dns)
    if resolved_google_dns:
        print("\tАдреса через Google DNS:\t", str(resolved_google_dns))
    else:
        print("\tНе удалось подключиться к Google DNS")
    resolved_az_dns = _get_a_records(sites_list, antizapret_dns)
    if resolved_az_dns:
        print("\tАдреса через DNS AntiZapret:\t", str(resolved_az_dns))
    else:
        print("\tНе удалось подключиться к DNS AntiZapret")

    if not resolved_google_dns or not resolved_default_dns:
        return 4

    if (remote_dns):
        # Если получили IP с сервера, используем их
        dns_records = remote_dns
    else:
        dns_records = sorted([item for sublist in sites.values() for item in sublist])

    if resolved_default_dns == resolved_google_dns:
        if set(resolved_az_dns) == {antizapret_dns}:
            print("[✓] DNS записи не подменяются")
            print("[✓] DNS не перенаправляется")
            return 0

        if resolved_default_dns == dns_records:
            print("[✓] DNS записи не подменяются")
            print("[☠] DNS перенаправляется")
            return 1
        else:
            print("[☠] DNS записи подменяются")
            print("[☠] DNS перенаправляется")
            return 2

    else:
        if resolved_google_dns == dns_records:
            print("[☠] DNS записи подменяются")
            print("[✓] DNS не перенаправляется")
            return 3

    print("[?] Способ блокировки DNS определить не удалось")
    return 5

def test_http_access(by_ip=False):
    sites = http_list
    proxy = proxy_addr

    print("[O] Тестируем HTTP")

    siteresults = []
    for site in sites:
        print("\tОткрываем ", site)
        result = _get_url(site, ip=sites[site].get('ip') if by_ip else None)
        if result[0] == sites[site]['status'] and result[1].find(sites[site]['lookfor']) != -1:
            print("[✓] Сайт открывается")
            siteresults.append(True)
        else:
            print("[☠] Сайт не открывается")
            siteresults.append(False)

    siteresults_proxy = []
    for site in sites:
        print("\tОткрываем через прокси ", site)
        result_proxy = _get_url(site, proxy)
        if result_proxy[0] == sites[site]['status'] and result_proxy[1].find(sites[site]['lookfor']) != -1:
            print("[✓] Сайт открывается")
            siteresults_proxy.append(True)
        else:
            print("[☠] Сайт не открывается")
            siteresults_proxy.append(False)

    if all(siteresults):
        # No blocks
        return 0
    elif any(siteresults) and all(siteresults_proxy):
        # IP-DPI
        return 1
    elif any(siteresults) and any(siteresults_proxy):
        # Full-DPI
        return 2
    else:
        # IP
        return 3

def test_https_cert():
    sites = https_list

    print("[O] Тестируем HTTPS")

    siteresults = []
    for site in sites:
        print("\tОткрываем ", site)
        result = _get_url(site, None)
        if result[0] == -1:
            print("[☠] Сертификат подменяется")
            siteresults.append(False)
        elif result[0] < 200:
            print("[☠] Сайт не открывается")
        else:
            print("[✓] Сайт открывается")
            siteresults.append(True)
    if all(siteresults):
        # No blocks
        return 0
    elif any(siteresults):
        # Blocked
        return 1
    else:
        # Unknown result
        return 2

def main():
    dns = test_dns()
    print()
    if dns == 0:
        http = test_http_access(False)
    else:
        http = test_http_access(True)
    print()
    https = test_https_cert()
    print()
    print("[!] Результат:")
    if dns == 4:
        print("[⚠] Ваш провайдер блокирует чужие DNS-серверы.\n",
              "Вам следует использовать шифрованный канал до DNS-серверов, например, через VPN, Tor или " + \
              "HTTPS/Socks прокси.")
    elif dns == 3:
        print("[⚠] Ваш провайдер подменяет DNS-записи, но не перенаправляет чужие DNS-серверы на свой.\n",
              "Вам поможет смена DNS, например, на Яндекс.DNS 77.88.8.8 или Google DNS 8.8.8.8 и 8.8.4.4.")
    elif dns == 2:
        print("[⚠] Ваш провайдер подменяет DNS-записи и перенаправляет чужие DNS-серверы на свой.\n",
              "Вам следует использовать шифрованный канал до DNS-серверов, например, через VPN, Tor или " + \
              "HTTPS/Socks прокси.")
    elif dns == 1:
        print("[⚠] Ваш провайдер перенаправляет чужие DNS-серверы на свой, но не подменяет DNS-записи.\n",
              "Это несколько странно и часто встречается в мобильных сетях.\n",
              "Вам следует использовать шифрованный канал до DNS-серверов, например, через VPN, Tor или " + \
              "HTTPS/Socks прокси.")

    if https == 1:
        print("[⚠] Ваш провайдер лезет в HTTPS.")

    if http == 3:
        print("[⚠] Ваш провайдер блокирует по IP-адресу.\n",
              "Используйте любой способ обхода блокировок.")
    elif http == 2:
        print("[⚠] У вашего провайдера \"полный\" DPI. Он отслеживает ссылки даже внутри прокси, поэтому вам следует " + \
              "использовать любое шифрованное соединение, например, VPN или Tor.")
    elif http == 1:
        print("[⚠] У вашего провайдера \"обычный\" DPI.\n",
              "Вам поможет HTTPS/Socks прокси, VPN или Tor.")
    elif http == 0 and https == 0:
        print("[☺] Ваш провайдер не блокирует сайты.")

    _get_url('http://blockcheck.antizapret.prostovpn.org/index.php?dns=' + str(dns) + '&http=' + str(http) + '&https=' + str(https))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Определитель типа блокировки сайтов у провайдера.')
    parser.add_argument('--console', action='store_true', help='Консольный режим. Отключает Tkinter GUI.')
    args = parser.parse_args()
    if args.console:
        tkusable = False

    if tkusable:
        root = tk.Tk()
        root.title("BlockCheck")
        text = ThreadSafeConsole(root)
        text.pack()
        threading.Thread(target=main).start()
        root.mainloop()
    else:
        try:
            main()
        except (KeyboardInterrupt, SystemExit):
            quit(1)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from django import forms
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from blog.conf import settings
from blog.models import Category, Entry, Link

class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ['name']}

class EntryAdmin(admin.ModelAdmin):
    exclude = ('author',)
    list_display = ('title', 'pub_date', 'status', 'author', 'enable_comments')
    search_fields = ['title',]
    list_filter = ('status', 'enable_comments')

    prepopulated_fields = {'slug': ['title']}

    fieldsets = [
        (None, {'fields': ['title', 'body', 'status']}),
        (_('Date information'), {'fields': ['pub_date'],
                                 'classes': ['collapse']}),
        (_('Options'), {'fields': ['slug', 'featured', 'enable_comments',
                                   'categories', 'tags'],
                        'classes': ['collapse', 'closed']}),
    ]

    def has_change_permission(self, request, obj=None):
        has_class_permission = super(EntryAdmin, self).has_change_permission(
            request, obj
        )

        if not has_class_permission:
            return False

        if (obj is not None and
            not request.user.is_superuser and
            request.user.id != obj.author.id):
                return False

        return True

    def queryset(self, request):
        if request.user.is_superuser:
            return Entry.objects.all()
        return Entry.objects.filter(author=request.user)

    def save_model(self, request, obj, form, change):
        if not change:
            obj.author = request.user
        obj.save()

    class Media:
        js = [settings.TINYMCE_URL, settings.TINYMCE_SETUP_URL]

admin.site.register(Category, CategoryAdmin)
admin.site.register(Entry, EntryAdmin)
admin.site.register(Link)
<commit_msg>Updated search fieldsand list filter<commit_after># -*- coding: utf-8 -*-
from django import forms
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from blog.conf import settings
from blog.models import Category, Entry, Link

class CategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ['name']}

class EntryAdmin(admin.ModelAdmin):
    exclude = ('author',)
    list_display = ('title', 'pub_date', 'status', 'author', 'enable_comments')
    search_fields = ['title', 'author', 'author__first_name',
                     'author__last_name', 'author__email']
    list_filter = ('status', 'enable_comments', 'featured')

    prepopulated_fields = {'slug': ['title']}

    fieldsets = [
        (None, {'fields': ['title', 'body', 'status']}),
        (_('Date information'), {'fields': ['pub_date'],
                                 'classes': ['collapse']}),
        (_('Options'), {'fields': ['slug', 'featured', 'enable_comments',
                                   'categories', 'tags'],
                        'classes': ['collapse', 'closed']}),
    ]

    def has_change_permission(self, request, obj=None):
        has_class_permission = super(EntryAdmin, self).has_change_permission(
            request, obj
        )

        if not has_class_permission:
            return False

        if (obj is not None and
            not request.user.is_superuser and
            request.user.id != obj.author.id):
                return False

        return True

    def queryset(self, request):
        if request.user.is_superuser:
            return Entry.objects.all()
        return Entry.objects.filter(author=request.user)

    def save_model(self, request, obj, form, change):
        if not change:
            obj.author = request.user
        obj.save()

    class Media:
        js = [settings.TINYMCE_URL, settings.TINYMCE_SETUP_URL]

admin.site.register(Category, CategoryAdmin)
admin.site.register(Entry, EntryAdmin)
admin.site.register(Link)
<|endoftext|>


<commit_before>from django.contrib import admin

from .models import Post, Category
from dpb.admin import PageDownAdmin

@admin.register(Post)
class PostAdmin(PageDownAdmin):
    list_display = ('title', 'category', 'archive', 'public', 'created')
    list_filter = ['created']
    search_fields = ['title']
    fieldsets = [
        (None,        {'fields': ['title', 'category', 'file', ('archive', 'public'), 'content']}),
        ('Erweitert', {'fields': ['created', 'author'], 'classes': ['collapse']}),
    ]

    def save_model(self, request, obj, form, change):
        # Automatic set author if None is set
        if getattr(obj, 'author', None) is None:
            obj.author = request.user
        obj.save()

admin.site.register(Category)
<commit_msg>Make created a read-only field in admin<commit_after>from django.contrib import admin

from dpb.admin import PageDownAdmin
from .models import Post, Category

@admin.register(Post)
class PostAdmin(PageDownAdmin):
    list_display = ('title', 'category', 'archive', 'public', 'created')
    list_filter = ['created']
    search_fields = ['title']
    readonly_fields = ['created']
    fieldsets = [
        (None, {'fields': ['title', 'category', 'file', ('archive', 'public'), 'content']}),
        ('Erweitert', {'fields': ['created', 'author'], 'classes': ['collapse']}),
    ]

    def save_model(self, request, obj, form, change):
        # Automatic set author if None is set
        if getattr(obj, 'author', None) is None:
            obj.author = request.user
        obj.save()

admin.site.register(Category)
<|endoftext|>