download.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #
  2. # Copyright (C) 2008 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import re
  17. import sys
  18. from command import Command
  19. CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
  20. class Download(Command):
  21. common = True
  22. helpSummary = "Download and checkout a change"
  23. helpUsage = """
  24. %prog {project change[/patchset]}...
  25. """
  26. helpDescription = """
  27. The '%prog' command downloads a change from the review system and
  28. makes it available in your project's local working directory.
  29. """
  30. def _Options(self, p):
  31. pass
  32. def _ParseChangeIds(self, args):
  33. if not args:
  34. self.Usage()
  35. to_get = []
  36. project = None
  37. for a in args:
  38. m = CHANGE_RE.match(a)
  39. if m:
  40. if not project:
  41. self.Usage()
  42. chg_id = int(m.group(1))
  43. if m.group(2):
  44. ps_id = int(m.group(2))
  45. else:
  46. ps_id = 1
  47. to_get.append((project, chg_id, ps_id))
  48. else:
  49. project = self.GetProjects([a])[0]
  50. return to_get
  51. def Execute(self, opt, args):
  52. for project, change_id, ps_id in self._ParseChangeIds(args):
  53. dl = project.DownloadPatchSet(change_id, ps_id)
  54. if not dl:
  55. print >>sys.stderr, \
  56. '[%s] change %d/%d not found' \
  57. % (project.name, change_id, ps_id)
  58. sys.exit(1)
  59. if not dl.commits:
  60. print >>sys.stderr, \
  61. '[%s] change %d/%d has already been merged' \
  62. % (project.name, change_id, ps_id)
  63. continue
  64. if len(dl.commits) > 1:
  65. print >>sys.stderr, \
  66. '[%s] %d/%d depends on %d unmerged changes:' \
  67. % (project.name, change_id, ps_id, len(dl.commits))
  68. for c in dl.commits:
  69. print >>sys.stderr, ' %s' % (c)
  70. project._Checkout(dl.commit)