~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2006-03-01 15:45:14 UTC
  • mto: (147.4.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: abentley@panoramicfeedback.com-20060301154514-265911f7d80255f9
Reverted bzrtools.py to mainline version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#    You should have received a copy of the GNU General Public License
15
15
#    along with this program; if not, write to the Free Software
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
import bzrlib
18
 
import bzrlib.errors
 
17
import codecs
 
18
import errno
19
19
import os
20
 
import os.path
21
 
import sys
 
20
import re
22
21
import tempfile
23
22
import shutil
24
 
import errno
25
23
from subprocess import Popen, PIPE
26
 
import codecs
27
 
import re
28
 
 
29
 
def temp_branch():
 
24
import sys
 
25
 
 
26
import bzrlib
 
27
import bzrlib.errors
 
28
from bzrlib.bzrdir import BzrDir
 
29
 
 
30
def temp_tree():
30
31
    dirname = tempfile.mkdtemp("temp-branch")
31
 
    return bzrlib.branch.Branch.initialize(dirname)
32
 
 
33
 
def rm_branch(br):
34
 
    shutil.rmtree(br.base)
35
 
 
36
 
def is_clean(cur_branch):
 
32
    return BzrDir.create_standalone_workingtree(dirname)
 
33
 
 
34
def rm_tree(tree):
 
35
    shutil.rmtree(tree.basedir)
 
36
 
 
37
def is_clean(cur_tree):
37
38
    """
38
39
    Return true if no files are modifed or unknown
39
40
    >>> import bzrlib.add
40
 
    >>> br = temp_branch()
41
 
    >>> is_clean(br)
 
41
    >>> tree = temp_tree()
 
42
    >>> is_clean(tree)
42
43
    (True, [])
43
 
    >>> fooname = os.path.join(br.base, "foo")
 
44
    >>> fooname = os.path.join(tree.basedir, "foo")
44
45
    >>> file(fooname, "wb").write("bar")
45
 
    >>> is_clean(br)
 
46
    >>> is_clean(tree)
46
47
    (True, [u'foo'])
47
 
    >>> br.working_tree().add(["foo"])
48
 
    >>> is_clean(br)
 
48
    >>> bzrlib.add.smart_add_tree(tree, [tree.basedir])
 
49
    ([u'foo'], {})
 
50
    >>> is_clean(tree)
49
51
    (False, [])
50
 
    >>> br.working_tree().commit("added file")
51
 
    >>> is_clean(br)
 
52
    >>> tree.commit("added file")
 
53
    >>> is_clean(tree)
52
54
    (True, [])
53
 
    >>> rm_branch(br)
 
55
    >>> rm_tree(tree)
54
56
    """
55
57
    from bzrlib.diff import compare_trees
56
 
    old_tree = cur_branch.basis_tree()
57
 
    new_tree = cur_branch.working_tree()
 
58
    old_tree = cur_tree.basis_tree()
 
59
    new_tree = cur_tree
58
60
    non_source = []
59
61
    for path, file_class, kind, file_id, entry in new_tree.list_files():
60
62
        if file_class in ('?', 'I'):
62
64
    delta = compare_trees(old_tree, new_tree, want_unchanged=False)
63
65
    return not delta.has_changed(), non_source
64
66
 
65
 
def set_push_data(br, location):
66
 
    push_file = file (br.control_files.controlfilename("x-push-data"), "wb")
 
67
def set_push_data(tree, location):
 
68
    push_file = file (tree._control_files.controlfilename("x-push-data"), "wb")
67
69
    push_file.write("%s\n" % location)
68
70
 
69
 
def get_push_data(br):
 
71
def get_push_data(tree):
70
72
    """
71
 
    >>> br = temp_branch()
72
 
    >>> get_push_data(br) is None
 
73
    >>> tree = temp_tree()
 
74
    >>> get_push_data(tree) is None
73
75
    True
74
 
    >>> set_push_data(br, 'http://somewhere')
75
 
    >>> get_push_data(br)
 
76
    >>> set_push_data(tree, 'http://somewhere')
 
77
    >>> get_push_data(tree)
76
78
    'http://somewhere'
77
 
    >>> rm_branch(br)
 
79
    >>> rm_tree(tree)
78
80
    """
79
 
    filename = br.control_files.controlfilename("x-push-data")
 
81
    filename = tree._control_files.controlfilename("x-push-data")
80
82
    if not os.path.exists(filename):
81
83
        return None
82
84
    push_file = file (filename, "rb")
112
114
def rsync(source, target, ssh=False, excludes=(), silent=False, 
113
115
          rsync_name="rsync"):
114
116
    """
 
117
    >>> new_dir = tempfile.mkdtemp()
 
118
    >>> old_dir = os.getcwd()
 
119
    >>> os.chdir(new_dir)
115
120
    >>> rsync("a", "b", silent=True)
116
121
    Traceback (most recent call last):
117
122
    RsyncNoFile: No such file a
121
126
    >>> rsync("a", "b", excludes=("*.py",), silent=True, rsync_name="rsyncc")
122
127
    Traceback (most recent call last):
123
128
    NoRsync: rsyncc not found.
 
129
    >>> os.chdir(old_dir)
 
130
    >>> os.rmdir(new_dir)
124
131
    """
125
132
    cmd = [rsync_name, "-av", "--delete"]
126
133
    if ssh:
226
233
    except RsyncNoFile:
227
234
        return True
228
235
 
229
 
def push(cur_branch, location=None, overwrite=False, working_tree=True):
230
 
    push_location = get_push_data(cur_branch)
 
236
def push(tree, location=None, overwrite=False, working_tree=True):
 
237
    push_location = get_push_data(tree)
231
238
    if location is not None:
232
239
        if not location.endswith('/'):
233
240
            location += '/'
242
249
    if push_location.find(':') == -1:
243
250
        raise bzrlib.errors.MustUseDecorated
244
251
 
245
 
    clean, non_source = is_clean(cur_branch)
 
252
    clean, non_source = is_clean(tree)
246
253
    if not clean:
247
254
        print """Error: This tree has uncommitted changes or unknown (?) files.
248
255
Use "bzr status" to list them."""
250
257
    if working_tree:
251
258
        final_exclusions = non_source[:]
252
259
    else:
253
 
        wt = cur_branch.working_tree()
 
260
        wt = tree
254
261
        final_exclusions = []
255
262
        for path, status, kind, file_id, entry in wt.list_files():
256
263
            final_exclusions.append(path)
258
265
    final_exclusions.extend(exclusions)
259
266
    if not overwrite:
260
267
        try:
261
 
            if not history_subset(push_location, cur_branch):
 
268
            if not history_subset(push_location, tree.branch):
262
269
                raise bzrlib.errors.BzrCommandError("Local branch is not a"
263
270
                                                    " newer version of remote"
264
271
                                                    " branch.")
272
279
                " specified location.  Please ensure that"
273
280
                ' "%s" is of the form "machine:/path".' % push_location)
274
281
    print "Pushing to %s" % push_location
275
 
    rsync(cur_branch.base+'/', push_location, ssh=True, 
 
282
    rsync(tree.basedir+'/', push_location, ssh=True, 
276
283
          excludes=final_exclusions)
277
284
 
278
 
    set_push_data(cur_branch, push_location)
 
285
    set_push_data(tree, push_location)
279
286
 
280
287
def short_committer(committer):
281
288
    new_committer = re.sub('<.*>', '', committer).strip(' ')