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