bin/fetch2
author hackbot
Mon, 31 May 2021 08:09:38 +0000
changeset 198 8d451676c241
permissions -rw-r--r--
<kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
198
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
     1
#!/usr/bin/env python
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
     2
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
     3
import sys
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
     4
import os
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
     5
import os.path
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
     6
import signal
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
     7
import resource
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
     8
import subprocess
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
     9
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    10
hackenv = os.path.join(os.path.realpath(os.environ['HACKENV']), '')
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    11
hackenv_hg = os.path.join(hackenv, '.hg', '')
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    12
hackenv_tmp = os.path.join(hackenv, 'tmp', '')
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    13
url = sys.argv[1]
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    14
output = None
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    15
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    16
if not url:
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    17
    sys.stdout.write('Usage:  fetch URL  or  fetch OUTPUT_FILE URL  (no spaces or quoting in OUTPUT_FILE)')
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    18
    sys.exit(1)
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    19
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    20
parts = url.split(None, 1)
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    21
if len(parts) == 2:
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    22
    output, url = parts
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    23
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    24
cmd = ['wget', '-nv']
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    25
if output is not None:
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    26
    if output.startswith('/hackenv/'):
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    27
        output = os.path.join(hackenv, output[9:])
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    28
    else:
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    29
        output = os.path.join(hackenv_tmp, output)
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    30
    real_output = os.path.realpath(output)
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    31
    if (os.path.commonprefix([hackenv, real_output]) != hackenv
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    32
        or os.path.commonprefix([hackenv_hg, real_output]) == hackenv_hg):
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    33
        sys.stdout.write('In another world: ' + output)
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    34
        sys.exit(1)
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    35
    if os.path.isdir(real_output):
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    36
        sys.stdout.write("That's a directory: " + output)
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    37
        sys.exit(1)
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    38
    cmd.extend(['-O', real_output])
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    39
cmd.extend(['--', url])
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    40
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    41
resource.setrlimit(resource.RLIMIT_FSIZE, (10*1024*1024, 10*1024*1024))
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    42
signal.alarm(30)
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    43
status = subprocess.Popen(
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    44
    cmd,
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    45
    stdout=subprocess.PIPE,
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    46
    stderr=subprocess.STDOUT,
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    47
    cwd=hackenv_tmp).communicate()[0]
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    48
if output is not None:
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    49
    status = status.replace(hackenv, '/hackenv/', 1)
8d451676c241 <kron> fetch /hackenv/bin/fetch2 https://raw.githubusercontent.com/fis/hackbot/master/multibot_cmds/lib/fetch
hackbot
parents:
diff changeset
    50
sys.stdout.write(status)