summaryrefslogtreecommitdiffstats
path: root/BuildManager/fileutil.py
blob: 05126da954f1756eb7895681de159e8db1ece12e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# This module was originally part of distutils.
from BuildManager import *
import os

__all__ = ["copy_file", "move_file"]

# for generating verbose output in 'copy_file()'
_copy_action = { None:   'copying',
                 'hard': 'hard linking',
                 'sym':  'symbolically linking' }


def _copy_file_contents (src, dst, buffer_size=16*1024):
    """Copy the file 'src' to 'dst'; both must be filenames.  Any error
    opening either file, reading from 'src', or writing to 'dst', raises
    BuildManagerFileError.  Data is read/written in chunks of 'buffer_size'
    bytes (default 16k).  No attempt is made to handle anything apart from
    regular files.
    """
    # Stolen from shutil module in the standard library, but with
    # custom error-handling added.

    fsrc = None
    fdst = None
    try:
        try:
            fsrc = open(src, 'rb')
        except os.error as e:
            (errno, errstr) = e.args
            raise Error("could not open %s: %s" % (src, errstr))
        
        try:
            fdst = open(dst, 'wb')
        except os.error as e:
            (errno, errstr) = e.args
            raise Error("could not create %s: %s" % (dst, errstr))
        
        while 1:
            try:
                buf = fsrc.read(buffer_size)
            except os.error as e:
                (errno, errstr) = e.args
                raise Error("could not read from %s: %s" % (src, errstr))
            
            if not buf:
                break

            try:
                fdst.write(buf)
            except os.error as e:
                (errno, errstr) = e.args
                raise Error("could not write to %s: %s" % (dst, errstr))
            
    finally:
        if fdst:
            fdst.close()
        if fsrc:
            fsrc.close()

def copy_file (src, dst, preserve_mode=1, preserve_times=1, link=None,
               dryrun=False):

    """Copy a file 'src' to 'dst'.  If 'dst' is a directory, then 'src' is
    copied there with the same name; otherwise, it must be a filename.  (If

			log_perror("could not create location dir");
			return -1;
		}
	} else if (!S_ISDIR(buf.st_mode)) {
		log_message("not a dir %s, will unlink and mkdir", location);
		if (unlink(location)) {
			log_perror("could not unlink");
			return -1;
		}
		if (mkdir(location, 0755)) {
			log_perror("could not create location dir");
			return -1;
		}
	}

#ifndef DISABLE_MEDIAS
	if (!strcmp(fs, "vfat")) {
		my_modprobe("nls_cp437", ANY_DRIVER_TYPE, NULL);
		my_modprobe("nls_iso8859_1", ANY_DRIVER_TYPE, NULL);
		my_modprobe("vfat", ANY_DRIVER_TYPE, NULL);
		opts = "check=relaxed";
	}

	if (!strcmp(fs, "ntfs")) {
		my_modprobe("ntfs", ANY_DRIVER_TYPE, NULL);
	}

	if (!strcmp(fs, "reiserfs"))
		my_modprobe("reiserfs", ANY_DRIVER_TYPE, NULL);

	if (!strcmp(fs, "reiser4"))
		my_modprobe("reiser4", ANY_DRIVER_TYPE, NULL);

	if (!strcmp(fs, "jfs"))
		my_modprobe("jfs", ANY_DRIVER_TYPE, NULL);

	if (!strcmp(fs, "xfs"))
		my_modprobe("xfs", ANY_DRIVER_TYPE, NULL);

	if (!strcmp(fs, "ext4"))
		my_modprobe("ext4", ANY_DRIVER_TYPE, NULL);

	if (!strcmp(fs, "btrfs"))
		my_modprobe("btrfs", ANY_DRIVER_TYPE, NULL);

#endif
	if (!strcmp(fs, "iso9660"))
		my_modprobe("isofs", ANY_DRIVER_TYPE, NULL);

#ifndef DISABLE_NETWORK
	if (!strcmp(fs, "nfs")) {
		my_modprobe("nfs", ANY_DRIVER_TYPE, NULL);
		log_message("preparing nfsmount for %s", dev);
		rc = nfsmount_prepare(dev, &opts);
		if (rc != 0)
			return rc;
	}
#endif

	rc = mount(dev, location, fs, flags, opts);
	if (rc != 0) {
		log_perror("mount failed");
		rmdir(location);
	}

	return rc;
}
n class="hl opt">, dryrun=False): """Move a file 'src' to 'dst'. If 'dst' is a directory, the file will be moved into it with the same name; otherwise, 'src' is just renamed to 'dst'. Return the new full name of the file. Handles cross-device moves on Unix using 'copy_file()'. What about other systems??? """ from os.path import exists, isfile, isdir, basename, dirname import errno logger.info("moving %s to %s" % (src, dst)) if dryrun: return dst if not isfile(src): raise Error("can't move %s: not a regular file" % src) if isdir(dst): dst = os.path.join(dst, basename(src)) elif exists(dst): raise Error("can't move %s: destination %s already exists" % \ (src, dst)) if not isdir(dirname(dst)): raise Error("can't move %s: destination %s not a valid path" % \ (src, dst)) copy_it = 0 try: os.rename(src, dst) except os.error as xxx_todo_changeme5: (num, msg) = xxx_todo_changeme5.args if num == errno.EXDEV: copy_it = 1 else: raise Error("couldn't move %s to %s: %s" % (src, dst, msg)) if copy_it: copy_file(src, dst) try: os.unlink(src) except os.error as xxx_todo_changeme4: (num, msg) = xxx_todo_changeme4.args try: os.unlink(dst) except os.error: pass raise Error("couldn't move %s to %s by copy/delete: " \ "delete %s failed: %s" % (src, dst, src, msg)) return dst def write_file (filename, contents): """Create a file with the specified name and write 'contents' (a sequence of strings without line terminators) to it. """ f = open(filename, "w") for line in contents: f.write(line + "\n") f.close()