DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d498b93b0d28)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
#  ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
# 
# The contents of this file are subject to the Mozilla Public License  
# Version
# 1.1 (the "License"); you may not use this file except in compliance  
# with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS"  
# basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the  
# License
# for the specific language governing rights and limitations under the
# License.
# 
# The Original Code is Bespin.
# 
# The Initial Developer of the Original Code is Mozilla.
# Portions created by the Initial Developer are Copyright (C) 2009
# the Initial Developer. All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 

# Top level pavement for putting together the Bespin project.
import os
import sys
import tarfile
import urllib2
from urlparse import urlparse

from paver.easy import *
import paver.misctasks
import paver.virtual

HEADER = """ ***** BEGIN LICENSE BLOCK *****
Version: MPL 1.1

The contents of this file are subject to the Mozilla Public License  
Version 1.1 (the "License"); you may not use this file except in 
compliance  with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS"  
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
See the License for the specific language governing rights and
limitations under the License.

The Original Code is Bespin.

The Initial Developer of the Original Code is Mozilla.
Portions created by the Initial Developer are Copyright (C) 2009
the Initial Developer. All Rights Reserved.

Contributor(s):
  Bespin Team (bespin@mozilla.com)

***** END LICENSE BLOCK *****
"""

options(
    version=Bunch(
        number="0.4.4",
        name="Bubba Ho-Tep",
        api="4"
    ),
    build_top=path("build"),
    build_dir=lambda: options.build_top / "BespinServer",
    compress_js=Bunch(
        compressor_version = "2.4.2",
        zip_name = lambda: "yuicompressor-%s.zip" % options.compressor_version,
        url = lambda: "http://www.julienlecomte.net/yuicompressor/%s" % \
                options.zip_name,
        beautiful_soup_url = "http://www.crummy.com/software/BeautifulSoup/download/BeautifulSoup.tar.gz"
    ),
    license=Bunch(
        extensions = set([
            ("py", "#"), ("js", "//")
        ]),
        exclude=set([
            './backend/python/lib',
            './frontend/js/external',
            './backend/python/bin',
            './backend/python/bootstrap.py'
        ])
    ),
    virtualenv=Bunch(
        packages_to_install=['backend/python/ext/pip-0.4.1.tar.gz'],
        paver_command_line="required"
    ),
    server=Bunch(
        # set to true to allow connections from other machines
        address="",
        port=8080,
        try_build=False,
        dburl=None,
        async=False,
        config_file=path("devconfig.py")
    ),
    dojo=Bunch(
        version="1.3.0",
        download_url=lambda:"http://download.dojotoolkit.org/release-%s/dojo-release-%s.tar.gz"
                            % (options.dojo.version, options.dojo.version),
        src_url=lambda:"http://download.dojotoolkit.org/release-%s/dojo-release-%s-src.tar.gz"
                            % (options.dojo.version, options.dojo.version),
        destination=path('frontend/js'),
        source=False
    ),
    jsparser=Bunch(
        download_urls=[
          "http://mxr.mozilla.org/mozilla/source/js/narcissus/jsdefs.js?raw=1",
          "http://mxr.mozilla.org/mozilla/source/js/narcissus/jsparse.js?raw=1"
        ]
    ),
)

def _clean_up_extra_egg_info(d):
    """pip freeze has a problem with the extra egg-info directories
    that setuptools leaves lying around when you install using
    --single-version-externally-managed (which is what pip does).
    
    This does the simplest thing possible to fix this, which is to
    get rid of all but the last egg-info directory.
    
    The directory passed in should be the "lib" directory" which
    is above the pythonX.X/site-packages directory. This function
    will figure out which version of python is in use."""
    d = path(d)
    vi = sys.version_info
    d = d / ("python%s.%s" % (vi[0], vi[1])) / "site-packages"
    lastname = None
    lastf = None
    for f in d.glob("*.egg-info"):
        fullname = f.basename()
        pkgname = fullname.split("-", 1)[0]
        # check for a match against the previous
        if lastname == pkgname:
            # we have a match, so delete the previous which is
            # in theory an older version
            info("Deleting old egg-info: %s", lastf)
            lastf.rmtree()
        lastf = f
        lastname = pkgname



