该模块旨在充当pathlib的逻辑后代,pathlib 是一个用于面向对象的路径操作的 Python 3 模块。因此,它实现的所有内容都尽可能接近原点,除了少数例外,例如 stat()。
使用示例
行走目录树
获取目录列表:
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://repo.jfrog.org/artifactory/gradle-ivy-local")
for p in path:
print p
递归查找当前目录中的所有 .gz 文件:
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://repo.jfrog.org/artifactory/distributions/org/")
for p in path.glob("**/*.gz"):
print p
下载工件
将工件下载到本地文件系统:
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://repo.jfrog.org/artifactory/distributions/org/apache/tomcat/apache-tomcat-7.0.11.tar.gz")
with path.open() as fd:
with open("tomcat.tar.gz", "wb") as out:
out.write(fd.read())
上传工件
部署常规文件myapp-1.0.tar.gz
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0")
path.mkdir()
path.deploy_file('./myapp-1.0.tar.gz')
部署 debian 包myapp-1.0.deb
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://my-artifactory/artifactory/ubuntu-local/pool")
path.deploy_deb('./myapp-1.0.deb',
distribution='trusty',
component='main',
architecture='amd64')
移动文物
将工件从该路径移动到目的地时,将suppress_layouts参数设置为True
,将允许将一个路径中的工件直接复制到另一路径中,而不强制执行存储库布局。默认行为是复制到存储库根目录,但将 [org]、[module]、[baseVer] 等结构重新映射到目标存储库。
from artifactory import ArtifactoryPath
source = ArtifactoryPath("http://example.com/artifactory/builds/product/product/1.0.0/")
dest = ArtifactoryPath("http://example.com/artifactory/published/production/")
source.move(dest)
# you can use dry run just to check if command will succeed without real change, adds debug message
source.move(dest, dry_run=True)
删除伪影
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://repo.jfrog.org/artifactory/distributions/org/apache/tomcat/apache-tomcat-7.0.11.tar.gz"
)
if path.exists():
path.unlink()
验证
要提供用户名和密码来访问受限资源,您可以将
auth
参数传递给 ArtifactoryPath:
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://my-artifactory/artifactory/myrepo/restricted-path",
auth=('admin', 'ilikerandompasswords'))
path.touch()
SSL 证书验证选项
有关更多详细信息,请参阅请求 - SSL 验证。
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0")
... 是相同的
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0",
verify=True)
指定本地证书用作客户端证书
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0",
cert="/path_to_file/server.pem")
禁用主机证书验证
from artifactory import ArtifactoryPath
path = ArtifactoryPath(
"http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0",
verify=False)
注意:如果禁用主机证书验证,urllib3 将抛出InsecureRequestWarning。
要禁用这些警告,需要调用 urllib3.disable_warnings()。
import requests.packages.urllib3 as urllib3
urllib3.disable_warnings()
全局配置文件
Artifactory Python 模块还可以在中央文件中指定所有与连接相关的设置,
~/.artifactory_python.cfg
该文件在创建第一个ArtifactoryPath
对象时读取并全局存储。例如,您可以指定身份验证令牌的每个实例设置,这样您就不需要显式地将auth
参数传递给ArtifactoryPath
.
例子:
[http://artifactory-instance.com/artifactory]
username = deployer
password = ilikerandompasswords
verify = false
[another-artifactory-instance.com/artifactory]
username = foo
password = @dmin
cert = ~/mycert
是否指定
http://
或https://
添加前缀并不重要。该模块将首先尝试找到最佳匹配,然后尝试匹配不带前缀的 URL。因此,如果您在配置中指定https://my-instance.local
并使用 调用ArtifactoryPath
,http://my-instance.local
它仍然会做正确的事情。