跳转到主要内容

为Git、SVN、Mercurial等提供的轻量级、类型化Python工具。

项目描述

libvcs · Python Package License Code Coverage

libvcs 是一个轻量级的、类型化、Pythonic 工具箱,用于检测和解析 URL,与 githgsvn 命令,以及同步。为 vcspull 提供动力。

概述

主要功能

  • URL 检测和解析:验证和解析 Git、Mercurial 和 Subversion URL。
  • 命令抽象:通过 Python API 与 VCS 系统交互。
  • 仓库同步:通过 Python API 本地克隆和更新仓库。
  • py.test 插件:为单元测试创建临时本地仓库和工作副本。

支持 Python 3.9 及以上版本,Git(包括 AWS CodeCommit)、Subversion 和 Mercurial。

开始使用,请参阅快速入门指南以获取更多信息。

$ pip install --user libvcs

URL 检测和解析

使用 libvcs.url 模块轻松验证和解析 VCS URL。

验证 URL

>>> from libvcs.url.git import GitURL

>>> GitURL.is_valid(url='https://github.com/vcs-python/libvcs.git')
True

解析和调整 Git URL

>>> from libvcs.url.git import GitURL

>>> git_location = GitURL(url='git@github.com:vcs-python/libvcs.git')

>>> git_location
GitURL(url=git@github.com:vcs-python/libvcs.git,
        user=git,
        hostname=github.com,
        path=vcs-python/libvcs,
        suffix=.git,
        rule=core-git-scp)

将库从 libvcs 转换为 vcspull

>>> from libvcs.url.git import GitURL

>>> git_location = GitURL(url='git@github.com:vcs-python/libvcs.git')

>>> git_location.path = 'vcs-python/vcspull'

>>> git_location.to_url()
'git@github.com:vcs-python/vcspull.git'

# Switch them to gitlab:
>>> git_location.hostname = 'gitlab.com'

# Export to a `git clone` compatible URL.
>>> git_location.to_url()
'git@gitlab.com:vcs-python/vcspull.git'

更多内容请参阅解析文档

命令抽象

通过轻量级的 subprocess 包装器抽象化 git(1)hg(1)svn(1) 的 CLI 命令。

运行 Git 命令

import pathlib
from libvcs.cmd.git import Git

git = Git(path=pathlib.Path.cwd() / 'my_git_repo')
git.clone(url='https://github.com/vcs-python/libvcs.git')

上述:libvcs.cmd.git.Git 使用 Git.clone()

仓库同步

使用 libvcs.sync 模块同步您的仓库。

克隆和更新仓库

import pathlib
from libvcs.sync.git import GitSync

repo = GitSync(
   url="https://github.com/vcs-python/libvcs",
   path=pathlib.Path().cwd() / "my_repo",
   remotes={
       'gitlab': 'https://gitlab.com/vcs-python/libvcs'
   }
)

# Update / clone repo:
>>> repo.update_repo()

# Get revision:
>>> repo.get_revision()
u'5c227e6ab4aab44bf097da2e088b0ff947370ab8'

上述:libvcs.sync.git.GitSync 仓库对象使用 GitSync.update_repo()GitSync.get_revision()

Pytest 插件:用于测试的临时 VCS 仓库

libvcs pytest 插件 提供了 py.test fixture,用于快速创建本地 VCS 仓库和工作副本以进行测试。测试结束后,仓库将自动清理。

在 py.test 中使用临时本地 VCS

import pathlib

from libvcs.pytest_plugin import CreateRepoPytestFixtureFn
from libvcs.sync.git import GitSync


def test_repo_git_remote_checkout(
    create_git_remote_repo: CreateRepoPytestFixtureFn,
    tmp_path: pathlib.Path,
    projects_path: pathlib.Path,
) -> None:
    git_server = create_git_remote_repo()
    git_repo_checkout_dir = projects_path / "my_git_checkout"
    git_repo = GitSync(path=str(git_repo_checkout_dir), url=f"file://{git_server!s}")

    git_repo.obtain()
    git_repo.update_repo()

    assert git_repo.get_revision() == "initial"

    assert git_repo_checkout_dir.exists()
    assert pathlib.Path(git_repo_checkout_dir / ".git").exists()

底层:fixture 使用 TmpPathFactory 启动一个临时的 $HOME 环境,以便自动清理和与 pytest-xdist 兼容。

捐赠

您的捐赠将用于开发新功能、测试和支持。您的钱将直接用于项目的维护和开发。如果您是个人,请根据您从项目中获得的价值自由捐赠。

有关捐赠选项,请参阅 https://www.git-pull.com/support.html

更多信息

Docs Build Status

项目详情


发布历史 发布通知 | RSS源

下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码发行版

libvcs-0.30.1.tar.gz (450.7 kB 查看哈希值)

上传时间 源码

构建发行版

libvcs-0.30.1-py3-none-any.whl (75.6 kB 查看哈希值)

上传时间 Python 3

支持者