ThirdParty/Send2Trash/send2trash/plat_other.py

Fri, 17 Mar 2017 19:45:49 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 17 Mar 2017 19:45:49 +0100
changeset 5624
cdd346d8858b
parent 5587
ea526b78ee6c
child 5994
cf0b37d2a28d
permissions
-rw-r--r--

Removed a bunch of __IGNORE_WARNING_M131__ markers.

3644
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # Copyright 2013 Hardcoded Software (http://www.hardcoded.net)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # This software is licensed under the "BSD" License as described in the "LICENSE" file,
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 # which should be included with this package. The terms are also available at
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5 # http://www.hardcoded.net/licenses/bsd_license
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 # This is a reimplementation of plat_other.py with reference to the
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 # freedesktop.org trash specification:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 # [1] http://www.freedesktop.org/wiki/Specifications/trash-spec
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 # [2] http://www.ramendik.ru/docs/trashspec.html
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 # See also:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 # [3] http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 #
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 # For external volumes this implementation will raise an exception if it can't
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 # find or create the user's trash directory.
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from __future__ import unicode_literals
4247
c5d6a59a8cdd Fixed an issue in a third party package causing a stacktrace on Python2 in a call to str().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3644
diff changeset
18 try:
5624
cdd346d8858b Removed a bunch of __IGNORE_WARNING_M131__ markers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
19 str = unicode
4247
c5d6a59a8cdd Fixed an issue in a third party package causing a stacktrace on Python2 in a call to str().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3644
diff changeset
20 except NameError:
c5d6a59a8cdd Fixed an issue in a third party package causing a stacktrace on Python2 in a call to str().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3644
diff changeset
21 pass
3644
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 import sys
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 import os
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 import os.path as op
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 from datetime import datetime
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 import stat
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 try:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 from urllib.parse import quote
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 except ImportError:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 # Python 2
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 from urllib import quote
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 FILES_DIR = 'files'
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 INFO_DIR = 'info'
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 INFO_SUFFIX = '.trashinfo'
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 # Default of ~/.local/share [3]
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 XDG_DATA_HOME = op.expanduser(os.environ.get('XDG_DATA_HOME', '~/.local/share'))
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 HOMETRASH = op.join(XDG_DATA_HOME, 'Trash')
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 uid = os.getuid()
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 TOPDIR_TRASH = '.Trash'
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 TOPDIR_FALLBACK = '.Trash-' + str(uid)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 def is_parent(parent, path):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 path = op.realpath(path) # In case it's a symlink
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 parent = op.realpath(parent)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 return path.startswith(parent)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 def format_date(date):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 return date.strftime("%Y-%m-%dT%H:%M:%S")
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 def info_for(src, topdir):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 # ...it MUST not include a ".."" directory, and for files not "under" that
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 # directory, absolute pathnames must be used. [2]
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 if topdir is None or not is_parent(topdir, src):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 src = op.abspath(src)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 else:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 src = op.relpath(src, topdir)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 info = "[Trash Info]\n"
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 info += "Path=" + quote(src) + "\n"
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 info += "DeletionDate=" + format_date(datetime.now()) + "\n"
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 return info
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 def check_create(dir):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 # use 0700 for paths [3]
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 if not op.exists(dir):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 os.makedirs(dir, 0o700)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 def trash_move(src, dst, topdir=None):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 filename = op.basename(src)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 filespath = op.join(dst, FILES_DIR)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 infopath = op.join(dst, INFO_DIR)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 base_name, ext = op.splitext(filename)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 counter = 0
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 destname = filename
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 while op.exists(op.join(filespath, destname)) or op.exists(op.join(infopath, destname + INFO_SUFFIX)):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 counter += 1
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 destname = '%s %s%s' % (base_name, counter, ext)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 check_create(filespath)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 check_create(infopath)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 os.rename(src, op.join(filespath, destname))
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 f = open(op.join(infopath, destname + INFO_SUFFIX), 'w')
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 f.write(info_for(src, topdir))
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 f.close()
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 # added by detlev@die-offenbachs.de to update the Trash metadata file
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 metadata = op.join(dst, "metadata")
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 entriesCount = len(os.listdir(filespath))
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 f = open(metadata, 'w')
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 f.write("[Cached]\nSize={0}\n".format(entriesCount))
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 f.close()
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 def find_mount_point(path):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 # Even if something's wrong, "/" is a mount point, so the loop will exit.
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 # Use realpath in case it's a symlink
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 path = op.realpath(path) # Required to avoid infinite loop
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 while not op.ismount(path):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 path = op.split(path)[0]
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 return path
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 def find_ext_volume_global_trash(volume_root):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 # from [2] Trash directories (1) check for a .Trash dir with the right
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 # permissions set.
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 trash_dir = op.join(volume_root, TOPDIR_TRASH)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 if not op.exists(trash_dir):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 return None
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 mode = os.lstat(trash_dir).st_mode
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 # vol/.Trash must be a directory, cannot be a symlink, and must have the
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 # sticky bit set.
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 if not op.isdir(trash_dir) or op.islink(trash_dir) or not (mode & stat.S_ISVTX):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 return None
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 trash_dir = op.join(trash_dir, str(uid))
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 try:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 check_create(trash_dir)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 except OSError:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 return None
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 return trash_dir
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 def find_ext_volume_fallback_trash(volume_root):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 # from [2] Trash directories (1) create a .Trash-$uid dir.
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 trash_dir = op.join(volume_root, TOPDIR_FALLBACK)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 # Try to make the directory, if we can't the OSError exception will escape
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 # be thrown out of send2trash.
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 check_create(trash_dir)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 return trash_dir
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 def find_ext_volume_trash(volume_root):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 trash_dir = find_ext_volume_global_trash(volume_root)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 if trash_dir is None:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 trash_dir = find_ext_volume_fallback_trash(volume_root)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 return trash_dir
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 # Pull this out so it's easy to stub (to avoid stubbing lstat itself)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 def get_dev(path):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 return os.lstat(path).st_dev
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 def send2trash(path):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 if not isinstance(path, str):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 path = str(path, sys.getfilesystemencoding())
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 if not op.exists(path):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 raise OSError("File not found: %s" % path)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 # ...should check whether the user has the necessary permissions to delete
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 # it, before starting the trashing operation itself. [2]
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 if not os.access(path, os.W_OK):
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 raise OSError("Permission denied: %s" % path)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 # if the file to be trashed is on the same device as HOMETRASH we
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 # want to move it there.
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 path_dev = get_dev(path)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 # If XDG_DATA_HOME or HOMETRASH do not yet exist we need to stat the
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 # home directory, and these paths will be created further on if needed.
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 trash_dev = get_dev(op.expanduser('~'))
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 if path_dev == trash_dev:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 topdir = XDG_DATA_HOME
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 dest_trash = HOMETRASH
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 else:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 topdir = find_mount_point(path)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 trash_dev = get_dev(topdir)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 if trash_dev != path_dev:
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 raise OSError("Couldn't find mount point for %s" % path)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 dest_trash = find_ext_volume_trash(topdir)
a2c88b9b1d16 Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 trash_move(path, dest_trash, topdir)

eric ide

mercurial