-
Notifications
You must be signed in to change notification settings - Fork 10
/
git-annex-remote-directory
executable file
·151 lines (121 loc) · 4.8 KB
/
git-annex-remote-directory
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
#!/usr/bin/env python
# git-annex external special remote program
#
# This is basically the same as git-annex's built-in directory special remote.
#
# Install in PATH as git-annex-remote-directory
#
# Copyright 2018 Silvio Ankermann; licenced under the GNU GPL version 3
import sys, os, errno
from shutil import copyfile
from annexremote import Master
from annexremote import ExportRemote
from annexremote import RemoteError, ProtocolError
class DirectoryRemote(ExportRemote):
def initremote(self):
self.directory = self.annex.getconfig("directory")
if not self.directory:
raise RemoteError("You need to set directory=")
self._mkdir(self.directory)
def listconfigs(self):
return {"directory": "directory where data is stored"}
def prepare(self):
self.directory = self.annex.getconfig("directory")
self.info = {"directory": self.directory}
if not os.path.exists(self.directory):
raise RemoteError("{} not found".format(self.directory))
def transfer_store(self, key, filename):
location = self._calclocation(key)
self._do_store(key, filename, location)
def transfer_retrieve(self, key, filename):
location = self._calclocation(key)
self._do_retrieve(key, location, filename)
def checkpresent(self, key):
location = self._calclocation(key)
return self._do_checkpresent(key, location)
def remove(self, key):
location = self._calclocation(key)
self._do_remove(key, location)
## Export methods
def transferexport_store(self, key, local_file, remote_file):
location = "/".join((self.directory, remote_file))
self._do_store(key, local_file, location)
def transferexport_retrieve(self, key, local_file, remote_file):
location = "/".join((self.directory, remote_file))
self._do_retrieve(key, location, local_file)
def checkpresentexport(self, key, remote_file):
location = "/".join((self.directory, remote_file))
return self._do_checkpresent(key, location)
def removeexport(self, key, remote_file):
location = "/".join((self.directory, remote_file))
self._do_remove(key, location)
def removeexportdirectory(self, remote_directory):
location = "/".join((self.directory, remote_directory))
try:
os.rmdir(location)
except OSError as e:
if e.errno != errno.ENOENT:
raise RemoteError(e)
def renameexport(self, key, filename, new_filename):
oldlocation = "/".join((self.directory, filename))
newlocation = "/".join((self.directory, new_filename))
try:
os.rename(oldlocation, newlocation)
except OSError as e:
raise RemoteError(e)
def _mkdir(self, directory):
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise RemoteError("Failed to write to {}".format(directory))
def _calclocation(self, key):
return "{dir}/{hash}{key}".format(
dir=self.directory, hash=self.annex.dirhash(key), key=key
)
def _info(self, message):
try:
self.annex.info(message)
except ProtocolError:
print(message)
def _do_store(self, key, filename, location):
self._mkdir(os.path.dirname(location))
templocation = "/".join((self.directory, "tmp", key))
self._mkdir(os.path.dirname(templocation))
try:
copyfile(filename, templocation)
os.rename(templocation, location)
except OSError as e:
raise RemoteError(e)
try:
os.rmdir(os.path.dirname(templocation))
except OSError:
self._info("Could not remove tempdir (Not empty)")
def _do_retrieve(self, key, location, filename):
try:
copyfile(location, filename)
except OSError as e:
raise RemoteError(e)
def _do_checkpresent(self, key, location):
if not os.path.exists(self.directory):
raise RemoteError("this remote is not currently available")
return os.path.isfile(location)
def _do_remove(self, key, location):
if not os.path.exists(self.directory):
raise RemoteError("this remote is not currently available")
try:
os.remove(location)
except OSError as e:
# It's not a failure to remove a file that is not present.
if e.errno != errno.ENOENT:
raise RemoteError(e)
def main():
# Redirect output to stderr to avoid messing up the protocol
output = sys.stdout
sys.stdout = sys.stderr
master = Master(output)
remote = DirectoryRemote(master)
master.LinkRemote(remote)
master.Listen()
if __name__ == "__main__":
main()