0

I have an app that deletes files that have been uploaded more than one month ago.

from django.core.management.base import BaseCommand, CommandError
from blacklist_app.models import EntradaHistorial
from datetime import datetime, timedelta
import pytz, subprocess, os
from blacklist.settings import BASE_DIR

class Command(BaseCommand):
    help = "Procesa archivos en cola para ser movidos al servidor de destino acorde a la configuración"
    tmp   = os.path.join(BASE_DIR, 'blacklist_app/tmp')
    excel = os.path.join(BASE_DIR, 'blacklist_app/uploaded_files/excel')
    json  = os.path.join(BASE_DIR, 'blacklist_app/uploaded_files/json')
    mv = 'mv {}/{} {}'
    rm = 'rm {}/{}'

    def handle(self, *args, **options):
        max_date = datetime.now() + timedelta(weeks=-1)
        preg = "*_{}*".format(max_date.strftime('%Y%m'))
        #Se mueven los archivos que deben ser conservados a la carpeta temporal
        self.mv_and_rm(self.excel, preg)
        self.mv_and_rm(self.json, preg)
        max_date.replace(day=1, hour=0, minute=0, tzinfo=pytz.UTC)
        #Actualiza los valores de los archivos borrados
        EntradaHistorial.objects.filter(fecha__lt=max_date, archivo_borrado=False).update(archivo_borrado=True)

    # Mueve los archivos a la carpeta temporal, borra los archivos que deben ser borrados y 
    # mueve de vuelta los archivos que deben ser conservados
    def mv_and_rm(self, dir, preg):
        move = self.mv.format(dir, preg, self.tmp)
        self.run_command(move)
        rm  = self.rm.format(dir, '*')
        self.run_command(rm)
        move_back = self.mv.format(self.tmp, preg, dir)
        self.run_command(move_back)

    def run_command(self, command):
        sh = os.path.join(BASE_DIR,'blacklist_app/management/commands/run_command.sh')
        call = "sh {} '{}'".format(sh, command)
        print(subprocess.check_output(call))

In the same directory I have the script run_command.sh

#!/bin/sh

$1

This command should run in the crontab on the first day of each month, that's why I subtract one week of the current date. Now I am testing it running it manually

The error I am getting is

FileNotFoundError: [Errno 2] No such file or directory: 
"/code/blacklist_app/management/commands/run_command.sh 'mv 
/code/blacklist_app/uploaded_files/excel/*_202003* /code/blacklist_app/tmp'"
3
  • I seem you are moving something that doesn't exist to being moved to a directory that does exist. try copying the path and put it in file explorer, and see if truly there exist such file or directory Commented Apr 7, 2020 at 15:36
  • It does, the directories exist with content in them Commented Apr 7, 2020 at 16:23
  • Okay this seems to be relative path issue. Is the stuff you moving inside the django root. If so try creating os.join path Commented Apr 7, 2020 at 21:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.