Python shutil.ignore_patterns() Examples

The following are 30 code examples of shutil.ignore_patterns(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module shutil , or try the search function .
Example #1
Source File: make_package.py    From gltf-blender-importer with MIT License 8 votes vote down vote up
def make_package(suffix=None):
    this_dir = os.path.dirname(os.path.abspath(__file__))
    dist_dir = os.path.join(this_dir, 'dist')

    if not os.path.exists(dist_dir):
        os.makedirs(dist_dir)

    with tempfile.TemporaryDirectory() as tmpdir:
        shutil.copytree(
            os.path.join(this_dir, 'addons', 'io_scene_gltf_ksons'),
            os.path.join(tmpdir, 'io_scene_gltf_ksons'),
            ignore=shutil.ignore_patterns('__pycache__'))

        zip_name = 'io_scene_gltf_ksons'
        if suffix:
            zip_name += '-' + suffix

        shutil.make_archive(
            os.path.join('dist', zip_name),
            'zip',
            tmpdir) 
Example #2
Source File: util.py    From mbuild with Apache License 2.0 6 votes vote down vote up
def copy_tree(src,tgt, ignore_patterns=None, symlinks=False):
    """Copy the tree at src to tgt. This will first remove tgt if it
    already exists."""
    if verbose(1):
        msgb("COPYTREE", tgt + " <- " + src)
    if not os.path.exists(src):
        error_msg("SRC TREE DOES NOT EXIST", src)
        raise Exception
    if os.path.exists(tgt):
        if verbose(1):
            msgb("Removing existing target tree", tgt)
        shutil.rmtree(tgt, ignore_errors=True)
    if verbose(1):
        msgb("Copying to tree", tgt)
    if ignore_patterns:
        sp = shutil.ignore_patterns(ignore_patterns)
    else:
        sp = None
    shutil.copytree(src,tgt,ignore=sp, symlinks=symlinks)
    if verbose(1):
        msgb("Done copying tree", tgt) 
Example #3
Source File: addon_updater.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def create_backup(self):
        if self._verbose: print("Backing up current addon folder")
        local = os.path.join(self._updater_path,"backup")
        tempdest = os.path.join(self._addon_root,
                        os.pardir,
                        self._addon+"_updater_backup_temp")

        if os.path.isdir(local) == True:
            shutil.rmtree(local)
        if self._verbose: print("Backup destination path: ",local)

        # make the copy
        if self._backup_ignore_patterns != None:
            shutil.copytree(
                self._addon_root,tempdest,
                ignore=shutil.ignore_patterns(*self._backup_ignore_patterns))
        else:
            shutil.copytree(self._addon_root,tempdest)
        shutil.move(tempdest,local)

        # save the date for future ref
        now = datetime.now()
        self._json["backup_date"] = "{m}-{d}-{yr}".format(
                m=now.strftime("%B"),d=now.day,yr=now.year)
        self.save_updater_json() 
Example #4
Source File: runtime_env.py    From icnn with Apache License 2.0 6 votes vote down vote up
def run(main, outdir):
    script = os.path.abspath(sys.modules['__main__'].__file__)
    scriptdir, scriptfile = os.path.split(script)

    if not os.path.exists(outdir):
        os.makedirs(outdir)

    print("outdir: " + outdir)

    if FLAGS.copy:
        shutil.copytree(run_folder, path, symlinks=True, ignore=shutil.ignore_patterns('.*'))

    Executor(main, outdir).execute()


# register clean up before anybody else does 
Example #5
Source File: osutils.py    From octavia with Apache License 2.0 6 votes vote down vote up
def create_netns_dir(
            self, network_dir=None, netns_network_dir=None, ignore=None):
        if not netns_network_dir:
            netns_network_dir = self.get_netns_network_dir()
        if not network_dir:
            network_dir = self.get_network_path()
        if not ignore:
            ignore = shutil.ignore_patterns('ifcfg-eth0*', 'ifcfg-lo*')
        super(RH, self).create_netns_dir(
            network_dir, netns_network_dir, ignore)

        # Copy /etc/sysconfig/network file
        src = '/etc/sysconfig/network'
        dst = '/etc/netns/{netns}/sysconfig'.format(
            netns=consts.AMPHORA_NAMESPACE)
        shutil.copy2(src, dst) 
Example #6
Source File: deployment.py    From Dallinger with MIT License 6 votes vote down vote up
def exclusion_policy():
    """Returns a callable which, when passed a directory path and a list
    of files in that directory, will return a subset of the files which should
    be excluded from a copy or some other action.

    See https://docs.python.org/3/library/shutil.html#shutil.ignore_patterns
    """
    patterns = set(
        [
            ".git",
            "config.txt",
            "*.db",
            "*.dmg",
            "node_modules",
            "snapshots",
            "data",
            "server.log",
            "__pycache__",
        ]
    )

    return shutil.ignore_patterns(*patterns) 
Example #7
Source File: utils.py    From Gerapy with MIT License 6 votes vote down vote up
def copy_tree(src, dst):
    """
    copy tree
    :param src:
    :param dst:
    :return:
    """
    ignore = ignore_patterns(*IGNORES)
    names = os.listdir(src)
    ignored_names = ignore(src, names)
    if not os.path.exists(dst):
        os.makedirs(dst)
    
    for name in names:
        if name in ignored_names:
            continue
        
        src_name = os.path.join(src, name)
        dst_name = os.path.join(dst, name)
        if os.path.isdir(src_name):
            copy_tree(src_name, dst_name)
        else:
            copy2(src_name, dst_name)
    copystat(src, dst) 
Example #8
Source File: file_util.py    From OP_Manager with MIT License 6 votes vote down vote up
def copytree(src, dst, symlinks=False, ignore=shutil.ignore_patterns('.*', '_*')):
    """
    Copy Entire Folder
    :param src: source path
    :param dst: destination path
    :param symlinks: optional
    :param ignore: pass shutil.ignore_patterns('.*', '_*')
    :return:
    """
    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if os.path.isdir(s):
            shutil.copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d) 
