Runs snort against the supplied pcap.

        :param pcap: Filepath to pcap file to scan
        :returns: tuple of version, list of alerts
186484
def run(self, pcap):
        
        proc = Popen(self._snort_cmd(pcap), stdout=PIPE,
                     stderr=PIPE, universal_newlines=True)
        stdout, stderr = proc.communicate()
        if proc.returncode != 0:
            raise Exception("\n".join(["Execution failed return code: {0}" \
                                .format(proc.returncode), stderr or ""]))

        return (parse_version(stderr),
                [ x for x in parse_alert(stdout) ])
1130834
def Run(self, args):
    """"""
    with vfs.VFSOpen(
        args.pathspec, progress_callback=self.Progress) as file_obj:
      fingerprinter = Fingerprinter(self.Progress, file_obj)
      response = rdf_client_action.FingerprintResponse()
      response.pathspec = file_obj.pathspec
      if args.tuples:
        tuples = args.tuples
      else:
        # There are none selected -- we will cover everything
        tuples = list()
        for k in self._fingerprint_types:
          tuples.append(rdf_client_action.FingerprintTuple(fp_type=k))

      for finger in tuples:
        hashers = [self._hash_types[h] for h in finger.hashers] or None
        if finger.fp_type in self._fingerprint_types:
          invoke = self._fingerprint_types[finger.fp_type]
          res = invoke(fingerprinter, hashers)
          if res:
            response.matching_types.append(finger.fp_type)
        else:
          raise RuntimeError(
              "Encountered unknown fingerprint type. %s" % finger.fp_type)

      # Structure of the results is a list of dicts, each containing the
      # name of the hashing method, hashes for enabled hash algorithms,
      # and auxilliary data where present (e.g. signature blobs).
      # Also see Fingerprint:HashIt()
      response.results = fingerprinter.HashIt()

      # We now return data in a more structured form.
      for result in response.results:
        if result.GetItem("name") == "generic":
          for hash_type in ["md5", "sha1", "sha256"]:
            value = result.GetItem(hash_type)
            if value is not None:
              setattr(response.hash, hash_type, value)

        if result["name"] == "pecoff":
          for hash_type in ["md5", "sha1", "sha256"]:
            value = result.GetItem(hash_type)
            if value:
              setattr(response.hash, "pecoff_" + hash_type, value)

          signed_data = result.GetItem("SignedData", [])
          for data in signed_data:
            response.hash.signed_data.Append(
                revision=data[0], cert_type=data[1], certificate=data[2])

      self.SendReply(response)
1058950
def pull(self):
        """"""

        # For each packet in the pcap process the contents
        for item in self.input_stream:

            # Print out the timestamp in UTC
            print('%s -' % item['timestamp'], end='')

            # Transport info
            if item['transport']:
                print(item['transport']['type'], end='')

            # Print out the Packet info
            packet_type = item['packet']['type']
            print(packet_type, end='')
            packet = item['packet']
            if packet_type in ['IP', 'IP6']:
                # Is there domain info?
                if 'src_domain' in packet:
                    print('%s(%s) --> %s(%s)' % (net_utils.inet_to_str(packet['src']), packet['src_domain'],
                                                 net_utils.inet_to_str(packet['dst']), packet['dst_domain']), end='')
                else:
                    print('%s --> %s' % (net_utils.inet_to_str(packet['src']), net_utils.inet_to_str(packet['dst'])), end='')
            else:
                print(str(packet))

            # Only include application if we have it
            if item['application']:
                print('Application: %s' % item['application']['type'], end='')
                print(str(item['application']), end='')

            # Just for newline
            print()
477681
def versioned_static(file_path):
    

    full_path = find(file_path)
    url = static(file_path)

    if type(full_path) is list and len(full_path) > 0:
        full_path = full_path[0]

    if not full_path:
        msg = 'Could not find static file: {0}'.format(file_path)
        logger.warning(msg)
        return url

    # Use MD5 as we care about speed a lot
    # and not security in this case
    file_hash = md5()
    with open(full_path, "rb") as file_contents:
        for chunk in iter(lambda: file_contents.read(4096), b""):
            file_hash.update(chunk)

    return url + '?v=' + file_hash.hexdigest()[:7]
