Bumped to version 1.0.1
[be.git] / libbe / command / init.py
1 # Copyright (C) 2005-2011 Aaron Bentley <abentley@panoramicfeedback.com>
2 #                         Chris Ball <cjb@laptop.org>
3 #                         Gianluca Montecchi <gian@grys.it>
4 #                         W. Trevor King <wking@drexel.edu>
5 #
6 # This file is part of Bugs Everywhere.
7 #
8 # Bugs Everywhere is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by the
10 # Free Software Foundation, either version 2 of the License, or (at your
11 # option) any later version.
12 #
13 # Bugs Everywhere is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Bugs Everywhere.  If not, see <http://www.gnu.org/licenses/>.
20
21 import os.path
22
23 import libbe
24 import libbe.bugdir
25 import libbe.command
26 import libbe.storage
27
28 class Init (libbe.command.Command):
29     """Create an on-disk bug repository
30
31     >>> import os, sys
32     >>> import libbe.storage.vcs
33     >>> import libbe.storage.vcs.base
34     >>> import libbe.util.utility
35     >>> io = libbe.command.StringInputOutput()
36     >>> io.stdout = sys.stdout
37     >>> ui = libbe.command.UserInterface(io=io)
38     >>> cmd = Init()
39
40     >>> dir = libbe.util.utility.Dir()
41     >>> vcs = libbe.storage.vcs.vcs_by_name('None')
42     >>> vcs.repo = dir.path
43     >>> try:
44     ...     vcs.connect()
45     ... except libbe.storage.ConnectionError:
46     ...     'got error'
47     'got error'
48     >>> ui.storage_callbacks.set_unconnected_storage(vcs)
49     >>> ui.run(cmd)
50     No revision control detected.
51     BE repository initialized.
52     >>> bd = libbe.bugdir.BugDir(vcs)
53     >>> vcs.disconnect()
54     >>> vcs.connect()
55     >>> bugdir = libbe.bugdir.BugDir(vcs, from_storage=True)
56     >>> vcs.disconnect()
57     >>> vcs.destroy()
58     >>> dir.cleanup()
59
60     >>> dir = libbe.util.utility.Dir()
61     >>> vcs = libbe.storage.vcs.installed_vcs()
62     >>> vcs.repo = dir.path
63     >>> vcs._vcs_init(vcs.repo)
64     >>> ui.storage_callbacks.set_unconnected_storage(vcs)
65     >>> if vcs.name in libbe.storage.vcs.base.VCS_ORDER:
66     ...     ui.run(cmd) # doctest: +ELLIPSIS
67     ... else:
68     ...     vcs.init()
69     ...     vcs.connect()
70     ...     print 'Using ... for revision control.\\nDirectory initialized.'
71     Using ... for revision control.
72     BE repository initialized.
73     >>> vcs.disconnect()
74     >>> vcs.connect()
75     >>> bugdir = libbe.bugdir.BugDir(vcs, from_storage=True)
76     >>> vcs.disconnect()
77     >>> vcs.destroy()
78     >>> dir.cleanup()
79     """
80     name = 'init'
81
82     def __init__(self, *args, **kwargs):
83         libbe.command.Command.__init__(self, *args, **kwargs)
84
85     def _run(self, **params):
86         storage = self._get_unconnected_storage()
87         if not os.path.isdir(storage.repo):
88             raise libbe.command.UserError(
89                 'No such directory: %s' % storage.repo)
90         try:
91             storage.connect()
92             raise libbe.command.UserError(
93                 'Directory already initialized: %s' % storage.repo)
94         except libbe.storage.ConnectionError:
95             pass
96         storage.init()
97         storage.connect()
98         self.ui.storage_callbacks.set_storage(storage)
99         bd = libbe.bugdir.BugDir(storage, from_storage=False)
100         self.ui.storage_callbacks.set_bugdir(bd)
101         if bd.storage.name is not 'None':
102             print >> self.stdout, \
103                 'Using %s for revision control.' % storage.name
104         else:
105             print >> self.stdout, 'No revision control detected.'
106         print >> self.stdout, 'BE repository initialized.'
107
108     def _long_help(self):
109         return """
110 This command initializes Bugs Everywhere support for the specified directory
111 and all its subdirectories.  It will auto-detect any supported revision control
112 system.  You can use "be set vcs_name" to change the vcs being used.
113
114 The directory defaults to your current working directory, but you can
115 change that by passing the --repo option to be
116   $ be --repo path/to/new/bug/root init
117
118 When initialized in a version-controlled directory, BE sinks to the
119 version-control root.  In that case, the BE repository will be created
120 under that directory, rather than the current directory or the one
121 passed in --repo.  Consider the following tree, versioned in Git.
122   ~
123   `--projectX
124      |-- .git
125      `-- src
126 Calling
127   ~$ be --repo ./projectX/src init
128 will create the BE repository rooted in projectX:
129   ~
130   `--projectX
131      |-- .be
132      |-- .git
133      `-- src
134 """