You can run shell commands in Python using the subprocess module. Below is a Python program that will execute git clone for a given repository URL and print a message when the clone operation is finished successfully.
python
import subprocess
def git_clone(repo_url):
try:
print(f"Cloning repository from {repo_url}...")
# Run the git clone command
subprocess.run(["git", "clone", repo_url], check=True)
# If the command was successful, print the following message
print("Clone finished successfully.")
except subprocess.CalledProcessError as e:
# If an error occurred during git clone, print an error message
print(f"An error occurred while cloning the repository: {e}")
except Exception as e:
# Handle other possible exceptions
print(f"An unexpected error occurred: {e}")
# Replace 'your-repository-url' with the actual URL of the repository you want to clone
repo_url = 'your-repository-url'
git_clone(repo_url)
Make sure to replace 'your-repository-url' with the URL of the Git repository you want to clone.
Note: Running this script requires that you have Git installed on your system and it's accessible from the command line (i.e., in your system's PATH).
The subprocess.run() function will raise a subprocess.CalledProcessError if the command returns a non-zero exit status (which generally indicates an error). The check=True argument is what enables this behavior. If the command is successful and returns an exit status of 0, the error block will be skipped, and the success message will be printed.
Before running this script, please ensure that you handle credentials and other sensitive data securely if they are required for the cloning process.