1019014
def index(in_cram, config):
    
    out_file = in_cram + ".crai"
    if not utils.file_uptodate(out_file, in_cram):
        with file_transaction(config, in_cram + ".crai") as tx_out_file:
            tx_in_file = os.path.splitext(tx_out_file)[0]
            utils.symlink_plus(in_cram, tx_in_file)
            cmd = "samtools index {tx_in_file}"
            do.run(cmd.format(**locals()), "Index CRAM file")
    return out_file
120746
def run():
    """"""

    # Use print() instead of logging when running in CLI mode:
    set_pyftpsync_logger(None)

    parser = argparse.ArgumentParser(
        description="Synchronize folders over FTP.",
        epilog="See also https://github.com/mar10/pyftpsync",
        parents=[verbose_parser],
    )

    # Note: we want to allow --version to be combined with --verbose. However
    # on Py2, argparse makes sub-commands mandatory, unless `action="version"` is used.
    if check_cli_verbose(3) > 3:
        version_info = "pyftpsync/{} Python/{} {}".format(
            __version__, PYTHON_VERSION, platform.platform()
        )
    else:
        version_info = "{}".format(__version__)

    parser.add_argument("-V", "--version", action="version", version=version_info)

    subparsers = parser.add_subparsers(help="sub-command help")

    # --- Create the parser for the "upload" command ---------------------------

    sp = subparsers.add_parser(
        "upload",
        parents=[verbose_parser, common_parser, matcher_parser, creds_parser],
        help="copy new and modified files to remote folder",
    )

    sp.add_argument(
        "local",
        metavar="LOCAL",
        default=".",
        help="path to local folder (default: %(default)s)",
    )
    sp.add_argument("remote", metavar="REMOTE", help="path to remote folder")
    sp.add_argument(
        "--force",
        action="store_true",
        help="overwrite remote files, even if the target is newer "
        "(but no conflict was detected)",
    )
    sp.add_argument(
        "--resolve",
        default="ask",
        choices=["local", "skip", "ask"],
        help="conflict resolving strategy (default: '%(default)s')",
    )
    sp.add_argument(
        "--delete",
        action="store_true",
        help="remove remote files if they don't exist locally",
    )
    sp.add_argument(
        "--delete-unmatched",
        action="store_true",
        help="remove remote files if they don't exist locally "
        "or don't match the current filter (implies '--delete' option)",
    )

    sp.set_defaults(command="upload")

    # --- Create the parser for the "download" command -------------------------

    sp = subparsers.add_parser(
        "download",
        parents=[verbose_parser, common_parser, matcher_parser, creds_parser],
        help="copy new and modified files from remote folder to local target",
    )

    sp.add_argument(
        "local",
        metavar="LOCAL",
        default=".",
        help="path to local folder (default: %(default)s)",
    )
    sp.add_argument("remote", metavar="REMOTE", help="path to remote folder")
    sp.add_argument(
        "--force",
        action="store_true",
        help="overwrite local files, even if the target is newer "
        "(but no conflict was detected)",
    )
    sp.add_argument(
        "--resolve",
        default="ask",
        choices=["remote", "skip", "ask"],
        help="conflict resolving strategy (default: '%(default)s')",
    )
    sp.add_argument(
        "--delete",
        action="store_true",
        help="remove local files if they don't exist on remote target",
    )
    sp.add_argument(
        "--delete-unmatched",
        action="store_true",
        help="remove local files if they don't exist on remote target "
        "or don't match the current filter (implies '--delete' option)",
    )

    sp.set_defaults(command="download")

    # --- Create the parser for the "sync" command -----------------------------

    sp = subparsers.add_parser(
        "sync",
        parents=[verbose_parser, common_parser, matcher_parser, creds_parser],
        help="synchronize new and modified files between remote folder and local target",
    )

    sp.add_argument(
        "local",
        metavar="LOCAL",
        default=".",
        help="path to local folder (default: %(default)s)",
    )
    sp.add_argument("remote", metavar="REMOTE", help="path to remote folder")
    sp.add_argument(
        "--resolve",
        default="ask",
        choices=["old", "new", "local", "remote", "skip", "ask"],
        help="conflict resolving strategy (default: '%(default)s')",
    )

    sp.set_defaults(command="sync")

    # --- Create the parser for the "run" command -----------------------------

    add_run_parser(subparsers)

    # --- Create the parser for the "scan" command -----------------------------

    add_scan_parser(subparsers)

    # --- Parse command line ---------------------------------------------------

    args = parser.parse_args()

    args.verbose -= args.quiet
    del args.quiet

    # print("verbose", args.verbose)

    ftp_debug = 0
    if args.verbose >= 6:
        ftp_debug = 1

    # Modify the `args` from the `pyftpsync.yaml` config:
    if getattr(args, "command", None) == "run":
        handle_run_command(parser, args)

    if callable(getattr(args, "command", None)):
        # scan_handler
        try:
            return args.command(parser, args)
        except KeyboardInterrupt:
            print("\nAborted by user.", file=sys.stderr)
            sys.exit(3)

    elif not hasattr(args, "command"):
        parser.error(
            "missing command (choose from 'upload', 'download', 'run', 'sync', 'scan')"
        )

    # Post-process and check arguments
    if hasattr(args, "delete_unmatched") and args.delete_unmatched:
        args.delete = True

    args.local_target = make_target(args.local, {"ftp_debug": ftp_debug})

    if args.remote == ".":
        parser.error("'.' is expected to be the local target (not remote)")
    args.remote_target = make_target(args.remote, {"ftp_debug": ftp_debug})
    if not isinstance(args.local_target, FsTarget) and isinstance(
        args.remote_target, FsTarget
    ):
        parser.error("a file system target is expected to be local")

    # Let the command handler do its thing
    opts = namespace_to_dict(args)
    if args.command == "upload":
        s = UploadSynchronizer(args.local_target, args.remote_target, opts)
    elif args.command == "download":
        s = DownloadSynchronizer(args.local_target, args.remote_target, opts)
    elif args.command == "sync":
        s = BiDirSynchronizer(args.local_target, args.remote_target, opts)
    else:
        parser.error("unknown command '{}'".format(args.command))

    s.is_script = True

    try:
        s.run()
    except KeyboardInterrupt:
        print("\nAborted by user.", file=sys.stderr)
        sys.exit(3)
    finally:
        # Prevent sporadic exceptions in ftplib, when closing in __del__
        s.local.close()
        s.remote.close()

    stats = s.get_stats()
    if args.verbose >= 5:
        pprint(stats)
    elif args.verbose >= 1:
        if args.dry_run:
            print("(DRY-RUN) ", end="")
        print(
            "Wrote {}/{} files in {} directories, skipped: {}.".format(
                stats["files_written"],
                stats["local_files"],
                stats["local_dirs"],
                stats["conflict_files_skipped"],
            ),
            end="",
        )
        if stats["interactive_ask"]:
            print()
        else:
            print(" Elap: {}.".format(stats["elap_str"]))

    return