@task
def required():
    """Install the required packages.
    
    Installs the requirements set in requirements.txt."""
    pip = path("bin/pip")
    if not pip.exists():
        # try Windows version
        pip = path("Scripts") / "pip"

    downloads = path("downloads")
    downloads.mkdir()
    os.environ["PIP_DOWNLOAD_CACHE"] = downloads.abspath()
    sh('%s install -U --install-option="--install-data=backend/python/installed-package-data" -r requirements.txt' % pip)
    
    call_pavement('backend/python/pavement.py', 'develop')
    
    _clean_up_extra_egg_info("lib")
    
    # clean up after urlrelay's installation
    path("README").unlink()
    path("include").rmtree()
    
@task
def devconfig(options):
    """Generate a developer webserver config file (devconfig.py)."""
    options.server.config_file.write_text("""
from bespin.config import c

# uncomment the following line to turn off stdout logging of requests
# c.log_requests_to_stdout = False

# uncomment the following line to turn off stdout logging of log statements
# c.log_to_stdout = False

# change the following line if you want to test Bespin
# with another database, such as MySQL
# c.dburl = mysql://user:password@localhost/databasename

# uncomment the following line to use the beanstalkd queue and
# bespin_worker jobs
# c.async_jobs = True

# should Project and User names be restricted to a subset
# of characters
# (see bespin.model._check_identifiers)
# uncomment the next line to turn off the restrictions
# c.restrict_identifiers = False

# if you are going to be working on the Thunderhead project code,
# you can point at the directory where Thunderhead is located
# and the script tags will be dynamically replaced
# c.th_src = c.static_dir / ".." / ".." / "th" / "src"

# Look in bespin.config to see more options you can set
""")
    info("Config file created in: %s", options.server.config_file)

@task
def start():
    """Starts the BespinServer on localhost port 8080 for development.
    
    You can change the port and allow remote connections by setting
    server.port or server.address on the command line.
    
    paver server.address=your.ip.address server.port=8000 start
    
    will allow remote connections (assuming you don't have a firewall
    blocking the connection) and start the server on port 8000.
    """
    # automatically install Dojo if it's not there already
    if not (options.dojo.destination / "dojo").exists():
        dojo()
        
    from bespin import config, controllers
    from paste.httpserver import serve
    
    options.order('server')
    
    config.set_profile('dev')
    
    if options.server.try_build:
        config.c.static_dir = (options.build_dir / "frontend").abspath()
    
    if options.server.dburl:
        config.c.dburl = options.server.dburl
    
    if options.server.async:
        config.c.async_jobs = True
    
    config_file = options.server.config_file
    if config_file.exists():
        info("Loading config: %s", config_file)
        code = compile(config_file.bytes(), config_file, "exec")
        exec code in {}
    
    config.activate_profile()
    port = int(options.port)
    serve(controllers.make_app(), options.address, port, use_threadpool=True)
    
@task
def try_build():
    """Starts the server using the compressed JavaScript."""
    options.server.try_build=True
    start()
    
@task
def clean_data():
    """Deletes the development data and recreates the database."""
    data_path = path("devdata.db")
    data_path.unlink()
    create_db()
    
@task
def create_db():
    """Creates the development database"""
    from bespin import config, database, db_versions
    from migrate.versioning.shell import main
    
    config.set_profile('dev')
    
    base_url = config.c.dburl
    
    config_file = options.server.config_file
    if config_file.exists():
        info("Loading config: %s", config_file)
        code = compile(config_file.bytes(), config_file, "exec")
        exec code in {}

    if config.c.dburl == base_url and path("devdata.db").exists():
        raise BuildFailure("Development database already exists")
    
    config.activate_profile()
    dry("Create database tables", database.Base.metadata.create_all, bind=config.c.dbengine)
    
    repository = str(path(db_versions.__file__).dirname())
    dburl = config.c.dburl
    dry("Turn on migrate versioning", main, ["version_control", dburl, repository])

@task
def upgrade():
    """Upgrade your database."""
    from bespin import config, model, db_versions
    from migrate.versioning.shell import main
    config.set_profile('dev')
    config.activate_profile()
    repository = str(path(db_versions.__file__).dirname())
    dburl = config.c.dburl
    dry("Run the database upgrade", main, ["upgrade", dburl, repository])

@task
def try_upgrade():
    """Run SQLAlchemy-migrate test on your database."""
    from bespin import config, model, db_versions
    from migrate.versioning.shell import main
    config.set_profile('dev')
    config.activate_profile()
    repository = str(path(db_versions.__file__).dirname())
    dburl = config.c.dburl
    dry("Test the database upgrade", main, ["test", repository, dburl])

