本文整理汇总了Python中datalad.support.gitrepo.GitRepo.get_toppath方法的典型用法代码示例。如果您正苦于以下问题:Python GitRepo.get_toppath方法的具体用法?Python GitRepo.get_toppath怎么用?Python GitRepo.get_toppath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类datalad.support.gitrepo.GitRepo
的用法示例。
在下文中一共展示了GitRepo.get_toppath方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_GitRepo_get_toppath
# 需要导入模块: from datalad.support.gitrepo import GitRepo [as 别名]
# 或者: from datalad.support.gitrepo.GitRepo import get_toppath [as 别名]
def test_GitRepo_get_toppath(repo, tempdir, repo2):
reporeal = op.realpath(repo)
eq_(GitRepo.get_toppath(repo, follow_up=False), reporeal)
eq_(GitRepo.get_toppath(repo), repo)
# Generate some nested directory
GitRepo(repo2, create=True)
repo2real = op.realpath(repo2)
nested = op.join(repo2, "d1", "d2")
os.makedirs(nested)
eq_(GitRepo.get_toppath(nested, follow_up=False), repo2real)
eq_(GitRepo.get_toppath(nested), repo2)
# and if not under git, should return None
eq_(GitRepo.get_toppath(tempdir), None)
示例2: __call__
# 需要导入模块: from datalad.support.gitrepo import GitRepo [as 别名]
# 或者: from datalad.support.gitrepo.GitRepo import get_toppath [as 别名]
def __call__(url, dataset=None, recursive=False):
# shortcut
ds = dataset
if ds is not None and not isinstance(ds, Dataset):
ds = Dataset(ds)
# if we have no dataset given, figure out which one we need to operate
# on, based on the current working directory of the process:
if ds is None:
# try to find a dataset at or above PWD:
dspath = GitRepo.get_toppath(getpwd())
if dspath is None:
raise ValueError("No dataset found at %s." % getpwd())
ds = Dataset(dspath)
assert(ds is not None)
if not ds.is_installed():
raise ValueError("No installed dataset found at "
"{0}.".format(ds.path))
assert(ds.repo is not None)
repos_to_update = [ds.repo]
if recursive:
repos_to_update += [GitRepo(opj(ds.path, sub_path))
for sub_path in
ds.get_dataset_handles(recursive=True)]
for handle_repo in repos_to_update:
parser = get_module_parser(handle_repo)
for submodule_section in parser.sections():
submodule_name = submodule_section[11:-1]
parser.set_value(submodule_section, "url",
url.replace("%NAME",
submodule_name.replace("/", "-")))
return # TODO: return value?
示例3: __call__
# 需要导入模块: from datalad.support.gitrepo import GitRepo [as 别名]
# 或者: from datalad.support.gitrepo.GitRepo import get_toppath [as 别名]
def __call__(dataset=None, path=None, source=None, recursive=False,
add_data_to_git=False):
lgr.debug("Installation attempt started")
# shortcut
ds = dataset
if ds is not None and not isinstance(ds, Dataset):
ds = Dataset(ds)
if isinstance(path, list):
if not len(path):
# normalize value to expected state when nothing was provided
path = None
elif len(path) == 1:
# we can simply continue with the function as called with a
# single argument
path = path[0]
else:
lgr.debug("Installation of multiple targets was requested: {0}".format(path))
return [Install.__call__(
dataset=ds,
path=p,
source=source,
recursive=recursive) for p in path]
# resolve the target location against the provided dataset
if path is not None:
# make sure it is not a URL, `resolve_path` cannot handle that
if is_url(path):
try:
path = get_local_path_from_url(path)
path = resolve_path(path, ds)
except ValueError:
# URL doesn't point to a local something
pass
else:
path = resolve_path(path, ds)
# any `path` argument that point to something local now resolved and
# is no longer a URL
# if we have no dataset given, figure out which one we need to operate
# on, based on the resolved target location (that is now guaranteed to
# be specified, but only if path isn't a URL (anymore) -> special case,
# handles below
if ds is None and path is not None and not is_url(path):
# try to find a dataset at or above the installation target
dspath = GitRepo.get_toppath(abspath(path))
if dspath is None:
# no top-level dataset found, use path as such
dspath = path
ds = Dataset(dspath)
if ds is None and source is None and path is not None:
# no dataset, no source
# this could be a shortcut install call, where the first
# arg identifies the source
if is_url(path) or os.path.exists(path):
# we have an actual URL -> this should be the source
# OR
# it is not a URL, but it exists locally
lgr.debug(
"Single argument given to install and no dataset found. "
"Assuming the argument identifies a source location.")
source = path
path = None
lgr.debug("Resolved installation target: {0}".format(path))
if ds is None and path is None and source is not None:
# we got nothing but a source. do something similar to git clone
# and derive the path from the source and continue
lgr.debug(
"Neither dataset not target installation path provided. "
"Assuming installation of a remote dataset. "
"Deriving destination path from given source {0}".format(
source))
ds = Dataset(_installationpath_from_url(source))
if not path and ds is None:
# no dataset, no target location, nothing to do
raise InsufficientArgumentsError(
"insufficient information for installation (needs at "
"least a dataset or an installation path")
assert(ds is not None)
lgr.debug("Resolved target dataset for installation: {0}".format(ds))
vcs = ds.repo
if vcs is None:
# TODO check that a "ds.path" actually points to a TOPDIR
# should be the case already, but maybe nevertheless check
try:
with swallow_logs():
vcs = Install._get_new_vcs(ds, source, vcs)
except GitCommandError:
lgr.debug("Cannot retrieve from URL: {0}".format(source))
# maybe source URL was missing a '/.git'
if source and not source.rstrip('/').endswith('/.git'):
#.........这里部分代码省略.........
示例4: __call__
# 需要导入模块: from datalad.support.gitrepo import GitRepo [as 别名]
# 或者: from datalad.support.gitrepo.GitRepo import get_toppath [as 别名]
def __call__(dataset=None, name=None, url=None,
pushurl=None, recursive=False, force=False):
# TODO: Detect malformed URL and fail?
if name is None or (url is None and pushurl is None):
raise ValueError("""insufficient information to add a sibling
(needs at least a dataset, a name and an URL).""")
if url is None:
url = pushurl
# shortcut
ds = dataset
if ds is not None and not isinstance(ds, Dataset):
ds = Dataset(ds)
if ds is None:
# try to find a dataset at or above CWD
dspath = GitRepo.get_toppath(abspath(getpwd()))
if dspath is None:
raise ValueError(
"No dataset found at or above {0}.".format(getpwd()))
ds = Dataset(dspath)
lgr.debug("Resolved dataset for target creation: {0}".format(ds))
assert(ds is not None and name is not None and url is not None)
if not ds.is_installed():
raise ValueError("Dataset {0} is not installed yet.".format(ds))
assert(ds.repo is not None)
ds_basename = basename(ds.path)
repos = {
ds_basename: {'repo': ds.repo}
}
if recursive:
for subds in ds.get_dataset_handles(recursive=True):
sub_path = opj(ds.path, subds)
repos[ds_basename + '/' + subds] = {
# repos[subds] = {
'repo': GitRepo(sub_path, create=False)
}
# Note: This is copied from create_publication_target_sshwebserver
# as it is the same logic as for its target_dir.
# TODO: centralize and generalize template symbol handling
# TODO: Check pushurl for template symbols too. Probably raise if only
# one of them uses such symbols
replicate_local_structure = False
if "%NAME" not in url:
replicate_local_structure = True
for repo in repos:
if not replicate_local_structure:
repos[repo]['url'] = url.replace("%NAME",
repo.replace("/", "-"))
if pushurl:
repos[repo]['pushurl'] = pushurl.replace("%NAME",
repo.replace("/",
"-"))
else:
repos[repo]['url'] = url
if pushurl:
repos[repo]['pushurl'] = pushurl
if repo != ds_basename:
repos[repo]['url'] = _urljoin(repos[repo]['url'], repo[len(ds_basename)+1:])
if pushurl:
repos[repo]['pushurl'] = _urljoin(repos[repo]['pushurl'], repo[len(ds_basename)+1:])
# collect existing remotes:
already_existing = list()
conflicting = list()
for repo in repos:
if name in repos[repo]['repo'].git_get_remotes():
already_existing.append(repo)
lgr.debug("""Remote '{0}' already exists
in '{1}'.""".format(name, repo))
existing_url = repos[repo]['repo'].git_get_remote_url(name)
existing_pushurl = \
repos[repo]['repo'].git_get_remote_url(name, push=True)
if repos[repo]['url'].rstrip('/') != existing_url.rstrip('/') \
or (pushurl and existing_pushurl and
repos[repo]['pushurl'].rstrip('/') !=
existing_pushurl.rstrip('/')) \
or (pushurl and not existing_pushurl):
conflicting.append(repo)
if not force and conflicting:
raise RuntimeError("Sibling '{0}' already exists with conflicting"
" URL for {1} dataset(s). {2}".format(
name, len(conflicting), conflicting))
runner = Runner()
successfully_added = list()
for repo in repos:
if repo in already_existing:
#.........这里部分代码省略.........
示例5: __call__
# 需要导入模块: from datalad.support.gitrepo import GitRepo [as 别名]
# 或者: from datalad.support.gitrepo.GitRepo import get_toppath [as 别名]
def __call__(name=None, dataset=None,
merge=False, recursive=False, fetch_all=False,
reobtain_data=False):
"""
"""
# TODO: Is there an 'update filehandle' similar to install and publish?
# What does it mean?
if reobtain_data:
# TODO: properly define, what to do
raise NotImplementedError("TODO: Option '--reobtain-data' not "
"implemented yet.")
# shortcut
ds = dataset
if ds is not None and not isinstance(ds, Dataset):
ds = Dataset(ds)
# if we have no dataset given, figure out which one we need to operate
# on, based on the current working directory of the process:
if ds is None:
# try to find a dataset at or above PWD:
dspath = GitRepo.get_toppath(getpwd())
if dspath is None:
raise ValueError("No dataset found at %s." % getpwd())
ds = Dataset(dspath)
assert(ds is not None)
if not ds.is_installed():
raise ValueError("No installed dataset found at "
"{0}.".format(ds.path))
assert(ds.repo is not None)
repos_to_update = [ds.repo]
if recursive:
repos_to_update += [GitRepo(opj(ds.path, sub_path))
for sub_path in
ds.get_dataset_handles(recursive=True)]
for repo in repos_to_update:
# get all remotes:
remotes = repo.git_get_remotes()
if name and name not in remotes:
lgr.warning("'%s' not known to dataset %s.\nSkipping" %
(name, repo.path))
continue
# Currently '--merge' works for single remote only:
# TODO: - condition still incomplete
# - We can merge if a remote was given or there is a
# tracking branch
# - we also can fetch all remotes independently on whether or
# not we merge a certain remote
if not name and len(remotes) > 1 and merge:
lgr.debug("Found multiple remotes:\n%s" % remotes)
raise NotImplementedError("No merge strategy for multiple "
"remotes implemented yet.")
lgr.info("Updating handle '%s' ..." % repo.path)
# fetch remote(s):
repo.git_fetch(name if name else '',
"--all" if fetch_all else '')
# if it is an annex and there is a tracking branch, and we didn't
# fetch the entire remote anyway, explicitly fetch git-annex
# branch:
# TODO: Is this logic correct? Shouldn't we fetch git-annex from
# `name` if there is any (or if there is no tracking branch but we
# have a `name`?
if knows_annex(repo.path) and not fetch_all:
# check for tracking branch's remote:
try:
std_out, std_err = \
repo._git_custom_command('',
["git", "config", "--get",
"branch.{active_branch}.remote".format(
active_branch=repo.git_get_active_branch())])
except CommandError as e:
if e.code == 1 and e.stdout == "":
std_out = None
else:
raise
if std_out: # we have a "tracking remote"
repo.git_fetch("%s git-annex" % std_out.strip())
# merge:
if merge:
lgr.info("Applying changes from tracking branch...")
cmd_list = ["git", "pull"]
if name:
cmd_list.append(name)
# branch needed, if not default remote
# => TODO: use default remote/tracking branch to compare
# (see above, where git-annex is fetched)
# => TODO: allow for passing a branch
# (or more general refspec?)
# For now, just use the same name
cmd_list.append(repo.git_get_active_branch())
#.........这里部分代码省略.........
示例6: __call__
# 需要导入模块: from datalad.support.gitrepo import GitRepo [as 别名]
# 或者: from datalad.support.gitrepo.GitRepo import get_toppath [as 别名]
def __call__(dataset=None, path=None, data_only=True, recursive=False):
# Note: copy logic from install to resolve dataset and path:
# shortcut
ds = dataset
if ds is not None and not isinstance(ds, Dataset):
ds = Dataset(ds)
if not path:
if ds is None:
# no dataset, no target location, nothing to do
raise ValueError(
"insufficient information for uninstallation (needs at "
"least a dataset or a path")
elif isinstance(path, list):
# TODO: not sure. might be possible to deal with that list directly
return [Uninstall.__call__(
dataset=ds,
path=p,
data_only=data_only,
recursive=recursive) for p in path]
# resolve the target location against the provided dataset
if path is not None:
path = resolve_path(path, ds)
lgr.debug("Resolved uninstallation target: {0}".format(path))
# if we have no dataset given, figure out which one we need to operate
# on, based on the resolved target location (that is now guaranteed to
# be specified
if ds is None:
# try to find a dataset at or above the installation target
dspath = GitRepo.get_toppath(abspath(path))
if dspath is None:
# no top-level dataset found, use path as such
dspath = path
ds = Dataset(dspath)
assert(ds is not None)
lgr.debug("Resolved target dataset for uninstallation: {0}".format(ds))
if not ds.is_installed():
if not path or path == ds.path:
# we want to uninstall the dataset itself, which is not
# installed => nothing to do
# TODO: consider `data` option! is_installed currently only
# checks for a repository
lgr.info("Dataset {0} not installed. Nothing to "
"do.".format(ds.path))
return
else:
# we want to uninstall something from a not installed dataset
# Doesn't make sense, does it? => fail
raise ValueError("Dataset {0} is not installed.".format(ds.path))
assert(ds.repo is not None)
if not path or path == ds.path:
# uninstall the dataset `ds`
# TODO: what to consider?
# - whether it is a submodule of another dataset
# - `data_only` ?
# - `recursive`
# - what to return in what case (data_only)?
raise NotImplementedError("TODO: Uninstall dataset %s" % ds.path)
# needed by the logic below
assert(isabs(path))
# express the destination path relative to the root of this dataset
relativepath = relpath(path, start=ds.path)
if path.startswith(pardir):
raise ValueError("uninstallation path outside dataset")
lgr.debug(
"Resolved uninstallation target relative to dataset {0}: {1}".format(
ds, relativepath))
# figure out, what path actually is pointing to:
if not exists(path):
# nothing there, nothing to uninstall
lgr.info("Nothing found to uninstall at %s" % path)
return
if relativepath in ds.get_dataset_handles(recursive=True):
# it's a submodule
# --recursive required or implied?
raise NotImplementedError("TODO: uninstall submodule %s from "
"dataset %s" % (relativepath, ds.path))
if isdir(path):
# don't know what to do yet
# in git vs. untracked?
# recursive?
raise NotImplementedError("TODO: uninstall directory %s from "
"dataset %s" % (path, ds.path))
# we know, it's an existing file
#.........这里部分代码省略.........
示例7: __call__
# 需要导入模块: from datalad.support.gitrepo import GitRepo [as 别名]
# 或者: from datalad.support.gitrepo.GitRepo import get_toppath [as 别名]
def __call__(sshurl, target=None, target_dir=None,
target_url=None, target_pushurl=None,
dataset=None, recursive=False,
existing='raise', shared=False):
if sshurl is None:
raise ValueError("""insufficient information for target creation
(needs at least a dataset and a SSH URL).""")
if target is None and (target_url is not None
or target_pushurl is not None):
raise ValueError("""insufficient information for adding the target
as a sibling (needs at least a name)""")
# shortcut
ds = dataset
if ds is not None and not isinstance(ds, Dataset):
ds = Dataset(ds)
if ds is None:
# try to find a dataset at or above CWD
dspath = GitRepo.get_toppath(abspath(getpwd()))
if dspath is None:
raise ValueError("""No dataset found
at or above {0}.""".format(getpwd()))
ds = Dataset(dspath)
lgr.debug("Resolved dataset for target creation: {0}".format(ds))
assert(ds is not None and sshurl is not None)
if not ds.is_installed():
raise ValueError("""Dataset {0} is not installed yet.""".format(ds))
assert(ds.repo is not None)
# determine target parameters:
parsed_target = urlparse(sshurl)
host_name = parsed_target.netloc
# TODO: Sufficient to fail on this condition?
if not parsed_target.netloc:
raise ValueError("Malformed URL: {0}".format(sshurl))
if target_dir is None:
if parsed_target.path:
target_dir = parsed_target.path
else:
target_dir = '.'
# TODO: centralize and generalize template symbol handling
replicate_local_structure = False
if "%NAME" not in target_dir:
replicate_local_structure = True
# collect datasets to use:
datasets = dict()
datasets[basename(ds.path)] = ds
if recursive:
for subds in ds.get_dataset_handles(recursive=True):
sub_path = opj(ds.path, subds)
# TODO: when enhancing Dataset/*Repo classes and therefore
# adapt to moved code, make proper distinction between name and
# path of a submodule, which are technically different. This
# probably will become important on windows as well as whenever
# we want to allow for moved worktrees.
datasets[basename(ds.path) + '/' + subds] = \
Dataset(sub_path)
# setup SSH Connection:
# TODO: Make the entire setup a helper to use it when pushing via
# publish?
# - build control master:
from datalad.utils import assure_dir
not_supported_on_windows("TODO")
from os import geteuid # Linux specific import
var_run_user_datalad = "/var/run/user/%s/datalad" % geteuid()
assure_dir(var_run_user_datalad)
control_path = "%s/%s" % (var_run_user_datalad, host_name)
control_path += ":%s" % parsed_target.port if parsed_target.port else ""
# - start control master:
cmd = "ssh -o ControlMaster=yes -o \"ControlPath=%s\" " \
"-o ControlPersist=yes %s exit" % (control_path, host_name)
lgr.debug("Try starting control master by calling:\n%s" % cmd)
import subprocess
proc = subprocess.Popen(cmd, shell=True)
proc.communicate(input="\n") # why the f.. this is necessary?
runner = Runner()
ssh_cmd = ["ssh", "-S", control_path, host_name]
lgr.info("Creating target datasets ...")
for current_dataset in datasets:
if not replicate_local_structure:
path = target_dir.replace("%NAME",
current_dataset.replace("/", "-"))
else:
# TODO: opj depends on local platform, not the remote one.
# check how to deal with it. Does windows ssh server accept
# posix paths? vice versa? Should planned SSH class provide
# tools for this issue?
#.........这里部分代码省略.........
示例8: __call__
# 需要导入模块: from datalad.support.gitrepo import GitRepo [as 别名]
# 或者: from datalad.support.gitrepo.GitRepo import get_toppath [as 别名]
def __call__(dataset=None, dest=None, path=None,
# Note: add remote currently disabled in publish
# dest_url=None, dest_pushurl=None,
with_data=None, recursive=False):
# Note: add remote currently disabled in publish
# if dest is None and (dest_url is not None
# or dest_pushurl is not None):
# raise ValueError("""insufficient information for adding the
# destination as a sibling (needs at least a name)""")
# shortcut
ds = dataset
if ds is not None and not isinstance(ds, Dataset):
ds = Dataset(ds)
if not path:
path = curdir
elif isinstance(path, list):
return [Publish.__call__(
dataset=ds,
dest=dest,
path=p,
# Note: add remote currently disabled in publish
# dest_url=dest_url,
# dest_pushurl=dest_pushurl,
with_data=with_data,
recursive=recursive) for p in path]
# resolve the location against the provided dataset
if path is not None:
path = resolve_path(path, ds)
lgr.info("Publishing {0}".format(path))
# if we have no dataset given, figure out which one we need to operate
# on, based on the resolved location (that is now guaranteed to
# be specified
if ds is None:
# try to find a dataset at or above the location
dspath = GitRepo.get_toppath(abspath(path))
if dspath is None:
# no top-level dataset found, use path as such
dspath = path
ds = Dataset(dspath)
lgr.debug("Resolved dataset for publication: {0}".format(ds))
assert(ds is not None)
# it might still be about a subdataset of ds:
if path is not None:
relativepath = relpath(path, start=ds.path)
subds = get_containing_subdataset(ds, relativepath)
if subds.path != ds.path:
# path belongs to a subdataset; hand it over
lgr.debug("Hand over to submodule %s" % subds.path)
return subds.publish(dest=dest,
path=relpath(path, start=subds.path),
# Note: add remote currently disabled in publish
# dest_url=dest_url,
# dest_pushurl=dest_pushurl,
with_data=with_data,
recursive=recursive)
# now, we know, we have to operate on ds. So, ds needs to be installed,
# since we cannot publish anything from a not installed dataset,
# can we?
# (But may be just the existence of ds.repo is important here.)
if not ds.is_installed():
raise ValueError("No installed dataset found at "
"{0}.".format(ds.path))
assert(ds.repo is not None)
# TODO: For now we can deal with a sibling(remote) name given by `dest`
# only. Figure out, when to allow for passing a local path or URL
# directly and what to do in that case.
# Note: we need an upstream remote, if there's none given. We could
# wait for git push to complain, but we need to explicitly figure it
# out for pushing annex branch anyway and we might as well fail right
# here.
# keep original dest in case it's None for passing to recursive calls:
dest_resolved = dest
if dest is None:
# check for tracking branch's remote:
try:
std_out, std_err = \
ds.repo._git_custom_command('',
["git", "config", "--get", "branch.{active_branch}.remote".format(active_branch=ds.repo.git_get_active_branch())],
expect_fail=True)
except CommandError as e:
if e.code == 1 and e.stdout == "":
std_out = None
else:
raise
if std_out:
dest_resolved = std_out.strip()
else:
# we have no remote given and no upstream => fail
#.........这里部分代码省略.........