570315
def RunSphinxAPIDoc(_):
  """"""
  current_directory = os.path.abspath(os.path.dirname(__file__))
  module = os.path.join(current_directory, '..', 'plaso')
  api_directory = os.path.join(current_directory, 'sources', 'api')
  apidoc.main(['-o', api_directory, module, '--force'])
33101
def _get_version(prog, path):
        ''''''
        assert prog in prog_to_version_cmd
        args, regex = prog_to_version_cmd[prog]
        cmd = path + ' ' + args
        if prog == 'spades':
            cmd_output = subprocess.Popen(['python3', path, args], shell=False, stdout=subprocess.PIPE,
                                          stderr=subprocess.PIPE).communicate()
        else:
            cmd_output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

        cmd_output = common.decode(cmd_output[0]).split('\n')[:-1] + common.decode(cmd_output[1]).split('\n')[:-1]

        for line in cmd_output:
            hits = regex.search(line)
            if hits:
                return True, hits.group(1)

        return False, 'I tried to get the version of ' + prog + ' with: "' + cmd + '" and the output didn\'t match this regular expression: "' + regex.pattern + '"'
1161955
def run(self):
        
        for p in self._stats:
            # End of the thread has been asked
            if self.stopped():
                break
            # Scan a port (ICMP or TCP)
            if 'port' in p:
                self._port_scan(p)
                # Had to wait between two scans
                # If not, result are not ok
                time.sleep(1)
            # Scan an URL
            elif 'url' in p and requests_tag:
                self._web_scan(p)
