~bzr-upload-devs/bzr-upload/trunk

« back to all changes in this revision

Viewing changes to cmds.py

  • Committer: Vincent Ladeuil
  • Date: 2012-03-15 20:53:45 UTC
  • Revision ID: v.ladeuil+lp@free.fr-20120315205345-6ilssa714tfcrntk
Migrate to config stacks

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from bzrlib import (
20
20
    branch,
21
21
    commands,
 
22
    config,
22
23
    lazy_import,
23
24
    option,
24
25
    )
41
42
    )
42
43
""")
43
44
 
44
 
def _get_branch_option(branch, option):
45
 
    return branch.get_config().get_user_option(option)
46
 
 
47
 
# FIXME: Get rid of that as soon as we depend on a bzr API that includes
48
 
# get_user_option_as_bool
49
 
def _get_branch_bool_option(branch, option):
50
 
    conf = branch.get_config()
51
 
    if hasattr(conf, 'get_user_option_as_bool'):
52
 
        value = conf.get_user_option_as_bool(option)
53
 
    else:
54
 
        value = conf.get_user_option(option)
55
 
        if value is not None:
56
 
            if value.lower().strip() == 'true':
57
 
                value = True
58
 
            else:
59
 
                value = False
60
 
    return value
61
 
 
62
 
def _set_branch_option(branch, option, value):
63
 
    branch.get_config().set_user_option(option, value)
64
 
 
65
 
 
66
 
def get_upload_location(branch):
67
 
    return _get_branch_option(branch, 'upload_location')
68
 
 
69
 
 
70
 
def set_upload_location(branch, location):
71
 
    _set_branch_option(branch, 'upload_location', location)
72
 
 
73
 
 
74
 
# FIXME: Add more tests around invalid paths used here or relative paths that
75
 
# doesn't exist on remote (if only to get proper error messages)
76
 
def get_upload_revid_location(branch):
77
 
    loc =  _get_branch_option(branch, 'upload_revid_location')
78
 
    if loc is None:
79
 
        loc = '.bzr-upload.revid'
80
 
    return loc
81
 
 
82
 
 
83
 
def set_upload_revid_location(branch, location):
84
 
    _set_branch_option(branch, 'upload_revid_location', location)
85
 
 
86
 
 
87
 
def get_upload_auto(branch):
88
 
    auto = _get_branch_bool_option(branch, 'upload_auto')
89
 
    if auto is None:
90
 
        auto = False # Default to False if not specified
91
 
    return auto
92
 
 
93
 
 
94
 
def set_upload_auto(branch, auto):
95
 
    # FIXME: What's the point in allowing a boolean here instead of requiring
96
 
    # the callers to use strings instead ?
97
 
    if auto:
98
 
        auto_str = "True"
99
 
    else:
100
 
        auto_str = "False"
101
 
    _set_branch_option(branch, 'upload_auto', auto_str)
102
 
 
103
 
 
104
 
def get_upload_auto_quiet(branch):
105
 
    quiet = _get_branch_bool_option(branch, 'upload_auto_quiet')
106
 
    if quiet is None:
107
 
        quiet = False # Default to False if not specified
108
 
    return quiet
109
 
 
110
 
 
111
 
def set_upload_auto_quiet(branch, quiet):
112
 
    _set_branch_option(branch, 'upload_auto_quiet', quiet)
 
45
auto_option = config.Option(
 
46
    'upload_auto', default=False, from_unicode=config.bool_from_store,
 
47
    help="""\
 
48
Whether upload should occur when the tip of the branch changes.
 
49
""")
 
50
auto_quiet_option = config.Option(
 
51
    'upload_auto_quiet', default=False, from_unicode=config.bool_from_store,
 
52
    help="""\
 
53
Whether upload should occur quietly.
 
54
""")
 
55
location_option = config.Option(
 
56
    'upload_location', default=None,
 
57
    help="""\
 
58
The url to upload the working tree to.
 
59
""")
 
60
revid_location_option = config.Option(
 
61
    'upload_revid_location', default=u'.bzr-upload.revid',
 
62
    help="""\
 
63
The relative path to be used to store the uploaded revid.
 
64
 
 
65
The only bzr-related info uploaded with the working tree is the corresponding
 
66
revision id. The uploaded working tree is not linked to any other bzr data.
 
67
 
 
68
If the layout of your remote server is such that you can't write in the
 
69
root directory but only in the directories inside that root, you will need
 
70
to use the 'upload_revid_location' configuration variable to specify the
 
71
relative path to be used. That configuration variable can be specified in
 
72
locations.conf or branch.conf.
 
73
 
 
74
For example, given the following layout:
 
75
 
 
76
  Project/
 
77
    private/
 
78
    public/
 
79
 
 
80
you may have write access in 'private' and 'public' but in 'Project'
 
81
itself. In that case, you can add the following in your locations.conf or
 
82
branch.conf file:
 
83
 
 
84
  upload_revid_location = private/.bzr-upload.revid
 
85
""")
 
86
 
 
87
 
 
88
# FIXME: Add more tests around invalid paths or relative paths that doesn't
 
89
# exist on remote (if only to get proper error messages) for
 
90
# 'upload_revid_location'
113
91
 
114
92
 
115
93
class BzrUploader(object):
154
132
 
155
133
    def set_uploaded_revid(self, rev_id):
156
134
        # XXX: Add tests for concurrent updates, etc.
157
 
        revid_path = get_upload_revid_location(self.branch)
 
135
        revid_path = self.branch.get_config_stack().get('upload_revid_location')
158
136
        self.to_transport.put_bytes(urlutils.escape(revid_path), rev_id)
159
137
        self._uploaded_revid = rev_id
160
138
 
161
139
    def get_uploaded_revid(self):
162
140
        if self._uploaded_revid is None:
163
 
            revid_path = get_upload_revid_location(self.branch)
 
141
            revid_path = self.branch.get_config_stack(
 
142
                ).get('upload_revid_location')
164
143
            try:
165
144
                self._uploaded_revid = self._up_get_bytes(revid_path)
166
145
            except errors.NoSuchFile:
167
 
                # We have not upload to here.
 
146
                # We have not uploaded to here.
168
147
                self._uploaded_revid = revision.NULL_REVISION
169
148
        return self._uploaded_revid
170
149
 
512
491
                if revision is None and  changes.has_changed():
513
492
                    raise errors.UncommittedChanges(wt)
514
493
 
 
494
            conf = branch.get_config_stack()
515
495
            if location is None:
516
 
                stored_loc = get_upload_location(branch)
 
496
                stored_loc = conf.get('upload_location')
517
497
                if stored_loc is None:
518
498
                    raise errors.BzrCommandError(
519
499
                        'No upload location known or specified.')
565
545
            locked.unlock()
566
546
 
567
547
        # We uploaded successfully, remember it
568
 
        if get_upload_location(branch) is None or remember:
569
 
            set_upload_location(branch, urlutils.unescape(to_transport.base))
570
 
        if auto is not None:
571
 
            set_upload_auto(branch, auto)
 
548
        branch.lock_write()
 
549
        try:
 
550
            upload_location = conf.get('upload_location')
 
551
            if upload_location is None or remember:
 
552
                conf.set('upload_location',
 
553
                         urlutils.unescape(to_transport.base))
 
554
            if auto is not None:
 
555
                conf.set('upload_auto', auto)
 
556
        finally:
 
557
            branch.unlock()
 
558