forked from blaenk/carson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
executable file
·140 lines (111 loc) · 3.5 KB
/
util.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python2
import rtorrent
import sys
import os
import sha
import os.path
import shutil
import datetime
import subprocess
import glob
import sqlite3
from flaskext.bcrypt import generate_password_hash
from dateutil import parser
from configobj import ConfigObj
def get_conf():
pre = os.path.dirname(os.path.abspath(__file__))
return ConfigObj(os.path.join(pre, 'carson.conf'))
# simple/naive validation
def validate_path(path):
base = get_conf()['rtorrent']['downloads']
return os.path.exists(path) and path.startswith(base) and len(path.replace(base, '')) > 0
def add(args):
path = args[0]
file = open(path, 'rb')
rtorrent.load_raw(file, 'system')
def erase(args):
name = args[0]
base = get_conf()['rtorrent']['downloads']
path = os.path.join(base, name)
if not validate_path(path):
return
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def extract(args):
name = args[0]
base = get_conf()['rtorrent']['downloads']
path = os.path.join(base, name)
if not validate_path(path) and not os.path.isdir(path):
return
os.chdir(path)
open('.extracting', 'a').close()
for root, dirs, files in os.walk('.'):
for name in [f for f in files if f.endswith('.rar')]:
file = os.path.join(root, name)[2:]
dir = file[:-4]
cmd = ['7z', 'x', file, '-oextract/' + dir, '-y']
subprocess.call(cmd)
os.remove('.extracting')
def prune():
ts = rtorrent.torrents('main', 'get_hash', 'get_custom=metadata')
expired = []
for t in ts:
metadata = rtorrent.decode_metadata(t['metadata'])
if metadata['locks']: continue
date = parser.parse(metadata['date']).replace(tzinfo=None)
# TODO: conf expiration
expires_at = date + datetime.timedelta(weeks=2)
now = datetime.datetime.utcnow()
if now > expires_at and not metadata['locks']:
expired.append(t['hash'])
rtorrent.erase(expired)
def stale():
base = get_conf()['rtorrent']['downloads']
resident = set(glob.glob(os.path.join(base, '*')))
current = set([os.path.join(base, res['name'])
for res in rtorrent.torrents('main', 'get_name')])
stale = resident - current
for path in stale:
# should never happen
if path == base: continue
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def init():
db = sqlite3.connect('carson.db')
cursor = db.cursor()
with open('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
email = raw_input("email: ")
name = raw_input("name: ")
password = raw_input("password: ")
pw = generate_password_hash(password)
token = token = sha.new(os.urandom(64)).hexdigest()
cursor.execute('INSERT INTO users(email, name, password, token, role)'
'VALUES (?, ?, ?, ?, ?)',
(email, name, pw, token, 'admin'))
db.commit()
secret_key = os.urandom(24)
conf = get_conf()
conf['carson']['secret_key'] = secret_key
conf.write()
if __name__ == "__main__":
action = sys.argv[1]
args = sys.argv[2:]
if action == 'add':
add(args)
elif action == 'erase':
erase(args)
elif action == 'extract':
extract(args)
elif action == 'prune':
prune()
elif action == 'stale':
stale()
elif action == 'init':
init()
else:
print "invalid parameter"