• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #!/usr/bin/python
2 
3 # Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6 
7 
8 """Retrieve the given file from googlesource.com."""
9 
10 
11 from contextlib import closing
12 
13 import base64
14 import sys
15 import urllib2
16 
17 
18 def get(repo_url, filepath):
19   """Retrieve the contents of the given file from the given googlesource repo.
20 
21   Args:
22       repo_url: string; URL of the repository from which to retrieve the file.
23       filepath: string; path of the file within the repository.
24 
25   Return:
26       string; the contents of the given file.
27   """
28   base64_url = '/'.join((repo_url, '+', 'master', filepath)) + '?format=TEXT'
29   with closing(urllib2.urlopen(base64_url)) as f:
30     return base64.b64decode(f.read())
31 
32 
33 if __name__ == '__main__':
34   if len(sys.argv) != 3:
35     print >> sys.stderr, 'Usage: %s <repo_url> <filepath>' % sys.argv[0]
36     sys.exit(1)
37   sys.stdout.write(get(sys.argv[1], sys.argv[2]))
38