Example #9
Source File: setup.py    From renku-python with Apache License 2.0 6 votes vote down vote up
def run(self):
        from renku.core.commands.init import fetch_template, \
            read_template_manifest

        with TemporaryDirectory() as tempdir:
            # download and extract template data
            temppath = Path(tempdir)
            print('downloading Renku templates...')
            fetch_template(URL, REFERENCE, temppath)
            read_template_manifest(temppath, checkout=True)

            # copy templates
            current_path = Path.cwd()
            template_path = current_path / 'renku' / 'templates'
            if template_path.exists():
                shutil.rmtree(str(template_path))
            shutil.copytree(
                str(temppath),
                str(template_path),
                ignore=shutil.ignore_patterns('.git')
            ) 
Example #10
Source File: utils.py    From transformer-kernel-ranking with Apache License 2.0 6 votes vote down vote up
def prepare_experiment(args, config):
    #if args.run_folder is not None:
    #    run_folder = args.run_folder
    #else:
    run_folder = prepare_experiment_folder(config["expirement_base_path"], args.run_name)
    #
    # saved uased config (with overwrites)
    #     
    save_config(os.path.join(run_folder,"config.yaml"),config)

    #
    # copy source code of matchmaker
    #
    dir_path = os.path.dirname(os.path.realpath(__file__))

    shutil.copytree(dir_path, os.path.join(run_folder,"matchmaker-src"), ignore=shutil.ignore_patterns("__pycache__"))

    return run_folder 
Example #11
Source File: Resources.py    From Uranium with GNU Lesser General Public License v3.0 6 votes vote down vote up
def copyVersionFolder(cls, src_path: str, dest_path: str) -> None:
        Logger.log("i", "Copying directory from '%s' to '%s'", src_path, dest_path)
        # we first copy everything to a temporary folder, and then move it to the new folder
        base_dir_name = os.path.basename(src_path)
        temp_root_dir_path = tempfile.mkdtemp("cura-copy")
        temp_dir_path = os.path.join(temp_root_dir_path, base_dir_name)
        # src -> temp -> dest
        try:
            # Copy everything, except for the logs, lock or really old (we used to copy old configs to the "old" folder)
            # config files.
            shutil.copytree(src_path, temp_dir_path,
                            ignore = shutil.ignore_patterns("*.lock", "*.log", "*.log.?", "old"))
            # if the dest_path exist, it needs to be removed first
            if not os.path.exists(dest_path):
                shutil.move(temp_dir_path, dest_path)
            else:
                Logger.log("e", "Unable to copy files to %s as the folder already exists", dest_path)
        except:
            Logger.log("e", "Something occurred when copying the version folder from '%s' to '%s'", src_path, dest_path) 
