# Licensed under a 3-clause BSD style license - see LICENSE.rstimportloggingimporttracebackfromcollectionsimportdefaultdictfrom..importenvironment,utilfrom..consoleimportlogfrom.importCommand,common_args
[docs]defsetup_arguments(cls,subparsers):parser=subparsers.add_parser("setup",help="Setup virtual environments",description="""Setup virtual environments for each combination of Python version and third-party requirement. This is called by the ``run`` command implicitly, and isn't generally required to be run on its own.""",)common_args.add_parallel(parser)common_args.add_environment(parser)parser.set_defaults(func=cls.run_from_args)returnparser
[docs]defperform_setup(cls,environments,parallel=-1):ifenvironment.is_existing_only(environments):# Nothing to do, so don't print anythingreturnenvironmentsparallel,multiprocessing=util.get_multiprocessing(parallel)log.info("Creating environments")withlog.indent():ifparallel!=1:try:# Run creation in parallel only for environments with# different dir_namesenvironment_groups=defaultdict(list)forenvinenvironments:environment_groups[env.dir_name].append(env)pool=util.get_multiprocessing_pool(parallel)try:pool.map(_create_parallel,environment_groups.values())pool.close()pool.join()finally:pool.terminate()exceptutil.ParallelFailureasexc:exc.reraise()else:list(map(_create,environments))