[docs]@typecheckeddefconfig_qlm_account(username:str,password:str,global_config:bool)->bool:"""Configures and saves locally QLM account's information. Args: username: QLM username. password: QLM password. global_config: If True, this QLM account will be configured to work even outside MPQP. Raises: QLMRemoteExecutionError: If the account could not be saved. """# store the username and password in environment variables QLM_USER and QLM_PASSWD in .mpqpprev_user=get_env_variable("QLM_USER")prev_pass=get_env_variable("QLM_PASSWD")prev_configure=get_env_variable("QLM_CONFIGURED")ifprev_configure=="":prev_configure="False"file_content=Nonenetrc_path=os.path.expanduser("~")+"/.netrc"ifglobal_config:try:withopen(netrc_path,'r')asfile:file_content=file.read()except:passsave_env_variable("QLM_USER",username)ifnotglobal_config:save_env_variable("QLM_PASSWD",password)try:ifglobal_config:print("we are in the global part")# if file doesn't exist, create it, or overwrite the credentials in the ~/.netrc filewithopen(netrc_path,"w")asfile:file.write(f"""\machine qlm35e.neasqc.eulogin {username}password {password}""")# Set the permissions to read and right for user onlyos.chmod(netrc_path,0o600)else:ifos.path.exists(netrc_path):rename_decision=input(f"'~/.netrc' already exists and will override the configuration. Do you want to rename it in '~/.netrc_back'? [Y/n]")ifrename_decision.lower().strip()in('','y','yes'):os.rename(netrc_path,netrc_path+"_back")fromqat.qlmaasimport(QLMaaSConnection,# pyright: ignore[reportAttributeAccessIssue])c=QLMaaSConnection(hostname="qlm35e.neasqc.eu",port=443,authentication="password",timeout=2500,)c.create_config()exceptExceptionaserr:save_env_variable("QLM_USER",prev_user)save_env_variable("QLM_PASSWD",prev_pass)save_env_variable("QLM_CONFIGURED",prev_configure)ifglobal_config:iffile_contentisNone:try:os.remove(netrc_path)exceptFileNotFoundError:print(f"{netrc_path} does not exist.")else:withopen(netrc_path,"w")asfile:file.write(file_content)if"Invalid credential"instr(err):returnFalseraiseQLMRemoteExecutionError("Error when saving the account.\nTrace: "+str(err))returnTrue
[docs]defsetup_qlm_account()->tuple[str,list[Any]]:"""Setups the QLM account, by looking at the existing configuration, asking for username/password and updating the current account."""already_configured=get_env_variable("QLM_CONFIGURED")=="True"ifalready_configured:decision=input("An QLM account is already configured. Do you want to update it? [y/N] ")ifdecision.lower().strip()!="y":return"Canceled.",[]username=input("Enter your QLM username: ")password=getpass("Enter your QLM password (hidden): ")global_decision=input("Do you want to configure this account only for MPQP? [y/N] ")global_config=Trueifglobal_decision.lower().strip()!="y"elseFalseconnection_success=config_qlm_account(username,password,global_config)ifconnection_success:save_env_variable("QLM_CONFIGURED","True")ifconnection_success:return"QLM account correctly configured",[]else:print(colored("Wrong credential","red"))getpass("Press 'Enter' to continue")return"",[]
[docs]defget_all_job_ids()->list[str]:"""Retrieves from the remote QLM all the job-ids associated with this account. Returns: List of all job-ids associated with this account. Example: >>> get_all_job_ids() ['Job144361', 'Job144360', 'Job144359', 'Job144358', 'Job144357', 'Job143334', 'Job143333', 'Job143332', 'Job141862', 'Job141861', 'Job141722', 'Job141720', 'Job141716', 'Job141715', 'Job141712', 'Job19341'] """ifget_env_variable("QLM_CONFIGURED")=="True":connection=get_QLMaaSConnection()return[job_info.idforjob_infoinconnection.get_jobs_info()]return[]
[docs]defget_QLMaaSConnection():"""Connects to the QLM and returns the QLMaaSConnection. If the connection was already established, we only return the one stored in global variable, otherwise we instantiate a new QLMaaSConnection."""globalQLM_connectionifQLM_connectionisNone:loaded=load_env_variables()ifnotloaded:raiseIOError("Could not load environment variables from ~/.mpqp")ifget_env_variable("QLM_CONFIGURED")=="False":raiseQLMRemoteExecutionError("Error when instantiating QLMaaSConnection. No QLM account configured.")fromqat.qlmaas.connectionimportQLMaaSConnectionQLM_connection=QLMaaSConnection(hostname="qlm35e.neasqc.eu",port=443,authentication="password",timeout=2500,)returnQLM_connection