<kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
#!/usr/bin/env python
import sys
import os
import os.path
import signal
import resource
import subprocess
hackenv = os.path.join(os.path.realpath(os.environ['HACKENV']), '')
hackenv_hg = os.path.join(hackenv, '.hg', '')
hackenv_tmp = os.path.join(hackenv, 'tmp', '')
url = sys.argv[1]
output = None
if not url:
sys.stdout.write('Usage: fetch URL or fetch OUTPUT_FILE URL (no spaces or quoting in OUTPUT_FILE)')
sys.exit(1)
parts = url.split(None, 1)
if len(parts) == 2:
output, url = parts
cmd = ['wget', '-nv']
if output is not None:
if output.startswith('/hackenv/'):
output = os.path.join(hackenv, output[9:])
else:
output = os.path.join(hackenv_tmp, output)
real_output = os.path.realpath(output)
if (os.path.commonprefix([hackenv, real_output]) != hackenv
or os.path.commonprefix([hackenv_hg, real_output]) == hackenv_hg):
sys.stdout.write('In another world: ' + output)
sys.exit(1)
if os.path.isdir(real_output):
sys.stdout.write("That's a directory: " + output)
sys.exit(1)
cmd.extend(['-O', real_output])
cmd.extend(['--', url])
resource.setrlimit(resource.RLIMIT_FSIZE, (10*1024*1024, 10*1024*1024))
signal.alarm(30)
status = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=hackenv_tmp).communicate()[0]
if output is not None:
status = status.replace(hackenv, '/hackenv/', 1)
sys.stdout.write(status)