optimized folder cleaning

This commit is contained in:
Félix Boisselier
2024-04-14 21:07:43 +02:00
parent 656f6d0d9e
commit 915e69d420
7 changed files with 64 additions and 65 deletions

View File

@@ -37,7 +37,7 @@ gcode:
RESPOND MSG="X axis frequency profile generation..." RESPOND MSG="X axis frequency profile generation..."
RESPOND MSG="This may take some time (1-3min)" RESPOND MSG="This may take some time (1-3min)"
RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type shaper --scv {scv} {% if max_sm is not none %}--max_smoothing {max_sm}{% endif %} {% if keep_csv %}--keep_csv{% endif %}" RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type shaper --scv {scv} {% if max_sm is not none %}--max_smoothing {max_sm}{% endif %} {% if keep_csv %}--keep_csv{% endif %} --keep_results {keep_results}"
{% endif %} {% endif %}
{% if Y %} {% if Y %}
@@ -46,8 +46,5 @@ gcode:
RESPOND MSG="Y axis frequency profile generation..." RESPOND MSG="Y axis frequency profile generation..."
RESPOND MSG="This may take some time (1-3min)" RESPOND MSG="This may take some time (1-3min)"
RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type shaper --scv {scv} {% if max_sm is not none %}--max_smoothing {max_sm}{% endif %} {% if keep_csv %}--keep_csv{% endif %}" RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type shaper --scv {scv} {% if max_sm is not none %}--max_smoothing {max_sm}{% endif %} {% if keep_csv %}--keep_csv{% endif %} --keep_results {keep_results}"
{% endif %} {% endif %}
M400
RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type clean --keep_results {keep_results}"

View File

@@ -20,6 +20,4 @@ gcode:
RESPOND MSG="Belts comparative frequency profile generation..." RESPOND MSG="Belts comparative frequency profile generation..."
RESPOND MSG="This may take some time (3-5min)" RESPOND MSG="This may take some time (3-5min)"
RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type belts {% if keep_csv %}--keep_csv{% endif %}" RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type belts {% if keep_csv %}--keep_csv{% endif %} --keep_results {keep_results}"
M400
RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type clean --keep_results {keep_results}"

View File

@@ -159,9 +159,7 @@ gcode:
RESPOND MSG="Machine vibrations profile generation..." RESPOND MSG="Machine vibrations profile generation..."
RESPOND MSG="This may take some time (3-5min)" RESPOND MSG="This may take some time (3-5min)"
RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type vibrations --accel {accel|int} --kinematics {kinematics} --chip_name {accel_chip} {% if keep_csv %}--keep_csv{% endif %}" RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type vibrations --accel {accel|int} --kinematics {kinematics} --chip_name {accel_chip} {% if keep_csv %}--keep_csv{% endif %} --keep_results {keep_results}"
M400
RUN_SHELL_COMMAND CMD=shaketune PARAMS="--type clean --keep_results {keep_results}"
# Restore the previous acceleration values # Restore the previous acceleration values
SET_VELOCITY_LIMIT ACCEL={old_accel} MINIMUM_CRUISE_RATIO={old_cruise_ratio} SQUARE_CORNER_VELOCITY={old_sqv} SET_VELOCITY_LIMIT ACCEL={old_accel} MINIMUM_CRUISE_RATIO={old_cruise_ratio} SQUARE_CORNER_VELOCITY={old_sqv}

View File

