~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

(jelmer) Use the absolute_import feature everywhere in bzrlib,
 and add a source test to make sure it's used everywhere. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""builtin bzr commands"""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
import os
20
22
 
21
23
import bzrlib.bzrdir
23
25
from bzrlib.lazy_import import lazy_import
24
26
lazy_import(globals(), """
25
27
import cStringIO
 
28
import errno
26
29
import sys
27
30
import time
28
31
 
753
756
    """
754
757
 
755
758
    takes_args = ['dir+']
 
759
    takes_options = [
 
760
        Option(
 
761
            'parents',
 
762
            help='No error if existing, make parent directories as needed.',
 
763
            short_name='p'
 
764
            )
 
765
        ]
756
766
    encoding_type = 'replace'
757
767
 
758
 
    def run(self, dir_list):
759
 
        for d in dir_list:
760
 
            wt, dd = WorkingTree.open_containing(d)
761
 
            base = os.path.dirname(dd)
762
 
            id = wt.path2id(base)
763
 
            if id != None:
764
 
                os.mkdir(d)
765
 
                wt.add([dd])
766
 
                if not is_quiet():
767
 
                    self.outf.write(gettext('added %s\n') % d)
 
768
    @classmethod
 
769
    def add_file_with_parents(cls, wt, relpath):
 
770
        if wt.path2id(relpath) is not None:
 
771
            return
 
772
        cls.add_file_with_parents(wt, osutils.dirname(relpath))
 
773
        wt.add([relpath])
 
774
 
 
775
    @classmethod
 
776
    def add_file_single(cls, wt, relpath):
 
777
        wt.add([relpath])
 
778
 
 
779
    def run(self, dir_list, parents=False):
 
780
        if parents:
 
781
            add_file = self.add_file_with_parents
 
782
        else:
 
783
            add_file = self.add_file_single
 
784
        for dir in dir_list:
 
785
            wt, relpath = WorkingTree.open_containing(dir)
 
786
            if parents:
 
787
                try:
 
788
                    os.makedirs(dir)
 
789
                except OSError, e:
 
790
                    if e.errno != errno.EEXIST:
 
791
                        raise
768
792
            else:
769
 
                raise errors.NotVersionedError(path=base)
 
793
                os.mkdir(dir)
 
794
            add_file(wt, relpath)
 
795
            if not is_quiet():
 
796
                self.outf.write(gettext('added %s\n') % dir)
770
797
 
771
798
 
772
799
class cmd_relpath(Command):
3148
3175
        Option('per-file-timestamps',
3149
3176
               help='Set modification time of files to that of the last '
3150
3177
                    'revision in which it was changed.'),
 
3178
        Option('uncommitted',
 
3179
               help='Export the working tree contents rather than that of the '
 
3180
                    'last revision.'),
3151
3181
        ]
3152
3182
    def run(self, dest, branch_or_subdir=None, revision=None, format=None,
3153
 
        root=None, filters=False, per_file_timestamps=False, directory=u'.'):
 
3183
        root=None, filters=False, per_file_timestamps=False, uncommitted=False,
 
3184
        directory=u'.'):
3154
3185
        from bzrlib.export import export
3155
3186
 
3156
3187
        if branch_or_subdir is None:
3157
 
            tree = WorkingTree.open_containing(directory)[0]
3158
 
            b = tree.branch
3159
 
            subdir = None
 
3188
            branch_or_subdir = directory
 
3189
 
 
3190
        (tree, b, subdir) = controldir.ControlDir.open_containing_tree_or_branch(
 
3191
            branch_or_subdir)
 
3192
        if tree is not None:
 
3193
            self.add_cleanup(tree.lock_read().unlock)
 
3194
 
 
3195
        if uncommitted:
 
3196
            if tree is None:
 
3197
                raise errors.BzrCommandError(
 
3198
                    gettext("--uncommitted requires a working tree"))
 
3199
            export_tree = tree
3160
3200
        else:
