Esempio n. 1
0
def grep_count(file_path, to_match, additional_flags=None, fixed_mode=True, starts_with=False):
    '''
        This uses grep for fast counting of strings in a file
    '''
    if not os.path.isfile(file_path) or os.path.getsize(file_path)==0:
        return 0

    env = os.environ.copy()
    env['LC_ALL'] = 'C' #use C locale rather than UTF8 for faster grep

    cmd = ["grep"]
    # '-c' returns the match count
    cmd.append("-c")
    if additional_flags:
        cmd.extend(additional_flags)

    # fixed mode cannot be used with starts_with, since it does not match regular expressions
    # only add the fixed_mode flag if we're not using starts_with
    if not starts_with:
        if fixed_mode:
            cmd.append("-F")
        cmd.append(to_match)
    else:
        cmd.append("^"+to_match)

    cmd.append(file_path)

    number_of_seqs = util.misc.run_and_print(cmd, silent=False, check=True, env=env)
    return int(number_of_seqs.stdout.decode("utf-8").rstrip(os.linesep))
Esempio n. 2
0
def grep_count(file_path, to_match, additional_flags=None, fixed_mode=True, starts_with=False):
    '''
        This uses grep for fast counting of strings in a file
    '''
    if not os.path.isfile(file_path) or os.path.getsize(file_path)==0:
        return 0

    env = os.environ.copy()
    env['LC_ALL'] = 'C' #use C locale rather than UTF8 for faster grep

    cmd = ["grep"]
    # '-c' returns the match count
    cmd.append("-c")
    if additional_flags:
        cmd.extend(additional_flags)

    # fixed mode cannot be used with starts_with, since it does not match regular expressions
    # only add the fixed_mode flag if we're not using starts_with
    if not starts_with:
        if fixed_mode:
            cmd.append("-F")
        cmd.append(to_match)
    else:
        cmd.append("^"+to_match)

    cmd.append(file_path)

    number_of_seqs = util.misc.run_and_print(cmd, silent=False, check=True, env=env)
    return int(number_of_seqs.stdout.decode("utf-8").rstrip(os.linesep))
Esempio n. 3
0
def _netstat(tcp, udp, ipv4, ipv6, listen):
	cmd = ["netstat", "-Wpn"]
	if tcp:
		cmd.append("-t")
	if udp:
		cmd.append("-u")
	if ipv4:
		cmd.append("-4")
	if ipv6:
		cmd.append("-6")
	if listen:
		cmd.append("-l")
	try:
		res = run(cmd)
	except CommandError, exc:
		raise NetstatError(NetstatError.CODE_EXECUTE, "Failed to execute netstat", {"cmd": cmd, "error": exc})
Esempio n. 4
0
def _netstat(tcp, udp, ipv4, ipv6, listen):
    cmd = ["netstat", "-Wpn"]
    if tcp:
        cmd.append("-t")
    if udp:
        cmd.append("-u")
    if ipv4:
        cmd.append("-4")
    if ipv6:
        cmd.append("-6")
    if listen:
        cmd.append("-l")
    try:
        res = run(cmd)
    except CommandError, exc:
        raise NetstatError(NetstatError.CODE_EXECUTE,
                           "Failed to execute netstat", {
                               "cmd": cmd,
                               "error": exc
                           })