Repository navigation
Installer: try to clean up old library files at the end of installation with out affecting the rest of a successful install #7649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7a5f3d1
nvda_slave: ensure nvdaHelperRemote.dll is correctly loaded so that n…
michaelDCurran f21f3ed
Unit tests: ensure gettext is initialized before logHandler is imported
michaelDCurran 97ce6b5
installer.tryRemoveFile: try and fix install fails.
michaelDCurran c26ce08
Abstract delete on reboot and copyFile in installer.
michaelDCurran efaad39
Address review actions
michaelDCurran 268009f
Merge branch 'master' into fixInstaller
michaelDCurran 0cdbee8
Merge branch 'master' into fixInstaller
michaelDCurran d01cd82
Revert installer code back to exactly what it used to be before fixIn…
michaelDCurran 2ae7a97
Merge branch 'master' into fixInstaller
michaelDCurran f45c0be
Fix typo
michaelDCurran 0d0cc87
Ensure that removeOldLibFiles deals with unicode paths.
michaelDCurran 8a1fec0
Fix a unicode decode error
michaelDCurran 3bed24f
fix more unicode decode errors
michaelDCurran f04622b
Allow delete on reboot for old lib files for portable copies as the …
michaelDCurran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,8 @@ | |
| import addonHandler | ||
| import easeOfAccess | ||
|
|
||
| MOVEFILE_DELAY_UNTIL_REBOOT=4 | ||
|
|
||
| _wsh=None | ||
| def _getWSH(): | ||
| global _wsh | ||
|
|
@@ -158,9 +160,13 @@ def removeOldProgramFiles(destPath): | |
| for topDir in ('lib','lib64'): | ||
| for parent,subdirs,files in os.walk(os.path.join(destPath,topDir),topdown=False): | ||
| for d in subdirs: | ||
| tryRemoveFile(os.path.join(parent,d),numRetries=1,rebootOK=True) | ||
| path=os.path.join(parent,d) | ||
| try: | ||
| os.rmdir(path) | ||
| except OSError as e: | ||
| log.debugWarning("Failed to remove directory %s, %s"%(path,e)) | ||
| for f in files: | ||
| tryRemoveFile(os.path.join(parent,f),numRetries=1,rebootOK=True) | ||
| tryRemoveFile(os.path.join(parent,f),tempDir=destPath) | ||
|
|
||
| # #4235: mpr.dll is a Windows system dll accidentally included with | ||
| # earlier versions of NVDA. Its presence causes problems in Windows Vista. | ||
|
|
@@ -323,52 +329,80 @@ def _deleteKeyAndSubkeys(key, subkey): | |
| class RetriableFailure(Exception): | ||
| pass | ||
|
|
||
| def tryRemoveFile(path,numRetries=6,retryInterval=0.5,rebootOK=False): | ||
| dirPath=os.path.dirname(path) | ||
| tempPath=tempfile.mktemp(dir=dirPath) | ||
| try: | ||
| os.rename(path,tempPath) | ||
| except (WindowsError,IOError): | ||
| raise RetriableFailure("Failed to rename file %s before remove"%path) | ||
| for count in xrange(numRetries): | ||
| def deleteFileOnReboot(path): | ||
| """ | ||
| Marks a file for delete on reboot. | ||
| This uses Windows' MoveFileEx (either unicode or ascii depending on the given path) and also prepends'\\?\' to allow for long file names. | ||
| @return: True if the action was successfull. False otherwise. | ||
| @rtype: boolean | ||
| """ | ||
| MoveFileEx=windll.kernel32.MoveFileExW if isinstance(path,unicode) else windll.kernel32.MoveFileExA | ||
| return MoveFileEx("\\\\?\\"+path,None,MOVEFILE_DELAY_UNTIL_REBOOT)!=0 | ||
|
|
||
| def copyFile(sourceFilePath,destFilePath): | ||
| """ | ||
| Copies the file at sourcePath to destPath. | ||
| This uses Windows' CopyFile and prepends'\\?\' to allow for long file names. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does the return value mean? |
||
| @return: True if the action was successfull. False otherwise. | ||
| @rtype: boolean | ||
| """ | ||
| return windll.kernel32.CopyFileW(u'\\\\?\\'+sourceFilePath,u'\\\\?\\'+destFilePath,False)!=0 | ||
|
|
||
| def tryRemoveFile(path,numTries=6,retryInterval=0.5,tempDir=None): | ||
| """ | ||
| Tries to remove a file multiple times using veris strategies. | ||
| It first tries a simple delete. If that fails, it tries to rename the file to a temp file in the given temp directory, and marks it for delete on next reboot. | ||
| If both the delete and or the rename fail, the function waits a small period of time and tries to remove the file again, just in case the file was temporarily locked. | ||
| @param path: the path to the file that should be removed | ||
| @ type path: string | ||
| @param numTries: the number of times the file should try to be removed. | ||
| @type numTries: int | ||
| @param retryInterval: the number of seconds the function waits before trying to remove the file again. | ||
| @type retryInterval: float | ||
| @param tempDir: the path to the temporary directory where files marked for delete on reboot should be stored. | ||
| This directory should be on the same physical drive as the file being removed. | ||
| A value of None (default) uses the same directory as the file being removed. | ||
| @type tempDir: string | ||
| @raises: RetriableFailure if the file could not be removed at all. | ||
| """ | ||
| if not tempDir: | ||
| tempDir=os.path.dirname(path) | ||
| for count in xrange(numTries): | ||
| if count>0: | ||
| log.debugWarning("Will try to remove file again after delay") | ||
| time.sleep(retryInterval) | ||
| try: | ||
| if os.path.isdir(tempPath): | ||
| shutil.rmtree(tempPath) | ||
| if os.path.isdir(path): | ||
| shutil.rmtree(path) | ||
| else: | ||
| os.remove(tempPath) | ||
| os.remove(path) | ||
| return | ||
| except OSError: | ||
| pass | ||
| time.sleep(retryInterval) | ||
| if rebootOK: | ||
| log.debugWarning("Failed to delete file %s, marking for delete on reboot"%tempPath) | ||
| MoveFileEx=windll.kernel32.MoveFileExW if isinstance(tempPath,unicode) else windll.kernel32.MoveFileExA | ||
| MoveFileEx("\\\\?\\"+tempPath,None,4) | ||
| except OSError as e: | ||
| log.debugWarning("Failed to delete file %s, %s, will try renaming"%(path,e)) | ||
| # The file could not be removed. | ||
| # Rename it to a temporary file, and try removing on reboot if allowed | ||
| tempPath=tempfile.mktemp(dir=tempDir) | ||
| log.debug("Renaming %s to %s"%(path,tempPath)) | ||
| try: | ||
| os.rename(path,tempPath) | ||
| except (WindowsError,IOError) as e: | ||
| log.debugWarning("Failed to rename file %s before remove on reboot"%path) | ||
| continue | ||
| log.debug("Marking file for delete on reboot: %s"%tempPath) | ||
| if not deleteFileOnReboot(tempPath): | ||
| log.warning("Could not mark file for remove on reboot: %s"%tempPath) | ||
| return | ||
| try: | ||
| os.rename(tempPath,path) | ||
| except: | ||
| log.error("Unable to rename back to %s before retriable failier"%path) | ||
| raise RetriableFailure("File %s could not be removed"%path) | ||
|
|
||
| def tryCopyFile(sourceFilePath,destFilePath): | ||
| if not sourceFilePath.startswith('\\\\'): | ||
| sourceFilePath=u"\\\\?\\"+sourceFilePath | ||
| if not destFilePath.startswith('\\\\'): | ||
| destFilePath=u"\\\\?\\"+destFilePath | ||
| if windll.kernel32.CopyFileW(sourceFilePath,destFilePath,False)==0: | ||
| if copyFile(sourceFilePath,destFilePath)==0: | ||
| errorCode=GetLastError() | ||
| log.debugWarning("Unable to copy %s, error %d"%(sourceFilePath,errorCode)) | ||
| if not os.path.exists(destFilePath): | ||
| raise OSError("error %d copying %s to %s"%(errorCode,sourceFilePath,destFilePath)) | ||
| tempPath=tempfile.mktemp(dir=os.path.dirname(destFilePath)) | ||
| try: | ||
| os.rename(destFilePath,tempPath) | ||
| except (WindowsError,OSError): | ||
| log.error("Failed to rename %s after failed overwrite"%destFilePath,exc_info=True) | ||
| raise RetriableFailure("Failed to rename %s after failed overwrite"%destFilePath) | ||
| windll.kernel32.MoveFileExW(tempPath,None,4) | ||
| if windll.kernel32.CopyFileW(sourceFilePath,destFilePath,False)==0: | ||
| tryRemoveFile(destFilePath) | ||
| log.debug("Trying to copy again after remove") | ||
| if copyFile(sourceFilePath,destFilePath)==0: | ||
| errorCode=GetLastError() | ||
| raise OSError("Unable to copy file %s to %s, error %d"%(sourceFilePath,destFilePath,errorCode)) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the type/meaning of the return value?