Skip to content

Commit

Permalink
Save persistence in .config of pre-sudo user
Browse files Browse the repository at this point in the history
  • Loading branch information
akkana committed Mar 13, 2013
1 parent 0c5a8c5 commit 1b51e98
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions netscheme
Expand Up @@ -500,7 +500,9 @@ def find_and_set_scheme(newscheme, add=False, make_persistent=False) :
# Found it; scheme is a NetScheme object. # Found it; scheme is a NetScheme object.
scheme.set_scheme(add) scheme.set_scheme(add)


config_dir = os.path.expanduser("~/.config") sudo_user = os.getenv("SUDO_USER")
config_dir = os.path.expanduser('~' + sudo_user + '/.config')
# if sudo_user isn't set, this will be ~/.config
netscheme_dir = os.path.join(config_dir, "netscheme") netscheme_dir = os.path.join(config_dir, "netscheme")
current_file = os.path.join(netscheme_dir, "current") current_file = os.path.join(netscheme_dir, "current")


Expand All @@ -517,7 +519,6 @@ def find_and_set_scheme(newscheme, add=False, make_persistent=False) :
# Amazingly, there's no way under sudo python to get the # Amazingly, there's no way under sudo python to get the
# real UID. But we can get the logged-in username, # real UID. But we can get the logged-in username,
# then get the UID from that: # then get the UID from that:
sudo_user = os.getenv("SUDO_USER")
if sudo_user : if sudo_user :
pw = pwd.getpwnam(sudo_user) pw = pwd.getpwnam(sudo_user)
uid = pw.pw_uid uid = pw.pw_uid
Expand All @@ -526,11 +527,9 @@ def find_and_set_scheme(newscheme, add=False, make_persistent=False) :
uid = os.getuid() uid = os.getuid()


if not os.path.exists(config_dir) : if not os.path.exists(config_dir) :
print "Making", config_dir
os.mkdir(config_dir) os.mkdir(config_dir)
os.chown(config_dir, uid, -1) os.chown(config_dir, uid, -1)
if not os.path.exists(netscheme_dir) : if not os.path.exists(netscheme_dir) :
print "Making", netscheme_dir
os.mkdir(netscheme_dir) os.mkdir(netscheme_dir)
os.chown(netscheme_dir, uid, -1) os.chown(netscheme_dir, uid, -1)
fp = open(current_file, "w") fp = open(current_file, "w")
Expand All @@ -542,6 +541,7 @@ def find_and_set_scheme(newscheme, add=False, make_persistent=False) :
# Otherwise, if there's a default scheme and it's different # Otherwise, if there's a default scheme and it's different
# from the one we're switching to, clear it. # from the one we're switching to, clear it.
elif os.access(current_file, os.R_OK & os.W_OK) : elif os.access(current_file, os.R_OK & os.W_OK) :
print "Removing persistent file", current_file
fp = open(current_file) fp = open(current_file)
filescheme = fp.readline().strip() filescheme = fp.readline().strip()
fp.close() fp.close()
Expand Down Expand Up @@ -673,6 +673,7 @@ if __name__ == "__main__" :
#print parser.usage #print parser.usage
print_current_scheme() print_current_scheme()
else : else :
find_and_set_scheme(args[0], options.multi, options.make_persistent) find_and_set_scheme(args[0], options.multi,
make_persistent=options.make_persistent)
except KeyboardInterrupt : except KeyboardInterrupt :
print "Interrupt" print "Interrupt"

0 comments on commit 1b51e98

Please sign in to comment.