Python example
Loading...
Overview
These Python examples demonstrate how to interact with the Unity Version Control CM REST API using the requests
library. You can use this code as a foundation for building Unity Version Control integrations into your Python scripts,
automation tools, or applications.
The examples cover both repository and workspace management operations:
The examples cover the following repository operations:
- List repositories: View all available repositories on your Unity Version Control server.
- Create repositories: Add new repositories with custom names and server configurations.
- Rename repositories: Update existing repository names.
- Delete repositories: Remove repositories from your server.
The examples cover the following workspace operations:
- List workspaces: Display all workspaces configured on your system.
- Create workspaces: Set up new workspaces linked to specific repositories.
- Delete workspaces: Remove workspace configurations.
Each example is self-contained and demonstrates proper error handling, user input collection, and API response processing patterns you can apply in your own Unity Version Control integrations.
List repositories
import sys
import requests
# Base URL for the Unity Version Control CM API
__api_url = "http://localhost:9090/api/v1"
def print_repositories(repositories):
"""Display repository information in a readable format"""
line = "{0} {1} {2}"
for repo in repositories:
print(line.format(repo["repId"]["id"],
repo["name"],
repo["server"]))
def get_repositories():
"""Retrieve all repositories from the Unity Version Control server"""
url = __api_url + "/repos"
req = requests.get(url)
if req.status_code is 200:
return req.json()
# Handle API errors gracefully
print("The repository list could not be retrieved.", file=sys.stderr)
exit(-1)
def main():
try:
print_repositories(get_repositories())
except requests.RequestException as e:
print(e)
exit(-1)
if __name__ == "__main__":
main()
Create repository
import sys
import requests
# Base URL for the Unity Version Control CM API
__api_url = "http://localhost:9090/api/v1"
def create_repository(name, server):
"""Send repository creation request to the Unity Version Control server"""
url = __api_url + "/repos"
params = {"name": name,
"server": server}
req = requests.post(url, params)
if req.status_code is not 200:
print("The repository could not be created", file=sys.stderr)
exit(-1)
def main():
# Collect repository details from user input
name = input("Write a name for the new repository: ")
server = input("Enter the PlasticSCM server address: ")
try:
create_repository(name, server)
except requests.RequestException as e:
print(e)
exit(-1)
if __name__ == "__main__":
main()
Rename repository
import sys
import requests
# Base URL for the Unity Version Control CM API
__api_url = "http://localhost:9090/api/v1"
def print_repositories(repositories):
"""Display repository information in a readable format"""
line = "{0} {1} {2}"
for repo in repositories:
print(line.format(repo["repId"]["id"],
repo["name"],
repo["server"]))
def get_repositories():
"""Retrieve all repositories from the Unity Version Control server"""
url = __api_url + "/repos"
req = requests.get(url)
if req.status_code is 200:
return req.json()
print("The repository list could not be retrieved.", file=sys.stderr)
exit(-1)
def rename_repo(old_name, new_name):
"""Update the name of an existing repository"""
url = __api_url + "/repos/" + old_name
params = {"name": new_name}
req = requests.put(url, params)
if req.status_code is not 200:
print("The repository could not be created.", file=sys.stderr)
exit(-1)
def main():
try:
# Show available repositories for user selection
print_repositories(get_repositories())
# Collect rename parameters from user
old_name = input("\nWrite the name of the repository to rename: ")
new_name = input("Write a new name for the repository " + old_name + ": ")
# Execute rename operation and display updated repository list
rename_repo(old_name, new_name)
print_repositories(get_repositories())
except requests.RequestException as e:
print(e, file=sys.stderr)
exit(-1)
if __name__ == "__main__":
main()
Delete repository
import sys
import requests
# Base URL for the Unity Version Control CM API
__api_url = "http://localhost:9090/api/v1"
def print_repositories(repositories):
"""Display repository information in a readable format"""
line = "{0} {1} {2}"
for repo in repositories:
print(line.format(repo["repId"]["id"],
repo["name"],
repo["server"]))
def get_repositories():
"""Retrieve all repositories from the Unity Version Control server"""
url = __api_url + "/repos"
req = requests.get(url)
if req.status_code is 200:
return req.json()
print("The repository list could not be retrieved.", file=sys.stderr)
exit(-1)
def remove_repo(name):
"""Delete a repository by name from the Unity Version Control server"""
url = __api_url + "/repos/" + name
req = requests.delete(url)
# Delete operations return 204 No Content on success
if req.status_code is not 204:
print("The repository could not be deleted", file=sys.stderr)
exit(-1)
def main():
try:
# Show available repositories for user selection
print_repositories(get_repositories())
repo = input("Write the name of the repository to delete: ")
# Execute deletion and display updated repository list
remove_repo(repo)
print_repositories(get_repositories())
except requests.RequestException as e:
print(e, file=sys.stderr)
exit(-1)
if __name__ == "__main__":
main()
List workspaces
import sys
import requests
# Base URL for the Unity Version Control CM API
__api_url = "http://localhost:9090/api/v1"
def print_workspaces(wkspaces):
"""Display workspace information in a readable format"""
line = "Name: {0}, path: {1}"
for wkspace in wkspaces:
print(line.format(wkspace["name"],
wkspace["path"]))
def get_workspaces():
"""Retrieve all workspaces configured on the system"""
url = __api_url + "/wkspaces"
req = requests.get(url)
if req.status_code is 200:
return req.json()
print("Workspace list could not be retrieved.", file=sys.stderr)
exit(-1)
def main():
try:
print_workspaces(get_workspaces())
except requests.RequestException as e:
print(e, file=sys.stderr)
exit(-1)
if __name__ == "__main__":
main()
Create workspace
import sys
import requests
# Base URL for the Unity Version Control CM API
__api_url = "http://localhost:9090/api/v1"
def print_repositories(repositories):
"""Display repository information in a readable format"""
line = "{0} {1} {2}"
for repo in repositories:
print(line.format(repo["repId"]["id"],
repo["name"],
repo["server"]))
def get_repositories():
"""Retrieve all repositories from the Unity Version Control server"""
url = __api_url + "/repos"
req = requests.get(url)
if req.status_code is 200:
return req.json()
print("The repository list could not be retrieved.", file=sys.stderr)
exit(-1)
def create_workspace(name, path, repository):
"""Create a new workspace linked to the specified repository"""
url = __api_url + "/wkspaces"
params = {"name": name,
"path": path,
"repository": repository}
req = requests.post(url, params)
if req.status_code is not 200:
print("The workspace could not be created.", file=sys.stderr)
exit(-1)
def main():
try:
# Show available repositories for workspace creation
print_repositories(get_repositories())
# Collect workspace configuration details from user
repository = input("Write the name of a repository to create a new workspace to: ")
name = input("Write the name for the new workspace: ")
path = input("Write the path for the new workspace: ")
create_workspace(name, path, repository)
except requests.RequestException as e:
print(e, file=sys.stderr)
exit(-1)
if __name__ == "__main__":
main()
Delete workspace
import sys
import requests
# Base URL for the Unity Version Control CM API
__api_url = "http://localhost:9090/api/v1"
def print_workspaces(wkspaces):
"""Display workspace information in a readable format"""
line = "Name: {0}, path: {1}"
for wkspace in wkspaces:
print(line.format(wkspace["name"],
wkspace["path"]))
def get_workspaces():
"""Retrieve all workspaces configured on the system"""
url = __api_url + "/wkspaces"
req = requests.get(url)
if req.status_code is 200:
return req.json()
print("Workspace list could not be retrieved.", file=sys.stderr)
exit(-1)
def remove_workspace(name):
"""Delete a workspace configuration by name"""
url = __api_url + "/wkspaces/" + name
req = requests.delete(url)
# Delete operations return 204 No Content on success
if req.status_code is not 204:
print("The workspace could not be deleted.", file=sys.stderr)
sys.exit(-1)
def main():
try:
# Show available workspaces for user selection
print_workspaces(get_workspaces())
name = input("Write the name of the workspace to delete: ")
remove_workspace(name)
except requests.RequestException as e:
print(e, file=sys.stderr)
exit(-1)
if __name__ == "__main__":
main()