1045487
def _check_ubridge_version(self, env=None):
        
        try:
            output = yield from subprocess_check_output(self._path, "-v", cwd=self._working_dir, env=env)
            match = re.search("ubridge version ([0-9a-z\.]+)", output)
            if match:
                self._version = match.group(1)
                if sys.platform.startswith("win") or sys.platform.startswith("darwin"):
                    minimum_required_version = "0.9.12"
                else:
                    # uBridge version 0.9.14 is required for packet filters 
                    # to work for IOU nodes.
                    minimum_required_version = "0.9.14"
                if parse_version(self._version) < parse_version(minimum_required_version):
                    raise UbridgeError("uBridge executable version must be >= {}".format(minimum_required_version))
            else:
                raise UbridgeError("Could not determine uBridge version for {}".format(self._path))
        except (OSError, subprocess.SubprocessError) as e:
            raise UbridgeError("Error while looking for uBridge version: {}".format(e))
643070
def pypi():
    ''''''
    if not query_yes_no('version updated in '
                        '`fabsetup/_version.py`?'):
        print('abort')
    else:
        print(cyan('\n## clean-up\n'))
        execute(clean)

        basedir = dirname(__file__)

        # latest_pythons = _determine_latest_pythons()
        # # e.g. highest_minor: '3.6'
        # highest_minor = _highest_minor(latest_pythons)
        # python = flo('python{highest_minor}')
        python = 'python'

        print(cyan('\n## build package'))
        local(flo('cd {basedir}  &&  {python}  setup.py  sdist'))

        print(cyan('\n## upload package'))
        local(flo('cd {basedir}  &&  {python} -m twine upload  dist/*'))
896274
def _make_pcaps(self):
        '''
        
        '''
        self._pcaps = {}
        for devname,intf in self._devinfo.items():
            if intf.iftype == InterfaceType.Loopback:
                senddev = _RawSocket(devname, protocol=IPProtocol.UDP)
                self._localsend[devname] = senddev
            pdev = PcapLiveDevice(devname) 
            self._pcaps[devname] = pdev
25468
def run(self):
        """"""
        while (self._run_thread):
            (mode, version, packet) = self._read_packet()
            if (mode == PROP_PACKET_SENSE):
                self._decode_sense_packet(version, packet)
            elif (mode == PROP_PACKET_CTRL):
                self._decode_ctrl_packet(version, packet)
1134949
def py3(check):
    

    if check in get_valid_checks():
        path_to_module = os.path.dirname(get_version_file(check))
    else:
        path_to_module = check

    if not os.path.exists(path_to_module):
        abort(u"{} does not exist.".format(path_to_module))

    echo_info(u"Validating python3 compatibility of {}...".format(check))
    results = validate_py3(path_to_module)

    if results:
        echo_failure(u"Incompatibilities were found for {}:".format(check))
        current_path = None
        for problem in sorted(results, key=itemgetter("path")):
            # validate_py3 returns an array a dicts like
            # {
            #     "message": "Line 23, Column 8: Calling a dict.iter*() method",
            #     "file": "/path/to/file.py",
            # }
            path = problem["path"]
            if current_path is None or path != current_path:
                echo_info(u"File {}:".format(path))
            echo_failure("  {}".format(problem["message"]))
            current_path = path
        abort()
    else:
        echo_success(u"{} is compatible with python3".format(check))
