From 1c94fd2a4a8370e17445f55815ad8d134665358a Mon Sep 17 00:00:00 2001 From: crouzet Date: Fri, 5 Feb 2021 12:22:55 +0100 Subject: [PATCH] sat #18855 python3 : suppression utilisation obsolete directe de l'objet file --- commands/application.py | 91 ++++++++++++++++++------------------ commands/launcher.py | 100 ++++++++++++++++++++-------------------- commands/template.py | 33 +++++++------ 3 files changed, 112 insertions(+), 112 deletions(-) diff --git a/commands/application.py b/commands/application.py index 1ce7189..f219c0b 100644 --- a/commands/application.py +++ b/commands/application.py @@ -339,52 +339,51 @@ def generate_catalog(machines, config, logger): user = getpass.getuser() catfile = src.get_tmp_filename(config, "CatalogResources.xml") - catalog = file(catfile, "w") - catalog.write("\n\n") - for k in machines: - if not src.architecture.is_windows(): - logger.write(" ssh %s " % (k + " ").ljust(20, '.'), 4) - logger.flush() - - ssh_cmd = 'ssh -o "StrictHostKeyChecking no" %s %s' % (k, cmd) - p = subprocess.Popen(ssh_cmd, shell=True, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - p.wait() - - machine_access = (p.returncode == 0) - if not machine_access: - logger.write(src.printcolors.printc(src.KO_STATUS) + "\n", 4) - logger.write(" " + src.printcolors.printcWarning(p.stderr.read()), - 2) - else: - logger.write(src.printcolors.printc(src.OK_STATUS) + "\n", 4) - lines = p.stdout.readlines() - freq = lines[0][:-1].split(':')[-1].split('.')[0].strip() - nb_proc = len(lines) -1 - memory = lines[-1].split(':')[-1].split()[0].strip() - memory = int(memory) / 1000 - - catalog.write(" \n") - catalog.write(" \n") - - catalog.write("\n") - catalog.close() + with open(catfile, 'w') as catalog: + catalog.write("\n\n") + for k in machines: + if not src.architecture.is_windows(): + logger.write(" ssh %s " % (k + " ").ljust(20, '.'), 4) + logger.flush() + + ssh_cmd = 'ssh -o "StrictHostKeyChecking no" %s %s' % (k, cmd) + p = subprocess.Popen(ssh_cmd, shell=True, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + p.wait() + + machine_access = (p.returncode == 0) + if not machine_access: + logger.write(src.printcolors.printc(src.KO_STATUS) + "\n", 4) + logger.write(" " + src.printcolors.printcWarning(p.stderr.read()), + 2) + else: + logger.write(src.printcolors.printc(src.OK_STATUS) + "\n", 4) + lines = p.stdout.readlines() + freq = lines[0][:-1].split(':')[-1].split('.')[0].strip() + nb_proc = len(lines) -1 + memory = lines[-1].split(':')[-1].split()[0].strip() + memory = int(memory) / 1000 + + catalog.write(" \n") + catalog.write(" \n") + + catalog.write("\n") return catfile ################################################## diff --git a/commands/launcher.py b/commands/launcher.py index e42436a..aec0ff8 100644 --- a/commands/launcher.py +++ b/commands/launcher.py @@ -210,57 +210,55 @@ def generate_catalog(machines, config, logger): # Create the catalog path catfile = src.get_tmp_filename(config, "CatalogResources.xml") - catalog = file(catfile, "w") - - # Write into it - catalog.write("\n\n") - for k in machines: - if not src.architecture.is_windows(): - logger.write(" ssh %s " % (k + " ").ljust(20, '.'), 4) - logger.flush() - - # Verify that the machine is accessible - ssh_cmd = 'ssh -o "StrictHostKeyChecking no" %s %s' % (k, cmd) - p = subprocess.Popen(ssh_cmd, shell=True, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - p.wait() - - machine_access = (p.returncode == 0) - if not machine_access: # The machine is not accessible - logger.write(src.printcolors.printc(src.KO_STATUS) + "\n", 4) - logger.write(" " + - src.printcolors.printcWarning(p.stderr.read()), 2) - else: - # The machine is accessible, write the corresponding section on - # the xml file - logger.write(src.printcolors.printc(src.OK_STATUS) + "\n", 4) - lines = p.stdout.readlines() - freq = lines[0][:-1].split(':')[-1].split('.')[0].strip() - nb_proc = len(lines) -1 - memory = lines[-1].split(':')[-1].split()[0].strip() - memory = int(memory) / 1000 - - catalog.write(" \n") - catalog.write(" \n") - - catalog.write("\n") - catalog.close() + with open(catfile, 'w') as catalog: + # Write into it + catalog.write("\n\n") + for k in machines: + if not src.architecture.is_windows(): + logger.write(" ssh %s " % (k + " ").ljust(20, '.'), 4) + logger.flush() + + # Verify that the machine is accessible + ssh_cmd = 'ssh -o "StrictHostKeyChecking no" %s %s' % (k, cmd) + p = subprocess.Popen(ssh_cmd, shell=True, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + p.wait() + + machine_access = (p.returncode == 0) + if not machine_access: # The machine is not accessible + logger.write(src.printcolors.printc(src.KO_STATUS) + "\n", 4) + logger.write(" " + + src.printcolors.printcWarning(p.stderr.read()), 2) + else: + # The machine is accessible, write the corresponding section on + # the xml file + logger.write(src.printcolors.printc(src.OK_STATUS) + "\n", 4) + lines = p.stdout.readlines() + freq = lines[0][:-1].split(':')[-1].split('.')[0].strip() + nb_proc = len(lines) -1 + memory = lines[-1].split(':')[-1].split()[0].strip() + memory = int(memory) / 1000 + + catalog.write(" \n") + catalog.write(" \n") + + catalog.write("\n") return catfile def copy_catalog(config, catalog_path): diff --git a/commands/template.py b/commands/template.py index b7ee487..c7e1980 100644 --- a/commands/template.py +++ b/commands/template.py @@ -313,15 +313,17 @@ def prepare_from_template(config, logger.write(" - %s\n" % fpath[pathlen:], 5) continue # read the file - m = file(fpath, 'r').read() - # make the substitution - template = CompoTemplate(m) - d = template.safe_substitute(dico) - # overwrite the file with substituted content + with open(fpath, 'r') as f: + m = f.read() + # make the substitution + template = CompoTemplate(m) + d = template.safe_substitute(dico) + changed = " " if d != m: changed = "*" - file(fpath, 'w').write(d) + with open(fpath, 'w') as f: + f.write(d) logger.write(" %s %s\n" % (changed, fpath[pathlen:]), 5) if not tsettings.has_pyconf: @@ -415,15 +417,16 @@ def get_template_info(config, template_name, logger): if not tsettings.check_file_for_substitution(fpath[pathlen:]): continue # read the file - m = file(fpath, 'r').read() - zz = re.findall(reexp, m) - zz = list(set(zz)) # reduce - zz = filter(lambda l: l not in pnames, zz) - if len(zz) > 0: - logger.write("Missing definition in %s: %s" % ( - src.printcolors.printcLabel( - fpath[pathlen:]), ", ".join(zz)), 3) - retcode = 1 + with open(fpath, 'r') as f: + m = f.read() + zz = re.findall(reexp, m) + zz = list(set(zz)) # reduce + zz = filter(lambda l: l not in pnames, zz) + if len(zz) > 0: + logger.write("Missing definition in %s: %s" % ( + src.printcolors.printcLabel( + fpath[pathlen:]), ", ".join(zz)), 3) + retcode = 1 if retcode == 0: logger.write(src.printcolors.printc("OK"), 3) -- 2.39.2