Beispiel #1
0
def inetd_sockets(max_socks):
    """
    """
    sock = fromfd(stdin.fileno())
    stdin.close()
    stdout.close()
    stderr.close()
    return sock
Beispiel #2
0
def runCommandOnHost(hostname,
                     command,
                     username='******',
                     localPrivKey=None,
                     timeout=None,
                     runSynchronously=True,
                     ssh=None):
    """
    :type timeout: int
    :type localPrivKey: str
    :type hostname: strs
    :type command: str
    """

    if not runSynchronously:
        command = "at -f %s now" % command
        LOG("For %s@%s will run init script asynchronously. Assuming 'at' exists on the target VM"
            % (username, hostname))

    LOG("For %s@%s EXEC %s" %
        (username, hostname,
         command if len(command) <= 80 else command[0:77] + "..."))

    if ssh is None:
        ssh = newSshClient(hostname,
                           username=username,
                           localPrivKey=localPrivKey)
    else:
        LOG("Reusing ssh client")

    chan = ssh.get_transport().open_session()
    chan.settimeout(timeout)
    chan.exec_command(command)

    stdin = chan.makefile('wb', -1)
    stdout = chan.makefile('r', -1)
    stderr = chan.makefile_stderr('r', -1)
    exitCode = chan.recv_exit_status()
    stdoutLines = stdout.readlines()
    stderrLines = stderr.readlines()

    stdin.close()
    stdout.close()
    stderr.close()
    ssh.close()

    for line in stdoutLines:
        LOG(">> [%s] STDOUT: %s" % (hostname, line))
    LOG(">> ")
    for line in stderrLines:
        LOG(">> [%s] STDERR: %s" % (hostname, line))

    return exitCode, stdoutLines, stderrLines
def get_flags(keys, usage="No Usage message included"):
    keys = keys.split(" ")
    flag = {}
    optional_keys = {}
    mandatory_keys = {}
    working = "--unflagged"
    flag[working] = ""
    keys = list(keys) + argv[1:]
    for x in keys:
        if "~" in x:
            optional_keys[x.split("~")[0]] = x.split("~")[1]
        elif "=" in x:
            mandatory_keys[x.split("=")[0]] = x.split("=")[1]
        elif re.search("^-", x):
            flag[x] = ""
            working = x
        else:
            flag[working] += " " + x
    quit = 0
    for x in flag:
        flag[x] = flag[x].lstrip(" ")
    for x in mandatory_keys:
        if mandatory_keys[x] not in flag:
            quit += 1
            if quit == 1:
                print "\033[91m\nThe following Errors occured!\033[0m"
            print "The " + mandatory_keys[x] + " flag is required!"
        else:
            mandatory_keys[x] = mandatory_keys[x].lstrip(" ")
            globals()[x] = flag[mandatory_keys[x]]
    if (quit > 0):
        from sys import stderr
        stderr.write(usage + "\n")
        stderr.close()
        exit()
    for x in optional_keys:
        if optional_keys[x] not in flag:
            continue
        flag[optional_keys[x]].lstrip(" ")
        globals()[x] = flag[optional_keys[x]]
    flag["--unflagged"].lstrip()
    re_dic = {}
    for x in mandatory_keys:
        re_dic[x] = flag[mandatory_keys[x]]
    for x in optional_keys:
        if optional_keys[x] in flag:
            re_dic[x] = flag[optional_keys[x]]
    if flag["--unflagged"]:
        re_dic['unflagged'] = flag["--unflagged"]
    return re_dic
def runCommandOnHost(hostname, command,
                     username='******',
                     localPrivKey=None,
                     timeout=None,
                     runSynchronously=True,
                     ssh=None):
    """
    :type timeout: int
    :type localPrivKey: str
    :type hostname: strs
    :type command: str
    """

    if not runSynchronously:
        command = "at -f %s now" % command
        LOG("For %s@%s will run init script asynchronously. Assuming 'at' exists on the target VM" % (username, hostname))

    LOG("For %s@%s EXEC %s" % (username, hostname, command if len(command) <= 80 else command[0:77] + "..."))

    if ssh is None:
        ssh = newSshClient(hostname, username=username, localPrivKey=localPrivKey)
    else:
        LOG("Reusing ssh client")

    chan = ssh.get_transport().open_session()
    chan.settimeout(timeout)
    chan.exec_command(command)

    stdin = chan.makefile('wb', -1)
    stdout = chan.makefile('r', -1)
    stderr = chan.makefile_stderr('r', -1)
    exitCode = chan.recv_exit_status()
    stdoutLines = stdout.readlines()
    stderrLines = stderr.readlines()

    stdin.close()
    stdout.close()
    stderr.close()
    ssh.close()

    for line in stdoutLines:
        LOG(">> [%s] STDOUT: %s" % (hostname, line))
    LOG(">> ")
    for line in stderrLines:
        LOG(">> [%s] STDERR: %s" % (hostname, line))

    return exitCode, stdoutLines, stderrLines
