Skip to content

Commit 87e1e11

Browse files
committed
Update TUTORIAL and test_tutorial
Improve the coding style in TUTORIAL in the case where absolute path to a file is needed to perform file system access and at the same time is rejected by Targets methods. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 36f0539 commit 87e1e11

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

docs/TUTORIAL.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,11 @@ the target filepaths to metadata.
371371
# the target's filepath, hash, and length.
372372
# Note: target path passed to add_target() method has to be relative
373373
# to the targets directory or an exception is raised.
374-
>>> target4_filepath = os.path.abspath("repository/targets/myproject/file4.txt")
375-
>>> octal_file_permissions = oct(os.stat(target4_filepath).st_mode)[4:]
374+
>>> target4_filepath = 'myproject/file4.txt'
375+
>>> target4_abspath = os.path.abspath(os.path.join('repository', 'targets', target4_filepath))
376+
>>> octal_file_permissions = oct(os.stat(target4_abspath).st_mode)[4:]
376377
>>> custom_file_permissions = {'file_permissions': octal_file_permissions}
377-
>>> repository.targets.add_target('myproject/file4.txt', custom_file_permissions)
378+
>>> repository.targets.add_target(target4_filepath, custom_file_permissions)
378379
```
379380

380381
The private keys of roles affected by the changes above must now be imported and

tests/test_tutorial.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,27 +224,26 @@ def test_tutorial(self):
224224

225225
repository = load_repository('repository')
226226

227-
# List of targets is hardcoded since get_filepaths_in_directory()
228-
# returns absolute paths and add_targets() raises an exception.
227+
# TODO: replace the hard-coded list of targets with a helper
228+
# method that returns a list of normalized relative target paths
229229
list_of_targets = ['file1.txt', 'file2.txt', 'file3.txt']
230230

231-
232231
repository.targets.add_targets(list_of_targets)
233232

234233
self.assertTrue('file1.txt' in repository.targets.target_files)
235234
self.assertTrue('file2.txt' in repository.targets.target_files)
236235
self.assertTrue('file3.txt' in repository.targets.target_files)
237236

238-
239-
target4_filepath = os.path.abspath(os.path.join(
240-
'repository', 'targets', 'myproject', 'file4.txt'))
241-
octal_file_permissions = oct(os.stat(target4_filepath).st_mode)[4:]
237+
target4_filepath = 'myproject/file4.txt'
238+
target4_abspath = os.path.abspath(os.path.join(
239+
'repository', 'targets', target4_filepath))
240+
octal_file_permissions = oct(os.stat(target4_abspath).st_mode)[4:]
242241
custom_file_permissions = {'file_permissions': octal_file_permissions}
243-
repository.targets.add_target('myproject/file4.txt', custom_file_permissions)
242+
repository.targets.add_target(target4_filepath, custom_file_permissions)
244243
# Note that target filepaths specified in the repo use '/' even on Windows.
245244
# (This is important to make metadata platform-independent.)
246245
self.assertTrue(
247-
os.path.join('myproject/file4.txt') in repository.targets.target_files)
246+
os.path.join(target4_filepath) in repository.targets.target_files)
248247

249248

250249
# Skipping user entry of password

0 commit comments

Comments
 (0)