Skip to content
This repository was archived by the owner on Jun 3, 2019. It is now read-only.

Commit 4690f7c

Browse files
VictorCarriPatrickDuncan
authored andcommitted
Resolves #31 - Cleanup files (#53)
1 parent 0ae90b4 commit 4690f7c

File tree

5 files changed

+136
-12
lines changed

5 files changed

+136
-12
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ __pycache__/
77

88
# Miscellaneous
99
*.swp
10+
11+
# Compiled Python files
12+
*.pyc
13+
14+
# VIM settings
15+
.vimrc
16+
17+
# Audio files
18+
*.flac
19+
*.m4a
20+
*.mp3
21+
*.ogg
22+
*.wav

cleansio/cleansio.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
""" Displays the lyrics of an audio file """
22

3-
import os
3+
# Standard imports
44
import sys
5+
from atexit import register
6+
from signal import signal, SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM
7+
8+
# Imports from our modules
59
from audio import AudioFile
610
from speech import transcribe
11+
from utils import cleanup
712

813
def valid_input():
914
""" Validates the user's input """
1015
return len(sys.argv) > 1
1116

12-
def cleanup():
13-
""" Removes temporary files """
14-
if 'CLEANSIO_TEMP_FILE' in os.environ:
15-
os.remove(os.environ.get('CLEANSIO_TEMP_FILE'))
16-
if 'CLEANSIO_SLICES_LIST' in os.environ:
17-
slices_list_env_var = os.environ['CLEANSIO_SLICES_LIST']
18-
slices_list = slices_list_env_var[2:-2].split('\', \'')
19-
for slice_file in slices_list:
20-
os.remove(slice_file)
21-
2217
if __name__ == '__main__':
18+
# Set the cleanup handler for each signal which we want to catch
19+
for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
20+
signal(sig, cleanup)
21+
# Register the cleanup function to be called if the program exits normally
22+
register(cleanup)
2323
if valid_input():
2424
transcribe(AudioFile(sys.argv[1]))
25-
cleanup()
2625
else:
2726
print('Please see the README.')

cleansio/utils/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
""" Runs after every import """
2+
3+
from .cleanup import cleanup

cleansio/utils/cleanup.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
""" Cleans up temporary files after the program runs """
2+
3+
# environ - To read the environment variables which we use for communication
4+
# remove - To remove the temporary files
5+
from os import environ, remove
6+
import sys
7+
8+
# Cleans up files on normal or abnormal exit
9+
# The arguments are unused - they are only here to satisfy atexit.
10+
def cleanup(_sig_num=None, _cur_stack_frame=None):
11+
""" Removes temporary files """
12+
remove_temp_file()
13+
remove_slices()
14+
sys.exit()
15+
16+
def remove_temp_file():
17+
""" Removes converted WAV file """
18+
19+
if 'CLEANSIO_TEMP_FILE' in environ:
20+
temp_file = environ.get('CLEANSIO_TEMP_FILE')
21+
try:
22+
remove(temp_file)
23+
except FileNotFoundError:
24+
pass
25+
26+
def remove_slices():
27+
""" Removes each slice of the converted WAV file """
28+
29+
if 'CLEANSIO_SLICES_LIST' in environ:
30+
slices_list_env_var = environ['CLEANSIO_SLICES_LIST']
31+
slices_list = slices_list_env_var[2:-2].split('\', \'')
32+
for slice_file in slices_list:
33+
try:
34+
remove(slice_file)
35+
except FileNotFoundError:
36+
pass

tests/test_cleanup.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#pylint: skip-file
2+
3+
# Standard imports
4+
from uuid import uuid4
5+
from random import randint
6+
import os
7+
import pytest
8+
9+
# Function to test
10+
from cleansio import cleanup
11+
12+
# Tests the cleanup function
13+
def test_cleanup():
14+
## Temporary file ##
15+
16+
# Temp file shouldn't exist
17+
if 'CLEANSIO_TEMP_FILE' in os.environ: # Temp file var exists
18+
# Fetch the name of the temp file
19+
temp_file = os.environ.get('CLEANSIO_TEMP_FILE')
20+
else: # Temp file variable doesn't exist
21+
temp_file_name = str(uuid4().hex) # Fetch a random temporary file name
22+
# Create a path to the temporary file
23+
temp_file = "./{0}".format(temp_file_name)
24+
os.environ['CLEANSIO_TEMP_FILE'] = temp_file # For function
25+
26+
create_temp_file(temp_file)
27+
28+
## Slices ##
29+
slices_list = [] # Holds names of slice files. Used later on in test.
30+
31+
if 'CLEANSIO_SLICES_LIST' in os.environ: # Slices exist
32+
slices_list_env_var = os.environ['CLEANSIO_SLICES_LIST']
33+
slices_list = slices_list_env_var.split[2:-2].split('\', \'')
34+
for slice_file in slices_list:
35+
create_temp_file(slice_file)
36+
else: # Slices don't exist
37+
# Generate a random number of slice files
38+
for i in range(0,randint(3, 10)+1):
39+
# Choose a random name for the slice which we'll generate
40+
temp_slice_name = str(uuid4().hex)
41+
create_temp_file(temp_slice_name)
42+
slices_list.append(temp_slice_name)
43+
os.environ['CLEANSIO_SLICES_LIST'] = str(slices_list)
44+
45+
with pytest.raises(SystemExit) as pytest_e: # Ignore sys.exit()
46+
cleanup() # Run the function
47+
48+
## Checking whether or not the function worked ##
49+
# The temporary file shouldn't exist after the cleanup function runs
50+
assert(not exists(temp_file))
51+
52+
for slice in slices_list: # None of the "slice files" should exist
53+
assert(not exists(slice))
54+
55+
def exists(file_name):
56+
""" Checks whether a file with the given name exists. """
57+
try:
58+
f = open(file_name, "r")
59+
f.close()
60+
return True
61+
except FileNotFoundError:
62+
return False
63+
64+
def create_temp_file(file_name):
65+
""" Creates a temp file with the given name, if it doesn't already exist """
66+
if not exists(file_name):
67+
f = open(file_name, "w")
68+
fconts = str(uuid4().hex)
69+
f.write(fconts)
70+
f.close()
71+
else:
72+
errStr = "File {0} already exists, won't overwrite".format(file_name)
73+
raise FileExistsError(errStr)

0 commit comments

Comments
 (0)