##// END OF EJS Templates
Add relpath option to FilesWriter
Jessica B. Hamrick -
Show More
@@ -25,6 +25,12 b' class FilesWriter(WriterBase):'
25 25 help="""Directory to write output to. Leave blank
26 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).""")
33
28 34
29 35 # Make sure that the output directory exists.
30 36 def _build_directory_changed(self, name, old, new):
@@ -81,8 +87,14 b' class FilesWriter(WriterBase):'
81 87 # Copy files that match search pattern
82 88 for matching_filename in glob.glob(filename):
83 89
90 # compute the relative path for the filename
91 if self.relpath:
92 dest_filename = os.path.relpath(matching_filename, self.relpath)
93 else:
94 dest_filename = matching_filename
95
84 96 # Make sure folder exists.
85 dest = os.path.join(self.build_directory, matching_filename)
97 dest = os.path.join(self.build_directory, dest_filename)
86 98 path = os.path.dirname(dest)
87 99 self._makedir(path)
88 100
@@ -201,3 +201,38 b' class Testfiles(TestsBase):'
201 201 with open(dest, 'r') as f:
202 202 output = f.read()
203 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')
General Comments 0
You need to be logged in to leave comments. Login now