Beispiel #5
0
def main() -> None:

    arg_parser = ArgumentParser(prog='same-same',
                                description='Git diff filter.')
    arg_parser.add_argument('-interactive',
                            action='store_true',
                            help="Accommodate git's interactive mode.")
    args = arg_parser.parse_args()

    # Git can generate utf8-illegal sequences; ignore them.
    stdin = open(0, errors='replace')

    if 'SAME_SAME_OFF' in environ:
        for line in stdin:
            stdout.write(line)
        exit(0)

    dbg = ('SAME_SAME_DBG' in environ)

    buffer: List[DiffLine] = []

    def flush_buffer() -> None:
        nonlocal buffer
        if buffer:
            handle_file_lines(buffer, interactive=args.interactive)
            buffer = []

    try:
        for rich_text in stdin:
            rich_text = rich_text.rstrip('\n')
            plain_text = sgr_pat.sub('', rich_text)  # remove colors.
            match = diff_pat.match(plain_text)
            assert match is not None
            kind = match.lastgroup
            assert kind is not None, match
            if dbg:
                print(kind, ':', repr(plain_text))
                continue
            if kind == 'diff':
                flush_buffer()
            buffer.append(DiffLine(kind, match, rich_text))
        flush_buffer()
    except BrokenPipeError:
        stderr.close()  # Prevents warning message.
Beispiel #6
0
 def wrapper(*args, **kwargs):
     try:
         return f(*args, **kwargs)
     except SystemExit:
         raise
     except:
         print_exc()
         exit(1)
     finally:
         try:
             stdout.flush()
         finally:
             try:
                 stdout.close()
             finally:
                 try:
                     stderr.flush()
                 finally:
                     stderr.close()
Beispiel #7
0
 def wrapper(*args, **kwargs):
     try:
         return f(*args, **kwargs)
     except SystemExit:
         raise
     except:
         print_exc()
         exit(1)
     finally:
         try:
             stdout.flush()
         finally:
             try:
                 stdout.close()
             finally:
                 try:
                     stderr.flush()
                 finally:
                     stderr.close()
Beispiel #8
0
                print("      * Number of Kills: %d" % rec['num_kills'])
                print("      * Number of Limit Uses:")
                for j in [1, 2, 3]:
                    print("        * Limit %d-1: %d" %
                          (j, rec['num_limit_uses_%d_1' % j]))
                print("      * Unknown 2: %d" % rec['unknown2'])
            print("    * Item Stock:")
            for v in d['stock']['item']:
                if v[0] != (255, 1):  # empty slot
                    print("      * %dx %s" % (v[1], ITEM_DB[v[0]]))
            print("    * Materia Stock:")
            for v in d['stock']['materia']:
                if v[0] != 255:  # empty slot
                    print("      * %s (%d AP)" % (MATERIA_DB[v[0]], v[1]))
            print("    * Materia Stolen by Yuffie:")
            for v in d['stolen_materia']:
                if v[0] != 255:  # empty slot
                    print("      * %s (%d AP)" % (MATERIA_DB[v[0]], v[1]))
            print("    * Picked Up Field Items:")
            for k in sorted(d['field_items'].keys()):
                print("      * %s: %s" % (k.replace('_', ' ').title(), {
                    True: 'None',
                    False: ', '.join(d['field_items'][k])
                }[len(d['field_items'][k]) == 0]))
            print("    * Unknown 4: %s" % d['unknown4'])
            print("    * Unknown 9: %s" % d['unknown9'])
            print("    * Unknown 10: %s" % d['unknown10'])
            print("    * Unknown 11: %s" % d['unknown11'])
    except BrokenPipeError:
        stderr.close()
        try:
            with open(gtfsfilename, 'rb') as fh:
                if zipfile.is_zipfile(fh):
                    stream.write('c Feed details:\n')
                    for line in get_additional_infos(gtfsfilename):
                        stream.write('c %s\n' %line)
                    stream.write('c\n')
                else:
                    logging.error('File "%s" is not a zipfile.' % gtfsfilename)
        except IOError:
            logging.error('File "%s" does not exit.' % gtfsfilename)

output_additional_feed_info(stream,options.filename)
        
for u,v in G.edges():
    stream.write('%s %s\n' %(u,v))


