# Path: snap/osregistry.py
# class OS:
#     '''helper methods to perform OS level operations'''
# 
#     current_os = lookup()
# 
#     def is_linux(operating_system=None):
#         '''return true if we're running a linux system'''
#         if operating_system == None:
#             operating_system = OS.current_os
#         return operating_system in ['fedora', 'rhel', 'centos', 'ubuntu', 'debian', 'mock']
#     is_linux = staticmethod(is_linux)
# 
#     def is_windows(operating_system=None):
#         '''return true if we're running an apt based system'''
#         if operating_system == None:
#             operating_system = OS.current_os
#         return operating_system in ['windows', 'mock_windows']
#     is_windows = staticmethod(is_windows)
# 
#     def apt_based(operating_system=None):
#         '''return true if we're running an apt based system'''
#         if operating_system == None:
#             operating_system = OS.current_os
#         return operating_system in ['ubuntu', 'debian']
#     apt_based = staticmethod(apt_based)
# 
#     def yum_based(operating_system=None):
#         '''return true if we're running an yum based system'''
#         if operating_system == None:
#             operating_system = OS.current_os
#         return operating_system in ['fedora', 'rhel', 'centos']
#     yum_based = staticmethod(yum_based)
# 
#     def get_root(operating_system=None):
#         '''return the root directory for the specified os'''
#         return 'C:\\' if OS.is_windows(operating_system) else '/'
#     get_root = staticmethod(get_root)
# 
#     def get_path_seperator(operating_system=None):
#         return '\\' if OS.is_windows(operating_system) else '/'
#     get_path_seperator = staticmethod(get_path_seperator)
# 
#     def default_backend_for_target(target, operating_system=None):
#         '''return the default backend configured for the given os / snapshot target'''
#         if operating_system == None:
#             operating_system = OS.current_os
#         return DEFAULT_BACKENDS[operating_system][target]
#     default_backend_for_target = staticmethod(default_backend_for_target)
# 
# class OSUtils:
# 
#     def null_file():
#         if OS.is_windows():
#             return 'nul'
#         else:
#             return '/dev/null'
#     null_file = staticmethod(null_file)
# 
#     def chown(cfile, uid=None, gid=None, username=None):
#         if os.path.isdir(cfile):
#             for f in os.listdir(cfile):
#                 OSUtils.chown(os.path.join(cfile, f),
#                               uid=uid, gid=gid, username=username)
#         
#         if OS.is_windows() and username != None:
#             # we don't actually change owner, just assign the user full permissions
#             null_file = open(OSUtils.null_file(), 'w')
#             popen = subprocess.Popen(["icacls", cfile, "/grant:r", username + ":F"],
#                                      stdout=null_file, stderr=null_file)
#             popen.wait()
#         elif OS.is_linux():
#             if (uid == None or gid == None) and username != None:
#                 import pwd
#                 user = pwd.getpwnam(username)
#                 uid = user.pw_uid
#                 gid = user.pw_gid
#             if uid != None and gid != None:
#                 os.chown(cfile, uid, gid)
#     chown = staticmethod(chown)
#     
#     def is_superuser():
#         if OS.is_windows():
#             # checks if snap is running under elevated privileges 
#             # Note this means more than running it as a user in the admin group. You must launch
#             # snaptool, the snap gui, or the development environment (cmd terminal, eclipse, or other)
#             # by right clicking the program and selecting 'run as administrator',
#             # even if your user is marked as an admin in the windows users control panel
#             
#             # XXX hacky way of doing this, but simplest way I could find outside of using the win32 api
#             try:
#                 file("C:\Windows\snap-check", "w")
#             except IOError, e:
#                 return False
#             
#             # We also check to see if the current user is in the admin group
#             user_name = os.environ["USERNAME"]            
#             process = subprocess.Popen(["net", "localgroup", "administrators"], stdout=subprocess.PIPE)
#             while True:
#                 line = process.stdout.readline()
#                 if not line:
#                     return False
#                 if re.match("^.*" + user_name + ".*$", line):
#                     return True
#         else:
#             return os.geteuid() == 0
#     is_superuser = staticmethod(is_superuser)


import os
import re
import shutil
import unittest
import tempfile
import subprocess
        import pwd
from snap.osregistry import OS, OSUtils
    def testOsRoot(self):
        self.assertEqual('/', OS.get_root('fedora'))
        self.assertEqual('/', OS.get_root('ubuntu'))
        self.assertEqual('C:\\', OS.get_root('windows'))
        
    @unittest.skipUnless(OS.is_windows(), "only relevant for windows")
    def testWindowsRoot(self):
        self.assertEqual('C:\\', OS.get_root())
        
    @unittest.skipUnless(OS.is_linux(), "only relevant for linux")
    def testLinuxRoot(self):
        self.assertEqual('/', OS.get_root())

    def testPathSeperator(self):
        self.assertEqual('/', OS.get_path_seperator('fedora'))
        self.assertEqual('/', OS.get_path_seperator('ubuntu'))
        self.assertEqual('\\', OS.get_path_seperator('windows'))
        
    @unittest.skipUnless(OS.is_windows(), "only relevant for windows")
    def testWindowsPathSeperator(self):
        self.assertEqual('\\', OS.get_path_seperator())
        
    @unittest.skipUnless(OS.is_linux(), "only relevant for linux")
    def testLinuxPathSeperator(self):
        self.assertEqual('/', OS.get_path_seperator())

    #def testDefaultBackendForTarget(self):
    
    @unittest.skipUnless(OS.is_windows(), "only relevant for windows")
    def testWindowsNullFile(self):

