-
Notifications
You must be signed in to change notification settings - Fork 10
/
manage.py
executable file
·107 lines (84 loc) · 2.96 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
from __future__ import absolute_import
import os
import shutil
import time
import datetime
from flask.ext.script import Manager
from modelconvert import create_app
from modelconvert.utils import fs
app = create_app()
manager = Manager(app)
@manager.command
def run():
app.run(threaded=True)
@manager.command
def celeryworker():
"""
Runs celery worker within the Flask app context
"""
from modelconvert.extensions import celery
with app.app_context():
if app.config['DEBUG']:
celery.worker_main(['worker', '-E', '-l', 'DEBUG'])
else:
celery.worker_main(['worker', '-E', '-l', 'INFO'])
#
# FIXME: move this to a celerybeats task
#
@manager.command
def cleanup(longevity=151200, uploads=False):
"""
Removes generated files. Use cleanup -h for more info
"""
download_path = os.path.abspath(app.config["DOWNLOAD_PATH"])
upload_path = os.path.abspath(app.config["UPLOAD_PATH"])
# simple protection against dummies. However it is questionable to
# give them Unix rm command in this case ;)
if not 'tmp/downloads' in download_path or download_path == '/':
print("You are using a non-standard location for the download path.")
print("Please create your own deletion procedure. If your fs is")
print("mounted with mtime support, this command will work fine:\n")
print(" find /your/path -mtime +30 -exec rm -rf '{}' \;\n")
exit(-1)
#longevity = 6300 * 24
longevity = int(longevity)
current_time = time.time();
print("Removing files older than {0}".format(datetime.timedelta(seconds=longevity)))
def _clean(path, longevity):
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
filepath = os.path.join(root, name)
filetime = os.path.getmtime(filepath)
if current_time - filetime > longevity:
print("Removing file %s" % filepath)
os.remove(filepath)
for name in dirs:
dirpath = os.path.join(root, name)
#dirtime = os.path.getmtime(dirpath)
#if current_time - dirtime > longevity:
if not os.listdir(dirpath):
print("Removing directory %s" % dirpath)
os.rmdir(dirpath)
_clean(download_path, longevity)
if uploads:
_clean(upload_path, longevity)
@manager.command
def purge():
""" Kill all files in download paths NOW"""
cleanup(0, uploads=True)
@manager.command
def mkdirs():
"""
Create required directories from settings
"""
dirs = [
app.config['UPLOAD_PATH'],
app.config['DOWNLOAD_PATH'],
]
for directory in dirs:
directory = os.path.abspath(directory)
print("Creating directory {0}".format(directory))
fs.mkdir_p(directory)
if __name__ == "__main__":
manager.run()