Skip to content

Commit

Permalink
Update TUTORIAL and test_tutorial
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
sechkova committed Apr 7, 2020
1 parent 1bfa00b commit 6d8a84d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
7 changes: 4 additions & 3 deletions docs/TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,11 @@ the target filepaths to metadata.
# the target's filepath, hash, and length.
# Note: target path passed to add_target() method has to be relative
# to the targets directory or an exception is raised.
>>> target4_filepath = os.path.abspath("repository/targets/myproject/file4.txt")
>>> octal_file_permissions = oct(os.stat(target4_filepath).st_mode)[4:]
>>> target4_filepath = 'myproject/file4.txt'
>>> target4_abspath = os.path.abspath(os.path.join('repository', 'targets', target4_filepath))
>>> octal_file_permissions = oct(os.stat(target4_abspath).st_mode)[4:]
>>> custom_file_permissions = {'file_permissions': octal_file_permissions}
>>> repository.targets.add_target('myproject/file4.txt', custom_file_permissions)
>>> repository.targets.add_target('target4_filepath', custom_file_permissions)
```

The private keys of roles affected by the changes above must now be imported and
Expand Down
17 changes: 8 additions & 9 deletions tests/test_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,26 @@ def test_tutorial(self):

repository = load_repository('repository')

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


repository.targets.add_targets(list_of_targets)

self.assertTrue('file1.txt' in repository.targets.target_files)
self.assertTrue('file2.txt' in repository.targets.target_files)
self.assertTrue('file3.txt' in repository.targets.target_files)


target4_filepath = os.path.abspath(os.path.join(
'repository', 'targets', 'myproject', 'file4.txt'))
octal_file_permissions = oct(os.stat(target4_filepath).st_mode)[4:]
target4_filepath = 'myproject/file4.txt'
target4_abspath = os.path.abspath(os.path.join(
'repository', 'targets', target4_filepath))
octal_file_permissions = oct(os.stat(target4_abspath).st_mode)[4:]
custom_file_permissions = {'file_permissions': octal_file_permissions}
repository.targets.add_target('myproject/file4.txt', custom_file_permissions)
repository.targets.add_target(target4_filepath, custom_file_permissions)
# Note that target filepaths specified in the repo use '/' even on Windows.
# (This is important to make metadata platform-independent.)
self.assertTrue(
os.path.join('myproject/file4.txt') in repository.targets.target_files)
os.path.join(target4_filepath) in repository.targets.target_files)


# Skipping user entry of password
Expand Down

0 comments on commit 6d8a84d

Please sign in to comment.