import logging
import os.path
from parallels.utils import mkdir_p
from parallels.common.utils.windows_utils import path_join as windows_path_join

logger = logging.getLogger(__name__)

class SessionDir(object):
	def __init__(self, session_dir):
		self.session_dir = session_dir

	def get_file_path(self, filename):
		return os.path.join(self.session_dir, filename)
	
	def init(self):
		if not os.path.exists(self.session_dir):
			mkdir_p(self.session_dir)
		logger.info(u"All temporary data files will be placed in '%s'", self.session_dir)

class RemoteSessionDir(object):
	def __init__(self, session_dir, runner):
		self.session_dir = session_dir
		self.runner = runner

	def get_file_path(self, filename):
		raise NotImplementedError()
	
	def init(self):
		self.runner.mkdir(self.session_dir)

class WindowsRemoteSessionDir(RemoteSessionDir):
	def get_file_path(self, filename):
		return windows_path_join(self.session_dir, filename)

	def init(self):
		self.runner.mkdir(self.session_dir)
		icacl_commands = ['/inheritance:r', '/grant Administrators:(OI)(CI)F', '/grant SYSTEM:(OI)(CI)F']
		for icacl_command in icacl_commands:
			self.runner.sh("icacls {session-dir} %s" % (icacl_command,), {'session-dir': self.session_dir})
