remove files

This commit is contained in:
Cayo Puigdefabregas 2023-07-24 12:33:04 +02:00
parent 1b4159d58b
commit 4f2cfe5c47
1 changed files with 28 additions and 8 deletions

View File

@ -4,9 +4,11 @@ import json
# from uuid import uuid4 # from uuid import uuid4
from io import BytesIO from io import BytesIO
from os import listdir
from os import remove as remove_file
from os.path import isfile, join
from pathlib import Path from pathlib import Path
import click
from decouple import config from decouple import config
from flask.testing import FlaskClient from flask.testing import FlaskClient
from flask_wtf.csrf import generate_csrf from flask_wtf.csrf import generate_csrf
@ -30,17 +32,19 @@ class UploadSnapshots:
self.schema = app.config.get('DB_SCHEMA') self.schema = app.config.get('DB_SCHEMA')
self.app.cli.command('snapshot', short_help='Upload snapshots.')(self.run) self.app.cli.command('snapshot', short_help='Upload snapshots.')(self.run)
@click.argument('file_snapshot') def run(self):
def run(self, file_snapshot):
"""Run command.""" """Run command."""
self.file_snapshot = file_snapshot
self.snapshot_json = None
self.json_wb = None self.json_wb = None
self.onlyfiles = []
with self.app.app_context(): with self.app.app_context():
self.get_user() self.get_user()
self.get_files()
for f in self.onlyfiles:
self.file_snapshot = f
self.open_snapshot() self.open_snapshot()
self.build_snapshot() self.build_snapshot()
self.remove_files()
def get_user(self): def get_user(self):
"""Get datamodel of user.""" """Get datamodel of user."""
@ -58,6 +62,11 @@ class UploadSnapshots:
} }
self.client.post('/login/', data=data, follow_redirects=True) self.client.post('/login/', data=data, follow_redirects=True)
def remove_files(self):
"""Open snapshot file."""
for f in self.onlyfiles:
remove_file(Path(__file__).parent.joinpath('snapshot_files').joinpath(f))
def open_snapshot(self): def open_snapshot(self):
"""Open snapshot file.""" """Open snapshot file."""
with Path(__file__).parent.joinpath('snapshot_files').joinpath( with Path(__file__).parent.joinpath('snapshot_files').joinpath(
@ -71,7 +80,7 @@ class UploadSnapshots:
"""Build the devices of snapshot.""" """Build the devices of snapshot."""
uri = '/inventory/upload-snapshot/' uri = '/inventory/upload-snapshot/'
if not self.snapshot_json: if not self.json_wb:
return return
self.client.get(uri) self.client.get(uri)
@ -79,4 +88,15 @@ class UploadSnapshots:
'snapshot': self.file_snap, 'snapshot': self.file_snap,
'csrf_token': generate_csrf(), 'csrf_token': generate_csrf(),
} }
self.client.post(uri, data=data, content_type="multipart/form-data") self.client.post(uri, data=data, content_type="multipart/form-data")
def get_files(self):
"""Read snaoshot_files dir."""
mypath = Path(__file__).parent.joinpath('snapshot_files')
for f in listdir(mypath):
if not isfile(join(mypath, f)):
continue
if not f[-5:] == ".json":
continue
self.onlyfiles.append(f)