Salome HOME
sat #18855 python3 : suppression utilisation obsolete directe de l'objet file
authorcrouzet <nicolas.crouzet@cea.fr>
Fri, 5 Feb 2021 11:22:55 +0000 (12:22 +0100)
committercrouzet <nicolas.crouzet@cea.fr>
Fri, 5 Feb 2021 11:22:55 +0000 (12:22 +0100)
commands/application.py
commands/launcher.py
commands/template.py

index 1ce718952e9ce8cbcb70c6d33a56570a532ee3a6..f219c0b2d8a5c7b0b4a705c2cf357bae98d7d80b 100644 (file)
@@ -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("<!DOCTYPE ResourcesCatalog>\n<resources>\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("    <machine\n")
-            catalog.write("        protocol=\"ssh\"\n")
-            catalog.write("        nbOfNodes=\"1\"\n")
-            catalog.write("        mode=\"interactif\"\n")
-            catalog.write("        OS=\"LINUX\"\n")
-
-            if (not src.architecture.is_windows()) and machine_access:
-                catalog.write("        CPUFreqMHz=\"%s\"\n" % freq)
-                catalog.write("        nbOfProcPerNode=\"%s\"\n" % nb_proc)
-                catalog.write("        memInMB=\"%s\"\n" % memory)
-
-            catalog.write("        userName=\"%s\"\n" % user)
-            catalog.write("        name=\"%s\"\n" % k)
-            catalog.write("        hostname=\"%s\"\n" % k)
-            catalog.write("    >\n")
-            catalog.write("    </machine>\n")
-
-    catalog.write("</resources>\n")
-    catalog.close()
+    with open(catfile, 'w') as catalog:
+        catalog.write("<!DOCTYPE ResourcesCatalog>\n<resources>\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("    <machine\n")
+                catalog.write("        protocol=\"ssh\"\n")
+                catalog.write("        nbOfNodes=\"1\"\n")
+                catalog.write("        mode=\"interactif\"\n")
+                catalog.write("        OS=\"LINUX\"\n")
+
+                if (not src.architecture.is_windows()) and machine_access:
+                    catalog.write("        CPUFreqMHz=\"%s\"\n" % freq)
+                    catalog.write("        nbOfProcPerNode=\"%s\"\n" % nb_proc)
+                    catalog.write("        memInMB=\"%s\"\n" % memory)
+
+                catalog.write("        userName=\"%s\"\n" % user)
+                catalog.write("        name=\"%s\"\n" % k)
+                catalog.write("        hostname=\"%s\"\n" % k)
+                catalog.write("    >\n")
+                catalog.write("    </machine>\n")
+
+        catalog.write("</resources>\n")
     return catfile
 
 ##################################################
index e42436a3a02979b2ec5cfdc8e53e621593af82a8..aec0ff8eb8c4fa6a08ed0c2fce76bccc8ea02278 100644 (file)
@@ -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("<!DOCTYPE ResourcesCatalog>\n<resources>\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("    <machine\n")
-        catalog.write("        protocol=\"ssh\"\n")
-        catalog.write("        nbOfNodes=\"1\"\n")
-        catalog.write("        mode=\"interactif\"\n")
-        catalog.write("        OS=\"LINUX\"\n")
-
-        if (not src.architecture.is_windows()) and machine_access :
-            catalog.write("        CPUFreqMHz=\"%s\"\n" % freq)
-            catalog.write("        nbOfProcPerNode=\"%s\"\n" % nb_proc)
-            catalog.write("        memInMB=\"%s\"\n" % memory)
-
-        catalog.write("        userName=\"%s\"\n" % user)
-        catalog.write("        name=\"%s\"\n" % k)
-        catalog.write("        hostname=\"%s\"\n" % k)
-        catalog.write("    >\n")
-        catalog.write("    </machine>\n")
-
-    catalog.write("</resources>\n")
-    catalog.close()
+    with open(catfile, 'w') as catalog:
+        # Write into it
+        catalog.write("<!DOCTYPE ResourcesCatalog>\n<resources>\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("    <machine\n")
+            catalog.write("        protocol=\"ssh\"\n")
+            catalog.write("        nbOfNodes=\"1\"\n")
+            catalog.write("        mode=\"interactif\"\n")
+            catalog.write("        OS=\"LINUX\"\n")
+
+            if (not src.architecture.is_windows()) and machine_access :
+                catalog.write("        CPUFreqMHz=\"%s\"\n" % freq)
+                catalog.write("        nbOfProcPerNode=\"%s\"\n" % nb_proc)
+                catalog.write("        memInMB=\"%s\"\n" % memory)
+
+            catalog.write("        userName=\"%s\"\n" % user)
+            catalog.write("        name=\"%s\"\n" % k)
+            catalog.write("        hostname=\"%s\"\n" % k)
+            catalog.write("    >\n")
+            catalog.write("    </machine>\n")
+
+        catalog.write("</resources>\n")
     return catfile
 
 def copy_catalog(config, catalog_path):
index b7ee487b545bda7b53d71e9b3cae8a8139ed2f8c..c7e1980cc93bbfcf16f0e6ce69840efdd6b1405e 100644 (file)
@@ -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)