Example #1
0
 def download(self, url, cookie=None):
     suffix = os.path.splitext(urlparse.urlsplit(url)[2])[1]
     fd, filename = tempfile.mkstemp(suffix=suffix, dir=self.dir)
     os.close(fd)
     with ctxtimer.print_time('Downloading', url, 'to', filename):
         opener = urllib2.build_opener()
         if cookie:
             opener.addheaders.append(('Cookie', cookie))
         num_tries = 2
         for i in range(num_tries):
             try:
                 f = opener.open(url)
             except urllib2.URLError, e:
                 print('Failed to open url', url)
                 continue
             length = f.headers.get('content-length')
             if not length:
                 print('Failed to get content-length')
                 continue
             length = int(length)
             with open(filename, 'wb') as out:
                 count = 0
                 while count < length:
                     data = f.read(1024 * 1024)
                     count += len(data)
                     out.write(data)
Example #2
0
File: download.py Project: ampl/mp
 def download(self, url, cookie=None):
   suffix = os.path.splitext(urlparse.urlsplit(url)[2])[1]
   fd, filename = tempfile.mkstemp(suffix=suffix, dir=self.dir)
   os.close(fd)
   with ctxtimer.print_time('Downloading', url, 'to', filename):
     opener = urllib2.build_opener()
     if cookie:
       opener.addheaders.append(('Cookie', cookie))
     num_tries = 2
     for i in range(num_tries):
       try:
         f = opener.open(url)
       except urllib2.URLError, e:
         print('Failed to open url', url)
         continue
       length = f.headers.get('content-length')
       if not length:
         print('Failed to get content-length')
         continue
       length = int(length)
       with open(filename, 'wb') as out:
         count = 0
         while count < length:
           data = f.read(1024 * 1024)
           count += len(data)
           out.write(data)
Example #3
0
 def test_print_time(self):
   stdout = util.CaptureStdout()
   with stdout:
     with print_time('doing', 'something'):
       do_something()
   self.assertEqual(
     'doing something\ndoing something finished in 0.01 second(s)\n',
     stdout.str())
Example #4
0
def package(basename, archive_format, package_dir):
  with ctxtimer.print_time('Creating', basename, 'package'):
    if archive_format == 'gztar':
      # Use command-line tar instead of shutil.make_archive because the
      # latter is too slow.
      archive = os.path.join(os.getcwd(), basename + '.tar.gz')
      command = ['tar', 'czf', archive] + os.listdir(package_dir)
      subprocess.check_call(command, cwd=package_dir)
    else:
      shutil.make_archive(basename, archive_format, package_dir, '.')
Example #5
0
def package(basename, archive_format, package_dir):
    with ctxtimer.print_time('Creating', basename, 'package'):
        if archive_format == 'gztar':
            # Use command-line tar instead of shutil.make_archive because the
            # latter is too slow.
            archive = os.path.join(os.getcwd(), basename + '.tar.gz')
            command = ['tar', 'czf', archive] + os.listdir(package_dir)
            subprocess.check_call(command, cwd=package_dir)
        else:
            shutil.make_archive(basename, archive_format, package_dir, '.')
Example #6
0
def install_mingw(arch):
  bits = '64' if arch.endswith('64') else '32'
  if os.path.exists(r'\mingw' + bits):
    return
  with download(
      'http://sourceforge.net/projects/mingw-w64/files/' +
      'Toolchains%20targetting%20Win' + bits + '/Personal%20Builds/' +
      'mingw-builds/4.8.2/threads-win32/sjlj/' + arch +
      '-4.8.2-release-win32-sjlj-rt_v3-rev4.7z/download') as f:
    with ctxtimer.print_time("Installing MinGW" + bits):
      output = check_output([sevenzip, 'x', '-oC:\\', f])
      for line in output.split('\n'):
        if not line.startswith('Extracting '):
          print(line)
Example #7
0
def install_mingw(arch):
    bits = '64' if arch.endswith('64') else '32'
    if os.path.exists(r'\mingw' + bits):
        return
    with download('http://sourceforge.net/projects/mingw-w64/files/' +
                  'Toolchains%20targetting%20Win' + bits +
                  '/Personal%20Builds/' +
                  'mingw-builds/4.8.2/threads-win32/sjlj/' + arch +
                  '-4.8.2-release-win32-sjlj-rt_v3-rev4.7z/download') as f:
        with ctxtimer.print_time("Installing MinGW" + bits):
            output = check_output([sevenzip, 'x', '-oC:\\', f])
            for line in output.split('\n'):
                if not line.startswith('Extracting '):
                    print(line)