32229d2e3b9ae8d20f659e3236232533704e1a4f
[mediagoblin.git] / mediagoblin / workbench.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import os
18 import shutil
19 import tempfile
20
21
22 DEFAULT_WORKBENCH_DIR = os.path.join(
23 tempfile.gettempdir(), u'mgoblin_workbench')
24
25
26 # Exception(s)
27 # ------------
28
29 class WorkbenchOutsideScope(Exception):
30 """
31 Raised when a workbench is outside a WorkbenchManager scope.
32 """
33 pass
34
35
36 # Actual workbench stuff
37 # ----------------------
38
39 class Workbench(object):
40 """
41 Represent the directory for the workbench
42 """
43 def __init__(self, dir):
44 self.dir = dir
45
46 def __unicode__(self):
47 return unicode(self.dir)
48 def __str__(self):
49 return str(self.dir)
50 def __repr__(self):
51 return repr(self.dir)
52
53 def joinpath(self, *args):
54 return os.path.join(self.dir, *args)
55
56 def localized_file(self, storage, filepath,
57 filename_if_copying=None,
58 keep_extension_if_copying=True):
59 """
60 Possibly localize the file from this storage system (for read-only
61 purposes, modifications should be written to a new file.).
62
63 If the file is already local, just return the absolute filename of that
64 local file. Otherwise, copy the file locally to the workbench, and
65 return the absolute path of the new file.
66
67 If it is copying locally, we might want to require a filename like
68 "source.jpg" to ensure that we won't conflict with other filenames in
69 our workbench... if that's the case, make sure filename_if_copying is
70 set to something like 'source.jpg'. Relatedly, if you set
71 keep_extension_if_copying, you don't have to set an extension on
72 filename_if_copying yourself, it'll be set for you (assuming such an
73 extension can be extacted from the filename in the filepath).
74
75 Returns:
76 localized_filename
77
78 Examples:
79 >>> wb_manager.localized_file(
80 ... '/our/workbench/subdir', local_storage,
81 ... ['path', 'to', 'foobar.jpg'])
82 u'/local/storage/path/to/foobar.jpg'
83
84 >>> wb_manager.localized_file(
85 ... '/our/workbench/subdir', remote_storage,
86 ... ['path', 'to', 'foobar.jpg'])
87 '/our/workbench/subdir/foobar.jpg'
88
89 >>> wb_manager.localized_file(
90 ... '/our/workbench/subdir', remote_storage,
91 ... ['path', 'to', 'foobar.jpg'], 'source.jpeg', False)
92 '/our/workbench/subdir/foobar.jpeg'
93
94 >>> wb_manager.localized_file(
95 ... '/our/workbench/subdir', remote_storage,
96 ... ['path', 'to', 'foobar.jpg'], 'source', True)
97 '/our/workbench/subdir/foobar.jpg'
98 """
99 if storage.local_storage:
100 return storage.get_local_path(filepath)
101 else:
102 if filename_if_copying is None:
103 dest_filename = filepath[-1]
104 else:
105 orig_filename, orig_ext = os.path.splitext(filepath[-1])
106 if keep_extension_if_copying and orig_ext:
107 dest_filename = filename_if_copying + orig_ext
108 else:
109 dest_filename = filename_if_copying
110
111 full_dest_filename = os.path.join(
112 self.dir, dest_filename)
113
114 # copy it over
115 storage.copy_locally(
116 filepath, full_dest_filename)
117
118 return full_dest_filename
119
120
121 class WorkbenchManager(object):
122 """
123 A system for generating and destroying workbenches.
124
125 Workbenches are actually just subdirectories of a temporary storage space
126 for during the processing stage.
127 """
128
129 def __init__(self, base_workbench_dir):
130 self.base_workbench_dir = os.path.abspath(base_workbench_dir)
131 if not os.path.exists(self.base_workbench_dir):
132 os.makedirs(self.base_workbench_dir)
133
134 def create_workbench(self):
135 """
136 Create and return the path to a new workbench (directory).
137 """
138 return Workbench(tempfile.mkdtemp(dir=self.base_workbench_dir))
139
140 def destroy_workbench(self, workbench):
141 """
142 Destroy this workbench! Deletes the directory and all its contents!
143
144 Makes sure the workbench actually belongs to this manager though.
145 """
146 # just in case
147 workbench = os.path.abspath(workbench.dir)
148
149 if not workbench.startswith(self.base_workbench_dir):
150 raise WorkbenchOutsideScope(
151 "Can't destroy workbench outside the base workbench dir")
152
153 shutil.rmtree(workbench)