def archive_scan(archive, scan_type, f_name, arc_type): """ Scans each file in an archive. This function take advantage of the fact that the tarfile and zipfile Python packages have identical method names. """ arc_scan = False if not f_name: sys.stderr.write("ERROR: Must specify an output file") archive.close() exit(1) else: f = open(f_name, 'w') if arc_type: if arc_type == "t": arc_scan = archive.getnames() elif arc_type == "z": arc_scan = archive.namelist() for name in arc_scan: path = paths.TEMP_ARCHIVE_UNPACK_PATH + "/" + name #Scan types can be n (Ninka) or f (FOSSology) if scan_type is 'n': f.write(str(ninka_scan(path))) elif scan_type is 'f': f.write(str(foss_scan(path)))
def archive_scan(archive, scantype, f_name): # scan types can be n (ninka) or f (fossology) if not f_name: print("Error: Must specify an output file") archive.close() exit(1) else: f = open(f_name, "w") for name in archive.getnames(): path = paths.TEMP_ARCHIVE_UNPACK_PATH + "/" + name if scantype is "n": f.write(ninka_scan(path)) elif scantype is "f": f.write(foss_scan(path))
def archive_scan(archive, scan_type, f_name, arc_type): """ Scans each file in an archive. This function take advantage of the fact that the tarfile and zipfile Python packages have identical method names. 04/25/2014: Added a SHA-1 checksum generating "scanner" to this method """ arc_scan = False if not f_name: sys.stderr.write("ERROR: Must specify an output file") archive.close() exit(1) else: f = open(f_name, 'w') if arc_type: if arc_type == "t": arc_scan = archive.getnames() elif arc_type == "z": arc_scan = archive.namelist() for name in arc_scan: path = paths.TEMP_ARCHIVE_UNPACK_PATH + "/" + name #Scan types can be n (Ninka) or f (FOSSology) if scan_type is 'n': f.write(str(ninka_scan(path))) elif scan_type is 'f': f.write(str(foss_scan(path))) elif scan_type is 'c': #checksum now included #make sure it's not a directory #(dirs cause the SHA-1 reader to die) if not os.path.isdir(path): checksumstr = str(name) + ";" + str(_hash_file(path)) checksumstr += "\n" else: checksumstr = str(name) + ";" + "ERROR" + "\n" f.write(checksumstr)
def run_scans(target): print("creating directories needed for the scanners to work") """ Checks to see if the paths we need are there If they are not, it creates them (the archive path is meant for destruction, so it is assumed that it should not be there unless something went wrong) """ if not os.path.isdir(paths.SCANNER_OUTPUT_PATH): subprocess.call(["mkdir", paths.SCANNER_OUTPUT_PATH]) if not os.path.isdir(paths.TEMP_ARCHIVE_UNPACK_PATH): subprocess.call(["mkdir", paths.TEMP_ARCHIVE_UNPACK_PATH]) else: subprocess.call(["rm", paths.TEMP_ARCHIVE_UNPACK_PATH + "/*.*"]) # To ensure the absolute path is not a part of the output file name out_name = get_file_from_absolute_path(target) ninka_out = paths.SCANNER_OUTPUT_PATH + "/" ninka_out += out_name + ".N_out.txt" check_file(ninka_out) foss_out = paths.SCANNER_OUTPUT_PATH + "/" foss_out += out_name + ".F_out.txt" check_file(foss_out) print("checking file format") if tarfile.is_tarfile(target): print(target + " identified as TAR file") archive = tarfile.open(target) print("extracting data") archive.extractall(paths.TEMP_ARCHIVE_UNPACK_PATH) print("starting ninka scan") archive_scan(archive, "n", ninka_out) print("ninka scan finished") print("starting fossology scan") archive_scan(archive, "f", foss_out) print("fossology scan finished") archive.close() # This is the same as the tarfile method but with zipfile elif zipfile.is_zipfile(target): print(target + " identified as ZIP file") archive = zipfile.open(target) print("extracting data") archive.extractall(paths.TEMP_ARCHIVE_UNPACK_PATH) print("starting ninka scan") archive.scan(archive, "n", ninka_out) print("ninka scan finished") print("starting fossology scan") archive.scan(archive, "f", foss_out) print("fossology scan finished") archive.close() else: # assumes a single file print("file is either not an archive or an unrecognized format") path = paths.TEMP_ARCHIVE_UNPACK_PATH + "/" + target subprocess.call(["cp", target, paths.TEMP_ARCHIVE_UNPACK_PATH]) # first with ninka n_file = open(ninka_out, "w") print("starting ninka scan") n_file.write(ninka_scan(path)) print("ninka scan fnished") n_file.close() # next with fossology f_file = open(foss_out, "w") print("starting fossology scan") f_file.write(foss_scan(path)) print("fossology scan finished") f_file.close() # clean() """
def run_scans(target, opts): verbose = False #Checks to see if any options are activated if opts: if opts == "-v": verbose = True """ Runs both FOSSology and Ninka on a given file or package. """ #print("Creating directories needed for the scanners to work") """ Checks to see if the paths we need are there If they are not, it creates them (the archive path is meant for destruction, so it is assumed that it should not be there unless something went wrong) """ if not os.path.isdir(paths.SCANNER_OUTPUT_PATH): subprocess.call(["mkdir", paths.SCANNER_OUTPUT_PATH]) if not os.path.isdir(paths.TEMP_ARCHIVE_UNPACK_PATH): subprocess.call(["mkdir", paths.TEMP_ARCHIVE_UNPACK_PATH]) else: subprocess.call(["rm", paths.TEMP_ARCHIVE_UNPACK_PATH + "/*.*"]) #To ensure the absolute path is not a part of the output file name out_name = get_file_from_absolute_path(target) ninka_out = paths.SCANNER_OUTPUT_PATH + "/" ninka_out += out_name + ".N_out.txt" check_file(ninka_out) foss_out = paths.SCANNER_OUTPUT_PATH + "/" foss_out += out_name + ".F_out.txt" check_file(foss_out) if verbose: print("Checking file format") if tarfile.is_tarfile(target): if verbose: print(target + " identified as TAR file") archive = tarfile.open(target) if verbose: print("Extracting data") archive.extractall(paths.TEMP_ARCHIVE_UNPACK_PATH) if verbose: print("Starting Ninka scan") archive_scan(archive, 'n', ninka_out, "t") if verbose: print("Ninka scan finished") print("Starting FOSSology scan") archive_scan(archive, 'f', foss_out, "t") if verbose: print("FOSSology scan finished") archive.close() #This is the same as the tarfile method but with zipfile elif zipfile.is_zipfile(target): if verbose: print(target + " identified as ZIP file") #archive = zipfile.open(target) archive = zipfile.ZipFile(target, "r") if verbose: print("Extracting data") archive.extractall(paths.TEMP_ARCHIVE_UNPACK_PATH) if verbose: print("Starting Ninka scan") archive_scan(archive,'n', ninka_out, "z") if verbose: print("Ninka scan finished") print("Starting FOSSology scan") archive_scan(archive,'f', foss_out, "z") if verbose: print("FOSSology scan finished") archive.close() else: #assumes a single file if verbose: print("File is either not an archive or an unrecognized format;" " scanning as single file") path = paths.TEMP_ARCHIVE_UNPACK_PATH + "/" + out_name subprocess.call(["cp", target, paths.TEMP_ARCHIVE_UNPACK_PATH]) #first with Ninka n_file = open(ninka_out, 'wb') if verbose: print("Starting Ninka scan") n_file.write(ninka_scan(path)) if verbose: print("Ninka scan fnished") n_file.close() #next with FOSSology f_file = open(foss_out, 'w') if verbose: print("Starting FOSSology scan") f_file.write(foss_scan(path)) if verbose: print("FOSSology scan finished") f_file.close() """