---
# System prepended metadata

title: 多處理機平行程式助教 指南
tags: [成大多處理機平行程式設計]

---

# 多處理機平行程式助教 指南
###### tags: `成大多處理機平行程式設計`


[ToC]

## judge homework
1. download student submission into server
```
# github classroom download command
gh classroom clone student-repos -a 670856
```
2. copy homework files into server
```
# example: scp [OPTIONS] /path/to/local/file username@remote_ip:/path/to/remote/destination
scp -r /home/pesudocode/Desktop/多處理/hw1-1 sivslab@140.116.154.66:/home/sivslab/HW_2024/hw1-1
```
3. write python script
```python=
import os
import shutil
import subprocess
import csv

# Configuration: Adjust these paths accordingly
SUBMISSION_FOLDER = "./hw1-1-submissions"
HOMEWORK_FOLDER = "./hw1-1"
CSV_FILE = "hw1-1-results.csv"

def get_student_list(submission_folder):
    """Get list of student submission folder names."""
    return [f for f in os.listdir(submission_folder) if os.path.isdir(os.path.join(submission_folder, f))]

def copy_homework_to_submissions(homework_folder, submission_folder, students):
    """Copy homework content (folders and files) to each student's submission folder."""
    for student in students:
        dest_folder = os.path.join(submission_folder, student)
        try:
            # Iterate over all items (files and folders) in the homework folder
            for item in os.listdir(homework_folder):
                src_path = os.path.join(homework_folder, item)
                dest_path = os.path.join(dest_folder, item)

                # Copy directory recursively or individual files
                if os.path.isdir(src_path):
                    if os.path.exists(dest_path):
                        shutil.rmtree(dest_path)  # Remove existing folder if any
                    shutil.copytree(src_path, dest_path)
                else:
                    shutil.copy(src_path, dest_path)

            print(f"Copied homework files to {student}'s folder.")
        except Exception as e:
            print(f"Failed to copy files for {student}: {e}")

def run_command_in_folder(command, folder):
    """Run a shell command in the specified folder and return success and output."""
    try:
        result = subprocess.run(command, cwd=folder, shell=True, capture_output=True, text=True)
        success = (result.returncode == 0)
        output = result.stdout + result.stderr
    except Exception as e:
        success = False
        output = str(e)
    return success, output

def main():
    # Get the list of students (submission folder names)
    students = get_student_list(SUBMISSION_FOLDER)

    # Prepare the CSV file
    with open(CSV_FILE, mode='w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(["Student", "Make Success", "Judge Output"])

        # Copy homework files to each student's submission folder
        copy_homework_to_submissions(HOMEWORK_FOLDER, SUBMISSION_FOLDER, students)

        # Process each student's submission folder
        for student in students:
            student_folder = os.path.join(SUBMISSION_FOLDER, student)

            # Run `make clean && make`
            make_success, _ = run_command_in_folder("make clean && make", student_folder)

            # Run `judge` and capture the output
            _, judge_output = run_command_in_folder("judge", student_folder)

            # Write the results to the CSV
            writer.writerow([student, "Success" if make_success else "Fail", judge_output])

            print(f"Processed {student}: Make {'Success' if make_success else 'Fail'}")

if __name__ == "__main__":
    main()
```
```
# copy python script into server
scp /home/pesudocode/Desktop/多處理/hw1-1/hw1_1_judge.py sivslab@140.116.154.66:/home/sivslab/HW_2024/hw1-1
```
4. 執行judge
```
python3 hw1-1_judge.py
```

4. get result csv
```
scp sivslab@140.116.154.66:/home/sivslab/HW_2024/hw1-1/hw1-1-results.csv /home/pesudocode/Desktop/多處理/hw1-1

```
