diff --git a/basharin_sevastyan_lab_2/README.md b/basharin_sevastyan_lab_2/README.md index e69de29..a85382d 100644 --- a/basharin_sevastyan_lab_2/README.md +++ b/basharin_sevastyan_lab_2/README.md @@ -0,0 +1,70 @@ +## Лабораторная работа 1. Вариант 5. + +#### Вариант первой программы: +Ищет в каталоге `/var/data` файл с самым коротким названием и перекладывает его в `/var/result/data.txt`. +#### Вариант второй программы: +Сохраняет произведение первого и последнего числа из файла `/var/data/data.txt` в `/var/result/result.txt`. +### Как запустить: +В директории с файлом `docker-compose` в консоли/терминале ввести команду `docker-compose up` + +#### Файл сборки docker-compose.yaml +```dockerfile +version: "3" +services: + worker-1: + build: + context: /app1 + dockerfile: Dockerfile + + worker-2: + build: + context: /app2 + dockerfile: Dockerfile +``` + +#### Dockerfile каждой программы +```dockerfile +FROM python:3.11 +COPY . . +COPY var/data /var/data +COPY var/result /var/result +CMD ["python", "main.py"] +``` + +#### Код первой программы +```python +import os + + +def main(): + data_dir = 'var/data' + result_file = '/var/result/data.txt' + min_name = min(os.listdir(data_dir)) # Ищем и записываем самое короткое название (включая расширение) + with open(result_file, 'w') as result: + result.write(min_name) + print(f'В файл {result_file} было записано {min_name}') + + +if __name__ == "__main__": + main() +``` + +#### Код второй программы +```python +def main(): + data_path = 'var/data/data.txt' + result_path = 'var/result/result.txt' + data = [] + with open(data_path, "r") as data_file: + data = list(map(lambda x: int(x), data_file.read().split())) + with open(result_path, 'w') as result: + result.write(f'{data[0] * data[-1]}') + print(f'В файл {result_path} было записано {data[0] * data[-1]}') + + +if __name__ == "__main__": + main() +``` + +### Видео +https://youtu.be/D7HKUqotLGA \ No newline at end of file diff --git a/basharin_sevastyan_lab_2/app1/Dockerfile b/basharin_sevastyan_lab_2/app1/Dockerfile index ca02ee7..3adf2cd 100644 --- a/basharin_sevastyan_lab_2/app1/Dockerfile +++ b/basharin_sevastyan_lab_2/app1/Dockerfile @@ -2,4 +2,4 @@ FROM python:3.11 COPY . . COPY var/data /var/data COPY var/result /var/result -CMD ["python", "app.py"] \ No newline at end of file +CMD ["python", "main.py"] \ No newline at end of file diff --git a/basharin_sevastyan_lab_2/app1/main.py b/basharin_sevastyan_lab_2/app1/main.py index cd9ac48..56613c1 100644 --- a/basharin_sevastyan_lab_2/app1/main.py +++ b/basharin_sevastyan_lab_2/app1/main.py @@ -1,5 +1,13 @@ +import os + + def main(): - pass + data_dir = 'var/data' + result_file = '/var/result/data.txt' + min_name = min(os.listdir(data_dir)) # Ищем и записываем самое короткое название (включая расширение) + with open(result_file, 'w') as result: + result.write(min_name) + print(f'В файл {result_file} было записано {min_name}') if __name__ == "__main__": diff --git a/basharin_sevastyan_lab_2/app1/var/data/max_data.txt b/basharin_sevastyan_lab_2/app1/var/data/max_data.txt new file mode 100644 index 0000000..e69de29 diff --git a/basharin_sevastyan_lab_2/app2/Dockerfile b/basharin_sevastyan_lab_2/app2/Dockerfile index ca02ee7..3adf2cd 100644 --- a/basharin_sevastyan_lab_2/app2/Dockerfile +++ b/basharin_sevastyan_lab_2/app2/Dockerfile @@ -2,4 +2,4 @@ FROM python:3.11 COPY . . COPY var/data /var/data COPY var/result /var/result -CMD ["python", "app.py"] \ No newline at end of file +CMD ["python", "main.py"] \ No newline at end of file diff --git a/basharin_sevastyan_lab_2/app2/main.py b/basharin_sevastyan_lab_2/app2/main.py index cd9ac48..27e5242 100644 --- a/basharin_sevastyan_lab_2/app2/main.py +++ b/basharin_sevastyan_lab_2/app2/main.py @@ -1,5 +1,12 @@ def main(): - pass + data_path = 'var/data/data.txt' + result_path = 'var/result/result.txt' + data = [] + with open(data_path, "r") as data_file: + data = list(map(lambda x: int(x), data_file.read().split())) + with open(result_path, 'w') as result: + result.write(f'{data[0] * data[-1]}') + print(f'В файл {result_path} было записано {data[0] * data[-1]}') if __name__ == "__main__": diff --git a/basharin_sevastyan_lab_2/app2/var/data/data.txt b/basharin_sevastyan_lab_2/app2/var/data/data.txt index e69de29..96c54d7 100644 --- a/basharin_sevastyan_lab_2/app2/var/data/data.txt +++ b/basharin_sevastyan_lab_2/app2/var/data/data.txt @@ -0,0 +1,10 @@ +30 +36 +20 +37 +39 +68 +22 +16 +5 +21 \ No newline at end of file diff --git a/basharin_sevastyan_lab_2/docker-compose.yaml b/basharin_sevastyan_lab_2/docker-compose.yaml index 4de2fdc..315ded8 100644 --- a/basharin_sevastyan_lab_2/docker-compose.yaml +++ b/basharin_sevastyan_lab_2/docker-compose.yaml @@ -4,16 +4,8 @@ services: build: context: /app1 dockerfile: Dockerfile - volumes: - - ./data:/var/data - - ./result:/var/result worker-2: - depends_on: - - worker-1 build: context: /app2 - dockerfile: Dockerfile - volumes: - - ./result:/var/data - - ./result:/var/result \ No newline at end of file + dockerfile: Dockerfile \ No newline at end of file