]> arthur.barton.de Git - bup.git/commitdiff
Add is_superuser() helper function
authorAneurin Price <aneurin.price@gmail.com>
Fri, 13 May 2011 13:19:42 +0000 (14:19 +0100)
committerAvery Pennarun <apenwarr@gmail.com>
Sun, 15 May 2011 19:29:55 +0000 (15:29 -0400)
This checks for an effective UID of 0 on most platforms, or the result
of the IsUserAnAdmin shell32 function on Cygwin.

Signed-off-by: Aneurin Price <aneurin.price@gmail.com>
lib/bup/helpers.py

index 13125afbfa8d373c7bf29f4ca88a29f4e8d372a5..d9d177cabf30931a14e074e828204c2b7ed13999 100644 (file)
@@ -1,7 +1,7 @@
 """Helper functions and classes for bup."""
 
 import sys, os, pwd, subprocess, errno, socket, select, mmap, stat, re, struct
-import heapq, operator, time
+import heapq, operator, time, platform
 from bup import _version, _helpers
 import bup._helpers as _helpers
 
@@ -203,6 +203,14 @@ def detect_fakeroot():
     return os.getenv("FAKEROOTKEY") != None
 
 
+def is_superuser():
+    if platform.system().startswith('CYGWIN'):
+        import ctypes
+        return ctypes.cdll.shell32.IsUserAnAdmin()
+    else:
+        return os.geteuid() == 0
+
+
 _username = None
 def username():
     """Get the user's login name."""