Transitioned comment to Command format
[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     >>> cmd = Init()
34     >>> cmd._setup_io = lambda i_enc,o_enc : None
35     >>> cmd.stdout = sys.stdout
36
37     >>> dir = libbe.util.utility.Dir()
38     >>> vcs = libbe.storage.vcs.vcs_by_name('None')
39     >>> vcs.repo = dir.path
40     >>> try:
41     ...     vcs.connect()
42     ... except libbe.storage.ConnectionError:
43     ...     True
44     True
45     >>> cmd.run(vcs)
46     No revision control detected.
47     BE repository initialized.
48     >>> bd = libbe.bugdir.BugDir(vcs)
49     >>> vcs.disconnect()
50     >>> vcs.destroy()
51     >>> dir.cleanup()
52
53     >>> dir = libbe.util.utility.Dir()
54     >>> vcs = libbe.storage.vcs.installed_vcs()
55     >>> vcs.repo = dir.path
56     >>> vcs._vcs_init(vcs.repo)
57     >>> if vcs.name in libbe.storage.vcs.base.VCS_ORDER:
58     ...     cmd.run(vcs) # doctest: +ELLIPSIS
59     ... else:
60     ...     vcs.init()
61     ...     vcs.connect()
62     ...     print 'Using ... for revision control.\\nDirectory initialized.'
63     Using ... for revision control.
64     BE repository initialized.
65     >>> vcs.disconnect()
66     >>> vcs.destroy()
67     >>> dir.cleanup()
68     """
69     name = 'init'
70
71     def __init__(self, *args, **kwargs):
72         libbe.command.Command.__init__(self, *args, **kwargs)
73         self.requires_unconnected_storage = True
74
75     def _run(self, storage, bugdir=None, **params):
76         if not os.path.isdir(storage.repo):
77             raise libbe.command.UserError(
78                 'No such directory: %s' % storage.repo)
79         try:
80             storage.connect()
81             raise libbe.command.UserError(
82                 'Directory already initialized: %s' % storage.repo)
83         except libbe.storage.ConnectionError:
84             pass
85         storage.init()
86         storage.connect()
87         bd = libbe.bugdir.BugDir(storage, from_storage=False)
88         bd.save()
89         if bd.storage.name is not 'None':
90             print >> self.stdout, \
91                 'Using %s for revision control.' % storage.name
92         else:
93             print >> self.stdout, 'No revision control detected.'
94         print >> self.stdout, 'BE repository initialized.'
95
96     def _long_help(self):
97         return """
98 This command initializes Bugs Everywhere support for the specified directory
99 and all its subdirectories.  It will auto-detect any supported revision control
100 system.  You can use "be set vcs_name" to change the vcs being used.
101
102 The directory defaults to your current working directory, but you can
103 change that by passing the --repo option to be
104   $ be --repo path/to/new/bug/root init
105
106 When initialized in a version-controlled directory, BE sinks to the
107 version-control root.  In that case, the BE repository will be created
108 under that directory, rather than the current directory or the one
109 passed in --repo.  Consider the following tree, versioned in Git.
110   ~
111   `--projectX
112      |-- .git
113      `-- src
114 Calling
115   ~$ be --repo ./projectX/src init
116 will create the BE repository rooted in projectX:
117   ~
118   `--projectX
119      |-- .be
120      |-- .git
121      `-- src
122 """