[docs]defget_gitlab_instance()->gitlab.Gitlab:"""Return an instance of the gitlab object for remote operations."""# tries to figure if we can authenticate using a global configurationcfgs=[pathlib.Path(k).expanduser()forkin["~/.python-gitlab.cfg","/etc/python-gitlab.cfg"]]ifany([k.exists()forkincfgs]):gl=gitlab.Gitlab.from_config("idiap",[str(k)forkincfgsifk.exists()])else:# ask the user for a token or use one from the current runnerserver=os.environ.get("CI_SERVER_URL","https://gitlab.idiap.ch")token=os.environ.get("CI_JOB_TOKEN")iftokenisNone:logger.debug("Did not find any of %s nor CI_JOB_TOKEN is defined. ""Asking for user token on the command line...","|".join([str(k)forkincfgs]),)token=input(f"{server} (private) token: ")gl=gitlab.Gitlab(server,private_token=token,api_version="4")returngl
[docs]defdownload_path(package:gitlab.v4.objects.projects.Project,path:str,output:pathlib.Path|None=None,ref:str|None=None,)->None:"""Download paths from gitlab, with an optional recurse. This method will download an archive of the repository from chosen reference, and then it will search inside the zip blob for the path to be copied into output. It uses :py:class:`zipfile.ZipFile` to do this search. This method will not be very efficient for larger repository references, but works recursively by default. Args: package: the gitlab package object to use (should be pre-fetched) path: the path on the project to download output: where to place the path to be downloaded - if not provided, use the basename of ``path`` as storage point with respect to the current directory ref: the name of the git reference (branch, tag or commit hash) to use. If None specified, defaults to the default branch of the input package """output=outputorpathlib.Path(os.path.realpath(os.curdir))ref=reforpackage.default_branchlogger.debug('Downloading archive of "%s" from "%s"...',ref,package.attributes["path_with_namespace"],)archive=package.repository_archive(ref=ref)logger.debug("Archive has %d bytes",len(archive))logger.debug('Searching for "%s" within archive...',path)withtempfile.TemporaryDirectory()asd:withtarfile.open(fileobj=BytesIO(archive),mode="r:gz")asf:f.extractall(path=d)# move stuff to "output"basedir=os.listdir(d)[0]shutil.move(pathlib.Path(d)/basedir/path,output)