88import logging
99import os
1010import shutil
11+ from pathlib import Path
1112
1213import click
1314
@@ -96,7 +97,7 @@ def initialize(
9697 if not access_level_overrides_file :
9798 overrides_file = LOCAL_ACCESS_OVERRIDES_FILE
9899 else :
99- overrides_file = access_level_overrides_file
100+ overrides_file = Path ( access_level_overrides_file )
100101 # Create the config directory
101102 database_path = create_policy_sentry_config_directory ()
102103
@@ -107,15 +108,15 @@ def initialize(
107108 # provided by AWS documentation
108109 file_list = [
109110 f
110- for f in os . listdir ( BUNDLED_DATA_DIRECTORY )
111- if os . path . isfile ( os . path . join ( BUNDLED_DATA_DIRECTORY , f ) )
111+ for f in BUNDLED_DATA_DIRECTORY . iterdir ( )
112+ if ( BUNDLED_DATA_DIRECTORY / f ). is_file ( )
112113 ]
113114
114115 for file in file_list :
115- if file .endswith ( ".yml" ) :
116- shutil .copy (os . path . join ( BUNDLED_DATA_DIRECTORY , file ) , CONFIG_DIRECTORY )
116+ if file .suffix == ".yml" :
117+ shutil .copy (BUNDLED_DATA_DIRECTORY / file , CONFIG_DIRECTORY )
117118 logger .debug ("copying overrides file %s to %s" , file , CONFIG_DIRECTORY )
118- print ("Database will be stored here: " + database_path )
119+ print (f "Database will be stored here: { database_path } " )
119120
120121 if not build and not fetch :
121122 # copy from the bundled database location to the destination path
@@ -140,24 +141,21 @@ def initialize(
140141 logger .debug (", " .join (all_aws_service_prefixes ))
141142
142143
143- def create_policy_sentry_config_directory () -> str :
144+ def create_policy_sentry_config_directory () -> Path :
144145 """
145146 Creates a config directory at $HOME/.policy_sentry/
146147 :return: the path of the database file
147148 """
148149 print ("Creating the database..." )
149150 logger .debug (f"We will store the new database here: { DATASTORE_FILE_PATH } " )
150151 # If the database file already exists, remove it
151- if os . path . exists (LOCAL_DATASTORE_FILE_PATH ):
152+ if LOCAL_DATASTORE_FILE_PATH . exists ():
152153 logger .debug (
153154 f"The database at { DATASTORE_FILE_PATH } already exists. Removing and replacing it."
154155 )
155- os .remove (LOCAL_DATASTORE_FILE_PATH )
156- elif os .path .exists (CONFIG_DIRECTORY ):
157- pass
158- # If the config directory does not exist
156+ LOCAL_DATASTORE_FILE_PATH .unlink ()
159157 else :
160- os .mkdir (CONFIG_DIRECTORY )
158+ CONFIG_DIRECTORY .mkdir (exist_ok = True )
161159 return LOCAL_DATASTORE_FILE_PATH
162160
163161
0 commit comments