def replace_block(f, begin, end, new_content):
    html_lines = f.lines()
    start_marker = None
    end_marker = None
    for i in range(0, len(html_lines)):
        if begin in html_lines[i]:
            start_marker = i
        elif end in html_lines[i]:
            end_marker = i
    del html_lines[start_marker:end_marker+1]
    html_lines.insert(start_marker, new_content)
    f.write_bytes("".join(html_lines))
    

def _install_compressed(html_file, jslayer):
    html_lines = html_file.lines()
    
    # strip out the MPL
    if html_lines[0].startswith("<!--"):
        end_marker = None
        for i in range(0, len(html_lines)):
            if "-->" in html_lines[i]:
                end_marker = i
                break
        del html_lines[0:end_marker+1]
    
    html_file.write_bytes("".join(html_lines))
    
    replace_block(html_file, "<!-- begin script tags -->", "<!-- end script tags -->",
                """
                        <script type="text/javascript" src="js/dojo/dojo.js"></script>
                        <script type="text/javascript" src="js/%s"></script>
""" % jslayer)

    
@task
def copy_front_end():
    build_dir = options.build_dir
    build_dir.mkdir()
    front_end_target = build_dir / "frontend"
    if front_end_target.exists():
        front_end_target.rmtree()
    front_end_source = path("frontend")
    front_end_source.copytree(front_end_target)
    
def update_python_version():
    version_file = path("backend/python/bespin/__init__.py")
    in_version_block = False
    lines = version_file.lines()
    replaced_lines = []
    for i in range(0, len(lines)):
        line = lines[i]
        if "BEGIN VERSION BLOCK" in line:
            in_version_block = True
            continue
        if "END VERSION BLOCK" in line:
            break
        if not in_version_block:
            continue
        if line.startswith("VERSION ="):
            lines[i] = "VERSION = '%s'\n" % (options.version.number)
        elif line.startswith("VERSION_NAME ="):
            lines[i] = 'VERSION_NAME = "%s"\n' % (options.version.name)
        elif line.startswith('API_VERSION'):
            lines[i] = "API_VERSION = '%s'\n" % (options.version.api)
        else:
            raise BuildFailure("Invalid Python version number line: %s" % line)
        replaced_lines.append(line)
    version_file.write_lines(lines)
    return replaced_lines
    
def update_javascript_version():
    version_file = path("frontend") / "js" / "bespin" / "bespin.js"
    in_version_block = False
    lines = version_file.lines()
    replaced_lines = []
    for i in range(0, len(lines)):
        line = lines[i]
        if "BEGIN VERSION BLOCK" in line:
            in_version_block = True
            continue
        if "END VERSION BLOCK" in line:
            break
        if not in_version_block:
            continue
            
        # ignore comment lines
        if "/**" in line:
            pass
        elif "versionNumber:" in line:
            lines[i] = "versionNumber: '%s',\n" % (options.version.number)
        elif 'versionCodename:' in line:
            lines[i] = 'versionCodename: "%s",\n' % (options.version.name)
        elif 'apiVersion:' in line:
            lines[i] = "apiVersion: '%s',\n" % (options.version.api)
        else:
            raise BuildFailure("Invalid JavaScript version number line: %s" % line)
        replaced_lines.append(line)
    version_file.write_lines(lines)
    return replaced_lines
    
def restore_javascript_version(replaced_lines):
    version_file = path("frontend") / "js" / "bespin" / "bespin.js"
    lines = version_file.lines()
    version_block_start = None
    version_block_end = None
    for i in range(0, len(lines)):
        line = lines[i]
        if "BEGIN VERSION BLOCK" in line:
            version_block_start = i
        if "END VERSION BLOCK" in line:
            version_block_end = i
            break
    lines[version_block_start+1:version_block_end] = replaced_lines
    version_file.write_lines(lines)

def restore_python_version(replaced_lines):
    version_file = path("backend/python/bespin/__init__.py")
    lines = version_file.lines()
    version_block_start = None
    version_block_end = None
    for i in range(0, len(lines)):
        line = lines[i]
        if "BEGIN VERSION BLOCK" in line:
            version_block_start = i
        if "END VERSION BLOCK" in line:
            version_block_end = i
            break
    lines[version_block_start+1:version_block_end] = replaced_lines
    version_file.write_lines(lines)

