bin/fetch2
changeset 199 85c186fb26e7
parent 198 8d451676c241
child 200 b90fc7204ab9
equal deleted inserted replaced
198:8d451676c241 199:85c186fb26e7
     1 #!/usr/bin/env python
       
     2 
       
     3 import sys
       
     4 import os
       
     5 import os.path
       
     6 import signal
       
     7 import resource
       
     8 import subprocess
       
     9 
       
    10 hackenv = os.path.join(os.path.realpath(os.environ['HACKENV']), '')
       
    11 hackenv_hg = os.path.join(hackenv, '.hg', '')
       
    12 hackenv_tmp = os.path.join(hackenv, 'tmp', '')
       
    13 url = sys.argv[1]
       
    14 output = None
       
    15 
       
    16 if not url:
       
    17     sys.stdout.write('Usage:  fetch URL  or  fetch OUTPUT_FILE URL  (no spaces or quoting in OUTPUT_FILE)')
       
    18     sys.exit(1)
       
    19 
       
    20 parts = url.split(None, 1)
       
    21 if len(parts) == 2:
       
    22     output, url = parts
       
    23 
       
    24 cmd = ['wget', '-nv']
       
    25 if output is not None:
       
    26     if output.startswith('/hackenv/'):
       
    27         output = os.path.join(hackenv, output[9:])
       
    28     else:
       
    29         output = os.path.join(hackenv_tmp, output)
       
    30     real_output = os.path.realpath(output)
       
    31     if (os.path.commonprefix([hackenv, real_output]) != hackenv
       
    32         or os.path.commonprefix([hackenv_hg, real_output]) == hackenv_hg):
       
    33         sys.stdout.write('In another world: ' + output)
       
    34         sys.exit(1)
       
    35     if os.path.isdir(real_output):
       
    36         sys.stdout.write("That's a directory: " + output)
       
    37         sys.exit(1)
       
    38     cmd.extend(['-O', real_output])
       
    39 cmd.extend(['--', url])
       
    40 
       
    41 resource.setrlimit(resource.RLIMIT_FSIZE, (10*1024*1024, 10*1024*1024))
       
    42 signal.alarm(30)
       
    43 status = subprocess.Popen(
       
    44     cmd,
       
    45     stdout=subprocess.PIPE,
       
    46     stderr=subprocess.STDOUT,
       
    47     cwd=hackenv_tmp).communicate()[0]
       
    48 if output is not None:
       
    49     status = status.replace(hackenv, '/hackenv/', 1)
       
    50 sys.stdout.write(status)