retrieve_from_googlesource.py 998 B

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