# Path: snap/osregistry.py
# class OS:
#     '''helper methods to perform OS level operations'''
# 
#     current_os = lookup()
# 
#     def is_linux(operating_system=None):
#         '''return true if we're running a linux system'''
#         if operating_system == None:
#             operating_system = OS.current_os
#         return operating_system in ['fedora', 'rhel', 'centos', 'ubuntu', 'debian', 'mock']
#     is_linux = staticmethod(is_linux)
# 
#     def is_windows(operating_system=None):
#         '''return true if we're running an apt based system'''
#         if operating_system == None:
#             operating_system = OS.current_os
#         return operating_system in ['windows', 'mock_windows']
#     is_windows = staticmethod(is_windows)
# 
#     def apt_based(operating_system=None):
#         '''return true if we're running an apt based system'''
#         if operating_system == None:
#             operating_system = OS.current_os
#         return operating_system in ['ubuntu', 'debian']
#     apt_based = staticmethod(apt_based)
# 
#     def yum_based(operating_system=None):
#         '''return true if we're running an yum based system'''
#         if operating_system == None:
#             operating_system = OS.current_os
#         return operating_system in ['fedora', 'rhel', 'centos']
#     yum_based = staticmethod(yum_based)
# 
#     def get_root(operating_system=None):
#         '''return the root directory for the specified os'''
#         return 'C:\\' if OS.is_windows(operating_system) else '/'
#     get_root = staticmethod(get_root)
# 
#     def get_path_seperator(operating_system=None):
#         return '\\' if OS.is_windows(operating_system) else '/'
#     get_path_seperator = staticmethod(get_path_seperator)
# 
#     def default_backend_for_target(target, operating_system=None):
#         '''return the default backend configured for the given os / snapshot target'''
#         if operating_system == None:
#             operating_system = OS.current_os
#         return DEFAULT_BACKENDS[operating_system][target]
#     default_backend_for_target = staticmethod(default_backend_for_target)
# 
# class OSUtils:
# 
#     def null_file():
#         if OS.is_windows():
#             return 'nul'
#         else:
#             return '/dev/null'
#     null_file = staticmethod(null_file)
# 
#     def chown(cfile, uid=None, gid=None, username=None):
#         if os.path.isdir(cfile):
#             for f in os.listdir(cfile):
#                 OSUtils.chown(os.path.join(cfile, f),
#                               uid=uid, gid=gid, username=username)
#         
#         if OS.is_windows() and username != None:
#             # we don't actually change owner, just assign the user full permissions
#             null_file = open(OSUtils.null_file(), 'w')
#             popen = subprocess.Popen(["icacls", cfile, "/grant:r", username + ":F"],
#                                      stdout=null_file, stderr=null_file)
#             popen.wait()
#         elif OS.is_linux():
#             if (uid == None or gid == None) and username != None:
#                 import pwd
#                 user = pwd.getpwnam(username)
#                 uid = user.pw_uid
#                 gid = user.pw_gid
#             if uid != None and gid != None:
#                 os.chown(cfile, uid, gid)
#     chown = staticmethod(chown)
#     
#     def is_superuser():
#         if OS.is_windows():
#             # checks if snap is running under elevated privileges 
#             # Note this means more than running it as a user in the admin group. You must launch
#             # snaptool, the snap gui, or the development environment (cmd terminal, eclipse, or other)
#             # by right clicking the program and selecting 'run as administrator',
#             # even if your user is marked as an admin in the windows users control panel
#             
#             # XXX hacky way of doing this, but simplest way I could find outside of using the win32 api
#             try:
#                 file("C:\Windows\snap-check", "w")
#             except IOError, e:
#                 return False
#             
#             # We also check to see if the current user is in the admin group
#             user_name = os.environ["USERNAME"]            
#             process = subprocess.Popen(["net", "localgroup", "administrators"], stdout=subprocess.PIPE)
#             while True:
#                 line = process.stdout.readline()
#                 if not line:
#                     return False
#                 if re.match("^.*" + user_name + ".*$", line):
#                     return True
#         else:
#             return os.geteuid() == 0
#     is_superuser = staticmethod(is_superuser)


# Path: test/osregistrytest.py
import os
import re
import shutil
import unittest
import tempfile
import subprocess
        import pwd
from snap.osregistry import OS, OSUtils

    def testOsRoot(self):
        self.assertEqual('/', OS.get_root('fedora'))
        self.assertEqual('/', OS.get_root('ubuntu'))
        self.assertEqual('C:\\', OS.get_root('windows'))
        
    @unittest.skipUnless(OS.is_windows(), "only relevant for windows")
    def testWindowsRoot(self):
        self.assertEqual('C:\\', OS.get_root())
        
    @unittest.skipUnless(OS.is_linux(), "only relevant for linux")
    def testLinuxRoot(self):
        self.assertEqual('/', OS.get_root())

    def testPathSeperator(self):
        self.assertEqual('/', OS.get_path_seperator('fedora'))
        self.assertEqual('/', OS.get_path_seperator('ubuntu'))
        self.assertEqual('\\', OS.get_path_seperator('windows'))
        
    @unittest.skipUnless(OS.is_windows(), "only relevant for windows")
    def testWindowsPathSeperator(self):
        self.assertEqual('\\', OS.get_path_seperator())
        
    @unittest.skipUnless(OS.is_linux(), "only relevant for linux")
    def testLinuxPathSeperator(self):
        self.assertEqual('/', OS.get_path_seperator())

    #def testDefaultBackendForTarget(self):
    
    @unittest.skipUnless(OS.is_windows(), "only relevant for windows")
    def testWindowsNullFile(self):

        self.assertEqual("nul", OSUtils.null_file())