Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
7.0 kB
0
Indexable
Never
 Scratch Pad of Coding

import subprocess

# Define the Git clone command as a list of strings
git_clone_command = ["git", "clone", "https://github.com/yourusername/yourrepository.git"]

try:
    # Run the Git clone command
    subprocess.run(git_clone_command, check=True)
    print("Repository cloned successfully!")
except subprocess.CalledProcessError as e:
    print(f"Error cloning repository: {e}")



#this script defines a function (calculate_event_diff) which will take 2 event IDs as inputs comparing them as integers. Within the function, compare the event IDs returning a message that'll show if an event occurred before, after or same time as the other one. Script will prompt user to input two event IDs as integer. Will then call the (calculate_event_diff) function and print the result.


def calculate_event_diff(first_eventID, second_eventID):
    if first_eventID < second_eventID:
        return f"Event {first_eventID} occurred before Event {second_eventID}"
    elif first_eventID > second_eventID:
        return f"Event {first_eventID} occurred after Event {second_eventID}"
    else:
        return f"Event {first_eventID} and Event {second_eventID} are the same"

# Input event IDs from the user
try:
    first_eventID = int(input("Enter the first event ID: "))
    second_eventID = int(input("Enter the second event ID: "))

    # Call the comparison function and print the result
    result = calculate_event_diff(first_eventID, second_eventID)
    print(result)
except ValueError:
    print("Invalid input. Please enter valid event IDs as integers.")

        # TODO: How to use getVersionSetDiff to get the commit ID ranges of each VS ID.
    












Printing the HEAD of multiple packages in a Git repository involves running the git rev-parse HEAD command for each package directory. 

import os
import subprocess

# List of package directories within the repository
package_directories = ["package1", "package2", "package3"]

# Function to print the HEAD for a given package directory
def print_package_head(package_dir):
    try:
        # Change directory to the package directory
        os.chdir(package_dir)

        # Run the Git command to print the HEAD
        result = subprocess.run(["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE, text=True)

        if result.returncode == 0:
            # Print the HEAD of the current package
            print(f"Package: {package_dir}, HEAD: {result.stdout.strip()}")
        else:
            print(f"Error in {package_dir}: Git command failed")
    except Exception as e:
        print(f"Error in {package_dir}: {str(e)}")
    finally:
        # Change back to the original working directory
        os.chdir("..")

# Loop through each package directory and print the HEAD
for package_dir in package_directories:
    print_package_head(package_dir)

This script defines a function called print_package_head that prints the HEAD of a given package directory, and then it loops through the list of package directories, calling this function for each one.

The subprocess module in Python is a built-in module that allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. It provides a way to interact with the system's shell, execute external commands, and manage input and output streams from those commands. 

Here's what the subprocess module does:
Running External Commands: You can use subprocess to run external commands, like shell commands or other programs, from within your Python script.
Controlling Input and Output: subprocess allows you to interact with the standard input (stdin), standard output (stdout), and standard error (stderr) streams of the external processes you run. You can provide input to a process, capture its output, and handle error messages.
Process Communication: You can establish communication between your Python script and the external processes. This is useful for tasks like automating command-line tasks, parsing output, and processing results.
Error Handling: subprocess provides mechanisms to handle errors and exceptions that may occur when running external commands, allowing you to capture error messages and make decisions based on the success or failure of the command.

example: okinawa vs print —versionset CherryBlossom/mainline —eventid


**bash file to print all the packages with their assigned/respective commits at the HEAD of each package in the repository. 
Git command with shell scripting

#!/bin/bash

# Replace 'path/to/your/repo' with the actual path to your Git repository
repo_path="path/to/your/repo"

# Change to the repository directory
cd "$repo_path" || exit 1

# List all subdirectories (assuming each subdirectory corresponds to a package)
for package_dir in */; do
# Enter the package directory
cd "$package_dir" || continue

# Get the current branch and commit at HEAD
branch=$(git symbolic-ref —short HEAD)
commit=$(git rev-parse HEAD)

# Print package name, branch, and commit
echo "Package: $package_dir"
echo "Branch: $branch"
echo "HEAD Commit: $commit"
echo

# Return to the parent directory
cd ..
done


Here's what this script does:

It starts by specifying the path to the repository (repo_path). Replace this "path/to/your/repo" with the actual path to IndigoAlcatraz or the DMA3P repository.

The script changes to the repository directory using cd. If the path is invalid or doesn't exist, it exits with an error.

It uses a for loop to iterate through all subdirectories within the repository directory. Each subdirectory is assumed to correspond to a package.

Inside the loop, it uses Git commands to get the current branch and the commit at the HEAD.

It prints the package name, branch name, and HEAD commit for each package.

Finally, it returns to the parent directory and continues the loop for the next package.



import os
import subprocess

# Replace 'path/to/your/repo' with the actual path to your Git repository
repo_path = "path/to/your/repo"

# Change to the repository directory
os.chdir(repo_path)

# Function to get the HEAD commit for a package and event ID
def get_head_commit(package_dir, event_id):
    try:
        os.chdir(package_dir)
        result = subprocess.run(["git", "rev-parse", event_id], stdout=subprocess.PIPE, text=True)
        return result.stdout.strip()
    except Exception as e:
        return str(e)
    finally:
        os.chdir("..")

# Get event IDs from the user
event_id_1 = input("Enter the first event ID: ")
event_id_2 = input("Enter the second event ID: ")

# List of packages included in the main version set
main_version_set_packages = ["package1", "package2", "package3"]

# Loop through the main version set packages
for package_dir in main_version_set_packages:
    print(f"Package: {package_dir}")
    print(f"HEAD Commit for Event ID 1: {get_head_commit(package_dir, event_id_1)}")
    print(f"HEAD Commit for Event ID 2: {get_head_commit(package_dir, event_id_2)}")
    print()