Import the json module only if it's the one we're expecting.

There's a json module out there named "json", which is the same name as
the one bundled with Python 2.6, but it doesn't have the "loads" method we
need. We now import specifically that function from the module, falling back
on simplejson if it's missing.
Improve Mercurial support in post-review.

This vastly improves our Mercurial support by doing the following:

* Fixes the assumption that `hg svn info` returning a non-error means that
  the working copy is a hgsubversion clone.
* Adds support for reading 'reviewboard.url' from the local hgrc file.
* Adds support for posting reviews of locally-committed changes (instead of
  requiring that all changes be uncommitted).
* Adds support for '--parent' for parent branches.
* Adds unit tests for Mercurial.

Patch by Dan Buch.
Reviewed at http://reviews.reviewboard.org/r/1464/
Fix post-review for bare hostnames.

Patch from Noah Kantrowitz. cookielib automatically appends .local to any
hostname with no '.'s, presumably because Mozilla does the same. This breaks the
cookie lookup for something like http://reviews/.
Perforce: Speed up creating review request diff for a path/range diff

Patch from Gyula Faller. If you posting a review request in a Perforce workspace
from a range of changes for a range of files, and not only one changelist, it is
very slow now, since it diffs all the files in the range.  But there is speed
up: When the revision numbers equal in Perforce, the two files are identical, by
definition.
git: slash-friendly strip of branch name

Patch from Petr Novák. git branch names can contains slashes, so stripping of
everything up to the last slash can break tracking branch detection. This patch
introduces a bit friendlier re.sub(r'^refs/heads/', '', ...)

Testing done:
I developed a test case to pass with branch "mybranch", for which it passed.
Switching test branch name to "my/branch" made it fail. Second commit which
introduces slash-friendly stripping method makes it pass again.
ClearCase directory diffs from post-review side

Patch from Jan Koprowski. This patch add support for ClearCase directory
revisions. This is diff between "ls -1" command for each of them.

Fixes bug 1626.
Gracefully handle ClearCase deployments where VOBs are mounted as subdirectories of /vobs

Patch from Bryan Halter to fix the following scenario:
1.On a system with ClearCase and vobs mounted under / instead of /vobs
2.run cleartool lsco -cview -me -recurse -short | xargs post-review

Fixes bug 1421
post-review parameter to reopen discarded review

Patch from Jan Koprowski. Adds a parameter --reopen to post-review, which will
reopen a discarded or submitted review request. Fixes bug 1607.
Provide --repository-url to ClearCase

Patch from Jan Koprowski. In my company there is some custom access to vobs in
dynamic view.
Difference is that there exist two assumptions:
1) We want ACL i old ClearCase so we do it on filesystem layer
2) Many users want to made their own dynamic view on unix system

This two assumptions follow to non standard clearcase dynamic view paths in
system. On filesystem looks like:

/view/user1_view/vobs
/view/user2_view/vobs
/view/user3_view/vobs

Now post review assume the path start from /vobs what is not true.
IMHO there is unused --repository-url parameter witch is perfect in meaning
to make customize "place of view == repository url" for clearcase.

--repository-url "/view/user1_view"

will make the given as parameter path will be the prefix to /vobs.
There is only one change. This parameter now works with --revision-range or
--diff-file. For clearcase they have sens also without any of this.

Testing done:
$ post-review -n -d --server "http://localhost:10000/" --repository-url "/view/user1_view/" index.wsgi
>>> svn info /view/user1_view/
>>> git rev-parse --git-dir
>>> hg root
>>> cleartool pwv -short
>>> repository info: Path: /view/reviewboard.diffview/vobs/, Base path: /view/reviewboard.diffview/vobs/, Supports changesets: False
>>> cleartool desc -pre /view/user1_view/vobs/SCM_tools/scripts/ReleaseRobot/index.wsgi
>>> cleartool desc -fmt %Vn /view/user1_view/vobs/SCM_tools
>>> cleartool desc -fmt %Vn /view/user1_view/vobs/SCM_tools/scripts
>>> cleartool desc -fmt %Vn /view/user1_view/vobs/SCM_tools/scripts/ReleaseRobot
--- /view/user1_view/vobs/SCM_tools/@@/main/int_02.04_GK/2/scripts/@@/main/6/ReleaseRobot/@@/main/3/index.wsgi/@@/main/int_02.04_GK/0   2002-02-21 23:30:39.942229878 -0800
+++ /view/user1_view/vobs/SCM_tools/scripts/ReleaseRobot/index.wsgi   2002-02-21 23:30:50.442260588 -0800
@@ -4,6 +4,8 @@

 from mako.lookup import TemplateLookup
 from werkzeug import Request
+
+# dummy checkout

 def bywwlistdir(directory, config):
     """Return directory sorted by work week"""

 post-review -d --server "http://localhost:10000/" --cc-root "/view/jkoprows_ccgsys" --repository-url "/view/jkoprows_ccgsys/vobs" index.wsgi
