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