##// END OF EJS Templates
Merge pull request #7912 from jhamrick/relpath...
Matthias Bussonnier -
r20630:a7059b17 merge
parent child Browse files
Show More
@@ -142,9 +142,10 b' class Exporter(LoggingConfigurable):'
142 resources = ResourcesDict()
142 resources = ResourcesDict()
143 if not 'metadata' in resources or resources['metadata'] == '':
143 if not 'metadata' in resources or resources['metadata'] == '':
144 resources['metadata'] = ResourcesDict()
144 resources['metadata'] = ResourcesDict()
145 basename = os.path.basename(filename)
145 path, basename = os.path.split(filename)
146 notebook_name = basename[:basename.rfind('.')]
146 notebook_name = basename[:basename.rfind('.')]
147 resources['metadata']['name'] = notebook_name
147 resources['metadata']['name'] = notebook_name
148 resources['metadata']['path'] = path
148
149
149 modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
150 modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
150 resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)
151 resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)
@@ -25,6 +25,14 b' class FilesWriter(WriterBase):'
25 help="""Directory to write output to. Leave blank
25 help="""Directory to write output to. Leave blank
26 to output to the current directory""")
26 to output to the current directory""")
27
27
28 relpath = Unicode(
29 "", config=True,
30 help="""When copying files that the notebook depends on, copy them in
31 relation to this path, such that the destination filename will be
32 os.path.relpath(filename, relpath). If FilesWriter is operating on a
33 notebook that already exists elsewhere on disk, then the default will be
34 the directory containing that notebook.""")
35
28
36
29 # Make sure that the output directory exists.
37 # Make sure that the output directory exists.
30 def _build_directory_changed(self, name, old, new):
38 def _build_directory_changed(self, name, old, new):
@@ -59,6 +67,12 b' class FilesWriter(WriterBase):'
59 # Pull the extension and subdir from the resources dict.
67 # Pull the extension and subdir from the resources dict.
60 output_extension = resources.get('output_extension', None)
68 output_extension = resources.get('output_extension', None)
61
69
70 # Get the relative path for copying files
71 if self.relpath == '':
72 relpath = resources.get('metadata', {}).get('path', '')
73 else:
74 relpath = self.relpath
75
62 # Write all of the extracted resources to the destination directory.
76 # Write all of the extracted resources to the destination directory.
63 # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
77 # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
64 # PREPROCESSOR SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
78 # PREPROCESSOR SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
@@ -81,8 +95,14 b' class FilesWriter(WriterBase):'
81 # Copy files that match search pattern
95 # Copy files that match search pattern
82 for matching_filename in glob.glob(filename):
96 for matching_filename in glob.glob(filename):
83
97
98 # compute the relative path for the filename
99 if relpath != '':
100 dest_filename = os.path.relpath(matching_filename, relpath)
101 else:
102 dest_filename = matching_filename
103
84 # Make sure folder exists.
104 # Make sure folder exists.
85 dest = os.path.join(self.build_directory, matching_filename)
105 dest = os.path.join(self.build_directory, dest_filename)
86 path = os.path.dirname(dest)
106 path = os.path.dirname(dest)
87 self._makedir(path)
107 self._makedir(path)
88
108
@@ -201,3 +201,107 b' class Testfiles(TestsBase):'
201 with open(dest, 'r') as f:
201 with open(dest, 'r') as f:
202 output = f.read()
202 output = f.read()
203 self.assertEqual(output, 'e')
203 self.assertEqual(output, 'e')
204
205 def test_relpath(self):
206 """Can the FilesWriter handle relative paths for linked files correctly?"""
207
208 # Work in a temporary directory.
209 with self.create_temp_cwd():
210
211 # Create test file
212 os.mkdir('sub')
213 with open(os.path.join('sub', 'c'), 'w') as f:
214 f.write('d')
215
216 # Create the resoruces dictionary
217 res = {}
218
219 # Create files writer, test output
220 writer = FilesWriter()
221 writer.files = [os.path.join('sub', 'c')]
222 writer.build_directory = u'build'
223 writer.relpath = 'sub'
224 writer.write(u'y', res, notebook_name="z")
225
226 # Check the output of the file
227 assert os.path.isdir(writer.build_directory)
228 dest = os.path.join(writer.build_directory, 'z')
229 with open(dest, 'r') as f:
230 output = f.read()
231 self.assertEqual(output, u'y')
232
233 # Check to make sure the linked file was copied
234 dest = os.path.join(writer.build_directory, 'c')
235 assert os.path.isfile(dest)
236 with open(dest, 'r') as f:
237 output = f.read()
238 self.assertEqual(output, 'd')
239
240 def test_relpath_default(self):
241 """Is the FilesWriter default relative path correct?"""
242
243 # Work in a temporary directory.
244 with self.create_temp_cwd():
245
246 # Create test file
247 os.mkdir('sub')
248 with open(os.path.join('sub', 'c'), 'w') as f:
249 f.write('d')
250
251 # Create the resoruces dictionary
252 res = dict(metadata=dict(path="sub"))
253
254 # Create files writer, test output
255 writer = FilesWriter()
256 writer.files = [os.path.join('sub', 'c')]
257 writer.build_directory = u'build'
258 writer.write(u'y', res, notebook_name="z")
259
260 # Check the output of the file
261 assert os.path.isdir(writer.build_directory)
262 dest = os.path.join(writer.build_directory, 'z')
263 with open(dest, 'r') as f:
264 output = f.read()
265 self.assertEqual(output, u'y')
266
267 # Check to make sure the linked file was copied
268 dest = os.path.join(writer.build_directory, 'c')
269 assert os.path.isfile(dest)
270 with open(dest, 'r') as f:
271 output = f.read()
272 self.assertEqual(output, 'd')
273
274 def test_relpath_default(self):
275 """Does the FilesWriter relpath option take precedence over the path?"""
276
277 # Work in a temporary directory.
278 with self.create_temp_cwd():
279
280 # Create test file
281 os.mkdir('sub')
282 with open(os.path.join('sub', 'c'), 'w') as f:
283 f.write('d')
284
285 # Create the resoruces dictionary
286 res = dict(metadata=dict(path="other_sub"))
287
288 # Create files writer, test output
289 writer = FilesWriter()
290 writer.files = [os.path.join('sub', 'c')]
291 writer.build_directory = u'build'
292 writer.relpath = 'sub'
293 writer.write(u'y', res, notebook_name="z")
294
295 # Check the output of the file
296 assert os.path.isdir(writer.build_directory)
297 dest = os.path.join(writer.build_directory, 'z')
298 with open(dest, 'r') as f:
299 output = f.read()
300 self.assertEqual(output, u'y')
301
302 # Check to make sure the linked file was copied
303 dest = os.path.join(writer.build_directory, 'c')
304 assert os.path.isfile(dest)
305 with open(dest, 'r') as f:
306 output = f.read()
307 self.assertEqual(output, 'd')
General Comments 0
You need to be logged in to leave comments. Login now