def run(self):
		if not hasattr(self, 'panel'):
			self.panel = ItdchelperProjectPanel(self.window, 'ItdchelperCreateCmsProject', 'Create ITDC CMS Project:'+"\n")

		key = self.getKey()

		self.panel.append('Creating project "'+self.domain+'"')
		self.panel.append('Loading')

		thread2 = ItdchelperLoading(self.panel)
		thread2.start()


		service_url = self.localservice_url + '?mode=create&name='+self.domain+'&key='+key

		#self.panel.append('DEBUG: request to '+service_url)

		request = ITDCRequest(service_url)
		if (request.isError()):
			thread2.stop()
			stderr = "Error: "+request.getError()
			elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
			txt = stderr + elapsed
			self.panel.replace(txt)
			self.panel.finish()
			return False
		json_data = request.getJSON()


		#self.panel.append(' Success', False)

		# insert in service.itdc.ge

		service_url = self.remoteservice_url + '/name/'+self.domain+'/key/'+key

		#self.panel.append('DEBUG: request to '+service_url)

		request = ITDCRequest(service_url)
		if (request.isError()):
			thread2.stop()
			stderr = "Error: "+request.getError()
			elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
			txt = stderr + elapsed
			self.panel.replace(txt)
			self.panel.finish()
			return False
		json_data2 = request.getJSON()

		#self.panel.append(' Success', False)

		thread2.stop()

		elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
		status = json_data['status']
		msg = json_data['msg'] + elapsed
		if (status == "ERROR"):
			msg = "Error: "+msg

		self.panel.replace(msg)
		self.panel.finish()
    def run(self):
        if not hasattr(self, 'panel'):
            self.panel = ItdchelperProjectPanel(self.window, 'ItdchelperPHPUnit', 'PHPUnit Tests: '+self.pu_folder+"\n")


        self.panel.append('PHPUnit is running')
        self.panel.append('Loading')

        thread2 = ItdchelperLoading(self.panel)
        thread2.start()



        try:
            if (sublime.platform() == "windows"):
                startupinfo = subprocess.STARTUPINFO()
                startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                startupinfo.wShowWindow = subprocess.SW_HIDE
                cmd = ["plink", "-pw", self.server_pass, self.server_user+"@"+self.server_addr, "cd "+self.pu_folder+" && phpunit"]
                p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo, shell=False, creationflags=subprocess.SW_HIDE)
            else:
                p = subprocess.Popen(["ssh", "", "-l", filters, "-f", "-", "-o", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            stdout, stderr = p.communicate()
        except Exception as e:
            stderr = str(e)
            thread2.stop()
            stderr = "Error: "+stderr
            elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
            txt = stderr + elapsed
            self.panel.replace(txt)
            self.panel.finish()
            return False



        if (not stderr and not stdout):
            thread2.stop()
            stderr = "Unknown error!"
            stderr = "Error: "+stderr
            elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
            txt = stderr + elapsed
            self.panel.replace(txt)
            self.panel.finish()
            return False


        thread2.stop()

        elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
        msg = stdout.decode('UTF-8') + elapsed

        self.panel.replace(msg)
        self.panel.finish()
    def run(self):
        if not hasattr(self, 'panel'):
            self.panel = ItdchelperProjectPanel(self.window, 'ItdchelperToolsCmd', 'Command: "'+self.cmd+'"'+"\n")


        self.panel.append('Executing command')
        self.panel.append('Loading')

        thread2 = ItdchelperLoading(self.panel)
        thread2.start()

        cmd = quote(self.cmd)

        service_url = self.localservice_url + '?cmd='+cmd
        #print(service_url)
        #self.panel.append('DEBUG: request to '+service_url)

        request = ITDCRequest(service_url)
        if (request.isError()):
            thread2.stop()
            stderr = "Error: "+request.getError()
            elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
            txt = stderr + elapsed
            self.panel.replace(txt)
            self.panel.finish()
            return False
        json_data = request.getJSON()


        #self.panel.append(' Success', False)

        thread2.stop()

        elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
        status = json_data['status']
        msg = json_data['msg'] + elapsed
        if (status == "ERROR"):
            msg = "Error: "+msg

        self.panel.replace(msg)
        self.panel.finish()
	def run(self):
		if not hasattr(self, 'panel'):
			self.panel = ItdchelperProjectPanel(self.window, 'ItdchelperBackupEmptyProject', 'Backup ITDC Empty Project:'+"\n")

		self.panel.append('Backuping project "'+self.domain+'"')
		self.panel.append('Loading')

		thread2 = ItdchelperLoading(self.panel)
		thread2.start()


		service_url = self.localservice_url + '?mode=backuphost&name='+self.domain

		#self.panel.append('DEBUG: request to '+service_url)

		request = ITDCRequest(service_url)
		if (request.isError()):
			thread2.stop()
			stderr = "Error: "+request.getError()
			elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
			txt = stderr + elapsed
			self.panel.replace(txt)
			self.panel.finish()
			return False
		json_data = request.getJSON()

		#self.panel.append(' Success', False)

		thread2.stop()

		elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
		status = json_data['status']
		msg = json_data['msg'] + elapsed
		if (status == "ERROR"):
			msg = "Error: "+msg

		self.panel.replace(msg)
		self.panel.finish()
class ItdchelperPhpUnitProcess(threading.Thread):
    settings = None
    view = None
    window = None
    start_time = None

    server_addr = None
    server_user = None
    server_pass = None
    plink_path = None
    pu_folder = 'cmsv3'


    def __init__(self, pview):
        self.settings = sublime.load_settings('ITDCHelper.sublime-settings')
        self.start_time = time.time()
        self.view = pview
        self.window = self.view.window()

        project_data = self.window.project_data()
        if (project_data is None):
            show_error("Project Data not found!")
            return


        project_folder = project_data['folders'][0]['path']
        if (project_folder is None):
            show_error("Project folder not found!")
            return


        if not os.path.isfile("%s" % os.path.join(project_folder, 'phpunit.xml')) and not os.path.isfile("%s" % os.path.join(project_folder, 'phpunit.xml.dist')):
            show_error("phpunit.xml or phpunit.xml.dist not found!")
            return


        path = project_folder.replace('\\', '/')
        pu_folder = path.rsplit("/",1)[1]
        if (pu_folder is None):
            show_error("Project folder not found!")
            return
        self.pu_folder = pu_folder


        self.server_addr = self.settings.get('server_addr')
        if ((self.server_addr is None) or (len(self.server_addr) == 0)):
            show_error("Server address not defined")
            return

        self.server_user = self.settings.get('server_user')
        if ((self.server_user is None) or (len(self.server_user) == 0)):
            show_error("Server user not defined")
            return

        self.server_pass = self.settings.get('server_pass')
        if ((self.server_pass is None) or (len(self.server_pass) == 0)):
            show_error("Server password not defined")
            return

        self.plink_path = self.settings.get('plink_path')

        threading.Thread.__init__(self)



    def run(self):
        if not hasattr(self, 'panel'):
            self.panel = ItdchelperProjectPanel(self.window, 'ItdchelperPHPUnit', 'PHPUnit Tests: '+self.pu_folder+"\n")


        self.panel.append('PHPUnit is running')
        self.panel.append('Loading')

        thread2 = ItdchelperLoading(self.panel)
        thread2.start()



        try:
            if (sublime.platform() == "windows"):
                startupinfo = subprocess.STARTUPINFO()
                startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                startupinfo.wShowWindow = subprocess.SW_HIDE
                cmd = ["plink", "-pw", self.server_pass, self.server_user+"@"+self.server_addr, "cd "+self.pu_folder+" && phpunit"]
                p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo, shell=False, creationflags=subprocess.SW_HIDE)
            else:
                p = subprocess.Popen(["ssh", "", "-l", filters, "-f", "-", "-o", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            stdout, stderr = p.communicate()
        except Exception as e:
            stderr = str(e)
            thread2.stop()
            stderr = "Error: "+stderr
            elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
            txt = stderr + elapsed
            self.panel.replace(txt)
            self.panel.finish()
            return False



        if (not stderr and not stdout):
            thread2.stop()
            stderr = "Unknown error!"
            stderr = "Error: "+stderr
            elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
            txt = stderr + elapsed
            self.panel.replace(txt)
            self.panel.finish()
            return False


        thread2.stop()

        elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
        msg = stdout.decode('UTF-8') + elapsed

        self.panel.replace(msg)
        self.panel.finish()


    def isRunning(self):
        return self.running
class ItdchelperBackupEmptyProjectProcess(threading.Thread):
	settings = None
	localservice_url = ''
	domain = ''
	view = None
	window = None
	start_time = None

	def __init__(self, text, pview):
		self.settings = sublime.load_settings('ITDCHelper.sublime-settings')
		self.start_time = time.time()
		self.domain = text
		self.view = pview
		self.window = self.view.window()



		self.localservice_url = 'http://tools.local.itdc.ge/project/process.php'

		threading.Thread.__init__(self)


	def run(self):
		if not hasattr(self, 'panel'):
			self.panel = ItdchelperProjectPanel(self.window, 'ItdchelperBackupEmptyProject', 'Backup ITDC Empty Project:'+"\n")

		self.panel.append('Backuping project "'+self.domain+'"')
		self.panel.append('Loading')

		thread2 = ItdchelperLoading(self.panel)
		thread2.start()


		service_url = self.localservice_url + '?mode=backuphost&name='+self.domain

		#self.panel.append('DEBUG: request to '+service_url)

		request = ITDCRequest(service_url)
		if (request.isError()):
			thread2.stop()
			stderr = "Error: "+request.getError()
			elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
			txt = stderr + elapsed
			self.panel.replace(txt)
			self.panel.finish()
			return False
		json_data = request.getJSON()

		#self.panel.append(' Success', False)

		thread2.stop()

		elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
		status = json_data['status']
		msg = json_data['msg'] + elapsed
		if (status == "ERROR"):
			msg = "Error: "+msg

		self.panel.replace(msg)
		self.panel.finish()
class ItdchelperCreateCmsProjectProcess(threading.Thread):
	settings = None
	localservice_url = ''
	remoteservice_url = ''
	domain = ''
	view = None
	window = None
	start_time = None


	def __init__(self, text, pview):
		self.settings = sublime.load_settings('ITDCHelper.sublime-settings')
		self.start_time = time.time()
		self.domain = text
		self.view = pview
		self.window = self.view.window()

		api_key = self.settings.get('api_key')
		if ((api_key is None) or (len(api_key) != 40)):
			show_error("API KEY not found")
			return

		self.localservice_url = 'http://tools.local.itdc.ge/project/process.php'
		self.remoteservice_url = 'http://service.itdc.ge/api/addproject/token/'+api_key

		threading.Thread.__init__(self)



	def run(self):
		if not hasattr(self, 'panel'):
			self.panel = ItdchelperProjectPanel(self.window, 'ItdchelperCreateCmsProject', 'Create ITDC CMS Project:'+"\n")

		key = self.getKey()

		self.panel.append('Creating project "'+self.domain+'"')
		self.panel.append('Loading')

		thread2 = ItdchelperLoading(self.panel)
		thread2.start()


		service_url = self.localservice_url + '?mode=create&name='+self.domain+'&key='+key

		#self.panel.append('DEBUG: request to '+service_url)

		request = ITDCRequest(service_url)
		if (request.isError()):
			thread2.stop()
			stderr = "Error: "+request.getError()
			elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
			txt = stderr + elapsed
			self.panel.replace(txt)
			self.panel.finish()
			return False
		json_data = request.getJSON()


		#self.panel.append(' Success', False)

		# insert in service.itdc.ge

		service_url = self.remoteservice_url + '/name/'+self.domain+'/key/'+key

		#self.panel.append('DEBUG: request to '+service_url)

		request = ITDCRequest(service_url)
		if (request.isError()):
			thread2.stop()
			stderr = "Error: "+request.getError()
			elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
			txt = stderr + elapsed
			self.panel.replace(txt)
			self.panel.finish()
			return False
		json_data2 = request.getJSON()

		#self.panel.append(' Success', False)

		thread2.stop()

		elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
		status = json_data['status']
		msg = json_data['msg'] + elapsed
		if (status == "ERROR"):
			msg = "Error: "+msg

		self.panel.replace(msg)
		self.panel.finish()

	def getKey(self):
		chars = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHKMNPQRSTUVWXYZ"
		key = ''.join(sample(chars, 40))
		return key

	def isRunning(self):
		return self.running
class ItdchelperToolsChmodProcess(threading.Thread):
    settings = None
    localservice_url = ''
    path = ''
    recursively = False
    chmod = False
    view = None
    window = None
    start_time = None


    def __init__(self, path, chmod, recursively, pview):
        self.settings = sublime.load_settings('ITDCHelper.sublime-settings')
        self.start_time = time.time()
        self.path = path
        if (recursively is True):
            self.recursively = 1
        else:
            self.recursively = 0


        self.chmod = chmod
        self.view = pview
        self.window = self.view.window()
        self.localservice_url = 'http://tools.local.itdc.ge/project/chmod.php'
        threading.Thread.__init__(self)



    def run(self):
        if not hasattr(self, 'panel'):
            self.panel = ItdchelperProjectPanel(self.window, 'ItdchelperToolsChmod', 'Chmod Path: "'+self.path+'"'+"\n")


        self.panel.append('Chmodding path "'+self.path+'"')
        self.panel.append('Loading')

        thread2 = ItdchelperLoading(self.panel)
        thread2.start()


        service_url = self.localservice_url + '?mode=chmod&chmod='+self.chmod+'&recursively='+str(self.recursively)+'&path='+self.path
        #print(service_url)
        #self.panel.append('DEBUG: request to '+service_url)

        request = ITDCRequest(service_url)
        if (request.isError()):
            thread2.stop()
            stderr = "Error: "+request.getError()
            elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
            txt = stderr + elapsed
            self.panel.replace(txt)
            self.panel.finish()
            return False
        json_data = request.getJSON()


        #self.panel.append(' Success', False)

        thread2.stop()

        elapsed = "\n= = = = = = = = = = = = = = =\nExecution time: %.3f sec" % round((time.time() - self.start_time), 3)
        status = json_data['status']
        msg = json_data['msg'] + elapsed
        if (status == "ERROR"):
            msg = "Error: "+msg

        self.panel.replace(msg)
        self.panel.finish()


    def isRunning(self):
        return self.running