if fromfile:
    inp.close()

try:
    stdout.close()
except:
    pass

try:
    stderr.close()
except:
    pass
Beispiel #10
0
def main():
    parser = argparse.ArgumentParser(
        epilog="""If both --classes and --auto-relabel are omitted,
        label values are left as-is. By default, features with value 0 are not
        printed. This can be overridden with --null""",
        usage="""%(prog)s [OPTION]... [FILE]

Convert CSV to Vowpal Wabbit input format.

  Examples:

  # Leave label values as is:
  $ csv2vw spam.csv --label target

  # Relabel values 'ham' to 0 and 'spam' to 1:
  $ csv2vw spam.csv --label target --classes ham,spam

  # Relabel values 'ham' to -1 and 'spam' to +1 (needed for logistic loss):
  $ csv2vw spam.csv --label target --classes ham,spam --minus-plus-one

  # Relabel first label value to 0, second to 1, and ignore the rest:
  $ csv2vw iris.csv -lspecies --auto-relabel --ignore-extra-classes

  # Relabel first label value to 1, second to 2, and so on:
  $ <iris.csv csv2vw -lspecies --multiclass --auto-relabel

  # Relabel 'versicolor' to 1, 'virginica' to 2, and 'setosa' to 3
  $ <iris.csv csv2vw -lspecies --multiclass -cversicolor,virginica,setosa""")

    parser.add_argument("file", nargs="?", type=argparse.FileType("r"),
                        default=stdin,
                        help="""Input CSV file. If omitted,
                        read from standard input.""",
                        metavar="FILE")
    parser.add_argument("-d", "--delimiter",
                        help="""Delimiting character of the input CSV file
                        (default: ,).""",
                        default=",")
    parser.add_argument("-l", "--label",
                        help="""Name of column that contains the class
                        labels.""")
    parser.add_argument("-c", "--classes",
                        help="""Ordered, comma-separated list of possible
                        class labels to relabel. If not specifying all possible
                        class labels, use --auto-relabel.""",
                        nargs="?")
    parser.add_argument("-n", "--null",
                        help="""Comma-separated list of null values (default:
                        '0').""",
                        nargs="?", default="0")
    parser.add_argument("-a", "--auto-relabel",
                        help="""Automatically relabel class labels in the order
                        in which they appear in the CSV file.""",
                        action="store_true")
    parser.add_argument("-m", "--multiclass",
                        help="""Indicates more than two classes; will start
                        counting at 1 instead of 0.""",
                        action="store_true")
    parser.add_argument("-+", "--minus-plus-one",
                        help="""Instead of relabeling to integers, relabel to
                        '-1' and '+1'. Needed when using VW with logistic or
                        hinge loss.""", action="store_true")
    parser.add_argument("-i", "--ignore-extra-classes",
                        help="""If there are more than two classes found, when
                        not using --multiclass, include the example with no
                        label instead of giving skipping it.""",
                        action="store_true")
    parser.add_argument("-t", "--tag",
                        help="""Name of column that contains the tags.""")

    args = parser.parse_args()

    auto_relabel = args.auto_relabel
    label_column = args.label
    tag_column = args.tag
    null_values = args.null.split(",")
    multiclass = args.multiclass
    minus_plus_one = args.minus_plus_one

    if minus_plus_one:
        new_classes = iter(["-1", "+1"])
    elif multiclass:
        new_classes = (str(i) for i in itertools.count(1))
    elif args.classes or auto_relabel:
        new_classes = iter(["0", "1"])
    else:
        new_classes = None

    if args.classes:
        old_classes = args.classes.split(",")
        relabel = dict(zip(old_classes, new_classes))
    else:
        relabel = dict()

    reader = csv.DictReader(args.file, delimiter=args.delimiter)
    try:
        for row in reader:
            label = row.pop(label_column, "")
            tag = row.pop(tag_column, "")

            if auto_relabel or new_classes:
                if auto_relabel:
                    if label not in relabel:
                        try:
                            relabel[label] = next(new_classes)
                        except StopIteration:
                            if args.ignore_extra_classes:
                                relabel[label] = ""
                            else:
                                stderr.write("Found too many different classes;"
                                             " skipping example. Use "
                                             "--multiclass or "
                                             "--ignore-extra-classes.\n")
                                continue
                label = relabel[label]

            features = " ".join([k + ":" + v for k, v in sorted(row.items())
                                 if v not in null_values])
            line = label + " " + tag + "| " + features + "\n"
            stdout.write(line)
            stdout.flush()
    except (IOError, KeyboardInterrupt, BrokenPipeError):
        stderr.close()