Error updating function in Python

0

I'm developing an install.py file that will serve as an alternative to install and create qq program executable in a simple and easy way. I am having difficulty updating function, I get to download the git update but I can not copy / move to the main folder, the files for some reason that I do not know, are now restricted and also become executable. It works on Python2 and Python3, I'm using Linux for testing.

Here's my try, tips and enhancements are also welcome:

def checkVersions():
    cv = getCurrentVersion() #Função que apenas retorna a versão instalada
    lv = checkLastVersion() #Função que apenas retorna a versão do git
    root = getPath() #Retorna o caminho até a pasta atual
    if (cv < lv):
        print ('Você tem a versão ' + str(cv) + ', enquanto a mais recente é a versão ' + str(lv) + '.')
        resp = input('Há uma versão mais recente desse aplicativo, deseja atualizar?(s/n)\n')
        if (resp == 'n'):
            pass
        elif (resp == 's'):
            import subprocess
            print('Atualizando para versão ' + str(lv) + '...')

            try:
                proc = subprocess.Popen(['git','--version'], stdout=subprocess.PIPE)
                print(proc.stdout.readlines())
            except OSError as error:
                print (error)
                print ('Git não instalado, deseja instalar para atualizar?(s/n)')
                if (resp == 'n'):
                    print ('Atualização cancelada.')
                    return
                elif (resp == 's'):
                    os.system('sudo apt-get install git')
                else:
                    print ('Opção invalida, atualização negada.')
                    return

            currentFolder = '/'.join(getPath().split('/')[:-1])
            os.chdir(currentFolder)

            os.mkdir(Repo + '-tmp')

            currentFolder = currentFolder + '/' + Repo + '-tmp'
            os.chdir(currentFolder)

            os.system("git clone https://github.com/{user}/{repo}.git".format(user = User, repo = Repo))

            tmpFolder = currentFolder + '/' + Repo + '/'

            print ('Copiando os novos arquivos...')
            # O Problema está dentro desse try
            try:
                for files in os.listdir(root):
                    if os.path.isfile(files):
                        if files != 'install.py':
                            os.system('sudo rm -f ' + root + '/' + files)
                #           os.remove(files)
                    else:
                        os.system('sudo rm -rf ' + root + '/' + files)
                #       rmtree(files)

                for files in os.listdir(tmpFolder):
                    src = os.path.join(tmpFolder, files)
                    dst = os.path.join(root, files)
                    if os.path.isfile(files):
                        if files != 'install.py':
                            copy2(src, dst)
                    else:
                        copyFolder(tmpFolder, root)

                changePerm(root)

            except OSError as error:
                os.chdir(root)
                os.system('sudo rm -rf ' + currentFolder)
                print (error)
                print ('Erro ao atualizar')
                return

            os.chdir(root)
            os.system('sudo rm -rf ' + currentFolder)
            print('Atualização para versão ' + str(lv) + ' concluida!')
        else:
            print('Resposta inválida...')
            checkVersions()

Complementary functions:

def copyFolder(src, dst, symlinks=False, ignore=None):
    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            if item != 'install.py':
                copy2(s, d)

def changePerm(path):
    os.chdir(path)
    for r, d, f in os.walk(path):
        os.chmod(r, 0o777)
    
asked by anonymous 01.10.2018 / 21:35

0 answers