-
Notifications
You must be signed in to change notification settings - Fork 220
Added wrappers for shutil.copytree and os.chdir (WIP) #1561
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
Changes from 3 commits
239d926
4570093
33b74df
47d72b7
464eae1
b236781
f80a4a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -717,6 +717,9 @@ def adjust_permissions(name, permissionBits, add=True, onlyfiles=False, onlydirs | |
| Add or remove (if add is False) permissionBits from all files (if onlydirs is False) | ||
| and directories (if onlyfiles is False) in path | ||
| """ | ||
| if build_option('extended_dry_run'): | ||
| dry_run_msg("Adjusting permissions for %s to %s" % (name, permissionBits), silent=build_option('silent')) | ||
| return | ||
|
|
||
| name = os.path.abspath(name) | ||
|
|
||
|
|
@@ -1003,80 +1006,9 @@ def cleanup(logfile, tempdir, testing, silent=False): | |
|
|
||
|
|
||
| def copytree(src, dst, symlinks=False, ignore=None): | ||
| """ | ||
| Copied from Lib/shutil.py in python 2.7, since we need this to work for python2.4 aswell | ||
| and this code can be improved... | ||
|
|
||
| Recursively copy a directory tree using copy2(). | ||
|
|
||
| The destination directory must not already exist. | ||
| If exception(s) occur, an Error is raised with a list of reasons. | ||
|
|
||
| If the optional symlinks flag is true, symbolic links in the | ||
| source tree result in symbolic links in the destination tree; if | ||
| it is false, the contents of the files pointed to by symbolic | ||
| links are copied. | ||
|
|
||
| The optional ignore argument is a callable. If given, it | ||
| is called with the `src` parameter, which is the directory | ||
| being visited by copytree(), and `names` which is the list of | ||
| `src` contents, as returned by os.listdir(): | ||
|
|
||
| callable(src, names) -> ignored_names | ||
|
|
||
| Since copytree() is called recursively, the callable will be | ||
| called once for each directory that is copied. It returns a | ||
| list of names relative to the `src` directory that should | ||
| not be copied. | ||
|
|
||
| XXX Consider this example code rather than the ultimate tool. | ||
|
|
||
| """ | ||
| class Error(EnvironmentError): | ||
| pass | ||
| try: | ||
| WindowsError # @UndefinedVariable | ||
| except NameError: | ||
| WindowsError = None | ||
|
|
||
| names = os.listdir(src) | ||
| if ignore is not None: | ||
| ignored_names = ignore(src, names) | ||
| else: | ||
| ignored_names = set() | ||
| _log.debug("copytree: skipping copy of %s" % ignored_names) | ||
| os.makedirs(dst) | ||
| errors = [] | ||
| for name in names: | ||
| if name in ignored_names: | ||
| continue | ||
| srcname = os.path.join(src, name) | ||
| dstname = os.path.join(dst, name) | ||
| try: | ||
| if symlinks and os.path.islink(srcname): | ||
| linkto = os.readlink(srcname) | ||
| os.symlink(linkto, dstname) | ||
| elif os.path.isdir(srcname): | ||
| copytree(srcname, dstname, symlinks, ignore) | ||
| else: | ||
| # Will raise a SpecialFileError for unsupported file types | ||
| shutil.copy2(srcname, dstname) | ||
| # catch the Error from the recursive copytree so that we can | ||
| # continue with other files | ||
| except Error, err: | ||
| errors.extend(err.args[0]) | ||
| except EnvironmentError, why: | ||
| errors.append((srcname, dstname, str(why))) | ||
| try: | ||
| shutil.copystat(src, dst) | ||
| except OSError, why: | ||
| if WindowsError is not None and isinstance(why, WindowsError): | ||
| # Copying file access times may fail on Windows | ||
| pass | ||
| else: | ||
| errors.extend((src, dst, str(why))) | ||
| if errors: | ||
| raise Error(errors) | ||
| """Has been replaced by copy_files""" | ||
| _log.deprecated("easybuild.tools.filetools.copytree has been replaced by copy_files", '3.0') | ||
| copy_files(src, dst, symlinks=symlinks, ignore=ignore) | ||
|
|
||
|
|
||
| def encode_string(name): | ||
|
|
@@ -1158,3 +1090,39 @@ def det_size(path): | |
| _log.warn("Could not determine install size: %s" % err) | ||
|
|
||
| return installsize | ||
|
|
||
|
|
||
| def copy_files(src, dst, symlinks=False, ignore=None): | ||
| """ | ||
| Wrapper around shutil.copytree to nicely handle extended dry run | ||
| Arguments are the same as for the original function | ||
| The destination directoy must not exist | ||
|
Member
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. typo: |
||
| """ | ||
| if build_option('extended_dry_run'): | ||
| dry_run_msg("Copying directory %s to %s" % (src, dst), silent=build_option('silent')) | ||
|
Member
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.
Member
Author
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. yes, it should. I will look if I find some time
Member
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. please make this |
||
| return | ||
|
|
||
| if os.path.exists(dst): | ||
| raise EasyBuildError("Destination directory %s of copy already exists.", dst) | ||
|
Member
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. why is this needed? do we know why it would be useful if we're able to cope with this...
Member
Author
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. it's in the docs that it must be so. We could integrate it here to delete the directory if it exists but I'm not sure if that is what we want
Member
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. I don't see why we shouldn't, if the directory is empty, that is.
Member
Author
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. well, it the dir is empty, sure |
||
|
|
||
| try: | ||
| _log.debug("Copying directory %s to %s" % (src, dst)) | ||
| shutil.copytree(src, dst, symlinks=symlinks, ignore=ignore) | ||
| except OSError, err: | ||
| raise EasyBuildError("Failed to copy %s to %s: %s", src, dst, err) | ||
|
|
||
|
|
||
| def change_to(dst): | ||
| """ | ||
| Wrapper for os.chdir: change the directory to dst | ||
| @dst the destination directory | ||
| """ | ||
| if build_option('extended_dry_run'): | ||
| dry_run_msg("Changing directory to %s" % dst, silent=build_option('silent')) | ||
| return | ||
|
|
||
| try: | ||
| _log.debug("Changing to directory %s" % dst) | ||
| os.chdir(dst) | ||
| except OSError, err: | ||
| raise EasyBuildError("Failed to move to directory %s: %s", dst, err) | ||
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.
include
DEPRECATEDin the docstring please