Lines 67-73
Options:
Link Here
|
67 |
%s. |
67 |
%s. |
68 |
This can be changed by setting the |
68 |
This can be changed by setting the |
69 |
EDITOR environment variable. |
69 |
EDITOR environment variable. |
70 |
-f, --force overwrite existing files.""" % editor |
70 |
-f, --force overwrite existing files. |
|
|
71 |
""" % editor |
71 |
|
72 |
|
72 |
|
73 |
|
73 |
def giveup(message): |
74 |
def giveup(message): |
Lines 81-98
def warn(message):
Link Here
|
81 |
|
82 |
|
82 |
def usage(long = 0): |
83 |
def usage(long = 0): |
83 |
""" print usage information. """ |
84 |
""" print usage information. """ |
84 |
print 'mved %s - an editor-focused multiple file move utility' % version |
85 |
stdout.write('mved %s - an editor-focused multiple file move utility\n' % version) |
85 |
if long: |
86 |
if long: |
86 |
print LICENSE |
87 |
stdout.write(LICENSE) |
87 |
print DIRECTIONS |
88 |
stdout.write(DIRECTIONS) |
88 |
print USAGE |
89 |
stdout.write(USAGE) |
89 |
|
90 |
|
90 |
def getoptions(): |
91 |
def getoptions(): |
91 |
""" get options. returns a list of all non-option arguments. """ |
92 |
""" get options. returns a list of all non-option arguments. """ |
92 |
global editor, force |
93 |
global editor, force |
93 |
try: |
94 |
try: |
94 |
options, args = getopt(argv[1:], 'he:f', ['help', 'editor=','force']) |
95 |
options, args = getopt(argv[1:], 'he:f', ['help', 'editor=','force']) |
95 |
except GetoptError, e: |
96 |
except GetoptError as e: |
96 |
giveup(str(e) + ", try 'mved --help' for more information.") |
97 |
giveup(str(e) + ", try 'mved --help' for more information.") |
97 |
|
98 |
|
98 |
for opt, arg in options: |
99 |
for opt, arg in options: |
Lines 120-128
def edit(oldnames):
Link Here
|
120 |
tmpfd, tmpfilename = mkstemp() |
121 |
tmpfd, tmpfilename = mkstemp() |
121 |
try: |
122 |
try: |
122 |
for name in oldnames: |
123 |
for name in oldnames: |
123 |
write(tmpfd, '%s\n' % name) |
124 |
write(tmpfd, (name + '\n').encode("ascii")) |
124 |
fsync(tmpfd) |
125 |
fsync(tmpfd) |
125 |
except IOError, e: |
126 |
except IOError as e: |
126 |
giveup("%s: %s", (tmpfilename, str(e))) |
127 |
giveup("%s: %s", (tmpfilename, str(e))) |
127 |
|
128 |
|
128 |
# warn user if EDITOR is not an absolute path |
129 |
# warn user if EDITOR is not an absolute path |
Lines 136-144
def edit(oldnames):
Link Here
|
136 |
newnames = readlines(tmpfd) |
137 |
newnames = readlines(tmpfd) |
137 |
close(tmpfd) |
138 |
close(tmpfd) |
138 |
remove(tmpfilename) |
139 |
remove(tmpfilename) |
139 |
except OSError, e: |
140 |
except OSError as e: |
140 |
giveup("%s: %s", (tmpfilename, str(e))) |
141 |
giveup("%s: %s", (tmpfilename, str(e))) |
141 |
except IOError, e: |
142 |
except IOError as e: |
142 |
giveup("%s: %s", (tmpfilename, str(e))) |
143 |
giveup("%s: %s", (tmpfilename, str(e))) |
143 |
for i in range(len(newnames)): |
144 |
for i in range(len(newnames)): |
144 |
newnames[i] = newnames[i].strip('\n') |
145 |
newnames[i] = newnames[i].strip('\n') |
Lines 151-157
def edit(oldnames):
Link Here
|
151 |
else: |
152 |
else: |
152 |
try: |
153 |
try: |
153 |
remove(tmpfilename) |
154 |
remove(tmpfilename) |
154 |
except IOError, e: |
155 |
except IOError as e: |
155 |
warn("Could not remove %s: %s", (tmpfilename, str(e))) |
156 |
warn("Could not remove %s: %s", (tmpfilename, str(e))) |
156 |
giveup('Editor exited ungracefully.') |
157 |
giveup('Editor exited ungracefully.') |
157 |
|
158 |
|
Lines 164-169
def readlines(fd):
Link Here
|
164 |
lseek(fd, 0, 0) |
165 |
lseek(fd, 0, 0) |
165 |
while True: |
166 |
while True: |
166 |
ch = read(fd, 1) |
167 |
ch = read(fd, 1) |
|
|
168 |
ch = ch.decode("ascii") |
167 |
if ch == '': |
169 |
if ch == '': |
168 |
if str: out.append(str) |
170 |
if str: out.append(str) |
169 |
break |
171 |
break |
Lines 401-427
if __name__ == '__main__':
Link Here
|
401 |
msg += '1 does not' |
403 |
msg += '1 does not' |
402 |
else: |
404 |
else: |
403 |
msg += '%i do not' % dontchange |
405 |
msg += '%i do not' % dontchange |
404 |
print msg |
406 |
stdout.write(msg + "\n") |
405 |
|
407 |
|
406 |
renamer.checkrenamings(force) |
408 |
renamer.checkrenamings(force) |
407 |
sequence = renamer.renamingsequence() |
409 |
sequence = renamer.renamingsequence() |
408 |
|
410 |
|
409 |
for i in sequence: |
411 |
for i in sequence: |
410 |
try: |
412 |
try: |
411 |
print "moving %s -> %s " % (i.frm, i.to) |
413 |
stdout.write("moving %s -> %s \n" % (i.frm, i.to)) |
412 |
if force == 0 and exists.exists(i.to): |
414 |
if force == 0 and exists.exists(i.to): |
413 |
sched = "" |
415 |
sched = "" |
414 |
for i in sequence: |
416 |
for i in sequence: |
415 |
sched += "\n%s -> %s" % (i.frm, i.to) |
417 |
sched += "\n%s -> %s" % (i.frm, i.to) |
416 |
print "error detected. moving schedule was: %s " % sched |
418 |
stdout.write("error detected. moving schedule was: %s \n" % sched) |
417 |
giveup("will not overwrite %s\n** RENAMING INTERRUPTED **" \ |
419 |
giveup("will not overwrite %s\n** RENAMING INTERRUPTED **" \ |
418 |
% i.to) |
420 |
% i.to) |
419 |
rename(i.frm, expanduser(i.to)) |
421 |
rename(i.frm, expanduser(i.to)) |
420 |
except OSError, e: |
422 |
except OSError as e: |
421 |
sched = "" |
423 |
sched = "" |
422 |
for i in sequence: |
424 |
for i in sequence: |
423 |
sched += "\n%s -> %s" % (i.frm, i.to) |
425 |
sched += "\n%s -> %s" % (i.frm, i.to) |
424 |
print "error detected. moving schedule was: %s" % sched |
426 |
stdout.write("error detected. moving schedule was: %s\n" % sched) |
425 |
giveup("Could not rename %s to %s: %s\n** RENAMING INTERRUPTED **" \ |
427 |
giveup("Could not rename %s to %s: %s\n** RENAMING INTERRUPTED **" \ |
426 |
% (i.frm, i.to, str(e))) |
428 |
% (i.frm, i.to, str(e))) |
427 |
|
429 |
|