Example #12
Source File: __init__.py    From build-calibre with GNU General Public License v3.0 6 votes vote down vote up
def add_qt_framework(self, f):
        libname = f
        f = f + '.framework'
        src = join(PREFIX, 'qt', 'lib', f)
        ignore = shutil.ignore_patterns('Headers', '*.h', 'Headers/*')
        dest = join(self.frameworks_dir, f)
        shutil.copytree(src, dest, symlinks=True,
                        ignore=ignore)
        lib = os.path.realpath(join(dest, libname))
        rpath = os.path.relpath(lib, self.frameworks_dir)
        self.set_id(lib, self.FID + '/' + rpath)
        self.fix_dependencies_in_lib(lib)
        # The following is needed for codesign in OS X >= 10.9.5
        # The presence of the .prl file in the root of the framework causes
        # codesign to fail.
        with current_dir(dest):
            for x in os.listdir('.'):
                if x != 'Versions' and not os.path.islink(x):
                    os.remove(x) 
Example #13
Source File: utils.py    From sigir19-neural-ir with Apache License 2.0 6 votes vote down vote up
def prepare_experiment(args, config):
    if args.run_folder is not None:
        run_folder = args.run_folder
    else:
        run_folder = prepare_experiment_folder(config["expirement_base_path"], args.run_name)
    #
    # saved uased config (with overwrites)
    #     
    save_config(os.path.join(run_folder,"config.yaml"),config)

    #
    # copy source code of matchmaker
    #
    dir_path = os.path.dirname(os.path.realpath(__file__))

    shutil.copytree(dir_path, os.path.join(run_folder,"matchmaker-src"), ignore=shutil.ignore_patterns("__pycache__"))

    return run_folder 
Example #14
Source File: push_package.py    From fplutil with Apache License 2.0 6 votes vote down vote up
def create_mirror(self, mirror_dir):
    """Create mirror of this package and it's dependencies.

    Args:
      mirror_dir: Directory where mirror will be stored.

    Raises:
      OSError: If this method fails to create mirror.
    """
    ignore_git = shutil.ignore_patterns('.git')
    logging.debug('Copying %s to %s', self.path, mirror_dir)
    shutil.copytree(self.path, mirror_dir, ignore=ignore_git)
    dependencies_dir = os.path.join(mirror_dir, 'dependencies')
    logging.debug('Creating ' + dependencies_dir)
    os.mkdir(dependencies_dir)
    for dependency in self.dependencies:
      mirrored_dependency = os.path.join(dependencies_dir, dependency.name)
      logging.debug('Copying %s to %s ', dependency.path, mirrored_dependency)
      shutil.copytree(dependency.path, mirrored_dependency, ignore=ignore_git) 
Example #15
Source File: file.py    From Amipy with MIT License 6 votes vote down vote up
def copy_files(src,dst,ignore_pattern,make=True,render=None,strips='.tpl'):
    if not check_path(src):
        raise PathDoesntExist
    if not check_path(dst,make):
        raise PathDoesntExist
    src_files = os.listdir(src)
    ignore_fun = shutil.ignore_patterns(*ignore_pattern)
    filter_matches = ignore_fun(src,src_files)
    for file in src_files:
        if file in filter_matches:
            continue
        srcname = os.path.join(src,file)
        dstname = os.path.join(dst,file.rstrip(strips))
        if os.path.isdir(srcname):
            copy_files(srcname,dstname,ignore_pattern)
        else:
            shutil.copy2(srcname,dstname)
            if render:
                if file in render:
                    _var = render[file]
                    render_template(srcname,dstname,_var) 
