download.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. to_get = []
  34. project = None
  35. for a in args:
  36. m = CHANGE_RE.match(a)
  37. if m:
  38. if not project:
  39. self.Usage()
  40. chg_id = int(m.group(1))
  41. if m.group(2):
  42. ps_id = int(m.group(2))
  43. else:
  44. ps_id = 1
  45. to_get.append((project, chg_id, ps_id))
  46. else:
  47. project = self.GetProjects([a])[0]
  48. return to_get
  49. def Execute(self, opt, args):
  50. for project, change_id, ps_id in self._ParseChangeIds(args):
  51. dl = project.DownloadPatchSet(change_id, ps_id)
  52. if not dl:
  53. print >>sys.stderr, \
  54. '[%s] change %d/%d not found' \
  55. % (project.name, change_id, ps_id)
  56. sys.exit(1)
  57. if not dl.commits:
  58. print >>sys.stderr, \
  59. '[%s] change %d/%d has already been merged' \
  60. % (project.name, change_id, ps_id)
  61. continue
  62. if len(dl.commits) > 1:
  63. print >>sys.stderr, \
  64. '[%s] %d/%d depends on %d unmerged changes:' \
  65. % (project.name, change_id, ps_id, len(dl.commits))
  66. for c in dl.commits:
  67. print >>sys.stderr, ' %s' % (c)
  68. project._Checkout(dl.commit)