3161
 
            b, subdir = Branch.open_containing(branch_or_subdir)
3162
 
            tree = None
3163
 
 
3164
 
        rev_tree = _get_one_revision_tree('export', revision, branch=b, tree=tree)
 
3201
            export_tree = _get_one_revision_tree('export', revision, branch=b, tree=tree)
3165
3202
        try:
3166
 
            export(rev_tree, dest, format, root, subdir, filtered=filters,
 
3203
            export(export_tree, dest, format, root, subdir, filtered=filters,
3167
3204
                   per_file_timestamps=per_file_timestamps)
3168
3205
        except errors.NoSuchExportFormat, e:
3169
 
            raise errors.BzrCommandError(gettext('Unsupported export format: %s') % e.format)
 
3206
            raise errors.BzrCommandError(
 
3207
                gettext('Unsupported export format: %s') % e.format)
3170
3208
 
3171
3209
 
3172
3210
class cmd_cat(Command):
3675
3713
            if directory is None:
3676
3714
                # use branch if we're inside one; otherwise global config
3677
3715
                try:
3678
 
                    c = Branch.open_containing(u'.')[0].get_config()
 
3716
                    c = Branch.open_containing(u'.')[0].get_config_stack()
3679
3717
                except errors.NotBranchError:
3680
 
                    c = _mod_config.GlobalConfig()
 
3718
                    c = _mod_config.GlobalStack()
3681
3719
            else:
3682
 
                c = Branch.open(directory).get_config()
 
3720
                c = Branch.open(directory).get_config_stack()
 
3721
            identity = c.get('email')
3683
3722
            if email:
3684
 
                self.outf.write(c.user_email() + '\n')
 
3723
                self.outf.write(_mod_config.extract_email_address(identity)
 
3724
                                + '\n')
3685
3725
            else:
3686
 
                self.outf.write(c.username() + '\n')
 
3726
                self.outf.write(identity + '\n')
3687
3727
            return
3688
3728
 
3689
3729
        if email:
3700
3740
        # use global config unless --branch given
3701
3741
        if branch:
3702
3742
            if directory is None:
3703
 
                c = Branch.open_containing(u'.')[0].get_config()
 
3743
                c = Branch.open_containing(u'.')[0].get_config_stack()
3704
3744
            else:
3705
 
                c = Branch.open(directory).get_config()
 
3745
                c = Branch.open(directory).get_config_stack()
3706
3746
        else:
3707
 
            c = _mod_config.GlobalConfig()
3708
 
        c.set_user_option('email', name)
 
3747
            c = _mod_config.GlobalStack()
 
3748
        c.set('email', name)
3709
3749
 
3710
3750
 
3711
3751
class cmd_nick(Command):
5019
5059
 
5020
5060
    def _run(self, b, revision_id_list, revision):
5021
5061
        import bzrlib.gpg as gpg
5022
 
        gpg_strategy = gpg.GPGStrategy(b.get_config())
 
5062
        gpg_strategy = gpg.GPGStrategy(b.get_config_stack())
5023
5063
        if revision_id_list is not None:
5024
5064
            b.repository.start_write_group()
5025
5065
            try:
6539
6579
    takes_options = [Option('plugin', 
6540
6580
                            help='Export help text from named command '\
6541
6581
                                 '(defaults to all built in commands).',
6542
 
                            type=str)]
 
6582
                            type=str),
 
6583
                     Option('include-duplicates',
 
6584
                            help='Output multiple copies of the same msgid '
 
6585
                                 'string if it appears more than once.'),
 
6586
                            ]
6543
6587
 
6544
 
    def run(self, plugin=None):
 
6588
    def run(self, plugin=None, include_duplicates=False):
6545
6589
        from bzrlib.export_pot import export_pot
6546
 
        export_pot(self.outf, plugin)
 
6590
        export_pot(self.outf, plugin, include_duplicates)
6547
6591
 
6548
6592
 
6549
6593
def _register_lazy_builtins():