Example #16
Source File: init_env.py    From kitty with GNU General Public License v3.0 6 votes vote down vote up
def build_c_extensions(ext_dir, args):
    writeable_src_dir = os.path.join(ext_dir, 'src')
    shutil.copytree(
        KITTY_DIR, writeable_src_dir, symlinks=True,
        ignore=shutil.ignore_patterns('b', 'build', 'dist', '*_commands.json', '*.o'))
    cmd = [PYTHON, 'setup.py']
    bundle = 'macos-freeze' if ismacos else 'linux-freeze'
    cmd.append(bundle)
    dest = kitty_constants['appname'] + ('.app' if ismacos else '')
    dest = os.path.join(ext_dir, dest)
    cmd += ['--prefix', dest]
    if run(*cmd, cwd=writeable_src_dir) != 0:
        print('Building of kitty package failed', file=sys.stderr)
        os.chdir(KITTY_DIR)
        run_shell()
        raise SystemExit('Building of kitty package failed')
    return ext_dir 
Example #17
Source File: rpmbuild.py    From rpmvenv with MIT License 6 votes vote down vote up
def copy_source(top, source, name=None):
    """Copy the source directory into the SOURCES directory.

    Args:
        top: The absolute path to the %_topdir.
        source: The absolute path to the source directory.
        name: The name of the directory to place in SOURCES.

    Returns:
        The absolute path to the copy.
    """
    name = name or os.path.basename(source)
    path = os.path.join(top, 'SOURCES', name)
    shutil.copytree(
        source,
        path,
        ignore=shutil.ignore_patterns(*IGNORED_PATTERNS),
    )
    return path 
Example #18
Source File: setup.py    From hvplot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def examples(path='hvplot-examples', verbose=False, force=False, root=__file__):
    """
    Copies the notebooks to the supplied path.
    """
    filepath = os.path.abspath(os.path.dirname(root))
    example_dir = os.path.join(filepath, './examples')
    if not os.path.exists(example_dir):
        example_dir = os.path.join(filepath, '../examples')
    if os.path.exists(path):
        if not force:
            print('%s directory already exists, either delete it or set the force flag' % path)
            return
        shutil.rmtree(path)
    ignore = shutil.ignore_patterns('.ipynb_checkpoints', '*.pyc', '*~')
    tree_root = os.path.abspath(example_dir)
    if os.path.isdir(tree_root):
        shutil.copytree(tree_root, path, ignore=ignore, symlinks=True)
    else:
        print('Cannot find %s' % tree_root) 
Example #19
Source File: utils.py    From eval-nas with MIT License 6 votes vote down vote up
def create_exp_dir(path, scripts_to_save=None):
    if not os.path.exists(path):
        os.makedirs(path, exist_ok=True)

    print('Experiment dir : {}'.format(path))
    if scripts_to_save is not None:
        try:
            os.mkdir(os.path.join(path, 'scripts'))
        except FileExistsError as e:
            logging.warning('Deleting all the previously stored scripts...')
            shutil.rmtree(os.path.join(path, 'scripts'))
            os.mkdir(os.path.join(path, 'scripts'))

        for script in scripts_to_save:
            dst_file = os.path.join(path, 'scripts', os.path.basename(script))
            if os.path.isdir(script):
                shutil.copytree(script, dst_file, ignore=shutil.ignore_patterns('*.pyc', 'tmp*', '*.ipynb'))
            else:
                shutil.copyfile(script, dst_file) 
Example #20
Source File: __init__.py    From holoviews with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def examples(path='holoviews-examples', verbose=False, force=False, root=__file__):
    """
    Copies the notebooks to the supplied path.
    """
    filepath = os.path.abspath(os.path.dirname(root))
    example_dir = os.path.join(filepath, './examples')
    if not os.path.exists(example_dir):
        example_dir = os.path.join(filepath, '../examples')
    if os.path.exists(path):
        if not force:
            print('%s directory already exists, either delete it or set the force flag' % path)
            return
        shutil.rmtree(path)
    ignore = shutil.ignore_patterns('.ipynb_checkpoints','*.pyc','*~')
    tree_root = os.path.abspath(example_dir)
    if os.path.isdir(tree_root):
        shutil.copytree(tree_root, path, ignore=ignore, symlinks=True)
    else:
        print('Cannot find %s' % tree_root) 
