Commit d7c01d3dac0148eebc93739a47f33bb87fcc7243

Authored by Василь Свідрик
1 parent 4cd28255f4
Exists in master

add install from reguirments.txt

Showing 6 changed files with 20 additions and 15 deletions Side-by-side Diff

__pycache__/main.cpython-310.pyc
No preview for this file type
__pycache__/routes.cpython-310.pyc
No preview for this file type
No preview for this file type
k2/__pycache__/root.cpython-310.pyc
No preview for this file type
... ... @@ -5,14 +5,12 @@
5 5 from flask_login import current_user
6 6 from flask_bcrypt import bcrypt
7 7 from flask_login import login_user, current_user, logout_user, login_required
8   -import requests
9 8 from datetime import datetime
10 9 from flask_login import UserMixin
11 10 import uuid
12 11 from flask_wtf import FlaskForm
13 12 from wtforms import StringField, PasswordField, SubmitField
14 13 from wtforms.validators import DataRequired, Length
15   -import subprocess
16 14 from flask import request
17 15 from db.database import db
18 16 from flask_bcrypt import Bcrypt
... ... @@ -20,6 +18,10 @@
20 18 from flask_migrate import Migrate
21 19 from flask import current_app as k2
22 20 from main import app
  21 +import requests
  22 +import subprocess
  23 +from flask import current_app
  24 +
23 25 import string
24 26  
25 27 with app.app_context():
... ... @@ -262,13 +264,6 @@
262 264 return 'Error installing : ' + str(e)
263 265  
264 266  
265   - import os
266   - import requests
267   - import shutil
268   - import subprocess
269   - from flask import current_app
270   -
271   -
272 267 @components_bp.route('/install_components/<string:component_id>')
273 268 @login_required
274 269 def install_component_from_archive(component_id):
... ... @@ -292,6 +287,8 @@
292 287 response.raise_for_status()
293 288 # Шлях до завантаженого архіву
294 289 archive_path = os.path.join(component_folder, f"{selected_component['name']}.zip")
  290 + # Встановити залежності з файлу requirements.txt
  291 +
295 292 # Зберегти архів на диск
296 293 with open(archive_path, "wb") as file:
297 294 for chunk in response.iter_content(chunk_size=8192):
298 295  
... ... @@ -299,13 +296,10 @@
299 296 # Розпакувати архів
300 297 with zipfile.ZipFile(archive_path, "r") as zip_ref:
301 298 zip_ref.extractall(component_folder)
  299 +
302 300 # Видалити архів
303 301 os.remove(archive_path)
304   - # Встановити залежності з файлу requirements.txt
305   - requirements_file = os.path.join(component_folder, "requirements.txt")
306   - if os.path.isfile(requirements_file):
307   - pip_command = f"{current_app.config['VENV_BIN_PATH']}/python -m pip install -r {requirements_file}"
308   - subprocess.run(pip_command, shell=True, check=True)
  302 +
309 303 # Шлях до файлу, до якого потрібно додати код
310 304 file_path = 'routes.py'
311 305 # Код, який потрібно додати
... ... @@ -319,6 +313,7 @@
319 313 file.write(code)
320 314 file.write('\n')
321 315  
  316 +
322 317 # Оновити базу даних з встановленою компонентою
323 318 component = Component.query.filter_by(name=selected_component['name']).first()
324 319 if not component:
325 320  
... ... @@ -339,10 +334,19 @@
339 334  
340 335 db.session.commit()
341 336  
342   - return f'Component installed successfully: {selected_component["name"]} v{version} <meta http-equiv="refresh" content="1;url=/dashboard" />'
  337 + return f'Component installed successfully: {selected_component["name"]} v{version}, please wait installing requirments... <meta http-equiv="refresh" content="0;url=/install-requirments/{selected_component["name"]}" />'
343 338  
344 339 except Exception as e:
345 340 return f'Error installing component: {str(e)}'
  341 +
  342 + @components_bp.route('/install-requirments/<string:selected_component_name>', methods=['GET'])
  343 + @login_required
  344 + def install_requirments(selected_component_name):
  345 + requirements_file = os.path.join('components', selected_component_name, "requirements.txt")
  346 + if os.path.isfile(requirements_file):
  347 + pip_command = f"{current_app.config['VENV_BIN_PATH']}/python -m pip install -r {requirements_file}"
  348 + subprocess.run(pip_command, shell=True, check=True)
  349 + return f'Requirements installed successfully: <meta http-equiv="refresh" content="1;url=/dashboard" />'
346 350  
347 351  
348 352 @components_bp.route('/remove_dependencies/<string:component_id>', methods=['GET'])
... ... @@ -10,6 +10,7 @@
10 10 app.config['TIMEZONE'] = pytz.timezone('Europe/Kiev')
11 11 app.config['SECRET_KEY'] = 'JH&INH987gFDHdsagh&8dwbjdw8ckw'
12 12 app.config['DOMAIN'] = 'http://127.0.0.1:5001/'
  13 +app.config['VENV_BIN_PATH'] = 'C:/Users/sv-work/PycharmProjects/k2root/venv/Scripts'
13 14 # setting flask app
14 15 init_db(app)
15 16 CORS(app)