@task
@needs('copy_front_end')
def compress_js():
    """Compress the JavaScript using Dojo's build system."""
    destination = options.dojo.destination.abspath()
    if not (destination / "util").exists():
        raise BuildFailure("You need to be using a Dojo source package. Run paver dojo -s.")

    replaced_lines = update_javascript_version()

    builder_dir = destination / "util/buildscripts"
    profile_file = destination / "buildProfile.js"
    embed_profile = destination / "embedProfile.js"
    release_dir = (options.build_dir / "frontend").abspath()
    embed_release_dir = (options.build_top / "embed" ).abspath()
    cwd = path.getcwd()
    try:
        builder_dir.chdir()
        # sh("sh build.sh action=release profileFile=%s version=%s "
        #     "releaseDir=%s optimize=shrinksafe releaseName=js "
        #     'scopeMap=[[\\"dojo\\",\\"bespindojo\\"],[\\"dijit\\",\\"bespindijit\\"]]' 
        #     % (embed_profile, options.version.number, embed_release_dir))
        sh("sh build.sh action=release profileFile=%s version=%s "
            "releaseDir=%s optimize=shrinksafe releaseName=js" 
            % (profile_file, options.version.number, release_dir))
    finally:
        cwd.chdir()
        restore_javascript_version(replaced_lines)
    
    dojo_js = embed_release_dir / "js" / "dojo/dojo.js"
    # dojo_js.copy(options.build_top / "embed.js")

    front_end_target = options.build_dir / "frontend"
    
    editor_filename = front_end_target / "editor.html"
    _install_compressed(editor_filename, "editor_all.js")
    
    index_filename = front_end_target / "index.html"
    _install_compressed(index_filename, "index_all.js")
    
    final_util_directory = front_end_target / "js" / "util"
    final_util_directory.rmtree()
    
    # put the th file back in
    (path("frontend") / "js" / "th.compressed.js").copy(front_end_target / "js")
        
@task
def prod_server():
    """Creates the production server code."""
    replaced_lines = dry("Updating Python version number", update_python_version)
    sh("bin/pip freeze -r requirements.txt > backend/python/production/requirements.txt")
    try:
        call_pavement("backend/python/pavement.py", "production")
    finally:
        dry("Restoring Python version number", restore_python_version, replaced_lines)

@task
@needs(['prod_server'])
def dist():
    """Generate a tarball that is ready for deployment to the server."""
    options.build_dir.rmtree()
    backend = path("backend/python/production")
    backend.copytree(options.build_dir)
    compress_js()
    docs = path("docs")
    docs.copytree(options.build_dir / "docs")
    
    current_directory = path.getcwd()
    
    options.build_top.chdir()
    tf = tarfile.open("BespinServer.tar.gz", mode="w:gz")
    tf.add("BespinServer")
    tf.close()
    current_directory.chdir()
    
    call_pavement("backend/python/pavement.py", "develop")
    
    info("Output file is in build/BespinServer.tar.gz")

def _apply_header_if_necessary(f, header, first_line, last_line):
    data = f.bytes()
    if data.startswith(header):
        debug("File is already tagged")
        return
    debug("Tagging %s", f)
    if data.startswith(first_line):
        pos = data.find(last_line) + len(last_line)
        data = data[pos:]
    data = header + data
    f.write_bytes(data)

@task
def license():
    """Tags the appropriate files with the license text."""
    cwd = path(".")
    info("Tagging license text")
    for extension, comment_marker in options.extensions:
        hlines = [comment_marker + " " + line for line in HEADER.split("\n")]
        header = "\n".join(hlines) + "\n\n"
        first_line = hlines[0]
        last_line = hlines[-1]
        for f in cwd.walkfiles("*.%s" % extension):
            exclude = False
            for pattern in options.exclude:
                if f.startswith(pattern):
                    exclude=True
                    break
            if exclude:
                continue
            debug("Checking %s", f)
            _apply_header_if_necessary(f, header, first_line, last_line)
    
@task
@cmdopts([('source', 's', "Grab a Dojo source release")])
def dojo(options):
    """Download Dojo and install it to the correct location.
    Provide the -s switch if you will need the Dojo source package
    to either build production-ready packages of Bespin or
    to debug Dojo issues."""
    download_url = options.src_url if options.source else options.download_url
    destfile = path(urlparse(download_url).path)
    ext_dir = path("ext")
    ext_dir.mkdir()
    destfile = ext_dir / destfile.basename()
    if not destfile.exists():
        info("Downloading Dojo to " + destfile)
        datafile = urllib2.urlopen(download_url)
        output_file = open(destfile, "wb")
        output_file.write(datafile.read())
        output_file.close()
        datafile.close()

    info("Expanding Dojo")
    destination = options.destination
    dojo = destination / "dojo"
    dijit = destination / "dijit"
    dojox = destination / "dojox"
    util = destination / "util"
    
    dojo.rmtree()
    dijit.rmtree()
    dojox.rmtree()
    util.rmtree()
    
    dojotar = tarfile.open(destfile)
    i = 1
    for member in dojotar.getmembers():
        name = member.name
        if member.type == tarfile.DIRTYPE:
            continue
        dropped_root = name.split('/')[1:]
        
        destpath = destination.joinpath(*dropped_root)
        destdir = destpath.dirname()
        if not destdir.exists():
            destdir.makedirs()
        f = dojotar.extractfile(member)
        destpath.write_bytes(f.read())
        f.close()