Example #21
Source File: launch.py    From mantra with Apache License 2.0 6 votes vote down vote up
def handle(self, args, unknown):
        """
        Creates a new project directory based on the name and template given by the user; configures the project
        """

        project_name = args.project_name

        library_path = '/'.join(os.path.realpath(__file__).split('/')[:-4])
        default_template_path = '%s/templates/projects/default' % library_path

        top_dir = os.path.join(os.getcwd(), project_name)
        
        # copy the project template to the path the user specified
        try:
            shutil.copytree(default_template_path, top_dir, ignore=shutil.ignore_patterns('*.pyc', '__pycache__*'))
        except FileExistsError:
            raise Exception("'%s' folder exists already" % top_dir)
        except OSError as e:
            raise Exception(e)

        self.configure_project(project_name, top_dir) 
Example #22
Source File: fetch_assignment.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def do_copy(self, src, dest):
        """Copy the src dir to the dest dir omitting the self.coursedir.ignore globs."""
        if os.path.isdir(self.dest_path):
            self.copy_if_missing(src, dest, ignore=shutil.ignore_patterns(*self.coursedir.ignore))
        else:
            shutil.copytree(src, dest, ignore=shutil.ignore_patterns(*self.coursedir.ignore)) 
Example #23
Source File: acdc_data_preparation.py    From Automated-Cardiac-Segmentation-and-Disease-Diagnosis with MIT License 5 votes vote down vote up
def copy(src, dest):
  """
  Copy function
  """
  try:
      shutil.copytree(src, dest, ignore=shutil.ignore_patterns())
  except OSError as e:
      # If the error was caused because the source wasn't a directory
      if e.errno == errno.ENOTDIR:
          shutil.copy(src, dest)
      else:
          print('Directory not copied. Error: %s' % e) 
Example #24
Source File: cmle_package.py    From ml-on-gcp with Apache License 2.0 5 votes vote down vote up
def _handle_dir(self):
        # ignore tests and test data
        shutil.copytree(
            self.source,
            self.destination,
            ignore=shutil.ignore_patterns('*_test.py', '*testing*')
        ) 
Example #25
Source File: utils.py    From musegan with MIT License 5 votes vote down vote up
def backup_src(dst):
    """Backup the source code."""
    if os.path.exists(dst):
        shutil.rmtree(dst)
    shutil.copytree(
        os.path.dirname(os.path.realpath(__file__)), dst,
        ignore=shutil.ignore_patterns('__pycache__'))

# --- Parameter file and dictionary utilities ---------------------------------- 
Example #26
Source File: post_gen_project.py    From DistributedDeepLearning with MIT License 5 votes vote down vote up
def _copy_directories(src, dst):
    try:
        shutil.copytree(src, dst, ignore=shutil.ignore_patterns(".git"))
    except PermissionError:
        print(f"Could not copy files from {src} to {dst}, permission error") 
Example #27
Source File: addon_updater.py    From Sorcar with GNU General Public License v3.0 5 votes vote down vote up
def create_backup(self):
		if self._verbose: print("Backing up current addon folder")
		local = os.path.join(self._updater_path,"backup")
		tempdest = os.path.join(self._addon_root,
						os.pardir,
						self._addon+"_updater_backup_temp")

		if self._verbose: print("Backup destination path: ",local)

		if os.path.isdir(local):
			try:
				shutil.rmtree(local)
			except:
				if self._verbose:print("Failed to removed previous backup folder, contininuing")

		# remove the temp folder; shouldn't exist but could if previously interrupted
		if os.path.isdir(tempdest):
			try:
				shutil.rmtree(tempdest)
			except:
				if self._verbose:print("Failed to remove existing temp folder, contininuing")
		# make the full addon copy, which temporarily places outside the addon folder
		if self._backup_ignore_patterns != None:
			shutil.copytree(
				self._addon_root,tempdest,
				ignore=shutil.ignore_patterns(*self._backup_ignore_patterns))
		else:
			shutil.copytree(self._addon_root,tempdest)
		shutil.move(tempdest,local)

		# save the date for future ref
		now = datetime.now()
		self._json["backup_date"] = "{m}-{d}-{yr}".format(
				m=now.strftime("%B"),d=now.day,yr=now.year)
		self.save_updater_json() 
