def uninstall(client): """Uninstall Git hooks.""" from git.index.fun import hook_path as get_hook_path for hook in HOOKS: hook_path = Path(get_hook_path(hook, client.repo.git_dir)) if hook_path.exists(): hook_path.unlink()
def test_file_modification_during_run(tmpdir, runner, project, client, run): """Test run in isolation.""" script = client.path / 'script.py' output = client.path / 'output' lock = Path(str(tmpdir.join('lock'))) with client.commit(): with script.open('w') as fp: fp.write('import os, time, sys\n' 'open("{lock}", "a")\n' 'while os.path.exists("{lock}"):\n' ' time.sleep(1)\n' 'sys.stdout.write(sys.stdin.read())\n' 'sys.stdout.flush()\n'.format(lock=str(lock))) prefix = [ sys.executable, '-m', 'renku', 'run', '--isolation', ] cmd = [ 'python', script.name, ] previous = client.repo.head.commit with output.open('wb') as stdout: process = subprocess.Popen(prefix + cmd, stdin=subprocess.PIPE, stdout=stdout) while not lock.exists(): time.sleep(1) with script.open('w') as fp: fp.write('print("edited")') lock.unlink() process.communicate(input=b'test') assert 0 == process.wait() with output.open('r') as fp: assert 'test' == fp.read().strip() diff = previous.diff(client.repo.head.commit) modifications = [ modification for modification in diff if modification.change_type == 'M' ] assert 0 == len(modifications)
def install(client, force): """Install Git hooks.""" import pkg_resources from git.index.fun import hook_path as get_hook_path for hook in HOOKS: hook_path = Path(get_hook_path(hook, client.repo.git_dir)) if hook_path.exists(): if not force: click.echo("Hook already exists. Skipping {0}".format( str(hook_path)), err=True) continue else: hook_path.unlink() # Make sure the hooks directory exists. hook_path.parent.mkdir(parents=True, exist_ok=True) Path(hook_path).write_bytes( pkg_resources.resource_string('renku.data', '{hook}.sh'.format(hook=hook))) hook_path.chmod(hook_path.stat().st_mode | stat.S_IEXEC)
def move(ctx, client, sources, destination): """Move files and check repository for potential problems.""" from renku.api._git import _expand_directories dst = Path(destination) def fmt_path(path): """Format path as relative to the client path.""" return str(Path(path).absolute().relative_to(client.path)) files = { fmt_path(source): fmt_path(file_or_dir) for file_or_dir in sources for source in _expand_directories((file_or_dir, )) } def fmt_dst(path): """Build a destination path for a source path.""" return str(dst / os.path.relpath(path, start=files[path])) destinations = {source: fmt_dst(source) for source in files} # 1. Check .gitignore. ignored = client.find_ignored_paths(*destinations.values()) if ignored: click.echo(WARNING + 'Renamed files match .gitignore.\n') if click.confirm('Do you want to edit ".gitignore" now?', default=False): click.edit(filename=str(client.path / '.gitignore')) # 2. Update dataset metadata files. with progressbar( client.datasets.items(), item_show_func=lambda item: str(item[1].short_id) if item else '', label='Updating dataset metadata', width=0, ) as bar: for (path, dataset) in bar: renames = {} for file_ in dataset.files: filepath = fmt_path(file_.path) if filepath in files: renames[file_.path] = destinations[filepath] if renames: dataset = dataset.rename_files( lambda key: renames.get(key, key)) dataset.to_yaml() # 3. Manage .gitattributes for external storage. tracked = tuple() if client.has_external_storage: tracked = tuple(path for path, attr in client.find_attr(*files).items() if attr.get('filter') == 'lfs') client.untrack_paths_from_storage(*tracked) if client.find_attr(*tracked): click.echo(WARNING + 'There are custom .gitattributes.\n') if click.confirm('Do you want to edit ".gitattributes" now?', default=False): click.edit(filename=str(client.path / '.gitattributes')) if tracked and client.has_external_storage: client.track_paths_in_storage(*(destinations[path] for path in tracked)) # 4. Handle symlinks. dst.parent.mkdir(parents=True, exist_ok=True) for source, target in destinations.items(): src = Path(source) if src.is_symlink(): Path(target).parent.mkdir(parents=True, exist_ok=True) Path(target).symlink_to( os.path.relpath(str(src.resolve()), start=os.path.dirname(target))) src.unlink() del files[source] # Finally move the files. final_sources = list(set(files.values())) if final_sources: run(['git', 'mv'] + final_sources + [destination], check=True)