Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: tools/pyutils/url_utils.py

Issue 385783002: roll "common" DEPS, and replace tools/pyutils with it (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rearrange DEPS a bit Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/pyutils/gs_utils.py ('k') | tools/pyutils/url_utils_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
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 TODO(epoger): move this into tools/utils for broader use?
12 """
13
14 # System-level imports
15 import contextlib
16 import os
17 import shutil
18 import urllib
19 import urlparse
20
21
22 def create_filepath_url(filepath):
23 """ Returns a file:/// URL pointing at the given filepath on local disk.
24
25 Args:
26 filepath: string; path to a file on local disk (may be absolute or relative,
27 and the file does not need to exist)
28
29 Returns:
30 A file:/// URL pointing at the file. Regardless of whether filepath was
31 specified as a relative or absolute path, the URL will contain an
32 absolute path to the file.
33
34 Raises:
35 An Exception, if filepath is already a URL.
36 """
37 if urlparse.urlparse(filepath).scheme:
38 raise Exception('"%s" is already a URL' % filepath)
39 return urlparse.urljoin(
40 'file:', urllib.pathname2url(os.path.abspath(filepath)))
41
42
43 def copy_contents(source_url, dest_path, create_subdirs_if_needed=False):
44 """ Copies the full contents of the URL 'source_url' into
45 filepath 'dest_path'.
46
47 Args:
48 source_url: string; complete URL to read from
49 dest_path: string; complete filepath to write to (may be absolute or
50 relative)
51 create_subdirs_if_needed: boolean; whether to create subdirectories as
52 needed to create dest_path
53
54 Raises:
55 Some subclass of Exception if unable to read source_url or write dest_path.
56 """
57 if create_subdirs_if_needed:
58 dest_dir = os.path.dirname(dest_path)
59 if not os.path.exists(dest_dir):
60 os.makedirs(dest_dir)
61 with contextlib.closing(urllib.urlopen(source_url)) as source_handle:
62 with open(dest_path, 'wb') as dest_handle:
63 shutil.copyfileobj(fsrc=source_handle, fdst=dest_handle)
OLDNEW
« no previous file with comments | « tools/pyutils/gs_utils.py ('k') | tools/pyutils/url_utils_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698