Example #28
Source File: provenance.py    From qiime2 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def add_ancestor(self, artifact):
        other_path = artifact._archiver.provenance_dir
        if other_path is None:
            # The artifact doesn't have provenance (e.g. version 0)
            # it would be possible to invent a metadata.yaml, but we won't know
            # the framework version for the VERSION file. Even if we did
            # it won't accomplish a lot and there shouldn't be enough
            # version 0 artifacts in the wild to be important in practice.
            # NOTE: this implies that it is possible for an action.yaml file to
            # contain an artifact UUID that is not in the artifacts/ directory.
            return NoProvenance(artifact.uuid)

        destination = self.ancestor_dir / str(artifact.uuid)
        # If it exists, then the artifact is already in the provenance
        # (and so are its ancestors)
        if not destination.exists():
            # Handle root node of ancestor
            shutil.copytree(
                str(other_path), str(destination),
                ignore=shutil.ignore_patterns(self.ANCESTOR_DIR + '*'))

            # Handle ancestral nodes of ancestor
            grandcestor_path = other_path / self.ANCESTOR_DIR
            if grandcestor_path.exists():
                for grandcestor in grandcestor_path.iterdir():
                    destination = self.ancestor_dir / grandcestor.name
                    if not destination.exists():
                        shutil.copytree(str(grandcestor), str(destination))

        return str(artifact.uuid) 
Example #29
Source File: updateaddon.py    From script.service.kodi.callbacks with GNU General Public License v3.0 5 votes vote down vote up
def backup(self, src=None, destdir=None, numbackups=5):
        if src is None:
            src = self.addondir
        if destdir is None:
            destdir = self.backupdir
        ts = strftime("%Y-%m-%d-%H-%M-%S")
        destname = os.path.join(destdir, '%s-%s' % (ts, self.addonid))
        if not os.path.exists(destdir):
            os.mkdir(destdir)
        else:
            if os.path.exists(destname):
                os.remove(destname)
        self.cleartemp(recreate=True)
        archivedir = os.path.join(self.tmpdir,
                                  '%s-%s' % (os.path.split(src)[1], xbmcaddon.Addon().getSetting('installedbranch')))
        shutil.copytree(src, archivedir, ignore=shutil.ignore_patterns('*.pyc', '*.pyo', '.git', '.idea'))
        UpdateAddon.zipdir(destname, self.tmpdir)
        self.cleartemp(recreate=False)
        sorteddir = UpdateAddon.datesorteddir(destdir)
        num = len(sorteddir)
        if num > numbackups:
            for i in xrange(0, num - numbackups):
                try:
                    os.remove(sorted(sorteddir)[i][2])
                except OSError:
                    raise
        return True 
Example #30
Source File: tasks.py    From galaxy-integration-humblebundle with GNU General Public License v3.0 5 votes vote down vote up
def build(c, output=DIST_PLUGIN):
    print(f'Preparing build to folder `{output}`')

    output = Path(output).resolve()
    print('Removing', output)
    if os.path.exists(output):
        try:
            shutil.rmtree(output)
        except OSError as e:
            if hasattr(e, 'winerror') and e.winerror in [145, 5]:  # type: ignore
                # something e.g. antivirus check holds a file. Try to wait to be released for a moment
                time.sleep(3)
                shutil.rmtree(output)
            else:
                raise

    print('Copying source code to ', str(output))
    shutil.copytree('src', output, ignore=shutil.ignore_patterns(
        '__pycache__', '.mypy_cache', 'tests'))

    args = [
        PYTHON, "-m", "pip", "install",
        "-r", REQUIREMENTS,
        "--target", str(output / THIRD_PARTY_RELATIVE_DEST),
        # "--implementation", "cp",
        # "--python-version", "37",
        # "--no-deps"
    ]
    print(f'Running `{" ".join(args)}`')
    subprocess.check_call(args)