Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
|
borenet
2014/07/10 19:42:32
Is this a pure copy/paste?
epoger
2014/07/10 19:44:03
Yes, both files were pure copy from https://skia.g
| |
| 2 | |
| 3 """ | |
| 4 Copyright 2014 Google Inc. | |
| 5 | |
| 6 Use of this source code is governed by a BSD-style license that can be | |
| 7 found in the LICENSE file. | |
| 8 | |
| 9 Utilities for working with URLs. | |
| 10 """ | |
| 11 | |
| 12 # System-level imports | |
| 13 import contextlib | |
| 14 import os | |
| 15 import shutil | |
| 16 import urllib | |
| 17 import urlparse | |
| 18 | |
| 19 | |
| 20 def create_filepath_url(filepath): | |
| 21 """ Returns a file:/// URL pointing at the given filepath on local disk. | |
| 22 | |
| 23 Args: | |
| 24 filepath: string; path to a file on local disk (may be absolute or relative, | |
| 25 and the file does not need to exist) | |
| 26 | |
| 27 Returns: | |
| 28 A file:/// URL pointing at the file. Regardless of whether filepath was | |
| 29 specified as a relative or absolute path, the URL will contain an | |
| 30 absolute path to the file. | |
| 31 | |
| 32 Raises: | |
| 33 An Exception, if filepath is already a URL. | |
|
borenet
2014/07/10 19:42:32
Why? Is this ever better than just returning the p
epoger
2014/07/10 19:44:03
I dunno. Figure I should just leave this as-is fo
| |
| 34 """ | |
| 35 if urlparse.urlparse(filepath).scheme: | |
| 36 raise Exception('"%s" is already a URL' % filepath) | |
| 37 return urlparse.urljoin( | |
| 38 'file:', urllib.pathname2url(os.path.abspath(filepath))) | |
| 39 | |
| 40 | |
| 41 def copy_contents(source_url, dest_path, create_subdirs_if_needed=False): | |
| 42 """ Copies the full contents of the URL 'source_url' into | |
| 43 filepath 'dest_path'. | |
| 44 | |
| 45 Args: | |
| 46 source_url: string; complete URL to read from | |
| 47 dest_path: string; complete filepath to write to (may be absolute or | |
| 48 relative) | |
| 49 create_subdirs_if_needed: boolean; whether to create subdirectories as | |
| 50 needed to create dest_path | |
| 51 | |
| 52 Raises: | |
| 53 Some subclass of Exception if unable to read source_url or write dest_path. | |
| 54 """ | |
| 55 if create_subdirs_if_needed: | |
| 56 dest_dir = os.path.dirname(dest_path) | |
| 57 if not os.path.exists(dest_dir): | |
| 58 os.makedirs(dest_dir) | |
| 59 with contextlib.closing(urllib.urlopen(source_url)) as source_handle: | |
| 60 with open(dest_path, 'wb') as dest_handle: | |
| 61 shutil.copyfileobj(fsrc=source_handle, fdst=dest_handle) | |
| OLD | NEW |