dohq-artifactory Python Library

repository·master·Indexed 43 days ago

https://github.com/devopshq/artifactory

A Python interface library for JFrog Artifactory that provides a pathlib-like experience for interacting with remote artifacts. It supports Artifactory SaaS via ArtifactorySaaSPath, AQL queries, artifact deployment by checksum, Debian package deployment, and management of users, groups, and repositories.

Tokens
10.5K
Snippets
38
Records
39
Agent score
5%

What's inside dohq-artifactory

  1. Manage PermissionTarget and roles

    master

    A PermissionTarget defines which users, groups, or repositories have specific access levels to patterns of artifacts.

    Predefined Roles

    • PermissionTarget.ROLE_ADMIN: ADMIN + DELETE + DEPLOY + ANNOTATE + READ
    • PermissionTarget.ROLE_DELETE: DELETE + DEPLOY + ANNOTATE + READ
    • PermissionTarget.ROLE_DEPLOY: DEPLOY + ANNOTATE + READ
    • PermissionTarget.ROLE_ANNOTATE: ANNOTATE + READ
    • PermissionTarget.ROLE_READ: READ

    Granular Permissions

    You can combine permissions using the + operator (e.g., PermissionTarget.ROLE_READ + PermissionTarget.ROLE_WRITE).

    • ADMIN: Change permission settings for the target.
    • DELETE: Delete or overwrite artifacts.
    • DEPLOY: Deploy artifacts or populate caches.
    • ANNOTATE: Annotate artifacts/folders with metadata.
    • READ: Read and download artifacts.
    from dohq_artifactory import PermissionTarget
    
    # Create a new permission target
    permission = PermissionTarget(
        artifactory_,
        "rule",
        repositories=["repo1", "repo2"],
        users=["user1"],
        groups=["group1"],
        includes_pattern="com.mycompany.myproject1.**",
        excludes_pattern="com.othercompany.projectX.**",
    )
    permission.create()
    
    # Modify permissions
    permission.add_user("user3", PermissionTarget.ROLE_ADMIN)
    permission.add_user("user4", PermissionTarget.ROLE_READ + PermissionTarget.ROLE_WRITE)
    permission.add_group("group3", PermissionTarget.ROLE_DEPLOY)
    
    # Apply changes
    permission.update()
  2. Enable debug logging for Artifactory

    master

    To gain insight into the library's operations, configure the standard Python logging module. You can target the artifactory logger specifically to avoid flooding your console with logs from other modules.

    import logging
    from artifactory import ArtifactoryPath
    
    logging.basicConfig()
    # Set level specifically for the artifactory module
    logging.getLogger("artifactory").setLevel(logging.DEBUG)
    
    path = ArtifactoryPath(
        "http://my-artifactory/artifactory/myrepo/restricted-path", apikey="MY_API_KEY"
    )
  3. Download an Artifactory folder as an archive

    master

    You can download an entire folder as a compressed archive (zip, tar, tar.gz, tgz). Note that archiving must be enabled on the Artifactory server. You can specify the archive_type and whether to verify the check_sum.

    from artifactory import ArtifactoryPath
    
    path = ArtifactoryPath(
        "http://my_url:8080/artifactory/my_repo/winx64/aas", auth=("user", "password")
    )
    
    # Download as zip archive
    with path.archive(archive_type="zip", check_sum=False).open() as archive:
        with open(r"D:\target.zip", "wb") as out:
            out.write(archive.read())
    
    # Download folder archive in chunks
    path.archive().writeto(out="my.zip", chunk_size=100 * 1024)
  4. Configure SSL certificate verification

    master

    When initializing ArtifactoryPath, you can control SSL/TLS verification using the verify and cert parameters, following the requests library pattern:

    • Enable verification (default): verify=True.
    • Disable verification: verify=False. Note that this will trigger urllib3.InsecureRequestWarning.
    • Use a specific client certificate: Pass the file path to the certificate via the cert parameter.
    from artifactory import ArtifactoryPath
    
    # Enable verification (default behavior)
    path = ArtifactoryPath(
        "http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0", verify=True
    )
    
    # Use a local client-side certificate
    path = ArtifactoryPath(
        "http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0",
        cert="/path_to_file/server.pem",
    )
    
    # Disable host certificate verification
    path = ArtifactoryPath(
        "http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0", verify=False
    )
  5. Manage Artifactory Projects

    master

    Use the Project class to manage Artifactory projects. You can find a project using artifactory_.find_project(project_key) and create one by providing the artifactory instance, the project key, and a display name.

    from dohq_artifactory import Project
    
    project = artifactory_.find_project("t1k1")
    if project is None:
        project = Project(artifactory_, "t1k1", "t1k1_display_name")
        project.create()
  6. Download artifacts from Artifactory

    master

    You can download artifacts using .open() for standard file operations or .writeto() for more efficient chunked downloads. Chunked downloads are recommended for large files to prevent high memory usage.

    ```python
    from artifactory import ArtifactoryPath
    
    path = ArtifactoryPath("http://repo.jfrog.org/artifactory/distributions/org/apache/tomcat/apache-tomcat-7.0.11.tar.gz")
    
    # Method 1: Using open()
    with path.open() as fd, open("tomcat.tar.gz", "wb") as out:
        out.write(fd.read())
    
    # Method 2: Using writeto() for chunked download
    path.writeto(out="tomcat.tar.gz")
    
    # Method 3: writeto() with custom chunk size and file object
    with open("tomcat3.tar.gz", "wb") as out:
        path.writeto(out, chunk_size=256)
    
    # Method 4: writeto() with a custom progress function
    def custom_print(bytes_now, total, custom):
        print(bytes_now, total, custom)
    
    path.writeto(
        out="tomcat5.tar.gz",
        progress_func=lambda x, y: custom_print(x, y, custom="test**/
  7. Manage Groups and LDAP Groups

    master

    Groups can be managed using the Group class. You can find a group with artifactory_.find_group(groupname), create it, or update its user list by assigning a list of usernames to group.users and calling group.create().

    For LDAP integration, use the GroupLDAP class. This requires providing a realm_attributes string containing the LDAP DN path and the groupsStrategy (e.g., STATIC).

    from dohq_artifactory import Group, GroupLDAP
    
    # Standard Group
    group = artifactory_.find_group("groupname")
    if group is None:
        group = Group(artifactory_, "groupname")
        group.create()
    
    # LDAP Group
    dn = "cn=R.DevOps.TestArtifactory,ou=Groups,dc=example,dc=com"
    attr = f"ldapGroupName=r.devops.testartifactory;groupsStrategy=STATIC;groupDn={dn}"
    test_group = GroupLDAP(artifactory=artifactory_, name="r.devops.testartifactory", realm_attributes=attr)
    test_group.create()
  8. Deploy artifacts by checksum

    master

    To avoid unnecessary content transfers, you can deploy an artifact by its checksum. If Artifactory already contains an artifact with the matching checksum, it will copy the content internally instead of uploading it again.

    from artifactory import ArtifactoryPath
    
    path = ArtifactoryPath("http://my-artifactory/artifactory/my_repo/foo")
    sha1 = "1be5d2dbe52ddee96ef2d17d354e2be0a155a951"
    
    # Deploy using specific checksum parameter
    path.deploy_by_checksum(sha1=sha1)
    path.deploy_by_checksum(checksum=sha1)
    
    # Deploy using sha256
    path.deploy_by_checksum(sha256="00bbf80ccca376893d60183e1a714e707fd929aea3e458f9ffda60f7ae75cc51")
    path.deploy_by_checksum(checksum="00bbf80ccca376893d60183e1a714e707fd929aea3e458f9ffda60f7ae75cc51")
  9. Set request timeouts

    master

    You can specify a timeout for requests in ArtifactoryPath using the same syntax as the requests library. A value of None (the default) means no timeout is applied.

    from artifactory import ArtifactoryPath
    
    # Set a 5-second timeout
    path = ArtifactoryPath(
        "http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0", timeout=5
    )
  10. Search for artifacts in Maven repositories

    master

    Maven searches allow you to retrieve artifacts by group name, group and package name, or a combination of group, package, and version.

    # Get repo
    repo = artifactory_.find_repository("maven.all")
    
    # Get artifacts by group name
    for artifacts in repo["my.group"]:
        print(artifact)
    
    # Get artifacts by group and package name
    for artifacts in repo["my.group:package"]:
        print(artifact)
    
    # Get artifacts by group, package name and version
    for artifacts in repo["my.group:package:1.0.0"]:
        print(artifact)
  11. Disable urllib3 InsecureRequestWarning

    master

    If you set verify=False in ArtifactoryPath, urllib3 will emit InsecureRequestWarning. You can suppress these warnings using the following code:

    import requests.packages.urllib3 as urllib3
    
    urllib3.disable_warnings()
  12. Manage Local, Virtual, and Remote Repositories

    master

    The library provides specific classes for different repository types:

    • Local Repositories: Use RepositoryLocal. You can specify the packageType using constants like RepositoryLocal.DEBIAN or RepositoryLocal.PYPI.
    • Virtual Repositories: Use RepositoryVirtual. These aggregate multiple local repositories. Use the repositories argument to pass a list of RepositoryLocal objects and specify the packageType.
    • Remote Repositories: Use RepositoryRemote. These proxy external repositories. You must provide a url and a packageType.

    You can find any repository by name using artifactory_.find_repository(name).

    from dohq_artifactory import RepositoryLocal, RepositoryVirtual, RepositoryRemote
    
    # Local
    repo = RepositoryLocal(artifactory_, "reponame", packageType=RepositoryLocal.DEBIAN)
    repo.create()
    
    # Virtual
    v_repo = RepositoryVirtual(artifactory_, "pypi.all", repositories=["pypi.snapshot", "pypi.release"], packageType=RepositoryVirtual.PYPI)
    v_repo.create()
    
    # Remote
    r_repo = RepositoryRemote(artifactory_, "pypi.all", url="https://files.pythonhosted.org", packageType=RepositoryVirtual.PYPI)
    r_repo.create()