Skip to content

Commit

Permalink
init: Fix sudo message on Ubuntu (OSGeo#2532)
Browse files Browse the repository at this point in the history
Create <mapset>/.sudo_as_admin_successful when $HOME/.sudo_as_admin_successful exists to avoid message about sudo for the a Bash instance for each session.

Contains several unrelated Black changes.

Fixes OSGeo#753.
  • Loading branch information
susi authored and neteler committed Nov 7, 2023
1 parent 74c15a8 commit cf20a66
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions lib/init/grass.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,7 @@ def create_fallback_session(gisrc, tmpdir):
"""Creates fallback temporary session"""
# Create temporary location
set_mapset(
gisrc=gisrc,
geofile="XY",
create_new=True,
tmp_location=True,
tmpdir=tmpdir,
gisrc=gisrc, geofile="XY", create_new=True, tmp_location=True, tmpdir=tmpdir
)


Expand Down Expand Up @@ -1762,9 +1758,7 @@ def script_path(batch_job):
script_in_addon_path = None
if "GRASS_ADDON_BASE" in os.environ:
script_in_addon_path = os.path.join(
os.environ["GRASS_ADDON_BASE"],
"scripts",
batch_job[0],
os.environ["GRASS_ADDON_BASE"], "scripts", batch_job[0]
)
if script_in_addon_path and os.path.exists(script_in_addon_path):
batch_job[0] = script_in_addon_path
Expand All @@ -1777,8 +1771,7 @@ def script_path(batch_job):
proc = Popen(batch_job, shell=False, env=os.environ)
except OSError as error:
error_message = _("Execution of <{cmd}> failed:\n" "{error}").format(
cmd=batch_job_string,
error=error,
cmd=batch_job_string, error=error
)
# No such file or directory
if error.errno == errno.ENOENT:
Expand Down Expand Up @@ -2044,6 +2037,21 @@ def sh_like_startup(location, location_name, grass_env_file, sh):
""".format(
sh_history=sh_history
)
# Ubuntu sudo creates a file .sudo_as_admin_successful and bash checks
# for this file in the home directory from /etc/bash.bashrc and prints a
# message if it's not detected. This can be suppressed with either
# creating the file ~/.sudo_as_admin_successful (it's always empty) or
# by creating a file ~/.hushlogin (also an empty file)
# Here we create the file in the Mapset directory if it exists in the
# user's home directory.
sudo_success_file = ".sudo_as_admin_successful"
if os.path.exists(os.path.join(userhome, sudo_success_file)):
try:
# Open with append so that if the file already exists there
# isn't any error.
fh = open(os.path.join(home, sudo_success_file), "a")
finally:
fh.close()

# double curly brackets means single one for format function
# setting LOCATION for backwards compatibility
Expand Down Expand Up @@ -2564,8 +2572,7 @@ def main():
from grass.grassdb.checks import can_start_in_mapset

last_mapset_usable = can_start_in_mapset(
mapset_path=last_mapset_path,
ignore_lock=params.force_gislock_removal,
mapset_path=last_mapset_path, ignore_lock=params.force_gislock_removal
)
debug(f"last_mapset_usable: {last_mapset_usable}")
if not last_mapset_usable:
Expand Down

0 comments on commit cf20a66

Please sign in to comment.