Refactor folder creation logic and added color debug messages
This commit is contained in:
parent
f4719b115a
commit
909fed29d4
9 changed files with 49 additions and 88 deletions
Binary file not shown.
Before Width: | Height: | Size: 78 KiB |
Binary file not shown.
Before Width: | Height: | Size: 72 MiB |
|
@ -1,21 +1,37 @@
|
||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
#If colorama is not installed, notify the user
|
||||||
|
try:
|
||||||
|
from colorama import init, Fore, Style
|
||||||
|
except ImportError:
|
||||||
|
print("Please install the colorama module using 'pip install colorama' to enable colored text.")
|
||||||
|
Fore = Style = lambda x: x
|
||||||
|
|
||||||
current_directory = os.path.dirname(os.path.abspath(__file__)) # Get the current directory
|
debug_mode = True
|
||||||
debug_mode = False
|
|
||||||
source_directory = os.path.join(current_directory, "AVP") # Set the source directory to the 'Projets' directory
|
# Initialize colorama
|
||||||
|
init()
|
||||||
|
|
||||||
# Function to pause if debug mode is enabled
|
# Function to pause if debug mode is enabled
|
||||||
def pause_debug(message="No message provided."):
|
def pause_debug(message="No message provided."):
|
||||||
if debug_mode:
|
if debug_mode:
|
||||||
input(message+"\nPlease press a key to continue.")
|
input(Fore.YELLOW + "[DEBUG] " + message + " - Enter to continue.\n" + Style.RESET_ALL)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
current_directory = os.path.dirname(os.path.abspath(__file__)) # Get the current directory
|
||||||
|
os.chdir(current_directory)
|
||||||
|
pause_debug(f"Current directory: {current_directory}")
|
||||||
|
|
||||||
|
avp_directory = os.path.join(current_directory, "AVP") # Set the source directory to the 'Projets' directory
|
||||||
|
pause_debug(f"AVP directory: {avp_directory}")
|
||||||
|
|
||||||
def createNewProject():
|
def createNewProject():
|
||||||
pause_debug("Checked parent directory. It is named 'Projets'.")
|
|
||||||
|
|
||||||
# Prompt the user for the project name
|
# Prompt the user for the project name
|
||||||
project_name = input("Enter the project name: ")
|
project_name = input("Enter the project name:\n")
|
||||||
pause_debug(f"Project name entered: {project_name}")
|
pause_debug(f"Project name entered: {project_name}")
|
||||||
|
|
||||||
# Get the current year and month
|
# Get the current year and month
|
||||||
|
@ -25,7 +41,13 @@ def createNewProject():
|
||||||
|
|
||||||
# Get all folders starting with the current year and month in the source directory
|
# Get all folders starting with the current year and month in the source directory
|
||||||
pattern = f"{current_year} {current_month}"
|
pattern = f"{current_year} {current_month}"
|
||||||
folders = [f for f in os.listdir(source_directory) if os.path.isdir(os.path.join(source_directory, f)) and f.startswith(pattern)]
|
pause_debug(f"Pattern to search for: {pattern}")
|
||||||
|
|
||||||
|
folders = [f for f in os.listdir(current_directory) if os.path.isdir(os.path.join(current_directory, f)) and f.startswith(pattern)]
|
||||||
|
if not folders:
|
||||||
|
latest_number = 0
|
||||||
|
else:
|
||||||
|
latest_number = max(int(f.split()[2]) for f in folders)
|
||||||
pause_debug(f"Folders matching pattern '{pattern}' found: {', '.join(folders)}")
|
pause_debug(f"Folders matching pattern '{pattern}' found: {', '.join(folders)}")
|
||||||
|
|
||||||
# Find the folder with the largest number after the year and month
|
# Find the folder with the largest number after the year and month
|
||||||
|
@ -41,19 +63,27 @@ def createNewProject():
|
||||||
pause_debug(f"New folder name: {new_folder_name}")
|
pause_debug(f"New folder name: {new_folder_name}")
|
||||||
|
|
||||||
# Create the new folder
|
# Create the new folder
|
||||||
new_folder_path = os.path.join(source_directory, new_folder_name)
|
new_folder_path = os.path.join(current_directory, new_folder_name)
|
||||||
print(f"Creating new folder: {new_folder_name}")
|
print(f"Creating new folder: {new_folder_name}")
|
||||||
os.makedirs(new_folder_path, exist_ok=True)
|
os.makedirs(new_folder_path, exist_ok=True)
|
||||||
pause_debug(f"New folder created: {new_folder_path}")
|
pause_debug(f"New folder created: {new_folder_path}")
|
||||||
|
|
||||||
|
# Copy this file to the new folder
|
||||||
|
source_file = os.path.abspath(__file__)
|
||||||
|
destination_file = os.path.join(new_folder_path, os.path.basename(__file__))
|
||||||
|
with open(source_file, 'rb') as src_file:
|
||||||
|
with open(destination_file, 'wb') as dst_file:
|
||||||
|
dst_file.write(src_file.read())
|
||||||
|
pause_debug(f"File copied to new folder: {destination_file}")
|
||||||
|
|
||||||
def createNewAVP():
|
def createNewAVP():
|
||||||
# Creating AVP folders
|
# Creating AVP folders
|
||||||
folders = [f for f in os.listdir(source_directory) if os.path.isdir(os.path.join(source_directory, f)) and f.startswith("AVP")]
|
folders = [f for f in os.listdir(avp_directory) if os.path.isdir(os.path.join(avp_directory, f)) and f.startswith("AVP")]
|
||||||
pause_debug(f"Folders found: {', '.join(folders)}")
|
pause_debug(f"Folders found: {', '.join(folders)}")
|
||||||
|
|
||||||
if not folders:
|
if not folders:
|
||||||
print("No folders found. Creating first folder: AVP01")
|
print("No folders found. Creating first folder: AVP01")
|
||||||
os.makedirs(os.path.join(source_directory, "AVP01"), exist_ok=True)
|
os.makedirs(os.path.join(avp_directory, "AVP01"), exist_ok=True)
|
||||||
pause_debug("First folder created: AVP01")
|
pause_debug("First folder created: AVP01")
|
||||||
else:
|
else:
|
||||||
pause_debug("Finding the latest folder")
|
pause_debug("Finding the latest folder")
|
||||||
|
@ -65,15 +95,15 @@ def createNewAVP():
|
||||||
input(f"\nWould you like to create {new_folder_name}? Press any key to continue or CTRL+C to exit...")
|
input(f"\nWould you like to create {new_folder_name}? Press any key to continue or CTRL+C to exit...")
|
||||||
pause_debug(f"User confirmed creation of new folder: {new_folder_name}")
|
pause_debug(f"User confirmed creation of new folder: {new_folder_name}")
|
||||||
|
|
||||||
new_folder_path = os.path.join(source_directory, new_folder_name)
|
new_folder_path = os.path.join(avp_directory, new_folder_name)
|
||||||
print(f"Creating {new_folder_name}")
|
print(f"Creating {new_folder_name}")
|
||||||
os.makedirs(new_folder_path, exist_ok=True)
|
os.makedirs(new_folder_path, exist_ok=True)
|
||||||
pause_debug(f"New folder created: {new_folder_path}")
|
pause_debug(f"New folder created: {new_folder_path}")
|
||||||
|
|
||||||
print(f"Copying files from {source_directory} to {new_folder_name} :")
|
print(f"Copying files from {avp_directory} to {new_folder_name} :")
|
||||||
|
|
||||||
# Copy files from the last folder to the new folder
|
# Copy files from the last folder to the new folder
|
||||||
last_folder_path = os.path.join(source_directory, f"AVP{str(latest_number).zfill(2)}")
|
last_folder_path = os.path.join(avp_directory, f"AVP{str(latest_number).zfill(2)}")
|
||||||
for filename in os.listdir(last_folder_path):
|
for filename in os.listdir(last_folder_path):
|
||||||
source_file = os.path.join(last_folder_path, filename)
|
source_file = os.path.join(last_folder_path, filename)
|
||||||
if os.path.isfile(source_file):
|
if os.path.isfile(source_file):
|
||||||
|
@ -128,13 +158,14 @@ def main():
|
||||||
displayWelcomeMessage()
|
displayWelcomeMessage()
|
||||||
|
|
||||||
# Check if the current directory is a parent directory named "Projets", and if so, ask the user if they want to initiate a new project
|
# Check if the current directory is a parent directory named "Projets", and if so, ask the user if they want to initiate a new project
|
||||||
if os.path.basename(os.path.dirname(source_directory)) == "Projets":
|
if os.path.basename(os.path.dirname(avp_directory)) == "Projets":
|
||||||
choice = input("You are in a parent directory named \"Projets\". Would you like to initiate a new project ? (y/n)").strip().upper()
|
pause_debug("Checked parent directory. It is named 'Projets'.")
|
||||||
|
choice = input("You are in a parent directory named \"Projets\". Would you like to initiate a new project ? (y/n)\n").strip().upper()
|
||||||
if choice == 'Y':
|
if choice == 'Y':
|
||||||
|
|
||||||
createNewProject()
|
createNewProject()
|
||||||
|
|
||||||
# Check if there is an AVP folder in the current directory, and if so, ask the user if they want to make a new AVP
|
# Check if there is an AVP folder in the current directory, and if so, ask the user if they want to make a new AVP
|
||||||
|
|
||||||
elif "AVP" in os.listdir(current_directory):
|
elif "AVP" in os.listdir(current_directory):
|
||||||
choice = input("You seem to be in a project directory. Would you like to make a new AVP ? (y/n)\n").strip().upper()
|
choice = input("You seem to be in a project directory. Would you like to make a new AVP ? (y/n)\n").strip().upper()
|
||||||
if choice == 'Y':
|
if choice == 'Y':
|
||||||
|
@ -144,6 +175,7 @@ def main():
|
||||||
print("invalid choice. Please try again.")
|
print("invalid choice. Please try again.")
|
||||||
|
|
||||||
# If the user is not in a parent directory named "Projets" or an AVP directory, display the menu
|
# If the user is not in a parent directory named "Projets" or an AVP directory, display the menu
|
||||||
|
|
||||||
else:
|
else:
|
||||||
while True:
|
while True:
|
||||||
display_menu()
|
display_menu()
|
|
@ -1,71 +0,0 @@
|
||||||
|
|
||||||
# Define the source directory
|
|
||||||
$sourceDirectory = ".\AVP"
|
|
||||||
|
|
||||||
# Check if the parent directory is named "Projets"
|
|
||||||
$parentDirectory = Split-Path -Path $sourceDirectory -Parent
|
|
||||||
if ((Split-Path -Path $parentDirectory -Leaf) -eq "Projets") {
|
|
||||||
# Prompt the user for the project name
|
|
||||||
$projectName = Read-Host "Enter the project name"
|
|
||||||
|
|
||||||
# Get the current year and month
|
|
||||||
$currentYear = (Get-Date).Year
|
|
||||||
$currentMonth = (Get-Date).Month.ToString("D2")
|
|
||||||
|
|
||||||
# Get all folders starting with the current year and month in the source directory
|
|
||||||
$pattern = "$currentYear $currentMonth*"
|
|
||||||
$folders = Get-ChildItem -Path $sourceDirectory -Directory | Where-Object { $_.Name -like $pattern }
|
|
||||||
|
|
||||||
# Find the folder with the largest number after the year and month
|
|
||||||
if ($folders.Count -eq 0) {
|
|
||||||
$newNumber = 1
|
|
||||||
} else {
|
|
||||||
$latestFolder = $folders | Sort-Object -Property { [int]($_.Name -replace "[^\d]") } | Select-Object -Last 1
|
|
||||||
$latestNumber = [int]($latestFolder.Name -replace "[^\d]")
|
|
||||||
$newNumber = $latestNumber + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Create the new folder name
|
|
||||||
$newFolderName = "$currentYear $currentMonth $newNumber $projectName"
|
|
||||||
|
|
||||||
# Create the new folder
|
|
||||||
$newFolderPath = Join-Path -Path $sourceDirectory -ChildPath $newFolderName
|
|
||||||
Write-Host "Creating new folder: $newFolderName"
|
|
||||||
New-Item -ItemType Directory -Path $newFolderPath -ErrorAction SilentlyContinue
|
|
||||||
|
|
||||||
# Copy the current script to the new folder
|
|
||||||
$currentScriptPath = $MyInvocation.MyCommand.Path
|
|
||||||
Copy-Item -Path $currentScriptPath -Destination $newFolderPath -ErrorAction SilentlyContinue
|
|
||||||
|
|
||||||
# Create "AVP" and "Donnees" folders inside the new folder
|
|
||||||
New-Item -ItemType Directory -Path (Join-Path -Path $newFolderPath -ChildPath "AVP") -ErrorAction SilentlyContinue
|
|
||||||
New-Item -ItemType Directory -Path (Join-Path -Path $newFolderPath -ChildPath "Donnees") -ErrorAction SilentlyContinue
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
|
||||||
# Existing logic for creating AVP folders
|
|
||||||
Write-Host "Getting folders in $sourceDirectory"
|
|
||||||
$folders = Get-ChildItem -Path $sourceDirectory -Directory | Where-Object { $_.Name -like "AVP*" }
|
|
||||||
|
|
||||||
if ($folders.Count -eq 0) {
|
|
||||||
Write-Host "No folders found. Creating first folder: AVP01"
|
|
||||||
New-Item -ItemType Directory -Path (Join-Path -Path $sourceDirectory -ChildPath "AVP01") -ErrorAction SilentlyContinue
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
Write-Host "Finding the latest folder"
|
|
||||||
$latestFolder = $folders | Sort-Object -Property { [int]($_.Name -replace "[^\d]") } | Select-Object -Last 1
|
|
||||||
|
|
||||||
$latestNumber = [int]($latestFolder.Name -replace "[^\d]")
|
|
||||||
$newNumber = $latestNumber + 1
|
|
||||||
$newFolderName = "AVP" + $newNumber.ToString().PadLeft(2, '0')
|
|
||||||
|
|
||||||
Write-Host "`nFolders found:"
|
|
||||||
$folders | ForEach-Object { Write-Host $_.Name }
|
|
||||||
Write-Host "`nWould you like to create the next AVP "$newFolderName" ? Press any key to continue or CTRL+C to exit..."
|
|
||||||
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
|
|
||||||
|
|
||||||
$newFolderPath = Join-Path -Path $sourceDirectory -ChildPath $newFolderName
|
|
||||||
Write-Host "Creating new folder: $newFolderName"
|
|
||||||
New-Item -ItemType Directory -Path $newFolderPath -ErrorAction SilentlyContinue
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue