Exemple #1
0
def main(argv):
    parser = argparse.ArgumentParser(description =
                                     "A source-file LIcense CHEcker.",
                                     epilog      = "Report bugs to " +
                                     "<" + program_bugreport + ">",
                                     add_help    = True,
                                     prog        = program_name)

    #parser.add_argument('-V', '--version',
    #                    action = 'version',
    #                    version = '%(prog)s ' +
    #                    liche.settings.package_version,
    #                    help    = "print version number, then exit")
    parser.add_argument('-V', '--version',
                        action = 'store_true',
                        dest   = 'show_version',
                        help    = "print version number, then exit")
    parser.add_argument('-v', '--verbose',
                        action = 'store_true',
                        dest   = 'want_verbose',
                        help   = 'produce verbose output')
    parser.add_argument('-d', '--debug',
                        action = 'store_true',
                        dest   = 'want_debug',
                        help   = 'produce debugging output')

    parser.add_argument('--quiet',
                        action = 'store_true',
                        dest   = 'quiet',
                        help   = 'perform actions quietly')
    parser.add_argument('--dry-run',
                        action = 'store_true',
                        dest   = 'dry_run',
                        help   = 'do not perform checks')
    parser.add_argument('--licenses',
                        action = 'store_true',
                        dest   = 'show_licenses',
                        help   = 'show licenses')
    #parser.add_argument('--exclude',
    #                    nargs   = 1,
    #                    action  = 'append',
    #                    dest    = 'exclude',
    #                    metavar = 'PATH',
    #                    help    = 'exclude paths, given as PATTERN')

    parser.add_argument('DIRECTORY',
                        nargs   = '*',
                        action  = 'store',
                        default = ".",
                        help    = 'input directory')

    args = parser.parse_args()
    #print args.__dict__

    if args.show_version:
        print("%s (%s) %s" %
              (program_name,
               liche.settings.package_name,
               liche.settings.package_version))
        return 0

    if not args.want_debug:
        liche.log.debug_off()

    if not args.want_verbose:
        liche.log.info_off()

    logger.debug("%s %s" % (program_name, liche.settings.package_version))

    licenses_filename = os.path.join(liche.settings.pkgdatadir, "licenses.txt")
    licenses_factory  = liche.license.LicensesFactory(licenses_filename)

    logger.debug("License factory contains %d licenses: %s" %
                 (len(licenses_factory.licenses()),
                  map(str, licenses_factory.licenses())))

    if args.show_licenses:
        for l in licenses_factory.licenses():
            #print("%s: %s" % (str(l),  map(str, l.compatibles()).join(' ')))
            print("%s" % str(l))
        return 0

    tags = set()
    licenses = licenses_factory.licenses()
    for l in licenses:
        tags = set.union(tags, l.tags())

    logger.debug("There are %d known tags: %s" % (len(tags), map(str, tags)))

    paths = set()
    for path in args.DIRECTORY:
        logger.debug("Adding path '%s'" % path)
        paths.add(os.path.abspath(path))
    logger.debug("Paths are: '%s'" % str(paths))

    jobs = set()
    for path in paths:
        jobs.add(liche.job.Job(path, licenses, ignore_filename))
    logger.debug("Jobs are: '%s'" % str(jobs))

    if args.quiet:
        s = None
    else:
        s = sys.stdout

    rets = map(lambda obj: obj.run(stream = s,
                                   dry    = args.dry_run), jobs)
    logger.debug("%d jobs completed" % len(rets))

    retval = 0
    if False in rets:
        logger.warning("Got problems")
        retval = 1

    logger.debug("Everything seems ok")

    return retval
    def __walk(self,
               root,
               file_callback,
               directory_callback,
               globs):

        assert(globs is not None)

        current_dir = os.path.abspath(root)

        logger.debug("Walking directory '%s'" % current_dir)

        entries = os.listdir(current_dir)
        logger.debug("Directory entries are: '%s'" % entries)

        assert("."  not in entries)
        assert(".." not in entries)

        if (self.__ignore is not None) and (self.__ignore in entries):
            p = os.path.join(current_dir, self.__ignore)
            logger.debug("Found ignore file '%s'" % p)

            gf = GlobsFile(p)
            logger.debug("Globs file parsed successfully")

            gl = gf.globs()
            logger.debug("Ignore file '%s' produced %d globs" %
                         (p, len(gl)))

            globs = globs + gl
            logger.debug("Globs are now %d" % len(globs))
            entries.remove(self.__ignore)

        assert(self.__ignore not in entries)
        assert(globs is not None)

        logger.debug("We have %d globs for directory '%s': %s" %
                     (len(globs), current_dir, map(str, globs)))

        for entry in entries:
            rel_path = entry
            abs_path = os.path.abspath(os.path.join(current_dir, rel_path))

            assert(not os.path.isabs(rel_path))
            assert(    os.path.isabs(abs_path))

            skip = False
            for g in globs:
                x = None
                if g.match(rel_path):
                    logger.debug("Relative path '%s' got a match with '%s'" %
                                 (rel_path, g.pattern()))
                    if g.is_inclusive():
                        skip = False
                    else:
                        skip = True
                    continue

                if g.match(abs_path):
                    logger.debug("Absolute path '%s' got a match with '%s'" %
                                 (abs_path, g.pattern()))
                    if g.is_inclusive():
                        skip = False
                    else:
                        skip = True
                    continue
            if skip:
                logger.info("Skipping '%s'" % abs_path)
                continue

            logger.debug("Handling path '%s'" % abs_path)
            liche.utils.path_exists_barrier(abs_path)

            if os.path.isdir(abs_path):
                liche.utils.directory_exists_barrier(abs_path)

                if directory_callback is not None:
                    directory_callback(abs_path)

                self.__walk(abs_path,
                            file_callback,
                            directory_callback,
                            globs)
            elif os.path.isfile(abs_path):
                liche.utils.file_exists_barrier(abs_path)

                if file_callback is not None:
                    file_callback(abs_path)
            elif os.path.ismount(abs_path):
                logger.warning("Skipping '%s' (mount point)" %
                               abs_path)
            else:
                logger.warning("Skipping '%s' (not a file or directory)" %
                               abs_path)

        logger.debug("Completed handling directory '%s' (%d globs)" %
                     (current_dir, len(globs)))