13ze commited on
Commit
1618dd5
·
verified ·
1 Parent(s): 5589c67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -1,30 +1,36 @@
1
  import gradio as gr
2
- import subprocess
3
  import os
4
 
5
  def execute_wp_cli(command, args):
6
  """
7
- Função para executar comandos WP-CLI com argumentos.
8
  """
9
  try:
10
  # Recupera as variáveis de ambiente seguras
11
- wp_path = os.getenv("WP_PATH")
12
  wp_user = os.getenv("WP_USER")
13
- wp_password = os.getenv("WP_PASSWORD")
14
 
15
- if not wp_path or not wp_user or not wp_password:
16
- return "Erro: Variáveis de ambiente WP_PATH, WP_USER ou WP_PASSWORD não definidas."
17
 
18
- # Configura o comando WP-CLI completo
19
- full_command = ['wp', '--path=' + wp_path, 'cli', command] + args.split()
20
 
21
- # Executa o comando WP-CLI
22
- result = subprocess.run(full_command, input=wp_password + "\n", capture_output=True, text=True)
 
 
 
 
 
 
 
 
 
23
 
24
- # Verifica se houve erro na execução
25
- if result.returncode != 0:
26
- return f"Erro ao executar o comando: {result.stderr}"
27
- return result.stdout
28
  except Exception as e:
29
  return f"Erro: {str(e)}"
30
 
@@ -54,4 +60,4 @@ iface = gr.Interface(
54
  )
55
 
56
  # Inicia a interface
57
- iface.launch()
 
1
  import gradio as gr
2
+ import requests
3
  import os
4
 
5
  def execute_wp_cli(command, args):
6
  """
7
+ Função para executar comandos WP-CLI via API REST.
8
  """
9
  try:
10
  # Recupera as variáveis de ambiente seguras
11
+ wp_app_password = os.getenv("WP_APP_PASSWORD")
12
  wp_user = os.getenv("WP_USER")
13
+ wp_url = os.getenv("WP_URL")
14
 
15
+ if not wp_app_password or not wp_user or not wp_url:
16
+ return "Erro: Variáveis de ambiente WP_APP_PASSWORD, WP_USER ou WP_URL não definidas."
17
 
18
+ # URL da API REST
19
+ api_url = f"{wp_url}/wp-json/wp/v2"
20
 
21
+ # Configura a autenticação
22
+ auth = (wp_user, wp_app_password)
23
+
24
+ # Monta a URL do comando WP-CLI
25
+ command_url = f"{api_url}/{command.replace(' ', '/')}"
26
+
27
+ # Executa o comando WP-CLI via API REST
28
+ response = requests.post(command_url, auth=auth, json={"args": args})
29
+
30
+ if response.status_code != 200:
31
+ return f"Erro ao executar o comando: {response.text}"
32
 
33
+ return response.json()
 
 
 
34
  except Exception as e:
35
  return f"Erro: {str(e)}"
36
 
 
60
  )
61
 
62
  # Inicia a interface
63
+ iface.launch()