ConfidentialLCA/template/computation.py

133 lines
3.8 KiB
Python
Executable File

#!/usr/bin/env python3
#############################################################################
# Copyright (c) 2019-2021 University of Exeter, UK
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#############################################################################
"""
Computation component.
"""
import sys
import argparse
import os
import logging
from pplca.config import const_log_format, const_verbose
from pplca.compute import update_env_flows_list, start_computation
from pplca.log_utils import log_info
def log_conf(cwd, conf):
"""Log configuration."""
log_info("Configuration:")
log_info(" Base dir: {}".format(cwd))
log_info(" Command: {}".format(conf.cmd))
def main(conf):
"""Main function: Computation Module."""
logger = logging.getLogger()
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(logging.Formatter(const_log_format("computation.py")))
logger.addHandler(ch)
# cwd = os.getcwd()
if conf.verbose:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.WARNING)
log_conf(conf.compDir, conf)
retval = False
if conf.cmd == "update":
retval = update_env_flows_list(conf.compDir, conf.compSuppDir)
elif conf.cmd == "computation":
retval = start_computation(
conf.companyName,
conf.suppDirName,
conf.compSuppDir,
conf.compDir,
conf.certificate,
conf.portNumber,
conf.envFlowValue,
)
return retval
if __name__ == "__main__":
main_parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter, description="Computation module."
)
main_parser.add_argument(
"-c",
"--cmd",
metavar="cmd",
choices=["computation", "update"],
default="computation",
help="Command:\n"
+ " computation: start computation\n"
+ " update: Update the list of environmental flows",
)
main_parser.add_argument(
"-p",
"--portNumber",
type=int,
default="0",
help="give port number for supplier",
)
main_parser.add_argument(
"-env",
"--envFlowValue",
type=bool,
default=True,
help="give the values of environmental flows for computation",
)
main_parser.add_argument(
"-cert", "--certificate", default="", help="give certificate that will be used"
)
main_parser.add_argument(
"-cn",
"--companyName",
default="",
help="give company(root) name that will be used",
)
main_parser.add_argument(
"-comsuppd",
"--compSuppDir",
default="",
help="give the directory of the supplier company",
)
main_parser.add_argument(
"-suppname",
"--suppDirName",
default="",
help="give the name of the directory of the supplier company",
)
main_parser.add_argument(
"-comd",
"--compDir",
default="",
help="give the directory of the company",
)
main_parser.add_argument(
"-v",
"--verbose",
action="store_true",
default=const_verbose(),
help="increase verbosity",
)
main_conf = main_parser.parse_args()
sys.exit(main(main_conf))