The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
| 16 | from command import PagedCommand |
| 17 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 18 | try: |
| 19 | import threading as _threading |
| 20 | except ImportError: |
| 21 | import dummy_threading as _threading |
| 22 | |
| 23 | import itertools |
| 24 | import sys |
| 25 | import StringIO |
| 26 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | class Status(PagedCommand): |
| 28 | common = True |
| 29 | helpSummary = "Show the working tree status" |
| 30 | helpUsage = """ |
| 31 | %prog [<project>...] |
| 32 | """ |
Shawn O. Pearce | 4c5c7aa | 2009-04-13 14:06:10 -0700 | [diff] [blame] | 33 | helpDescription = """ |
| 34 | '%prog' compares the working tree to the staging area (aka index), |
| 35 | and the most recent commit on this branch (HEAD), in each project |
| 36 | specified. A summary is displayed, one line per file where there |
| 37 | is a difference between these three states. |
| 38 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 39 | The -j/--jobs option can be used to run multiple status queries |
| 40 | in parallel. |
| 41 | |
Shawn O. Pearce | 4c5c7aa | 2009-04-13 14:06:10 -0700 | [diff] [blame] | 42 | Status Display |
| 43 | -------------- |
| 44 | |
| 45 | The status display is organized into three columns of information, |
| 46 | for example if the file 'subcmds/status.py' is modified in the |
| 47 | project 'repo' on branch 'devwork': |
| 48 | |
| 49 | project repo/ branch devwork |
| 50 | -m subcmds/status.py |
| 51 | |
| 52 | The first column explains how the staging area (index) differs from |
| 53 | the last commit (HEAD). Its values are always displayed in upper |
| 54 | case and have the following meanings: |
| 55 | |
| 56 | -: no difference |
| 57 | A: added (not in HEAD, in index ) |
| 58 | M: modified ( in HEAD, in index, different content ) |
| 59 | D: deleted ( in HEAD, not in index ) |
| 60 | R: renamed (not in HEAD, in index, path changed ) |
| 61 | C: copied (not in HEAD, in index, copied from another) |
| 62 | T: mode changed ( in HEAD, in index, same content ) |
| 63 | U: unmerged; conflict resolution required |
| 64 | |
| 65 | The second column explains how the working directory differs from |
| 66 | the index. Its values are always displayed in lower case and have |
| 67 | the following meanings: |
| 68 | |
| 69 | -: new / unknown (not in index, in work tree ) |
| 70 | m: modified ( in index, in work tree, modified ) |
| 71 | d: deleted ( in index, not in work tree ) |
| 72 | |
| 73 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 75 | def _Options(self, p): |
| 76 | p.add_option('-j', '--jobs', |
| 77 | dest='jobs', action='store', type='int', default=2, |
| 78 | help="number of projects to check simultaneously") |
| 79 | |
| 80 | def _StatusHelper(self, project, clean_counter, sem, output): |
| 81 | """Obtains the status for a specific project. |
| 82 | |
| 83 | Obtains the status for a project, redirecting the output to |
| 84 | the specified object. It will release the semaphore |
| 85 | when done. |
| 86 | |
| 87 | Args: |
| 88 | project: Project to get status of. |
| 89 | clean_counter: Counter for clean projects. |
| 90 | sem: Semaphore, will call release() when complete. |
| 91 | output: Where to output the status. |
| 92 | """ |
| 93 | try: |
| 94 | state = project.PrintWorkTreeStatus(output) |
| 95 | if state == 'CLEAN': |
| 96 | clean_counter.next() |
| 97 | finally: |
| 98 | sem.release() |
| 99 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | def Execute(self, opt, args): |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 101 | all = self.GetProjects(args) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 102 | counter = itertools.count() |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 103 | |
Shawn O. Pearce | 8bd5e60 | 2009-04-18 15:31:36 -0700 | [diff] [blame] | 104 | on = {} |
| 105 | for project in all: |
| 106 | cb = project.CurrentBranch |
| 107 | if cb: |
| 108 | if cb not in on: |
| 109 | on[cb] = [] |
| 110 | on[cb].append(project) |
| 111 | |
| 112 | branch_names = list(on.keys()) |
| 113 | branch_names.sort() |
| 114 | for cb in branch_names: |
| 115 | print '# on branch %s' % cb |
| 116 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 117 | if opt.jobs == 1: |
| 118 | for project in all: |
| 119 | state = project.PrintWorkTreeStatus() |
| 120 | if state == 'CLEAN': |
| 121 | counter.next() |
| 122 | else: |
| 123 | sem = _threading.Semaphore(opt.jobs) |
| 124 | threads_and_output = [] |
| 125 | for project in all: |
| 126 | sem.acquire() |
| 127 | output = StringIO.StringIO() |
| 128 | t = _threading.Thread(target=self._StatusHelper, |
| 129 | args=(project, counter, sem, output)) |
| 130 | threads_and_output.append((t, output)) |
| 131 | t.start() |
| 132 | for (t, output) in threads_and_output: |
| 133 | t.join() |
| 134 | sys.stdout.write(output.getvalue()) |
| 135 | output.close() |
| 136 | if len(all) == counter.next(): |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 137 | print 'nothing to commit (working directory clean)' |