1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-09 06:01:28 +00:00

Address some pep8 issues with zenmap_wrapper.py

This commit is contained in:
dmiller
2015-06-23 22:00:05 +00:00
parent 8941682afb
commit 4aeabca374

View File

@@ -1,8 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
# This is a wrapper script around the zenmap executable, used in a Mac OS X .app # This is a wrapper script around the zenmap executable, used in a Mac OS X
# bundle. It sets environment variables, fills in template configuration files, # .app bundle. It sets environment variables, fills in template configuration
# starts X11 if necessary, and execs the real zenmap executable. # files, starts X11 if necessary, and execs the real zenmap executable.
# #
# This program is the second link in the chain # This program is the second link in the chain
# zenmap_auth -> zenmap_wrapper.py -> zenmap.bin # zenmap_auth -> zenmap_wrapper.py -> zenmap.bin
@@ -14,6 +14,7 @@ import sys
HOME = os.path.expanduser("~") HOME = os.path.expanduser("~")
def create_dir(path): def create_dir(path):
"""Create a directory with os.makedirs without raising an error if the """Create a directory with os.makedirs without raising an error if the
directory already exists.""" directory already exists."""
@@ -31,6 +32,7 @@ def create_dir(path):
# The format of pango/pangorc is called "key file." It's described at # The format of pango/pangorc is called "key file." It's described at
# http://library.gnome.org/devel/glib/stable/glib-Key-value-file-parser. # http://library.gnome.org/devel/glib/stable/glib-Key-value-file-parser.
# Escape a string as appropriate for a "key file." # Escape a string as appropriate for a "key file."
def escape_key_file_value(value): def escape_key_file_value(value):
result = [] result = []
@@ -49,11 +51,13 @@ def escape_key_file_value(value):
result = "".join(result) result = "".join(result)
return result return result
def substitute_key_file_line(line, replacements): def substitute_key_file_line(line, replacements):
for text, rep in replacements.items(): for text, rep in replacements.items():
line = line.replace(text, escape_key_file_value(rep)) line = line.replace(text, escape_key_file_value(rep))
return line return line
# Substitute a dict of replacements into a "key file." # Substitute a dict of replacements into a "key file."
def substitute_key_file(in_file_name, out_file_name, replacements): def substitute_key_file(in_file_name, out_file_name, replacements):
in_file = open(in_file_name, "r") in_file = open(in_file_name, "r")
@@ -63,6 +67,7 @@ def substitute_key_file(in_file_name, out_file_name, replacements):
in_file.close() in_file.close()
out_file.close() out_file.close()
def escape_shell(arg): def escape_shell(arg):
"""Escape a string to be a shell argument.""" """Escape a string to be a shell argument."""
result = [] result = []
@@ -72,6 +77,7 @@ def escape_shell(arg):
result.append(c) result.append(c)
return "\"" + "".join(result) + "\"" return "\"" + "".join(result) + "\""
def hack_xinitrc(system_xinitrc_filename, home_xinitrc_filename): def hack_xinitrc(system_xinitrc_filename, home_xinitrc_filename):
"""Hack the system xinitrc file and put the modified contents into another """Hack the system xinitrc file and put the modified contents into another
file. The parameter names reflect the fact that this is intended to copy file. The parameter names reflect the fact that this is intended to copy
@@ -99,16 +105,18 @@ def hack_xinitrc(system_xinitrc_filename, home_xinitrc_filename):
system_xinitrc.close() system_xinitrc.close()
home_xinitrc.close() home_xinitrc.close()
def start_x11(): def start_x11():
"""Start the X11 server if necessary and set the DISPLAY environment as """Start the X11 server if necessary and set the DISPLAY environment as
appropriate. If the user hasn't set up a custom ~/.xinitrc, call appropriate. If the user hasn't set up a custom ~/.xinitrc, call
hack_xinitrc to make a ~/.xinitrc that will not start an xterm along with hack_xinitrc to make a ~/.xinitrc that will not start an xterm along with
the application. A similar approach is taken by Wireshark and Inkscape.""" the application. A similar approach is taken by Wireshark and Inkscape."""
if os.environ.has_key("DISPLAY"): if "DISPLAY" in os.environ:
return return
system_xinitrc_filename = "/usr/X11R6/lib/X11/xinit/xinitrc" system_xinitrc_filename = "/usr/X11R6/lib/X11/xinit/xinitrc"
home_xinitrc_filename = os.path.join(HOME, ".xinitrc") home_xinitrc_filename = os.path.join(HOME, ".xinitrc")
if os.path.exists(system_xinitrc_filename) and not os.path.exists(home_xinitrc_filename): if (os.path.exists(system_xinitrc_filename) and not
os.path.exists(home_xinitrc_filename)):
hack_xinitrc(system_xinitrc_filename, home_xinitrc_filename) hack_xinitrc(system_xinitrc_filename, home_xinitrc_filename)
os.system("open -a X11") os.system("open -a X11")
os.environ["DISPLAY"] = ":0" os.environ["DISPLAY"] = ":0"
@@ -140,7 +148,8 @@ if __name__ == "__main__":
os.environ["FONTCONFIG_PATH"] = os.path.join(resourcedir, "etc", "fonts") os.environ["FONTCONFIG_PATH"] = os.path.join(resourcedir, "etc", "fonts")
# Use the packaged gtkrc only if the user doesn't have a custom one. # Use the packaged gtkrc only if the user doesn't have a custom one.
if not os.path.exists(os.path.expanduser("~/.gtkrc-2.0")): if not os.path.exists(os.path.expanduser("~/.gtkrc-2.0")):
os.environ["GTK2_RC_FILES"] = os.path.join(resourcedir, "etc", "gtk-2.0", "gtkrc") os.environ["GTK2_RC_FILES"] = os.path.join(
resourcedir, "etc", "gtk-2.0", "gtkrc")
# The following environment variables refer to files within ~/.zenmap-etc # The following environment variables refer to files within ~/.zenmap-etc
# that are automatically generated from templates. # that are automatically generated from templates.
@@ -166,4 +175,7 @@ if __name__ == "__main__":
start_x11() start_x11()
# exec the real program. # exec the real program.
os.execl(os.path.join(os.path.dirname(sys.argv[0]), "zenmap.bin"), *sys.argv) os.execl(
os.path.join(os.path.dirname(sys.argv[0]), "zenmap.bin"),
*sys.argv
)