def is_write_permitted(a_top_root): api = EnviApi() api.reset(a_top_root) log.info("Loading %s as user %s" % (a_top_root, getpass.getuser())) api.load_data() if not api.is_valid(): log.error("This is not a valid config %s" % a_top_root) return False varea = api.get_userdev_config_path(getpass.getuser()) log.info("is_write_permitted: Checking if %s in %s\n" % (a_top_root, " ".join(varea))) if a_top_root in varea: return True return False
def move_folder(cls, src, dst, folder_permissions=0o775, rm_dir=False, do_log=True): """Moves a directory. First copies all content into target. Then deletes all content from sources. Skips files that won't delete. :param src: Source path to copy from :param dst: Destination to copy to :param folder_permissions: permissions to use for new folders """ if os.path.exists(src): if do_log is True: log.debug("Moving directory: %s -> %s" % (src, dst)) # first copy the content in the core folder src_files = cls.copy_folder_nocheck(src, dst, folder_permissions) # now clear out the install location if do_log is True: log.debug("Clearing out source location...") for f in src_files: try: # on windows, ensure all files are writable if sys.platform == "win32": attr = os.stat(f)[0] if (not attr & stat.S_IWRITE): # file is readonly! - turn off this attribute os.chmod(f, stat.S_IWRITE) os.remove(f) except Exception as e: log.error("Could not delete file %s: %s" % (f, e)) if rm_dir: try: all_files = os.listdir(src) if len(all_files) == 0: os.rmdir(src) if do_log is True: log.debug("dir %s: removed" % (src)) else: if do_log is True: log.info("no Empty %s" % all_files) except Exception as e: log.error("Could not delete dir %s: %s" % (src, e))
def copy_folder(cls, src, dst, folder_permissions=0o775, skip_list=None): """ Alternative implementation to ``shutil.copytree`` Copies recursively and creates folders if they don't already exist. Always skips system files such as ``"__MACOSX"``, ``".DS_Store"``, etc. Files will the extension ``.sh``, ``.bat`` or ``.exe`` will be given executable permissions. Returns a list of files that were copied. :param src: Source path to copy from :param dst: Destination to copy to :param folder_permissions: permissions to use for new folders :param skip_list: List of file names to skip. If this parameter is omitted or set to None, common files such as ``.git``, ``.gitignore`` etc will be ignored. :returns: List of files copied """ # files or directories to always skip SKIP_LIST_ALWAYS = ["__MACOSX", ".DS_Store"] # compute full skip list # note: we don't do # actual_skip_list = skip_list or SKIP_LIST_DEFAULT # because we want users to be able to pass in # skip_list=[] in order to clear the default skip list. if skip_list is None: actual_skip_list = SKIP_LIST_DEFAULT else: actual_skip_list = skip_list # add the items we always want to skip actual_skip_list.extend(SKIP_LIST_ALWAYS) files = [] if not os.path.exists(dst): os.mkdir(dst, folder_permissions) names = os.listdir(src) for name in names: # get rid of system files if name in actual_skip_list: continue srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: if os.path.isdir(srcname): if os.path.exists(dstname): try: cls.move_folder(dstname, tempfile.mkdtemp(), rm_dir=True, do_log=False) except Exception as e: log.error("Could not delete directory %s: %s" % (dstname, e)) pass # maybe we should raise files.extend( cls.copy_folder(srcname, dstname, folder_permissions)) else: shutil.copy(srcname, dstname) files.append(srcname) except (IOError, os.error) as e: raise IOError("Can't copy %s to %s: %s" % (srcname, dstname, e)) return files