def copy(src, dst, hardlink=False, keep_symlink=True): '''Copy a file to another location with a bunch of link handling''' assert not P.isdir(src), 'Source path must not be a dir' assert not P.isdir(dst), 'Destination path must not be a dir' if keep_symlink and P.islink(src): assert not P.isabs(readlink(src)), 'Cannot copy symlink that points to an absolute path (%s)' % src logger.debug('%8s %s -> %s' % ('symlink', src, dst)) if P.exists(dst): assert readlink(dst) == readlink(src), 'Refusing to retarget already-exported symlink %s' % dst else: symlink(readlink(src), dst) return if P.exists(dst): assert hash_file(src) == hash_file(dst), 'Refusing to overwrite already exported dst %s' % dst else: if hardlink: try: link(src, dst) logger.debug('%8s %s -> %s' % ('hardlink', src, dst)) return except OSError, o: if o.errno != errno.EXDEV: # Invalid cross-device link, not an error, fall back to copy raise logger.debug('%8s %s -> %s' % ('copy', src, dst)) shutil.copy2(src, dst) # Bugfix, make it writeable mode = os.stat(dst)[stat.ST_MODE] os.chmod(dst, mode | stat.S_IWUSR)
def copy(src, dst, hardlink=False, keep_symlink=True): '''Copy a file to another location with a bunch of link handling''' assert not P.isdir(src), 'Source path must not be a dir' assert not P.isdir(dst), 'Destination path must not be a dir' if keep_symlink and P.islink(src): assert not P.isabs(readlink(src)), 'Cannot copy symlink that points to an absolute path (%s)' % src logger.debug('%8s %s -> %s' % ('symlink', src, dst)) if P.exists(dst): assert readlink(dst) == readlink(src), 'Refusing to retarget already-exported symlink %s' % dst else: symlink(readlink(src), dst) return if P.exists(dst): assert hash_file(src) == hash_file(dst), 'Refusing to overwrite already exported dst %s' % dst else: if hardlink: try: link(src, dst) logger.debug('%8s %s -> %s' % ('hardlink', src, dst)) return except OSError, o: # Invalid cross-device link, not an error, fall back to copy if o.errno != errno.EXDEV: raise logger.debug('%8s %s -> %s' % ('copy', src, dst)) shutil.copyfile(src, dst) # Bugfix, make it writeable mode = os.stat(dst)[stat.ST_MODE] os.chmod(dst, mode | stat.S_IWUSR)
def copy(src, dst, hardlink=False, keep_symlink=True): assert not P.isdir(src), 'Source path must not be a dir' assert not P.isdir(dst), 'Destination path must not be a dir' if keep_symlink and P.islink(src): assert not P.isabs(readlink(src)), 'Cannot copy symlink that points to an absolute path (%s)' % src logger.debug('%8s %s -> %s' % ('symlink', src, dst)) if P.exists(dst): assert readlink(dst) == readlink(src), 'Refusing to retarget already-exported symlink %s' % dst else: symlink(readlink(src), dst) return if P.exists(dst): assert hash_file(src) == hash_file(dst), 'Refusing to overwrite already exported dst %s' % dst else: if hardlink: try: link(src, dst) logger.debug('%8s %s -> %s' % ('hardlink', src, dst)) return except OSError, o: if o.errno != errno.EXDEV: # Invalid cross-device link, not an error, fall back to copy raise logger.debug('%8s %s -> %s' % ('copy', src, dst)) shutil.copy2(src, dst)
def copy(src, dst, hardlink=False, keep_symlink=True): '''Copy a file to another location with a bunch of link handling''' assert not P.isdir(src), 'Source path must not be a dir' assert not P.isdir(dst), 'Destination path must not be a dir' # There is nothing we can do about absolute sym links in system dirs. We just # trace those to the source and copy. if keep_symlink and P.islink(src): if P.isabs(readlink(src)): m = re.match('^/usr', readlink(src)) if m: print("Resolving absolute link: ", src) while P.islink(src) and \ P.isabs(readlink(src)) \ and os.path.basename(src) == os.path.basename(readlink(src)): src = readlink(src) print("Resolved to: ", src) if keep_symlink and P.islink(src): assert not P.isabs(readlink(src)), \ 'Cannot copy symlink that points to an absolute path (%s)' % src logger.debug('%8s %s -> %s' % ('symlink', src, dst)) # Some of the libraries are both in our install dir and in USGS conda's package. # That is because they do not provide headers for cspice for example. # So below we will run into trouble. Just overwrite any library we built # with the one from conda. if P.exists(dst): is_soft_link = True try: link_val = readlink(dst) except: is_soft_link = False if (not is_soft_link) or (readlink(dst) != readlink(src)): print("Will overwrite " + dst + " with " + src) os.remove(dst) if not P.exists(dst): try: symlink(readlink(src), dst) except: pass return if P.exists(dst): if hash_file(src) != hash_file(dst): print("Will overwrite " + dst + " with " + src + " having a different hash.") else: if hardlink: try: link(src, dst) logger.debug('%8s %s -> %s' % ('hardlink', src, dst)) return except OSError as o: # Invalid cross-device link, not an error, fall back to copy if o.errno != errno.EXDEV: raise logger.debug('%8s %s -> %s' % ('copy', src, dst)) shutil.copyfile(src, dst) # Bugfix, make it writeable mode = os.stat(dst)[stat.ST_MODE] os.chmod(dst, mode | stat.S_IWUSR)