Esempio n. 1
0
def _main( args ):
	"""
	Aggregate JSON into a Matrix then call the Matrix' analyze method.
	This function allows 
	"""
	import os.path
	import json
	import bdqc.dir
	from bdqc.statpath import selectors

	statistic_filters = {}
	if args.use:
		statistic_filters["include"] = selectors(args.use )
	if args.ignore:
		statistic_filters["exclude"] = selectors(args.ignore)
	m = Matrix( **statistic_filters )
	for s in args.sources:
		if os.path.isdir( s ):
			# Look for "*.bdqc" files under "s/" each of which contains
			# *one* file's analysis as a single JSON object.
			# dir.walk calls a visitor with the filename
			bdqc.dir.walk( s, args.depth, args.include, args.exclude, 
				_Loader( m ) )
		elif isfile( s ):
			# s is assumed to contain a ("pre") aggregated collection of analyses
			# of multiple files.
			with open(s) as fp:
				for filename,content in json.load( fp ).items():
					m.add_file_data( filename, content )
		else:
			raise RuntimeError( "{} is neither file nor directory".format(s) )

	m.analyze()

	if m.status: # ...is other than STATUS_NO_OUTLIERS
		if args.report:
			with open(args.report,"w") as fp:
				if args.report.lower().endswith("html"):
					m.summary().render_html( fp )
				else:
					m.summary().render_text( fp )
	if args.dump:
		with open(args.dump,"w") as fp:
			m.dump( fp )
Esempio n. 2
0
def _main( args ):

	# Build lists of plugins...

	plugins = []
	# ...maybe including defaults...

	# check if plugins.txt exists in expected path and handle if not
	plugins_textfile_message= "plugins.txt is missing! Please report and add to {0} manually".format(DEFAULT_PLUGIN_RCFILE)
	assert(os.path.isfile(DEFAULT_PLUGIN_RCFILE)),  plugins_textfile_message

	if (args.plugins == None or args.plugins[0] == '+') and \
			os.path.isfile(DEFAULT_PLUGIN_RCFILE):
		with open(DEFAULT_PLUGIN_RCFILE) as fp:
			plugins.extend([l.rstrip() for l in fp.readlines()])

	# ...and maybe including additional.
	if args.plugins:
		for plugname in args.plugins.split(','):
			if os.path.isfile( plugname ): # ...if it's a plugin manifest
				with open( plugname ) as fp:
					plugins.extend( [ l.rstrip() for l in fp.readlines() ] )
			else:
				plugins.append( plugname )
	mgr = bdqc.plugin.Manager( plugins )

	# ...and subjects from command line args.

	subjects = build_input( args.subjects, args.include, args.exclude, args.depth )

	if args.dryrun:
		print( "These plugins..." )
		i = 0
		for p in iter(mgr):
			i += 1
			print( "{}. {} (from {})".format( i, p.__name__, p.__path__ ) )
		print( "...would be executed as follows..." )

	# Finally, create and run an Executor.

	_exec = Executor( mgr, subjects,
			dryrun = args.dryrun,
			clobber = args.clobber,
			adjacent = (not args.no_adjacent) )

	status = bdqc.analysis.STATUS_NO_OUTLIERS

	if args.dryrun:
		missing = _exec.run() # ...no need for progress indicator or an accumulator.
	else:
		prog_fp = _open_output_file( args.progress ) \
			if args.progress else None
		accum_fp = _open_output_file( args.accum ) \
			if args.accum else None

		if args.skip_analysis:
			m = None
		else:
			statistic_filters = {
				# Need wildcard suffixes to turn plugin names into path selectors...
				"include": selectors( [ "{}/*".format(name) for name in mgr.leaves ] ),
			}
			if args.ignore:
				statistic_filters["exclude"] = selectors(args.ignore)
			m = Matrix( **statistic_filters )

		missing = _exec.run( m, accumulator=accum_fp, progress_output=prog_fp )

		if accum_fp:
			accum_fp.close()
		if prog_fp:
			prog_fp.close()

		if m:

			status = m.analyze()

			if status: # ...is other than STATUS_NO_OUTLIERS
				if args.report:
					with open(args.report,"w") as fp:
						if args.report.lower().endswith("html"):
							m.summary().render_html( fp )
						else:
							m.summary().render_text( fp )

	if missing > 0:
		logging.warning( "{} file(s) were missing".format( missing ) )

	return status 
Esempio n. 3
0
def _main(args):

    # Build lists of plugins...

    plugins = []
    # ...maybe including defaults...
    if (args.plugins == None or args.plugins[0] == "+") and os.path.isfile(DEFAULT_PLUGIN_RCFILE):
        with open(DEFAULT_PLUGIN_RCFILE) as fp:
            plugins.extend([l.rstrip() for l in fp.readlines()])
            # ...and maybe including additional.
    if args.plugins:
        for plugname in args.plugins.split(","):
            if os.path.isfile(plugname):  # ...if it's a plugin manifest
                with open(plugname) as fp:
                    plugins.extend([l.rstrip() for l in fp.readlines()])
            else:
                plugins.append(plugname)
    mgr = bdqc.plugin.Manager(plugins)

    # ...and subjects from command line args.

    subjects = build_input(args.subjects, args.include, args.exclude, args.depth)

    if args.dryrun:
        print("These plugins...")
        i = 0
        for p in iter(mgr):
            i += 1
            print("{}. {} (from {})".format(i, p.__name__, p.__path__))
        print("...would be executed as follows...")

        # Finally, create and run an Executor.

    _exec = Executor(mgr, subjects, dryrun=args.dryrun, clobber=args.clobber, adjacent=(not args.no_adjacent))

    status = bdqc.analysis.STATUS_NO_OUTLIERS

    if args.dryrun:
        missing = _exec.run()  # ...no need for progress indicator or an accumulator.
    else:
        prog_fp = _open_output_file(args.progress) if args.progress else None
        accum_fp = _open_output_file(args.accum) if args.accum else None

        if args.skip_analysis:
            m = None
        else:
            statistic_filters = {
                # Need wildcard suffixes to turn plugin names into path selectors...
                "include": selectors(["{}/*".format(name) for name in mgr.leaves])
            }
            if args.ignore:
                statistic_filters["exclude"] = selectors(args.ignore)
            m = Matrix(**statistic_filters)

        missing = _exec.run(m, accumulator=accum_fp, progress_output=prog_fp)

        if accum_fp:
            accum_fp.close()
        if prog_fp:
            prog_fp.close()

        if m:

            status = m.analyze()

            if status:  # ...is other than STATUS_NO_OUTLIERS
                if args.report:
                    with open(args.report, "w") as fp:
                        if args.report.lower().endswith("html"):
                            m.summary().render_html(fp)
                        else:
                            m.summary().render_text(fp)

    if missing > 0:
        logging.warning("{} file(s) were missing".format(missing))

    return status