# Path: htpc/root.py
# def do_restart():
#     arguments = sys.argv[:]
#     arguments.insert(0, sys.executable)
#     if sys.platform == 'win32':
#         arguments = ['"%s"' % arg for arg in arguments]
#     os.chdir(os.getcwd())
#     cherrypy.engine.exit()
#     os.execv(sys.executable, arguments)


import os
import urllib2
import subprocess
import re
import cherrypy
import htpc
import logging
import tarfile
import shutil
from threading import Thread
from json import loads
from htpc.root import do_restart

    """ Do update from source """
    def update(self):
        self.logger.info("Attempting update from source.")

        self.UPDATING = 1
        cherrypy.engine.exit()

        tarUrl = 'https://github.com/%s/%s/tarball/%s' % (gitUser, gitRepo, gitBranch)

        # Download tar
        downloaded = self.__downloadTar(tarUrl, self.updateFile)
        if (downloaded is False):
            return False

        # Extract to temp folder
        extracted = self.__extractUpdate(self.updateFile, self.updateDir)
        if (extracted is False):
            return False

        # Overwite app source with source from extracted file
        overwritten = self.__updateSourcecode()
        if (overwritten is False):
            return False

        # Write new version to file
        self.__updateVersionFile(self.latestHash)

        # Restart HTPC Manager to make sure all new code is loaded
        self.logger.warning('Restarting HTPC Manager after update.')

# Path: htpc/root.py
# def do_restart():
#     arguments = sys.argv[:]
#     arguments.insert(0, sys.executable)
#     if sys.platform == 'win32':
#         arguments = ['"%s"' % arg for arg in arguments]
#     os.chdir(os.getcwd())
#     cherrypy.engine.exit()
#     os.execv(sys.executable, arguments)


# Path: htpc/updater.py
import os
import urllib2
import subprocess
import re
import cherrypy
import htpc
import logging
import tarfile
import shutil
from threading import Thread
from json import loads
from htpc.root import do_restart


    """ Do update from source """
    def update(self):
        self.logger.info("Attempting update from source.")

        self.UPDATING = 1
        cherrypy.engine.exit()

        tarUrl = 'https://github.com/%s/%s/tarball/%s' % (gitUser, gitRepo, gitBranch)

        # Download tar
        downloaded = self.__downloadTar(tarUrl, self.updateFile)
        if (downloaded is False):
            return False

        # Extract to temp folder
        extracted = self.__extractUpdate(self.updateFile, self.updateDir)
        if (extracted is False):
            return False

        # Overwite app source with source from extracted file
        overwritten = self.__updateSourcecode()
        if (overwritten is False):
            return False

        # Write new version to file
        self.__updateVersionFile(self.latestHash)

        # Restart HTPC Manager to make sure all new code is loaded
        self.logger.warning('Restarting HTPC Manager after update.')

        do_restart()