#! /usr/bin/env python3 # -*- coding: utf-8 -*- """ licenser.py: Main executable of py-licenser""" __author__ = "Guillaume Anciaux and Nicolas Richart" __credits__ = [ "Guillaume Anciaux ", "Nicolas Richart ", ] __copyright__ = "Copyright (©) 2010-2021 EPFL (Ecole Polytechnique Fédérale" \ " de Lausanne) Laboratory (LSMS - Laboratoire de Simulation" \ " en Mécanique des Solides)" __license__ = "GPLv3" __version__ = "2.0" import argparse import datetime as dt import sys import tqdm import multiprocessing as mp import pylicenser as pylic def mkdate(datestring): return dt.datetime.strptime(datestring, '%Y-%m-%d').date() if __name__ == "__main__": parser = argparse.ArgumentParser(prog='licenser', add_help=True) parser.add_argument( "-i,--input", help="Filename to check", dest="filename", default=None) parser.add_argument( "-f,--file_list", help="File containing a list of files", dest="file_list", default=None) parser.add_argument( "--repo", help="Repository to consider", dest="repo", default=None) parser.add_argument( "-p,--path", help="Folder where to find the files", dest="path", default="") parser.add_argument( "-s,--skip-first", help="Skip the first files when using the -f option", dest="skip_first", type=int, default=-1) parser.add_argument( "-v,--versioning-backend", dest="vc_backend", help="Backend used as versioning system (svn, git, none)") parser.add_argument( "-r,--release-date", help="Date at which the release is prepared", dest='release_date', type=mkdate, default=dt.datetime.now().date()) parser.add_argument( "-a,--no-author-check", help="Do not check the author list", dest="no_author_check", action='store_true', default=False) parser.add_argument( "-b,--no-brief-check", help="Do not check the brief", dest="no_brief_check", action='store_true', default=False) parser.add_argument( "--ignore-threshold", help="Limit of number of line to consider an author from the VC system", dest="ignore_threshold", type=int, default=0) parser.add_argument( "--ignore-filled-briefs", help="Do not check the brief if they are not empty", dest="ignore_filled_briefs", action='store_true', default=False) parser.add_argument( "--dry-run", help="Do nothing for real", dest='dry_run', action='store_true', default=False) parser.add_argument( "-l,--force-license", help="Force a give license", dest="force_license", default=None) parser.add_argument( "--force", help="Force to update the header even it is considered up-to-date", dest="force", action='store_true', default=False) parser.add_argument( "--yes", help="Answers yes to keep author and brief questions", dest="yes", action='store_true', default=False) parser.add_argument( "configuration_file", help="File containing the configuration, .csv or .db (sqlite)") args = parser.parse_args() if (args.filename is None) and (args.file_list is None): print("You should at least give a filename or a file_list") parser.print_help() sys.exit(-1) if (args.filename is not None) and (args.file_list is not None): print("You should give only on of the option filename or file_list") parser.print_help() sys.exit(-1) file_list = [] if args.filename is not None: file_list.append(args.filename) if args.file_list is not None: with open(args.file_list, "r") as fh: file_list = [l.strip() for l in fh] db = pylic.LicenserDB(args.configuration_file) c = 0 t = len(file_list) _kwargs = vars(args) _kwargs.pop("filename", None) file_tqdm = tqdm.tqdm(total=0, position=0, bar_format='{desc}') exception_tqdm = tqdm.tqdm(total=0, position=2, bar_format='{desc}') def process_file(t): c, f = t res = pylic.print_colored(f, attrs=['bold']) skip = False suffix = "" if c <= args.skip_first: suffix = "skipped" skip = True elif args.force: suffix = "forced" if suffix: res = f"""[{pylic.print_colored(suffix, "red", attrs=['bold'])}] {res}""" file_tqdm.set_description_str(f"Current : {res}") if not args.path == "": path = args.path.rstrip("/") + "/" else: path = "" if not skip: try: ft = pylic.FileTransformer(path + f, db, **_kwargs) ft.replace_file(args.dry_run) except Exception as e: exception_tqdm.set_description_str(f"Last error on file: {f} - {e}") pass with mp.Pool(10) as p: list(tqdm.tqdm(p.imap(process_file, enumerate(file_list)), total=len(file_list), position=1, desc='Files'))