/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: Szilveszter Farkas (Phanatic)
  • Date: 2006-07-20 14:31:08 UTC
  • mto: (0.14.1 main) (93.1.1 win32.bialix)
  • mto: This revision was merged to the branch mainline in revision 83.
  • Revision ID: Szilveszter.Farkas@gmail.com-20060720143108-12c16a5c52431578
2006-07-20  Szilveszter Farkas <Szilveszter.Farkas@gmail.com>

    * olive/frontend/gtk/__init__.py: implemented OliveCommunication.refresh_right()
    * olive/frontend/gtk/handler.py: implemented browsing in the file list
    * olive/backend/fileops.py: tweaked status() (to get proper status in fact)

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
import bzrlib.errors as errors
 
22
 
 
23
from errors import (DirectoryAlreadyExists, MissingArgumentError,
 
24
                    MultipleMoveError, NoFilesSpecified, NoMatchingFiles,
 
25
                    NonExistingSource, NotVersionedError)
 
26
 
 
27
def add(file_list):
 
28
    """ Add listed files to the branch. 
 
29
    
 
30
    :param file_list - list of files to be added (using full paths)
 
31
    
 
32
    :return: count of ignored files
 
33
    """
 
34
    import bzrlib.add
 
35
    
 
36
    added, ignored = bzrlib.add.smart_add(file_list)
 
37
    
 
38
    match_len = 0
 
39
    for glob, paths in ignored.items():
 
40
        match_len += len(paths)
 
41
    
 
42
    return match_len
 
43
 
 
44
def mkdir(directory):
 
45
    """ Create new versioned directory.
 
46
    
 
47
    :param directory: the full path to the directory to be created
 
48
    """
 
49
    from bzrlib.workingtree import WorkingTree
 
50
    
 
51
    try:
 
52
        os.mkdir(directory)
 
53
    except OSError, e:
 
54
        if e.errno == 17:
 
55
            raise DirectoryAlreadyExists(directory)
 
56
    else:
 
57
        wt, dd = WorkingTree.open_containing(directory)
 
58
        wt.add([dd])
 
59
 
 
60
def move(names_list):
 
61
    """ Move or rename given files.
 
62
    
 
63
    :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
 
64
    """
 
65
    from bzrlib.builtins import tree_files
 
66
    
 
67
    if len(names_list) < 2:
 
68
        raise MissingArgumentError
 
69
    tree, rel_names = tree_files(names_list)
 
70
        
 
71
    if os.path.isdir(names_list[-1]):
 
72
        # move into existing directory
 
73
        for pair in tree.move(rel_names[:-1], rel_names[-1]):
 
74
            pass
 
75
    else:
 
76
        if len(names_list) != 2:
 
77
            raise MultipleMoveError
 
78
        tree.rename_one(rel_names[0], rel_names[1])
 
79
 
 
80
def remove(file_list, new=False):
 
81
    """ Make selected files unversioned.
 
82
    
 
83
    :param file_list: list of files/directories to be removed
 
84
    
 
85
    :param new: if True, the 'added' files will be removed
 
86
    """
 
87
    from bzrlib.builtins import tree_files
 
88
    
 
89
    tree, file_list = tree_files(file_list)
 
90
    
 
91
    if new is False:
 
92
        if file_list is None:
 
93
            raise NoFilesSpecified
 
94
    else:
 
95
        from bzrlib.delta import compare_trees
 
96
        added = [compare_trees(tree.basis_tree(), tree,
 
97
                               specific_files=file_list).added]
 
98
        file_list = sorted([f[0] for f in added[0]], reverse=True)
 
99
        if len(file_list) == 0:
 
100
            raise NoMatchingFiles
 
101
    
 
102
    try:
 
103
        tree.remove(file_list)
 
104
    except errors.NotVersionedError:
 
105
        raise NotVersionedError
 
106
 
 
107
def rename(source, target):
 
108
    """ Rename a versioned file
 
109
    
 
110
    :param source: full path to the original file
 
111
    
 
112
    :param target: full path to the new file
 
113
    """
 
114
    if os.access(source, os.F_OK) is not True:
 
115
        raise NonExistingSource(source)
 
116
    
 
117
    move([source, target])
 
118
 
 
119
def status(filename):
 
120
    """ Get the status of a file.
 
121
    
 
122
    :param filename: the full path to the file
 
123
    
 
124
    :return: renamed | added | removed | modified | unchanged | unknown
 
125
    """
 
126
    from bzrlib.delta import compare_trees
 
127
    from bzrlib.workingtree import WorkingTree
 
128
    
 
129
    try:
 
130
        tree1 = WorkingTree.open_containing(filename)[0]
 
131
    except errors.NotBranchError:
 
132
        return 'unknown'
 
133
    
 
134
    branch = tree1.branch
 
135
    tree2 = tree1.branch.repository.revision_tree(branch.last_revision())
 
136
    
 
137
    # find the relative path to the given file (needed for proper delta)
 
138
    wtpath = tree1.basedir
 
139
    fullpath = filename
 
140
    i = 0
 
141
    wtsplit = wtpath.split('/')
 
142
    fpsplit = fullpath.split('/')
 
143
    fpcopy = fullpath.split('/')
 
144
    for item in fpsplit:
 
145
        if i is not len(wtsplit):
 
146
            if item == wtsplit[i]:
 
147
                del fpcopy[0]
 
148
            i = i + 1
 
149
    rel = '/'.join(fpcopy)
 
150
    
 
151
    delta = compare_trees(old_tree=tree2,
 
152
                          new_tree=tree1,
 
153
                          want_unchanged=True,
 
154
                          specific_files=[rel])
 
155
    
 
156
    """ Debug information (could be usable in the future, so didn't cut out)
 
157
    print "DEBUG: delta.renamed:"
 
158
    for path, id, kind, text_modified, meta_modified in delta.renamed:
 
159
        print path
 
160
    print
 
161
    print "DEBUG: delta.added:"
 
162
    for path, id, kind, text_modified, meta_modified in delta.added:
 
163
        print path
 
164
    print
 
165
    print "DEBUG: delta.removed:"
 
166
    for path, id, kind, text_modified, meta_modified in delta.removed:
 
167
        print path
 
168
    print
 
169
    print "DEBUG: delta.modified:"
 
170
    for path, id, kind, text_modified, meta_modified in delta.modified:
 
171
        print path
 
172
    print
 
173
    print "DEBUG: delta.unchanged:"
 
174
    for path, id, kind in delta.unchanged:
 
175
        print path
 
176
    """
 
177
    
 
178
    if len(delta.renamed):
 
179
        return 'renamed'
 
180
    elif len(delta.added):
 
181
        return 'added'
 
182
    elif len(delta.removed):
 
183
        return 'removed'
 
184
    elif len(delta.modified):
 
185
        return 'modified'
 
186
    elif len(delta.unchanged):
 
187
        return 'unchanged'
 
188
    else:
 
189
        return 'unknown'