@@ -529,7 +529,7 @@ def belts_calibration(lognames, klipperdir='~/klipper', max_freq=200.0, st_versi
ax_logo.axis('off') ax_logo.axis('off')
# Adding Shake&Tune version in the top right corner # Adding Shake&Tune version in the top right corner
if st_version is not None: if st_version != 'unknown':
fig.text(0.995, 0.985, st_version, ha='right', va='bottom', fontsize=8, color=KLIPPAIN_COLORS['purple']) fig.text(0.995, 0.985, st_version, ha='right', va='bottom', fontsize=8, color=KLIPPAIN_COLORS['purple'])
return fig return fig

View File

@@ -386,7 +386,7 @@ def shaper_calibration(lognames, klipperdir='~/klipper', max_smoothing=None, scv
ax_logo.axis('off') ax_logo.axis('off')
# Adding Shake&Tune version in the top right corner # Adding Shake&Tune version in the top right corner
if st_version is not None: if st_version != 'unknown':
fig.text(0.995, 0.985, st_version, ha='right', va='bottom', fontsize=8, color=KLIPPAIN_COLORS['purple']) fig.text(0.995, 0.985, st_version, ha='right', va='bottom', fontsize=8, color=KLIPPAIN_COLORS['purple'])
return fig return fig

View File

@@ -709,7 +709,7 @@ def vibrations_profile(
ax_logo.axis('off') ax_logo.axis('off')
# Adding Shake&Tune version in the top right corner # Adding Shake&Tune version in the top right corner
if st_version is not None: if st_version != 'unknown':
fig.text(0.995, 0.985, st_version, ha='right', va='bottom', fontsize=8, color=KLIPPAIN_COLORS['purple']) fig.text(0.995, 0.985, st_version, ha='right', va='bottom', fontsize=8, color=KLIPPAIN_COLORS['purple'])
return fig return fig

View File

@@ -20,9 +20,8 @@ import time
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from git import GitCommandError, Repo
from analyze_axesmap import axesmap_calibration from analyze_axesmap import axesmap_calibration
from git import GitCommandError, Repo
from graph_belts import belts_calibration from graph_belts import belts_calibration
from graph_shaper import shaper_calibration from graph_shaper import shaper_calibration
from graph_vibrations import vibrations_profile from graph_vibrations import vibrations_profile
@@ -52,7 +51,7 @@ class Config:
version = repo.head.commit.hexsha[:7] # If no tag is found, use the simplified commit SHA instead version = repo.head.commit.hexsha[:7] # If no tag is found, use the simplified commit SHA instead
return version return version
except Exception: except Exception:
return None return 'unknown'
@staticmethod @staticmethod
def parse_arguments(): def parse_arguments():
@@ -61,7 +60,7 @@ class Config:
'-t', '-t',
'--type', '--type',
dest='type', dest='type',
choices=['belts', 'shaper', 'vibrations', 'axesmap', 'clean'], choices=['belts', 'shaper', 'vibrations', 'axesmap'],
required=True, required=True,
help='Type of output graph to produce', help='Type of output graph to produce',
) )
@@ -155,42 +154,6 @@ class FileManager:
folder = os.path.join(Config.RESULTS_BASE_FOLDER, subfolder) folder = os.path.join(Config.RESULTS_BASE_FOLDER, subfolder)
os.makedirs(folder, exist_ok=True) os.makedirs(folder, exist_ok=True)
@staticmethod
def clean_old_files(type, keep_results=3, extension='.png'):
folder = Config.get_results_folder(type)
files = [os.path.join(folder, f) for f in os.listdir(folder) if f.endswith(extension)]
files.sort(key=os.path.getmtime, reverse=True)
if 'belts' in folder:
if len(files) <= keep_results + 1:
return
else: # delete the older files
for old_file in files[keep_results + 1 :]:
file_date = '_'.join(os.path.splitext(os.path.basename(old_file))[0].split('_')[1:3])
for suffix in ['A', 'B']:
csv_file = os.path.join(folder, f'belt_{file_date}_{suffix}.csv')
if os.path.exists(csv_file):
os.remove(csv_file)
os.remove(old_file)
elif 'shaper' in folder:
if len(files) <= 2 * keep_results + 1:
return
else: # delete the older files
for old_file in files[2 * keep_results + 1 :]:
csv_file = os.path.join(folder, os.path.splitext(os.path.basename(old_file))[0] + '.csv')
if os.path.exists(csv_file):
os.remove(csv_file)
os.remove(old_file)
elif 'vibrations' in folder:
if len(files) <= keep_results + 1:
return
else: # delete the older files
for old_file in files[keep_results + 1 :]:
os.remove(old_file)
tar_file = os.path.join(folder, os.path.splitext(os.path.basename(old_file))[0] + '.tar.gz')
if os.path.exists(tar_file):
os.remove(tar_file)
class GraphCreator(abc.ABC): class GraphCreator(abc.ABC):
def __init__(self, keep_csv, dpi): def __init__(self, keep_csv, dpi):
@@ -250,6 +213,10 @@ class GraphCreator(abc.ABC):
def create_graph(self): def create_graph(self):
pass pass
@abc.abstractmethod
def clean_old_files(self, keep_results):
pass
class BeltsGraphCreator(GraphCreator): class BeltsGraphCreator(GraphCreator):
def __init__(self, keep_csv=False, dpi=150): def __init__(self, keep_csv=False, dpi=150):
@@ -266,6 +233,22 @@ class BeltsGraphCreator(GraphCreator):
fig = belts_calibration(lognames, Config.KLIPPER_FOLDER, self._version) fig = belts_calibration(lognames, Config.KLIPPER_FOLDER, self._version)
self._save_figure_and_cleanup(fig, lognames) self._save_figure_and_cleanup(fig, lognames)
def clean_old_files(self, keep_results=3):
folder = self._folder
files = [os.path.join(folder, f) for f in os.listdir(folder) if f.endswith('.png')]
files.sort(key=os.path.getmtime, reverse=True)
if len(files) <= keep_results + 1:
return
else:
for old_file in files[keep_results + 1 :]:
file_date = '_'.join(os.path.splitext(os.path.basename(old_file))[0].split('_')[1:3])
for suffix in ['A', 'B']:
csv_file = os.path.join(folder, f'belt_{file_date}_{suffix}.csv')
if os.path.exists(csv_file):
os.remove(csv_file)
os.remove(old_file)
class ShaperGraphCreator(GraphCreator): class ShaperGraphCreator(GraphCreator):
def __init__(self, max_smoothing=None, scv=5.0, keep_csv=False, dpi=150): def __init__(self, max_smoothing=None, scv=5.0, keep_csv=False, dpi=150):
@@ -287,6 +270,20 @@ class ShaperGraphCreator(GraphCreator):
) )
self._save_figure_and_cleanup(fig, lognames, lognames[0].split('_')[-1].split('.')[0]) self._save_figure_and_cleanup(fig, lognames, lognames[0].split('_')[-1].split('.')[0])
def clean_old_files(self, keep_results=3):
folder = self._folder
files = [os.path.join(folder, f) for f in os.listdir(folder) if f.endswith('.png')]
files.sort(key=os.path.getmtime, reverse=True)
if len(files) <= 2 * keep_results + 1:
return
else: # delete the older files
for old_file in files[2 * keep_results + 1 :]:
csv_file = os.path.join(folder, os.path.splitext(os.path.basename(old_file))[0] + '.csv')
if os.path.exists(csv_file):
os.remove(csv_file)
os.remove(old_file)
class VibrationsGraphCreator(GraphCreator): class VibrationsGraphCreator(GraphCreator):
def __init__(self, kinematics, accel, chip_name, keep_csv=False, dpi=150): def __init__(self, kinematics, accel, chip_name, keep_csv=False, dpi=150):
@@ -298,6 +295,11 @@ class VibrationsGraphCreator(GraphCreator):
self._setup_folder('vibrations') self._setup_folder('vibrations')
def _archive_files(self, lognames):
with tarfile.open(os.path.join(self._folder, f'{self._type}_{self._graph_date}.tar.gz'), 'w:gz') as tar:
for csv_file in lognames:
tar.add(csv_file, arcname=os.path.basename(csv_file), recursive=False)
def create_graph(self): def create_graph(self):
lognames = self._move_and_prepare_files( lognames = self._move_and_prepare_files(
glob_pattern=f'/tmp/{self._chip_name}-*.csv', glob_pattern=f'/tmp/{self._chip_name}-*.csv',
@@ -307,10 +309,19 @@ class VibrationsGraphCreator(GraphCreator):
fig = vibrations_profile(lognames, Config.KLIPPER_FOLDER, self._kinematics, self._accel, self._version) fig = vibrations_profile(lognames, Config.KLIPPER_FOLDER, self._kinematics, self._accel, self._version)
self._save_figure_and_cleanup(fig, lognames) self._save_figure_and_cleanup(fig, lognames)
def _archive_files(self, lognames): def clean_old_files(self, keep_results=3):
with tarfile.open(os.path.join(self._folder, f'{self._type}_{self._graph_date}.tar.gz'), 'w:gz') as tar: folder = self._folder
for csv_file in lognames: files = [os.path.join(folder, f) for f in os.listdir(folder) if f.endswith('.png')]
tar.add(csv_file, arcname=os.path.basename(csv_file), recursive=False) files.sort(key=os.path.getmtime, reverse=True)
if len(files) <= keep_results + 1:
return
else: # delete the older files
for old_file in files[keep_results + 1 :]:
os.remove(old_file)
tar_file = os.path.join(folder, os.path.splitext(os.path.basename(old_file))[0] + '.tar.gz')
if os.path.exists(tar_file):
os.remove(tar_file)
class AxesMapFinder: class AxesMapFinder:
@@ -355,17 +366,12 @@ def main():
) )
elif options.type == 'axesmap': elif options.type == 'axesmap':
graph_creator = AxesMapFinder(options.accel_used, options.chip_name) graph_creator = AxesMapFinder(options.accel_used, options.chip_name)
elif options.type == 'clean':
print_with_c_locale(f'Cleaning output folder to keep only the last {options.keep_results} results...')
FileManager.clean_old_files(options.type, options.keep_results)
return
if graph_creator: if graph_creator:
graph_creator.create_graph() graph_creator.create_graph()
print_with_c_locale(f'{options.type} graphs created successfully!') print_with_c_locale(f'{options.type} graphs created successfully!')
graph_creator.clean_old_files(options.keep_results)
if options.keep_csv is False and options.type != 'clean': print_with_c_locale(f'Cleaned output folder to keep only the last {options.keep_results} results!')
print_with_c_locale('Deleting raw CSV files... If you want to keep them, use the --keep_csv option!')
if __name__ == '__main__': if __name__ == '__main__':