def sanitise_report(report_file, semaphore):
    match = re.search("^" + re.escape(settings.reports_directory) + "(.*)", report_file)

    # read report file
    report = Report(report_file)
    report_header = report.header
    report_header["report_file"] = match.group(1)

    report_filename = os.path.split(report_file)[-1]
    report_filename_sanitised = os.path.join(settings.sanitised_directory, report_filename)

    if os.path.isfile(report_filename_sanitised):
        log.info("Sanitised report name already exists, overwriting: %s" % report_filename_sanitised)
    else:
        log.info("New report file: %s" % report_filename_sanitised)

    report_file_sanitised = open(report_filename_sanitised, "w")

    safe_dump(report_header, report_file_sanitised, explicit_start=True, explicit_end=True)

    safe_dump_all(report, report_file_sanitised, explicit_start=True, explicit_end=True, default_flow_style=False)

    log.info("Moving original unsanitised file %s to archive" % report_file)

    archive_report(report_file)

    report_file_sanitised.close()
    report.close()

    os.remove(report_file)

    semaphore.release()
def main(archive_file):
    if not os.path.isfile(archive_file):
        print "Archive file does not exist: " + archive_file
        return 1

    # we only support tar.gz and .gz
    if archive_file.endswith("tar.gz"):
        archive_tar = tarfile.open(archive_file)
        for element in archive_tar:
            f = archive_tar.extractfile(element)
            fp, report_file = tempfile.mkstemp()
            while True:
                data = f.read()
                if not data:
                    break
                os.write(fp, data)
            f.close()
    elif archive_file.endswith(".gz"):
        fp, report_file = tempfile.mkstemp()
        f = gzip.open(archive_file, 'rb')
        while True:
            data = f.read()
            if not data:
                break
            os.write(fp, data)
        f.close()

    report = Report(report_file, action="sanitise")
    report_filename = generate_filename(report.header)
    report_filename_sanitised = os.path.join(settings.sanitised_directory,
                                             report_filename)
    report.header['report_file'] = "%s/%s" % (report.header['probe_cc'],
                                              report_filename)

    report_file_sanitised = open(report_filename_sanitised, "w")

    safe_dump(
        report.header,
        report_file_sanitised,
        explicit_start=True,
        explicit_end=True)
    safe_dump_all(
        report,
        report_file_sanitised,
        explicit_start=True,
        explicit_end=True,
        default_flow_style=False)
    delete_existing_report_entries(report.header)

    public_report_file = os.path.join(
        settings.public_directory, report.header['probe_cc'], report_filename)
    if os.path.isfile(public_report_file):
        os.remove(public_report_file)

    report_file_sanitised.close()
    os.remove(report_file)
def main(archive_file):
    if not os.path.isfile(archive_file):
        print "Archive file does not exist: " + archive_file
        return 1

    # we only support tar.gz and .gz
    if archive_file.endswith("tar.gz"):
        archive_tar = tarfile.open(archive_file)
        for element in archive_tar:
            f = archive_tar.extractfile(element)
            fp, report_file = tempfile.mkstemp()
            while True:
                data = f.read()
                if not data:
                    break
                os.write(fp, data)
            f.close()
    elif archive_file.endswith(".gz"):
        fp, report_file = tempfile.mkstemp()
        f = gzip.open(archive_file, 'rb')
        while True:
            data = f.read()
            if not data:
                break
            os.write(fp, data)
        f.close()

    report = Report(report_file, action="sanitise")
    report_filename = generate_filename(report.header)
    report_filename_sanitised = os.path.join(settings.sanitised_directory,
                                             report_filename)
    report.header['report_file'] = "%s/%s" % (report.header['probe_cc'],
                                              report_filename)

    report_file_sanitised = open(report_filename_sanitised, "w")

    safe_dump(report.header,
              report_file_sanitised,
              explicit_start=True,
              explicit_end=True)
    safe_dump_all(report,
                  report_file_sanitised,
                  explicit_start=True,
                  explicit_end=True,
                  default_flow_style=False)
    delete_existing_report_entries(report.header)

    public_report_file = os.path.join(settings.public_directory,
                                      report.header['probe_cc'],
                                      report_filename)
    if os.path.isfile(public_report_file):
        os.remove(public_report_file)

    report_file_sanitised.close()
    os.remove(report_file)
Example #4
0
def sanitise_report(report_file, semaphore):
    match = re.search("^" + re.escape(settings.reports_directory) + "(.*)",
                      report_file)

    # read report file
    report = Report(report_file)
    report_header = report.header
    report_header['report_file'] = match.group(1)

    report_filename = os.path.split(report_file)[-1]
    report_filename_sanitised = os.path.join(settings.sanitised_directory,
                                             report_filename)

    if os.path.isfile(report_filename_sanitised):
        log.info("Sanitised report name already exists, overwriting: %s" %
                 report_filename_sanitised)
    else:
        log.info("New report file: %s" % report_filename_sanitised)

    report_file_sanitised = open(report_filename_sanitised, 'w')

    safe_dump(report_header,
              report_file_sanitised,
              explicit_start=True,
              explicit_end=True)

    safe_dump_all(report,
                  report_file_sanitised,
                  explicit_start=True,
                  explicit_end=True,
                  default_flow_style=False)

    log.info("Moving original unsanitised file %s to archive" % report_file)

    archive_report(report_file)

    report_file_sanitised.close()
    report.close()

    os.remove(report_file)

    semaphore.release()