### Setup script for Ubuntu system Source: https://github.com/devopshq/artifactory/blob/master/docs/CONTRIBUTE.md Installs necessary dependencies and runs pre-commit hooks for development on Ubuntu. ```bash python3 -m pip install tox python3 -m pip install .[tests] python3 -m pytest -munit # install ruby (required for pre-commit) sudo apt install ruby # run pre-commit python3 -m pre_commit install python3 -m pre_commit run --all-files ``` -------------------------------- ### General setup script Source: https://github.com/devopshq/artifactory/blob/master/docs/CONTRIBUTE.md A general script for setting up the development environment, applicable to systems other than Ubuntu. ```bash python -m pip install tox python -m pip install .[tests] python -m pytest -munit # Install `pre-commit` hooks after clone: pre-commit install pre-commit run --all-files ``` -------------------------------- ### AQL Query Example Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of constructing and executing an AQL query to find artifacts. ```python aqlargs = [ "items.find", { "$and": [ {"repo": "docker-prod"}, {"type": "file"}, {"created": {"$gt": "2019-07-10T19:20:30.45+01:00"}}, ] }, ".include", ["repo", "path", "name", "type"], ".sort", {"$asc": ["repo", "path", "name"]}, ]artifacts_list = arti_path.aql(*aqlargs) # You can convert to pathlib object: artifact_pathlib = map(arti_path.from_aql, artifacts_list) artifact_pathlib_list = list(map(arti_path.from_aql, artifacts_list)) ``` -------------------------------- ### Artifactory SaaS Path Example Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of using ArtifactorySaaSPath for Artifactory SaaS solutions. ```python from artifactory import ArtifactorySaaSPath path = ArtifactorySaaSPath( "https://myartifactorysaas.jfrog.io/myartifactorysaas/folder/path.xml", apikey="MY_API_KEY", ) ``` -------------------------------- ### API Keys Source: https://github.com/devopshq/artifactory/blob/master/README.md Code examples for creating, getting, regenerating, and revoking API keys for a user. ```python from dohq_artifactory import User user = User(artifactory_, "username") # create an API key user.api_key.create() # get API key user.api_key.get() # or using str() method my_key = str(user.api_key) # or using repr method print(user.api_key) # regenerate API key if one already exists user.api_key.regenerate() # remove API key for current user user.api_key.revoke() # remove all API keys in system, only if user has admin rights user.api_key.revoke_for_all_users() ``` -------------------------------- ### Download Statistics Example Source: https://github.com/devopshq/artifactory/blob/master/README.md Getting download statistics for an artifact, such as download count and last downloaded by. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://repo.jfrog.org/artifactory/distributions/org/apache/tomcat/apache-tomcat-7.0.11.tar.gz" ) # Get FileStat download_stat = path.download_stats() print(download_stat) print(download_stat.last_downloaded) print(download_stat.last_downloaded_by) print(download_stat.download_count) print(download_stat.remote_download_count) print(download_stat.remote_last_downloaded) print(download_stat.uri) ``` -------------------------------- ### Logging - Simplest Example Source: https://github.com/devopshq/artifactory/blob/master/README.md Simplest example to add debug messages to a console for the Artifactory module. ```python import logging from artifactory import ArtifactoryPath logging.basicConfig() # set level only for artifactory module, if omitted, then global log level is used, eg from basicConfig logging.getLogger("artifactory").setLevel(logging.DEBUG) path = ArtifactoryPath( "http://my-artifactory/artifactory/myrepo/restricted-path", apikey="MY_API_KEY" ) ``` -------------------------------- ### Install latest development version Source: https://github.com/devopshq/artifactory/blob/master/README.md Installs the latest development version of dohq-artifactory, which may contain errors. ```bash pip install dohq-artifactory --upgrade --pre ``` -------------------------------- ### Promote Docker Image Example Source: https://github.com/devopshq/artifactory/blob/master/README.md Promoting a Docker image from one registry to another. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath("http://example.com/artifactory") path.promote_docker_image("docker-staging", "docker-prod", "my-application", "0.5.1") ``` -------------------------------- ### Install specific version Source: https://github.com/devopshq/artifactory/blob/master/README.md Installs a specific version of the dohq-artifactory package. ```bash pip install dohq-artifactory==0.5.dev243 ``` -------------------------------- ### Install dohq-artifactory Source: https://github.com/devopshq/artifactory/blob/master/README.md Installs or upgrades the dohq-artifactory package to the latest version. ```bash pip install dohq-artifactory --upgrade ``` -------------------------------- ### Run Artifactory Pro inside docker Source: https://github.com/devopshq/artifactory/blob/master/docs/CONTRIBUTE.md Command to start Artifactory Pro in a Docker container for integration testing. ```bash docker run --name artifactory-pro -d -p 8081:8081 -p8082:8082 docker.bintray.io/jfrog/artifactory-pro ``` -------------------------------- ### Get Directory Listing Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of iterating through a directory to get a listing of its contents. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath("http://repo.jfrog.org/artifactory/gradle-ivy-local") for p in path: print(p) ``` -------------------------------- ### Download Artifact to Local Filesystem Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of downloading an artifact from Artifactory to the local filesystem. ```python 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, open("tomcat.tar.gz", "wb") as out: out.write(fd.read()) ``` -------------------------------- ### Artifactory Query Language (AQL) - Basic Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of using Artifactory Query Language (AQL) for basic queries, including dictionary and list support. ```python from artifactory import ArtifactoryPath arti_path = ArtifactoryPath( "http://my-artifactory/artifactory" ) # path to artifactory, NO repo # dict support # Send query: # items.find({"repo": "myrepo"}) artifacts = arti_path.aql("items.find", {"repo": "myrepo"}) ``` ```python # list support. # Send query: # items.find().include("name", "repo") artifacts = arti_path.aql("items.find()", ".include", ["name", "repo"]) ``` -------------------------------- ### HTTP Basic Authentication Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of using HTTP Basic Authentication with ArtifactoryPath. ```python from requests.auth import HTTPBasicAuth path = ArtifactoryPath( "http://my-artifactory/artifactory/myrepo/restricted-path", auth=("USERNAME", "PASSWORD"), auth_type=HTTPBasicAuth, ) ``` -------------------------------- ### Builds Management - Get All Builds Source: https://github.com/devopshq/artifactory/blob/master/README.md Initializing ArtifactoryBuildManager and retrieving all builds. ```python from artifactory import ArtifactoryBuildManager arti_build = ArtifactoryBuildManager( "https://repo.jfrog.org/artifactory", project="proj_name", auth=("admin", "admin") ) # Get all builds all_builds = arti_build.builds print(all_builds) ``` -------------------------------- ### RepositoryVirtual Source: https://github.com/devopshq/artifactory/blob/master/README.md Examples for finding, creating, reading, and deleting virtual repositories, including specifying associated local repositories and package types. ```python # Find from dohq_artifactory import RepositoryVirtual repo = artifactory_.find_repository_virtual("pypi.all") # Create if repo is None: # or RepositoryVirtual.PYPI, RepositoryLocal.NUGET, etc repo = RepositoryVirtual( artifactory_, "pypi.all", repositories=["pypi.snapshot", "pypi.release"], packageType=RepositoryVirtual.PYPI, ) repo.create() # You can re-read from Artifactory repo.read() local_repos = repo.repositories # return List repo.delete() ``` -------------------------------- ### RepositoryLocal Source: https://github.com/devopshq/artifactory/blob/master/README.md Code examples for finding, creating, reading, and deleting local repositories, specifying package types. ```python # Find from dohq_artifactory import generate_password, RepositoryLocal repo = artifactory_.find_repository_local("reponame") # Create if repo is None: # or RepositoryLocal.PYPI, RepositoryLocal.NUGET, etc repo = RepositoryLocal(artifactory_, "reponame", packageType=RepositoryLocal.DEBIAN) repo.create() # You can re-read from Artifactory repo.read() repo.delete() ``` -------------------------------- ### Exception Handling Example Source: https://github.com/devopshq/artifactory/blob/master/README.md Catching and inspecting ArtifactoryException, including the underlying HTTP error. ```python from artifactory import ArtifactoryPath from dohq_artifactory.exception import ArtifactoryException path = ArtifactoryPath( "http://my_arti:8080/artifactory/installer/", auth=("wrong_user", "wrong_pass") ) try: path.stat() except ArtifactoryException as exc: print(exc) # clean artifactory error message # >>> Bad credentials print( exc.__cause__ ) # HTTP error that triggered exception, you can use this object for more info # >>> 401 Client Error: Unauthorized for url: http://my_arti:8080/artifactory/installer/ ``` -------------------------------- ### Project Source: https://github.com/devopshq/artifactory/blob/master/README.md Examples for finding, creating, reading, and deleting projects within Artifactory, using ArtifactoryPath for connection. ```python # Find from artifactory import ArtifactoryPath from dohq_artifactory import Project artifactory_ = ArtifactoryPath( "http://my-artifactory/artifactory/myrepo/restricted-path", token="MY_TOKEN" ) project = artifactory_.find_project("t1k1") # Create if project is None: project = Project(artifactory_, "t1k1", "t1k1_display_name") project.create() # You can re-read from Artifactory project.read() project.delete() ``` -------------------------------- ### Loading Credentials from Global Config Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of using ArtifactoryPath with credentials loaded from global configuration. ```python path = ArtifactoryPath( "http://my-artifactory/artifactory/myrepo/restricted-path", auth_type=HTTPBasicAuth, ) path.touch() ``` -------------------------------- ### Artifact Stat Example Source: https://github.com/devopshq/artifactory/blob/master/README.md Retrieving file statistics for an artifact, including hashes, creator, and dates. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://repo.jfrog.org/artifactory/distributions/org/apache/tomcat/apache-tomcat-7.0.11.tar.gz" ) # Get FileStat stat = path.stat() print(stat) print(stat.ctime) print(stat.mtime) print(stat.created_by) print(stat.modified_by) print(stat.mime_type) print(stat.size) print(stat.sha1) print(stat.sha256) print(stat.md5) print(stat.is_dir) print(stat.children) print(stat.repo) ``` -------------------------------- ### Get artifacts by exact path Source: https://github.com/devopshq/artifactory/blob/master/README.md This snippet shows how to generate and perform an AQL query to get artifacts by an exact repository path. ```python # Will generate and perform AQL query for getting artifacts by path or name for artifacts in repo["my_package"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.0-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.0"], ...} # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.1.dev5-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.1.dev5"], ...} # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.1.0-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.1.0"], ...} # ... ``` -------------------------------- ### Artifactory Query Language (AQL) - Complex Query Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of constructing and sending a complex AQL query with multiple conditions and operators. ```python from artifactory import ArtifactoryPath arti_path = ArtifactoryPath( "http://my-artifactory/artifactory" ) # path to artifactory, NO repo # support complex query # Example 1 # items.find( # { # "$and": [ # {"repo": {"$eq": "repo"}}, # {"$or": [{"path": {"$match": "*path1"}}, {"path": {"$match": "*path2"}}鼐 # ] # } # ) aqlargs = [ "items.find", { "$and": [ {"repo": {"$eq": "repo"}}, { "$or": [ {"path": {"$match": "*path1"}}, {"path": {"$match": "*path2"}}, ] }, ] }, ] # artifacts_list contains raw data (list of dict) # Send query artifacts_list = arti_path.aql(*aqlargs) ``` -------------------------------- ### HTTP Digest Authentication Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of using HTTP Digest Authentication with ArtifactoryPath. ```python from requests.auth import HTTPDigestAuth path = ArtifactoryPath( "http://my-artifactory/artifactory/myrepo/restricted-path", auth=("USERNAME", "PASSWORD"), auth_type=HTTPDigestAuth, ) ``` -------------------------------- ### Authentication with Access Token Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of authenticating with an access token to access restricted Artifactory resources. ```python from artifactory import ArtifactoryPath # Access Token path = ArtifactoryPath( "http://my-artifactory/artifactory/myrepo/restricted-path", token="MY_ACCESS_TOKEN" ) ``` -------------------------------- ### Authentication with API Key Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of authenticating with an API key to access restricted Artifactory resources. ```python from artifactory import ArtifactoryPath # API_KEY path = ArtifactoryPath( "http://my-artifactory/artifactory/myrepo/restricted-path", apikey="MY_API_KEY" ) ``` -------------------------------- ### Artifact Retrieval Example Source: https://github.com/devopshq/artifactory/blob/master/README.md This snippet demonstrates how to retrieve artifacts by group, package name, and version using a Python-like syntax, and shows the expected output including artifact URLs and properties. ```python # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/package-1.0.0-javadoc.jar # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/package-1.0.0.pom # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/package-1.0.0.jar # {"build.number": ["123"], "build.name": ["1.0.0"], ...} # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.1/maven-metadata.xml # ... # Get artifacts by group, package name and version for artifacts in repo["my.group:package:1.0.0"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/maven-metadata.xml # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/package-1.0.0-source.jar # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/package-1.0.0-javadoc.jar # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/package-1.0.0.pom # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/package-1.0.0.jar # {"build.number": ["123"], "build.name": ["1.0.0"], ...} ``` -------------------------------- ### Get artifacts using partial match Source: https://github.com/devopshq/artifactory/blob/master/README.md This snippet demonstrates how to use a partial match (wildcard) in the AQL query to retrieve artifacts. ```python # Using partial match for artifacts in repo["my_pack*"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.0-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.0"], ...} # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.1.dev5-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.1.dev5"], ...} # http://my.artifactory.com/artifactory/pypi.all/my_package_new_/my_package_new-0.0.1-py3.whl # {"pypi.name": ["my_package_new"], "pypy_version": ["0.0.1"], ...} ``` -------------------------------- ### Session Management Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of reusing an established connection by passing a session parameter to ArtifactoryPath. ```python from artifactory import ArtifactoryPath import requests ses = requests.Session() ses.auth = ("username", "password") path = ArtifactoryPath( "http://my-artifactory/artifactory/myrepo/my-path-1", sesssion=ses ) path.touch() path = ArtifactoryPath( "http://my-artifactory/artifactory/myrepo/my-path-2", sesssion=ses ) path.touch() ``` -------------------------------- ### Get repository of any type Source: https://github.com/devopshq/artifactory/blob/master/README.md A simple function to find any type of repository by its name. ```python # Find any repo by name repo = artifactory_.find_repository("pypi.all") ``` -------------------------------- ### Authentication with Username and Password or API Key Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of authenticating with username and password or an API key to access restricted Artifactory resources. ```python from artifactory import ArtifactoryPath # User and password OR API_KEY path = ArtifactoryPath( "http://my-artifactory/artifactory/myrepo/restricted-path", auth=("USERNAME", "PASSWORD or API_KEY") ) ``` -------------------------------- ### Download Artifact Folder as Archive Source: https://github.com/devopshq/artifactory/blob/master/README.md Examples of downloading an artifact folder as an archive (zip, tar, tar.gz, tgz), with options for specifying archive type, checksum, and chunked writing. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://my_url:8080/artifactory/my_repo/winx64/aas", auth=("user", "password") ) 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) ``` -------------------------------- ### Manage Artifact Properties Source: https://github.com/devopshq/artifactory/blob/master/README.md Demonstrates getting, setting, updating, and removing properties from an artifact. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://repo.jfrog.org/artifactory/distributions/org/apache/tomcat/apache-tomcat-7.0.11.tar.gz" ) # Get properties properties = path.properties print(properties) # Update a property or add if does not exist properties["qa"] = "tested" path.properties = properties # add/replace set of properties new_props = { "test": ["test_property"], "time": ["2018-01-16 12:17:44.135143"], "addthis": ["addthis"], } path.properties = new_props # Remove properties properties.pop("release") path.properties = properties ``` -------------------------------- ### Group - Internal Source: https://github.com/devopshq/artifactory/blob/master/README.md Examples for finding, creating, updating, and deleting internal groups, including adding and removing users. ```python # Find from dohq_artifactory import generate_password, Group group = artifactory_.find_group("groupname") # Create if group is None: group = Group(artifactory_, "groupname") group.create() # You can re-read from Artifactory group.read() # You can add multiple users at once to Group group.users = ["admin", "anonymous"] group.create() # You can remove all users from a Group group.users = [] group.create() group.delete() ``` -------------------------------- ### Find Files Recursively Source: https://github.com/devopshq/artifactory/blob/master/README.md Example of using glob to find all files matching a pattern recursively within a directory. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath("http://repo.jfrog.org/artifactory/distributions/org/") for p in path.glob("**/*.gz"): print(p) ``` -------------------------------- ### Get Repository Scheduled Replication Status Source: https://github.com/devopshq/artifactory/blob/master/README.md Retrieves the status of scheduled cron-based replication jobs for a repository. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "https://repo.jfrog.org/artifactory/repo1-cache/archetype-catalog.xml" ) rep_status = path.replication_status print("status: ", rep_status["status"]) ``` -------------------------------- ### Download Artifact in Chunks Source: https://github.com/devopshq/artifactory/blob/master/README.md Examples of downloading artifacts in chunks to manage memory usage, including options for specifying output file, progress function, and chunk size. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://repo.jfrog.org/artifactory/distributions/org/apache/tomcat/apache-tomcat-7.0.11.tar.gz" ) # download by providing path to out file and use default chunk 1024 path.writeto(out="tomcat.tar.gz") # download and suppress progress messages path.writeto(out="tomcat2.tar.gz", progress_func=None) # download by providing out as file object and specify chunk size with open("tomcat3.tar.gz", "wb") as out: path.writeto(out, chunk_size=256) # download and use custom print function def custom_print(bytes_now, total, custom): """ Custom function that accepts first two arguments as [int, int] in its signature """ print(bytes_now, total, custom) # since writeto requires [int, int] in its signature, all custom arguments you have to provide via lambda function or # similar methods path.writeto( out="tomcat5.tar.gz", progress_func=lambda x, y: custom_print(x, y, custom="test"), ) ``` -------------------------------- ### Common Admin Object Operations Source: https://github.com/devopshq/artifactory/blob/master/README.md Provides examples of common operations supported by AdminObjects, such as reading raw JSON, creating new repositories with additional parameters, and performing CRUD operations (read, create, update, delete). ```python user = artifactory_.find_user("username") print(user.raw) # JSON response from Artifactory new_repo = RepositoryLocal(artifactory, "reponame") # If some key you can't find in object, you can use this: new_repo.additional_params["property_sets"] = ["my", "properties_sets"] new_repo.create() # All object support CRUD operations: obj.read() # Return True if user exist (and read from Artifactory), else return False obj.create() obj.update() obj.delete() # ArtifactoryPath have different find_ method: artifactory_.find_user("name") artifactory_.find_group("name") artifactory_.find_repository_local("name") artifactory_.find_permission_target("name") artifactory_.find_project("project_key") ``` -------------------------------- ### Run unit tests Source: https://github.com/devopshq/artifactory/blob/master/docs/CONTRIBUTE.md Command to specifically run unit tests. ```bash python -m pytest -munit ``` -------------------------------- ### Run tests via tox Source: https://github.com/devopshq/artifactory/blob/master/docs/CONTRIBUTE.md Commands to execute unit and integration tests using tox. ```bash # Run unit tests tox # Run integration tests tox -- -mintegration # Run Unit and Integration tests tox -- "" ``` -------------------------------- ### Useful commands with docker Source: https://github.com/devopshq/artifactory/blob/master/docs/CONTRIBUTE.md Docker commands for managing the Artifactory Pro container. ```bash # Stop artifactory, but save License and user\password docker stop artifactory-pro # Start stopped Artifactory. docker start artifactory-pro # Remove installed Artifactory. You'll have to comlete initialize steps again! docker rm artifactory-pro ``` -------------------------------- ### Run integration tests Source: https://github.com/devopshq/artifactory/blob/master/docs/CONTRIBUTE.md Commands to run integration tests, either with the current Python environment or via tox. ```bash # Run with current python python -mpytest -mintegration # Run through all versions with we do support tox -- -mintegration ``` -------------------------------- ### Builds Management - Build Promotion Source: https://github.com/devopshq/artifactory/blob/master/README.md Promoting a build, optionally moving artifacts and setting properties. ```python build_number1.promote( ci_user="admin", properties={"components": ["c1", "c3", "c14"], "release-name": ["fb3-ga"]}, ) ``` -------------------------------- ### PermissionTarget Management Source: https://github.com/devopshq/artifactory/blob/master/README.md Demonstrates how to find, create, and manage permission targets, including adding and removing repositories, users, and groups with specific roles. ```python from dohq_artifactory import PermissionTarget permission = artifactory_.find_permission_target("rule") if permission is None: # Permission target does not exist permission = PermissionTarget( artifactory_, "rule", repositories=Object, # , users=Object, # , groups=Object, # , includes_pattern="com.mycompany.myproject1.**,com.othercompany.**" excludes_pattern="com.othercompany.projectX.**,com.othercompany.projectY.**" ) permission.create() # See repositories, users or groups permission.repositories # Result: # # permission.users # Result: # # permission.groups # Result: # # # Add repo (string or Repository) object permission.add_repository("repo3", "repo4") permission.add_repository(repo5_object) # Or remove permission.remove_repository("repo1", "repo2") # Add user (string or User object) with specific permission permission.add_user("user3", PermissionTarget.ROLE_ADMIN) permission.add_user( user4_object, PermissionTarget.ROLE_READ + PermissionTarget.ROLE_WRITE ) # You can add sum of permissions # Or remove permission.remove_user("user1", "user2") # Add group (string or Group object) with permission permission.add_group("group3", PermissionTarget.ROLE_ADMIN) permission.add_group( group4_object, PermissionTarget.ROLE_READ + PermissionTarget.ROLE_WRITE ) # You can add sum of permissions # Or remove permission.remove_group("group1", "group2") permission.update() # Update!! permission.repositories # Result: # # # permission.users # Result: # # permission.groups # Result: # # ``` -------------------------------- ### User Management - Find or Create User (Method 1) Source: https://github.com/devopshq/artifactory/blob/master/README.md Finding a user by username, or creating a new user if not found. ```python from dohq_artifactory import generate_password, User user = artifactory_.find_user("username") if user is None: # User does not exist user = User( artifactory_, "username", "username@example.com", password=generate_password() ) user.create() ``` -------------------------------- ### Token Management Source: https://github.com/devopshq/artifactory/blob/master/README.md Illustrates how to read and create access tokens using the Artifactory Python SDK, including setting scopes, usernames, expiration, and refreshability. ```python from requests.auth import HTTPBasicAuth from artifactory import ArtifactoryPath from dohq_artifactory import Token session = ArtifactoryPath( "https://artifactory_dns/artifactory", auth=("admin", "admin_password"), auth_type=HTTPBasicAuth, verify=False, ) # Read token for readers group group_name = "readers" scope = "api:* member-of-groups:" + group_name token = Token(session, scope=scope) token.read() # Create token for member of the readers group_name = "readers" scope = "api:* member-of-groups:" + group_name subject = group_name token = Token( session, scope=scope, username=subject, expires_in=31557600, refreshable=True ) response = token.create() print("Readonly token:") print("Username: " + token.username) print("Token: " + token.token["access_token"]) ``` -------------------------------- ### Deploy a regular file Source: https://github.com/devopshq/artifactory/blob/master/README.md Deploys a regular file to Artifactory and calculates all available checksums. ```python 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") ``` -------------------------------- ### User Management - Find or Create User (Method 2) Source: https://github.com/devopshq/artifactory/blob/master/README.md An alternative method to find or create a user, checking existence with read(). ```python user = User(artifactory_, "username") if not user.read(): # Return True if user exist # User does not exist user = User( artifactory_, "username", "username@example.com", password=generate_password() ) user.create() ``` -------------------------------- ### Builds Management - Build Info Source: https://github.com/devopshq/artifactory/blob/master/README.md Retrieving detailed information for a specific build run. ```python build_number1 = all_runs[0] print(build_number1.info) ``` -------------------------------- ### GroupLDAP Source: https://github.com/devopshq/artifactory/blob/master/README.md Demonstrates how to create an LDAP group in Artifactory using its full DN path. ```python # Full DN path in artifactory dn = "cn=R.DevOps.TestArtifactory,ou=Groups,dc=example,dc=com" attr = "ldapGroupName=r.devops.testartifactory;groupsStrategy=STATIC;groupDn={}".format( dn ) test_group = GroupLDAP( artifactory=artifactory_, name="r.devops.testartifactory", realm_attributes=attr ) test_group.create() ``` -------------------------------- ### SSL Cert Verification Options - Specify Local Cert Source: https://github.com/devopshq/artifactory/blob/master/README.md Specifying a local cert file to use as a client-side certificate for SSL verification. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0", cert="/path_to_file/server.pem", ) ``` -------------------------------- ### Docker Artifact Search Source: https://github.com/devopshq/artifactory/blob/master/README.md Demonstrates how to search for Docker artifacts by image name and tag, including partial searches. ```python # Get repo repo = artifactory_.find_repository("docker.all") # Get artifacts by image name for artifacts in repo["my_image"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/docker.all/my_image/latest/manifest.json # {"docker.repoName": ["my_image"], "docker.manifest": ["latest"], ...} # http://my.artifactory.com/artifactory/docker.all/my_image/1.0.0/manifest.json # {"docker.repoName": ["my_image"], "docker.manifest": ["1.0.0"], ...} # http://my.artifactory.com/artifactory/docker.all/my_image/1.1.0/manifest.json # {"docker.repoName": ["my_image"], "docker.manifest": ["1.1.0"], ...} # ... # Get artifacts by specific version for artifacts in repo["my_image:1.0.0"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/docker.all/my_image/1.0.0/manifest.json # {"docker.repoName": ["my_image"], "docker.manifest": ["1.0.0"], ...} # ... for artifacts in repo["my_image:latest"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/docker.all/my_image/latest/manifest.json # {"docker.repoName": ["my_image"], "docker.manifest": ["latest"], ...} # ... # Partial search for artifacts in repo["my_package:"dev*"]: print(artifact) print(artifact.properties) # http://my.artifactory.com/artifactory/docker.all/my_image/dev/manifest.json # {"pypi.name": ["my_package"], "pypy_version": ["dev"]} # http://my.artifactory.com/artifactory/docker.all/my_image/1.0.1-dev5/manifest.json # {"pypi.name": ["my_package"], "pypy_version": ["1.0.1-dev5"]} # ... ``` -------------------------------- ### PyPi Artifact Search Source: https://github.com/devopshq/artifactory/blob/master/README.md Demonstrates how to search for PyPi artifacts by package name and version using various operators. ```python # Get repo repo = artifactory_.find_repository("pypi.all") # Get artifacts by package name for artifacts in repo["my_package"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.0-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.0"], ...} # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.1.dev5-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.1.dev5"], ...} # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.1.0-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.1.0"], ...} # ... # Get artifacts by specific version for artifacts in repo["my_package==1.0.0"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.0-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.0"], ...} # Using other pip operators (result should be additionaly checked!) for artifacts in repo["my_package!=1.0.0"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.1.dev5-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.1.dev5"], ...} # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.1.0-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.1.0"], ...} # ... # In case of using > or < operators, the result should be additionaly checked # because Artifactory compares strings, not versions for artifacts in repo["my_package>=1.0.0"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.1.dev5-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.1.dev5"], ...} # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.1.0-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.1.0"], ...} # ... ``` -------------------------------- ### SSL Cert Verification Options - Default Source: https://github.com/devopshq/artifactory/blob/master/README.md Default SSL certificate verification, equivalent to verify=True. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0" ) ``` -------------------------------- ### SSL Cert Verification Options - Explicit True Source: https://github.com/devopshq/artifactory/blob/master/README.md Explicitly setting SSL certificate verification to True. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0", verify=True ) ``` -------------------------------- ### Add to group Source: https://github.com/devopshq/artifactory/blob/master/README.md Demonstrates how to add a user to a group by name or by group object, and the necessity of updating the user. ```python user.add_to_group("byname") group = artifactory_.find_group("groupname") user.add_to_group(group) user.update() # Don't forget update :) ``` -------------------------------- ### Builds Management - Builds Diff Source: https://github.com/devopshq/artifactory/blob/master/README.md Comparing a build with an older build to identify changes. ```python print(build_number1.diff(3)) ``` -------------------------------- ### RepositoryRemote Source: https://github.com/devopshq/artifactory/blob/master/README.md Code snippets for finding, creating, reading, and deleting remote repositories, specifying the URL and package type. ```python # Find from dohq_artifactory import RepositoryRemote repo = artifactory_.find_repository_virtual("pypi.all") # Create if repo is None: # or RepositoryRemote.PYPI, RepositoryRemote.NUGET, etc repo = RepositoryRemote( artifactory_, "pypi.all", url="https://files.pythonhosted.org", packageType=RepositoryVirtual.PYPI, ) repo.create() # You can re-read from Artifactory repo.read() repo.delete() ``` -------------------------------- ### Deploy artifact by checksum Source: https://github.com/devopshq/artifactory/blob/master/README.md Deploys an artifact by checking if the artifact content already exists in Artifactory using SHA1 or SHA256 checksums. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath("http://my-artifactory/artifactory/my_repo/foo") sha1 = "1be5d2dbe52ddee96ef2d17d354e2be0a155a951" sha256 = "00bbf80ccca376893d60183e1a714e707fd929aea3e458f9ffda60f7ae75cc51" # If you don't know sha value, you can calculate it via # sha1 = artifactory.sha1sum("local_path_of_your_file") # or # sha256 = artifactory.sha256sum("local_path_of_your_file") # Each of the following 4 methods works fine if the artifact content already # exists in Artifactory. path.deploy_by_checksum(sha1=sha1) # deploy by sha1 via checksum parameter path.deploy_by_checksum(checksum=sha1) # deploy by sha256 via sha256 parameter path.deploy_by_checksum(sha256=sha256) # deploy by sha256 via checksum parameter path.deploy_by_checksum(checksum=sha256) ``` -------------------------------- ### Deploy a debian package to an existent folder Source: https://github.com/devopshq/artifactory/blob/master/README.md Deploys a debian package to an existing folder in Artifactory. ```python 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" ) ``` -------------------------------- ### SSL Cert Verification Options - Disable InsecureRequestWarning Source: https://github.com/devopshq/artifactory/blob/master/README.md Disabling InsecureRequestWarning when host certificate verification is disabled. ```python import requests.packages.urllib3 as urllib3 urllib3.disable_warnings() ``` -------------------------------- ### Admin Area - Initialize ArtifactoryPath Source: https://github.com/devopshq/artifactory/blob/master/README.md Initializing ArtifactoryPath for admin operations without specifying a repository. ```python from artifactory import ArtifactoryPath artifactory_ = ArtifactoryPath( "https://artifactory.example.com/artifactory", auth=("user", "password") ) ``` -------------------------------- ### Maven Artifact Search Source: https://github.com/devopshq/artifactory/blob/master/README.md Demonstrates how to search for Maven artifacts by group name and group/package name. ```python # Get repo repo = artifactory_.find_repository("maven.all") # Get artifacts by group name for artifacts in repo["my.group"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/maven-metadata.xml # ... # http://my.artifactory.com/artifactory/maven.all/my/group/another_package/1.2.3/maven-metadata.xml # ... # Get artifacts by group and package name for artifacts in repo["my.group:package"]: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/maven-metadata.xml # http://my.artifactory.com/artifactory/maven.all/my/group/package/1.0.0/package-1.0.0-source.jar ``` -------------------------------- ### Access repository child item Source: https://github.com/devopshq/artifactory/blob/master/README.md Shows how to access folders and files within a repository using path-like operations. ```python # Get repo repo = artifactory_.find_repository("pypi.all") # Access a folder within the repo package = repo / "my_package" # Result: # ArtifactPath('http://my.artifactory.com/artifactory/pypi.all/my_package') # Access a file within the repo package = repo / "my_package" / "my_artifact.tar.gz" # Result: # ArtifactPath('http://my.artifactory.com/artifactory/pypi.all/my_package/my_artifact.tar.gz') ``` -------------------------------- ### Deploy a debian package to a non-existent folder Source: https://github.com/devopshq/artifactory/blob/master/README.md Deploys a debian package to a non-existent folder in Artifactory, with options for setting multiple distribution and architecture values. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://my-artifactory/artifactory/ubuntu-local/pool/myapp-1.0.deb" ) path.deploy_deb( "./myapp-1.0.deb", distribution="trusty", component="main", architecture="amd64" ) # if you want to set multiple values you can use list to set them path.deploy_deb( "./myapp-1.0.deb", distribution=["dist1", "dist2"], component="main", architecture=["amd64", "i386"], ) ``` -------------------------------- ### Iterate over repository artifacts Source: https://github.com/devopshq/artifactory/blob/master/README.md Demonstrates how to iterate through all artifacts within a repository and access their properties. ```python # Get repo repo = artifactory_.find_repository("pypi.all") # Iterate over repo for artifact in repo: print(artifact) print(artifact.properties) # Result: # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.0-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.0"]} # http://my.artifactory.com/artifactory/pypi.all/my_package/my_package-1.0.1.dev5-py3.whl # {"pypi.name": ["my_package"], "pypy_version": ["1.0.1.dev5"]} # http://my.artifactory.com/artifactory/pypi.all/other_package/other_package-0.0.1-py3.whl # {"pypi.name": ["other_package"], "pypy_version": ["0.0.1"]} # ... ``` -------------------------------- ### SSL Cert Verification Options - Disable Host Cert Verification Source: https://github.com/devopshq/artifactory/blob/master/README.md Disabling host certificate verification. Note: This will cause urllib3 to throw an InsecureRequestWarning. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0", verify=False ) ``` -------------------------------- ### Deploy artifacts from archive Source: https://github.com/devopshq/artifactory/blob/master/README.md Deploys artifacts from an archive, extracting contents on the server while preserving archive paths. ```python 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", explode_archive=True) ``` -------------------------------- ### Builds Management - Build Runs Source: https://github.com/devopshq/artifactory/blob/master/README.md Accessing build runs for a specific build. ```python build1 = all_builds[0] all_runs = build1.runs print(all_runs) ``` -------------------------------- ### Atomically deploy artifacts from archive Source: https://github.com/devopshq/artifactory/blob/master/README.md Atomically deploys artifacts from an archive, extracting contents on the server. Useful for indexing purposes. ```python 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", explode_archive=True, explode_archive_atomic=True ) ``` -------------------------------- ### Timeout on requests - Set Timeout Source: https://github.com/devopshq/artifactory/blob/master/README.md Setting a 5-second timeout for requests. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0", timeout=5 ) ``` -------------------------------- ### Timeout on requests - Explicit None Source: https://github.com/devopshq/artifactory/blob/master/README.md Explicitly setting the timeout to None. ```python from artifactory import ArtifactoryPath path = ArtifactoryPath( "http://my-artifactory/artifactory/libs-snapshot-local/myapp/1.0", timeout=None ) ``` -------------------------------- ### Move Artifacts Source: https://github.com/devopshq/artifactory/blob/master/README.md Moves artifacts from a source path to a destination path. The `dry_run=True` option allows checking the command without making changes. ```python 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) ``` ```python source.move(dest, dry_run=True) ```