@task
def testfrontend():
    """Run the frontend tests via DOH"""
    basedir = options.dojo.destination.abspath()
    if not (basedir / "util").exists():
        raise BuildFailure("You need to be using a Dojo source package. Run paver dojo -s.")

    # copy the fixed runner script into place in dojo. Shouldn't do this each time :/
    fixedrunner = basedir / ".." / "tests" / "unit" / "runner.js"
    fixedrunner.copy(basedir / "util" / "doh" / "runner.js")

    sh("java -jar frontend/js/util/shrinksafe/js.jar frontend/js/util/doh/runner.js dojoUrl=frontend/js/dojo/dojo.js testUrl=frontend/tests/unit/loadfiles.js testModule=tests.unit.loadfiles dohBase=frontend/js/util/doh/")


"""
Cannot be used because jsparse needed to be slightly patched
@task
@cmdopts([('source', 's', "Grab JSParser from trunk :)")])
def jsparser(options):
    download_urls = options.jsparser.download_urls
    
    for download_url in download_urls:
        destfile = path(urlparse(download_url).path)
        destdir  = path("frontend/js/jsparse")
        destfile = destdir / destfile.basename()
        if not destdir.exists():
            destdir.makedirs()
            
        if not destfile.exists():
            info("Downloading JSParser to " + destfile)
            datafile = urllib2.urlopen(download_url)
            output_file = open(destfile, "w")
            output_file.write(datafile.read())
            output_file.close()
            datafile.close()"""
            

@task
@cmdopts([('user=', 'u', 'User to set up for Bespin editing'),
          ('file=', 'f', 'Passwordless SSH private key file')])
def installkey(options):
    """Install an SSH key into your Bespin keychain. This will
    prompt you for your Bespin keychain password. You must
    give a Bespin username with -u. You must also specify
    a passwordless SSH private key file with -f."""
    
    if not 'installkey' in options or not options.installkey.user:
        raise BuildFailure("You must specify a user with -u for this task.")
    
    if not options.installkey.file:
        raise BuildFailure("You must specify a private key with with -f")
    
    private_key = path(options.installkey.file)
    public_key = private_key + ".pub"
    
    if not private_key.exists() or not public_key.exists():
        raise BuildFailure("Either your private key file or public key file does not exist")
        
    
        
    user = options.installkey.user
    
    import getpass
    
    password = getpass.getpass("Enter your keychain password: ")
    
    from bespin import config
    from bespin import database, filesystem, vcs
    from sqlalchemy.orm.exc import NoResultFound
    
    config.set_profile("dev")
    config.activate_profile()
    session = config.c.session_factory()
    try:
        user = session.query(database.User).filter_by(username=user).one()
    except NoResultFound:
        raise BuildFailure("I couldn't find %s in the database. Sorry!" % (user))
    
    keychain = vcs.KeyChain(user, password)
    public_key = public_key.bytes()
    private_key = private_key.bytes()
    keychain.set_ssh_key(private_key, public_key)


@task
@cmdopts([('user=', 'u', 'User to set up for Bespin editing')])
def editbespin(options):
    """Use Bespin to edit Bespin. This will change the given
    user's file location to the directory above Bespin, allowing
    you to edit Bespin (and any other projects you have
    in that directory)."""
    
    if not 'editbespin' in options or not options.editbespin.user:
        raise BuildFailure("You must specify a user with -u for this task.")
        
    user = options.editbespin.user
    
    from bespin import config
    from bespin import database, filesystem
    from sqlalchemy.orm.exc import NoResultFound
    
    config.set_profile("dev")
    config.activate_profile()
    session = config.c.session_factory()
    try:
        user = session.query(database.User).filter_by(username=user).one()
    except NoResultFound:
        raise BuildFailure("I couldn't find %s in the database. Sorry!" % (user))
    
    location = path.getcwd().parent.abspath()
    user.file_location = location
    user.recompute_files()
    session.commit()
    bespinsettings_loc = location / "BespinSettings"
    if not bespinsettings_loc.exists():
        project = filesystem.get_project(user, user, "BespinSettings", create=True)
        project.install_template('usertemplate')
    info("User %s set up to access directory %s" % (user, location))

