mirror of
https://github.com/nmap/nmap.git
synced 2025-12-07 21:21:31 +00:00
Use 'with' statements for file open/close. #1834
This commit is contained in:
@@ -238,13 +238,10 @@ for dir in dirs:
|
|||||||
if INSTALLED_FILES_NAME == self.record:
|
if INSTALLED_FILES_NAME == self.record:
|
||||||
distutils.log.warn("warning: installation record is overwriting "
|
distutils.log.warn("warning: installation record is overwriting "
|
||||||
"--record file '%s'." % self.record)
|
"--record file '%s'." % self.record)
|
||||||
f = open(INSTALLED_FILES_NAME, "w")
|
with open(INSTALLED_FILES_NAME, "w") as f:
|
||||||
try:
|
|
||||||
for output in self.get_installed_files():
|
for output in self.get_installed_files():
|
||||||
assert "\n" not in output
|
assert "\n" not in output
|
||||||
print >> f, output
|
print >> f, output
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
|
||||||
class my_uninstall(distutils.cmd.Command):
|
class my_uninstall(distutils.cmd.Command):
|
||||||
|
|||||||
@@ -485,13 +485,10 @@ for dir in dirs:
|
|||||||
if INSTALLED_FILES_NAME == self.record:
|
if INSTALLED_FILES_NAME == self.record:
|
||||||
distutils.log.warn("warning: installation record is overwriting "
|
distutils.log.warn("warning: installation record is overwriting "
|
||||||
"--record file '%s'." % self.record)
|
"--record file '%s'." % self.record)
|
||||||
f = open(INSTALLED_FILES_NAME, "w")
|
with open(INSTALLED_FILES_NAME, "w") as f:
|
||||||
try:
|
|
||||||
for output in self.get_installed_files():
|
for output in self.get_installed_files():
|
||||||
assert "\n" not in output
|
assert "\n" not in output
|
||||||
print >> f, output
|
print >> f, output
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
|
||||||
class my_uninstall(Command):
|
class my_uninstall(Command):
|
||||||
|
|||||||
@@ -48,13 +48,10 @@ if directory is not None:
|
|||||||
os.chdir(directory)
|
os.chdir(directory)
|
||||||
|
|
||||||
for fn in filenames:
|
for fn in filenames:
|
||||||
f = open(fn, "r")
|
with open(fn, "r") as f:
|
||||||
try:
|
|
||||||
parser = xml.sax.make_parser()
|
parser = xml.sax.make_parser()
|
||||||
parser.setContentHandler(Handler())
|
parser.setContentHandler(Handler())
|
||||||
parser.parse(f)
|
parser.parse(f)
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
if len(filenames) < 2:
|
if len(filenames) < 2:
|
||||||
parser = xml.sax.make_parser()
|
parser = xml.sax.make_parser()
|
||||||
|
|||||||
@@ -817,12 +817,9 @@ class NmapParserSAX(ParserBasics, ContentHandler):
|
|||||||
|
|
||||||
def parse_file(self, filename):
|
def parse_file(self, filename):
|
||||||
"""Parse an Nmap XML file from the named file."""
|
"""Parse an Nmap XML file from the named file."""
|
||||||
f = open(filename, "r")
|
with open(filename, "r") as f:
|
||||||
try:
|
|
||||||
self.parse(f)
|
self.parse(f)
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
def _parse_nmaprun(self, attrs):
|
def _parse_nmaprun(self, attrs):
|
||||||
run_tag = "nmaprun"
|
run_tag = "nmaprun"
|
||||||
|
|||||||
@@ -151,13 +151,10 @@ class ScriptDB (object):
|
|||||||
def __init__(self, script_db_path=None):
|
def __init__(self, script_db_path=None):
|
||||||
self.unget_buf = ""
|
self.unget_buf = ""
|
||||||
|
|
||||||
self.f = open(script_db_path, "r")
|
|
||||||
self.lineno = 1
|
self.lineno = 1
|
||||||
self.line = ""
|
self.line = ""
|
||||||
try:
|
with open(script_db_path, "r") as self.f:
|
||||||
self.entries_list = self.parse()
|
self.entries_list = self.parse()
|
||||||
finally:
|
|
||||||
self.f.close()
|
|
||||||
|
|
||||||
def syntax_error(self, message):
|
def syntax_error(self, message):
|
||||||
e = ScriptDBSyntaxError(message)
|
e = ScriptDBSyntaxError(message)
|
||||||
@@ -368,15 +365,12 @@ class ScriptMetadata (object):
|
|||||||
self.get_string_variable(filename, "author")]
|
self.get_string_variable(filename, "author")]
|
||||||
|
|
||||||
filepath = os.path.join(self.scripts_dir, filename)
|
filepath = os.path.join(self.scripts_dir, filename)
|
||||||
f = open(filepath, "r")
|
with open(filepath, "r") as f:
|
||||||
try:
|
|
||||||
for tag_name, tag_text in nsedoc_tags_iter(f):
|
for tag_name, tag_text in nsedoc_tags_iter(f):
|
||||||
if tag_name == "output" and not entry.output:
|
if tag_name == "output" and not entry.output:
|
||||||
entry.output = tag_text
|
entry.output = tag_text
|
||||||
elif tag_name == "usage" and not entry.usage:
|
elif tag_name == "usage" and not entry.usage:
|
||||||
entry.usage = tag_text
|
entry.usage = tag_text
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
entry.description = "Error getting metadata: {}".format(e)
|
entry.description = "Error getting metadata: {}".format(e)
|
||||||
|
|
||||||
@@ -384,11 +378,8 @@ class ScriptMetadata (object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_file_contents(filename):
|
def get_file_contents(filename):
|
||||||
f = open(filename, "r")
|
with open(filename, "r") as f:
|
||||||
try:
|
|
||||||
contents = f.read()
|
contents = f.read()
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
return contents
|
return contents
|
||||||
|
|
||||||
def get_string_variable(self, filename, varname):
|
def get_string_variable(self, filename, varname):
|
||||||
@@ -421,11 +412,8 @@ class ScriptMetadata (object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_requires(filename):
|
def get_requires(filename):
|
||||||
f = open(filename, "r")
|
with open(filename, "r") as f:
|
||||||
try:
|
|
||||||
requires = ScriptMetadata.get_requires_from_file(f)
|
requires = ScriptMetadata.get_requires_from_file(f)
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
return requires
|
return requires
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -440,11 +428,8 @@ class ScriptMetadata (object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_script_args(filename):
|
def get_script_args(filename):
|
||||||
f = open(filename, "r")
|
with open(filename, "r") as f:
|
||||||
try:
|
|
||||||
args = ScriptMetadata.get_script_args_from_file(f)
|
args = ScriptMetadata.get_script_args_from_file(f)
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
return args
|
return args
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user