>>> svn info /view/jkoprows_ccgsys/vobs
>>> git rev-parse --git-dir
>>> hg root
>>> cleartool pwv -short
>>> repository info: Path: /view/jkoprows_ccgsys/vobs, Base path: /view/jkoprows_ccgsys/vobs, Supports changesets: False
>>> cleartool desc -pre /view/jkoprows_ccgsys/vobs/SCM_tools/scripts/ReleaseRobot/index.wsgi
>>> cleartool desc -fmt %Vn /view/jkoprows_ccgsys/vobs/SCM_tools
>>> cleartool desc -fmt %Vn /view/jkoprows_ccgsys/vobs/SCM_tools/scripts
>>> cleartool desc -fmt %Vn /view/jkoprows_ccgsys/vobs/SCM_tools/scripts/ReleaseRobot
>>> Looking for 'localhost /' cookie in /user/jkoprows/.post-review-cookies.txt
>>> Cookie file loaded, but no cookie for this server
==> Review Board Login Required
Enter username and password for Review Board at http://localhost:10000/
Username: admin
Password:
>>> Logging in with username "admin"
>>> HTTP POSTing to http://localhost:10000/api/json/accounts/login/: {'username': 'admin', 'password': '**************'}
>>> Logged in.
>>> Attempting to create review request on /view/jkoprows_ccgsys/vobs for None
>>> HTTP POSTing to http://localhost:10000/api/json/reviewrequests/new/: {'repository_path': '/view/jkoprows_ccgsys/vobs'}
>>> Review request created
>>> Attempting to set field 'description' to 'jkoprows_ccgsys
VIEWTYPE: dynamic
' for review request '2'
>>> HTTP POSTing to http://localhost:10000/api/json/reviewrequests/2/draft/set/: {'description': 'jkoprows_ccgsys\nVIEWTYPE: dynamic\n'}
>>> Uploading diff, size: 494
>>> HTTP POSTing to http://localhost:10000/api/json/reviewrequests/2/diff/new/: {'basedir': '/view/jkoprows_ccgsys/vobs'}
Review request #2 posted.

http://localhost:10000/r/2
Don't use the login cookie if username/password or submit-as are provided.

If a user is giving submit-as or a username and password on the command line,
it's unlikely that they want to log in using the session stored in the cookie.
This change moves the cookie check until after we've looked at the command-line
arguments. This is based loosely on http://reviews.reviewboard.org/r/713/ but is
a different implementation.

Fixes bug 762.
Fix has_valid_cookie check.

Last night, when I was fixing bug 762, I was working from an ancient change on
reviewboard, and missed a "self". This change fixes it.
Improvements to post-review error-handling with Perforce

A handful of changes to improve error handling in post-review.

* Die with an error when the provided Perforce changelist does not exist
* Die with an error if diffs are empty
* Die with an error when there are no changes in a Perforce changelist. Fixes
  issue 832.
* Default to the default changelist when no Perforce changelist is provided.
  Fixes issue 1191.

This corresponds to the top four commits at
http://github.com/bhollis/rbtools/commits/errorhandling/. Note that
2bd8897b133aa81e2b32 changes the default of running "post-review" in a Perforce
client to uploading the default change - the other changes should not introduce
any new behavior.

Testing done: These changes have been in use for a while.
Reviewed at http://reviews.reviewboard.org/r/1886/
Don't allow external diff programs when posting against git

Having a bad configuration for the diff tool in a users .gitconfig would break
post-review. This is fixed by adding --no-ext-diff to our command-line.
Fixes bug 1524.
Don't die if the cookie file can't be saved.

Saving the cookie file isn't critical for post-review's function, so if it
fails, we don't particularly care. This adds a try/catch, with some associated
debug output if it does happen.

Fixes bug 1557.
Support both --revision-range and files with CVS

It turns out bug 1852 had a ridiculously simple patch to make this work!
Add the INSTALL and COPYING files to the manifest.
Update post-review to work with the Review Board 1.5 API.

This updates post-review to use the new API in Review Board 1.5 when
available. The deprecated API won't be used in this case. However, if
the new API is not available, we fall back on the deprecated API in order
to continue supporting older installations.

This will require some fixes/enhancements to the API that are going in to
1.5.2. The API checks will be updated to check for the version once that
release has been done.
Load cookies on initialization.

We used to load cookies on login, but that doesn't work with the new API.
We now simply load the cookie file right after creating the MozillaCookieJar.
Fix the --username= option and prompt after invalid credentials.

We now support pre-setting the authorization information when --username=
is used. We have a new PresetHTTPAuthHandler class that sets the
header before the first request goes through.

We also now re-prompt for credentials if an authorization request failed.
Python's not very good at handling this sort of situation, so it required
a special ReviewBoardHTTPBasicAuthHandler class that could only retry once
and then fail, allowing our existing retry with forced login code to go
through.
Fix HTTP response handling on Python 2.4 and 2.5.

HTTP response handling on Python 2.4 and 2.5 is broken, and only accepts
HTTP 200 and 206 as successful responses. Since we make use of 201 and other
200 codes, this broke us. We now have our own HTTP error processor class
that knows how to accept these codes.

This will be needed as long as we support Python 2.4 and 2.5.