1200275
def execute(self, eopatch):
        
        data_arr = eopatch[FeatureType.MASK]['IS_DATA']
        _, height, width, _ = data_arr.shape

        request = self._get_wms_request(eopatch.bbox, width, height)

        request_data, = np.asarray(request.get_data())

        if isinstance(self.raster_value, dict):
            raster = self._map_from_multiclass(eopatch, (height, width), request_data)
        elif isinstance(self.raster_value, (int, float)):
            raster = self._map_from_binaries(eopatch, (height, width), request_data)
        else:
            raise ValueError("Unsupported raster value type")

        if self.feature_type is FeatureType.MASK_TIMELESS and raster.ndim == 2:
            raster = raster[..., np.newaxis]

        eopatch[self.feature_type][self.feature_name] = raster

        return eopatch
1067150
def main():
    
    opts, argparser = get_args()

    start_time = time()

    url = opts.url
    if re.search(r":([0-9]+)$", opts.url):
        if opts.listenerPort is not None:
            argparser.error('Simultaneous url with port and -p port option '
                            'invalid')
    else:
        if opts.listenerPort is None:
            url = '%s:%s' % (opts.url, 5000)
        else:
            url = '%s:%s' % (opts.url, opts.listenerPort)

    if opts.verbose:
        print('url=%s' % url)

    cim_protocol_version = '1.4'

    # requests module combines the verification flag and certfile attribute
    # If verify=False, there is no verification of the server cert. If
    # verify=<file_name or dir name> it is the directory of the cert to
    # use for verification.
    verification = False if opts.cert_file is None else opts.cert_file

    headers = {'content-type' : 'application/xml; charset=utf-8',
               'CIMExport' : 'MethodRequest',
               'CIMExportMethod' : 'ExportIndication',
               'Accept-Encoding' : 'Identity',
               'CIMProtocolVersion' : cim_protocol_version}
    # includes accept-encoding because of requests issue.  He supplies it if
    # we don't TODO try None

    delta_time = time() - start_time
    rand_base = randint(1, 1000)
    timer = ElapsedTimer()
    source_id = 'send_indications.py'
    for i in range(opts.deliver):

        msg_id = '%s' % (i + rand_base)
        payload = create_indication_data(msg_id, i, source_id, delta_time,
                                         cim_protocol_version)

        if opts.verbose:
            print('headers=%s\n\npayload=%s' % (headers, payload))

        success = send_indication(url, headers, payload, opts.verbose,
                                  verify=verification)

        if success:
            if opts.verbose:
                print('sent # %s' % i)
            else:
                if i % 100 == 0:
                    sys.stdout.write('.')
                    sys.stdout.flush()
        else:
            print('Error return from send. Terminating.')
            return
    endtime = timer.elapsed_sec()
    print('Sent %s in %s sec or %.2f ind/sec' % (opts.deliver, endtime,
                                                 (opts.deliver/endtime)))
1052828
def _start_streaming_pcap(self):
        

        if os.path.exists(self.capture_file_path):
            try:
                os.remove(self.capture_file_path)
            except OSError as e:
                raise aiohttp.web.HTTPConflict(text="Could not delete old capture file '{}': {}".format(self.capture_file_path, e))

        try:
            stream_content = yield from self.read_pcap_from_source()
        except aiohttp.web.HTTPException as e:
            error_msg = "Could not stream PCAP file: error {}: {}".format(e.status, e.text)
            log.error(error_msg)
            self._capturing = False
            self._project.notification.emit("log.error", {"message": error_msg})
            self._project.controller.notification.emit("link.updated", self.__json__())

        with stream_content as stream:
            try:
                with open(self.capture_file_path, "wb") as f:
                    while self._capturing:
                        # We read 1 bytes by 1 otherwise the remaining data is not read if the traffic stops
                        data = yield from stream.read(1)
                        if data:
                            f.write(data)
                            # Flush to disk otherwise the live is not really live
                            f.flush()
                        else:
                            break
            except OSError as e:
                raise aiohttp.web.HTTPConflict(text="Could not write capture file '{}': {}".format(self.capture_file_path, e))