This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive_contents.py
47 lines (38 loc) · 1.77 KB
/
archive_contents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import shutil
import logging
from datetime import datetime
from pathlib import Path
def archive_contents(shopping_list_file: str, recipes: list[str]):
"""
Save shopping list to yyyy/yyyy-mm-dd-recipes[0]-...-recipes[n]/yyyy-mm-dd-recipes[0]-...-recipes[n].txt.
Create hard links of the used recipes next to it, to have all resources close at hand.
"""
# TODO: Remove following line <31-01-2024>
print()
# Get the current date in the format 'yyyy-mm-dd'
current_date = datetime.now().strftime('%Y-%m-%d')
# Create the directory for the current year
current_year = datetime.now().strftime('%Y')
# year_directory = os.path.join(os.getcwd(), current_year)
year_dir = current_year
os.makedirs(year_dir, exist_ok=True)
# Create the subdirectory with the specified scheme
recipe_names = [Path(recipe).stem for recipe in recipes]
subdir_name = f'{current_date}-{"-".join(recipe_names)}'
subdir_path = os.path.join(year_dir, subdir_name)
os.makedirs(subdir_path, exist_ok=True)
# Copy shopping list into archive folder
shopping_list_dst = os.path.join(subdir_path, f'{subdir_name}.txt')
shutil.copy(shopping_list_file, shopping_list_dst)
logging.info(f"File '{shopping_list_file}' copied to '{shopping_list_dst}' successfully.")
# Generate hard links for each specified file
for recipe in recipes:
dst = os.path.join(subdir_path, os.path.basename(recipe)) # Only filename & extension
os.link(recipe, dst)
logging.info(f"Hard link created for {recipe} at {dst}")
# TODO: Remove following line <31-01-2024>
print()
if __name__ == "__main__":
lines = ["Line 1", "Line 2", "Line 3"]
archive_contents(lines, "recipes/Pesto_alla_Trapanese.yaml", "recipes/Rührei.yaml")