/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to olive/backend/fileops.py

  • Committer: Jelmer Vernooij
  • Date: 2006-09-05 01:20:15 UTC
  • mto: (0.8.83 merge)
  • mto: This revision was merged to the branch mainline in revision 83.
  • Revision ID: jelmer@samba.org-20060905012015-afc711bf6c67e963
Integrate olive.backend.init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
 
2
# Some parts of the code are:
 
3
# Copyright (C) 2005, 2006 by Canonical Ltd
 
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
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
 
19
import os
 
20
 
 
21
from bzrlib.errors import (BzrError, NotBranchError, NotVersionedError, 
 
22
                           PermissionDenied)
 
23
 
 
24
 
 
25
class MultipleMoveError(BzrError):
 
26
    """ Occurs when moving/renaming more than 2 files, but the last argument is not a directory
 
27
    
 
28
    May occur in:
 
29
        fileops.move()
 
30
    """
 
31
 
 
32
 
 
33
class NoMatchingFiles(BzrError):
 
34
    """ No files found which could match the criteria
 
35
    
 
36
    May occur in:
 
37
        fileops.remove()
 
38
    """
 
39
 
 
40
 
 
41
def move(names_list):
 
42
    """ Move or rename given files.
 
43
    
 
44
    :param file_list: if two elements, then rename the first to the second, if more elements then move all of them to the directory specified in the last element
 
45
    """
 
46
    from bzrlib.builtins import tree_files
 
47
    
 
48
    tree, rel_names = tree_files(names_list)
 
49
        
 
50
    if os.path.isdir(names_list[-1]):
 
51
        # move into existing directory
 
52
        for pair in tree.move(rel_names[:-1], rel_names[-1]):
 
53
            pass
 
54
    else:
 
55
        if len(names_list) != 2:
 
56
            raise MultipleMoveError
 
57
        tree.rename_one(rel_names[0], rel_names[1])
 
58
 
 
59
 
 
60
def remove(file_list, new=False):
 
61
    """ Make selected files unversioned.
 
62
    
 
63
    :param file_list: list of files/directories to be removed
 
64
    
 
65
    :param new: if True, the 'added' files will be removed
 
66
    """
 
67
    import bzrlib
 
68
    from bzrlib.builtins import tree_files
 
69
    
 
70
    tree, file_list = tree_files(file_list)
 
71
    
 
72
    if new:
 
73
        from bzrlib.delta import compare_trees
 
74
        if (bzrlib.version_info[0] == 0) and (bzrlib.version_info[1] < 9):
 
75
            added = [compare_trees(tree.basis_tree(), tree,
 
76
                                   specific_files=file_list).added]
 
77
        else:
 
78
            added = [tree.changes_from(tree.basis_tree(),
 
79
                                       specific_files=file_list).added]
 
80
        file_list = sorted([f[0] for f in added[0]], reverse=True)
 
81
        if len(file_list) == 0:
 
82
            raise NoMatchingFiles
 
83
    
 
84
    tree.remove(file_list)
 
85
 
 
86
 
 
87
def status(filename):
 
88
    """ Get the status of a file.
 
89
    
 
90
    :param filename: the full path to the file
 
91
    
 
92
    :return: renamed | added | removed | modified | unchanged | unknown
 
93
    """
 
94
    import bzrlib
 
95
    from bzrlib.delta import compare_trees
 
96
    from bzrlib.workingtree import WorkingTree
 
97
    
 
98
    try:
 
99
        tree1 = WorkingTree.open_containing(filename)[0]
 
100
    except NotBranchError:
 
101
        return 'unknown'
 
102
    
 
103
    branch = tree1.branch
 
104
    tree2 = tree1.branch.repository.revision_tree(branch.last_revision())
 
105
    
 
106
    # find the relative path to the given file (needed for proper delta)
 
107
    wtpath = tree1.basedir
 
108
    fullpath = filename
 
109
    i = 0
 
110
    wtsplit = wtpath.split('/')
 
111
    fpsplit = fullpath.split('/')
 
112
    fpcopy = fullpath.split('/')
 
113
    for item in fpsplit:
 
114
        if i is not len(wtsplit):
 
115
            if item == wtsplit[i]:
 
116
                del fpcopy[0]
 
117
            i = i + 1
 
118
    rel = '/'.join(fpcopy)
 
119
    
 
120
    delta = tree1.changes_from(tree2,
 
121
                                   want_unchanged=True,
 
122
                                   specific_files=[rel])
 
123
    
 
124
    if len(delta.renamed):
 
125
        return 'renamed'
 
126
    elif len(delta.added):
 
127
        return 'added'
 
128
    elif len(delta.removed):
 
129
        return 'removed'
 
130
    elif len(delta.modified):
 
131
        return 'modified'
 
132
    elif len(delta.unchanged):
 
133
        return 'unchanged'
 
134
    else:
 
135
        return 'unknown'