Error due to update in Python

Asked

Viewed 45 times

0

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

Follow 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)
  • It would be interesting to add more information about the error. What fails? What error message? In the end, which files were not copied? In which line does the problem occur? Have complete error traceback?

  • If the problem is inside the try, may be that the try is "hiding" the error message. Remove the try so that the error is displayed completely and then paste it into the question.

  • But I said the error, I can’t copy/move the downloaded files to the original folder without changing their permissions. All I did was point to the Ry, which is where I delete and copy the files. No error appears on the screen itself, but the end result as said is not expected.

  • How come you can’t? Files are copied but permissions are not? What are the initial permissions of files? Which permissions end in the final file? shutil.copy2 should copy permissions... make sure the target file system supports the permissions you are trying to copy?

  • Try to create a MVCE so that we can reproduce the problem and find out what is wrong

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.