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 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 104 | if opt.jobs == 1: |
| 105 | for project in all: |
| 106 | state = project.PrintWorkTreeStatus() |
| 107 | if state == 'CLEAN': |
| 108 | counter.next() |
| 109 | else: |
| 110 | sem = _threading.Semaphore(opt.jobs) |
| 111 | threads_and_output = [] |
| 112 | for project in all: |
| 113 | sem.acquire() |
Cezary Baginski | ccf8643 | 2012-04-23 23:55:35 +0200 | [diff] [blame] | 114 | |
| 115 | class BufList(StringIO.StringIO): |
| 116 | def dump(self, ostream): |
| 117 | for entry in self.buflist: |
| 118 | ostream.write(entry) |
| 119 | |
| 120 | output = BufList() |
| 121 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 122 | t = _threading.Thread(target=self._StatusHelper, |
| 123 | args=(project, counter, sem, output)) |
| 124 | threads_and_output.append((t, output)) |
David 'Digit' Turner | e212665 | 2012-09-05 10:35:06 +0200 | [diff] [blame] | 125 | t.daemon = True |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 126 | t.start() |
| 127 | for (t, output) in threads_and_output: |
| 128 | t.join() |
Cezary Baginski | ccf8643 | 2012-04-23 23:55:35 +0200 | [diff] [blame] | 129 | output.dump(sys.stdout) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 130 | output.close() |
| 131 | if len(all) == counter.next(): |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 132 | print 'nothing to commit (working directory clean)' |