@task
def test():
    #import nose
    #nose.main("bespin.tests")
    from os import system
    system("nosetests backend/python/bespin")

@task
def mobwrite():
    from bespin.mobwrite import mobwrite_daemon
    mobwrite_daemon.main()

@task
def seeddb():
    from bespin import config, filesystem
    from bespin.database import User, Connection
    config.set_profile("dev")
    config.activate_profile()

    def get_user(name):
        user = User.find_user(name)
        if user == None:
            user = User.create_user(name, name, name + "@foo.com")
            session.commit()
            info("Created user called '" + name + "'")
        try:
            filesystem.get_project(user, user, "BespinSettings")
        except:
            settings = filesystem.get_project(user, user, "BespinSettings", create=True)
            settings.install_template('usertemplate')
            info("Created BespinSettings project for '" + name + "'")
        return user

    # Seriously there is something wrong with my ego today ;-)
    bgalbraith = get_user("bgalbraith")
    kdangoor = get_user("kdangoor")
    dalmaer = get_user("d")
    mattb = get_user("mattb")
    zuck = get_user("zuck")
    tom = get_user("tom")
    ev = get_user("ev")
    j = get_user("j")

    bgalbraith.follow(j)
    kdangoor.follow(j)
    dalmaer.follow(j)
    mattb.follow(j)
    zuck.follow(j)
    tom.follow(j)
    ev.follow(j)

    jproject = filesystem.get_project(j, j, "SampleProject", create=True)

    j.add_sharing(jproject, bgalbraith, edit=True)
    j.add_sharing(jproject, kdangoor, edit=True)
    j.add_sharing(jproject, dalmaer, edit=True)
    j.add_sharing(jproject, mattb, edit=False)
    j.add_sharing(jproject, zuck, edit=False)
    j.add_sharing(jproject, tom, edit=False)
    j.add_sharing(jproject, ev, edit=False)

@task
def generate_vcs():
    """Generates the JavaScript for the native VCS commands. Uses the information
    directly from the installed version of UVC. The output is displayed on the 
    screen for cutting and pasting into the proper place."""
    from uvc import svn
    for command in svn.get_native_commands():
        output = ""
        desc = command.parser.description
        short_description = desc[:desc.find(".")]
        short_description = short_description.replace("\n", "")
        
        # take each line, wrap it in quotes (with a \n) and concatenate with +
        long_description = "\n".join('        "%s\\n" +' % line for line in command.parser.format_help().split("\n"))
        
        # remove the trailing +
        long_description = long_description[:-2]
        
        aliases = "" if not command.aliases else "\n    aliases: %r," % (command.aliases,)
        
        keychain = ""
        
        # svn copy has crazy rules for prompting
        # WC -> WC  - no prompting
        # URL -> WC - keychain
        # WC -> URL - keychain and message
        # URL -> URL - keychain and message
        if command.__name__ == "SVN_copy":
            prompting = """\n    prompting: function(command) {
        var dst = command[command.length - 1];
        var lastsrc = command[command.length - 2];
        
        // if dst is a URL, that means we need keychain and message
        if (dst.indexOf("://")) {
            return {getKeychain: true, getMessage: true};
        }
        
        // if we have a URL source, we do need to prompt for keychain.
        if (lastsrc.indexOf("://")) {
            return {getKeychain: true};
        }
        
        return null;
    },"""
        elif command.__name__ == "SVN_commit":
            prompting = """\n    prompting: function(command) {
        if (bespin.util.include(command, "-m")) {
            return {getKeychain: true};
        }
        return {getKeychain: true, getMessage: true};
    },"""
        elif "help" in command.__name__:
            # use the help that's defined in Bespin
            continue
        else:
            prompting = ""
            keychain = "" if not command.reads_remote and not command.writes_remote \
                    else "\n    keychain: true,"
                
        output += """
bespin.vcs.svnCommands.addCommand({
    name: '%s',
    takes: ['*'],%s%s%s
    preview: '%s',
    description: %s,
    execute: bespin.vcs.svnCommands.genericExecute
});

""" % (command.name(), aliases, keychain, prompting, short_description, long_description)
        print output