Created
March 30, 2020 20:22
-
-
Save frantzmiccoli/5f4de0af9a11cce14f7bab0cbac80b23 to your computer and use it in GitHub Desktop.
Small naive script to look for missing variables from a reference .env file to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Usage: | |
# envcheck source/.env.template source/.env | |
import sys | |
def main(): | |
variables_sets_list = _get_variables_set_list() | |
file_paths = sys.argv[1:] | |
reference_file_variables = variables_sets_list[0] | |
compared_sets_list = variables_sets_list[1:] | |
missing = False | |
for compared_file_variables_index in range(0, len(compared_sets_list)): | |
compared_file_variables = \ | |
compared_sets_list[compared_file_variables_index] | |
relevant_file_path = file_paths[compared_file_variables_index + 1] | |
missing_variables_message = _get_missing_variables_message( | |
reference_file_variables, compared_file_variables, | |
relevant_file_path) | |
if missing_variables_message is None: | |
continue | |
missing = True | |
print(missing_variables_message) | |
return_code = {True: 1, False: 0}[missing] | |
sys.exit(return_code) | |
def _get_variables_set_list(): | |
variables_sets_list = [] | |
file_paths = sys.argv[1:] | |
for file_path in file_paths: | |
variables_sets_list.append(_get_file_variables_from_file(file_path)) | |
if len(variables_sets_list) == 0: | |
raise Exception('Missing target files') | |
if len(variables_sets_list) == 1: | |
raise Exception('Missing files to compare with reference') | |
return variables_sets_list | |
def _get_file_variables_from_file(file_path): | |
handle = open(file_path, 'r') | |
variables = [] | |
for line in handle.readlines(): | |
parts = line.split('=') | |
if len(parts) < 2: | |
continue | |
variables.append(parts[0]) | |
handle.close() | |
return set(variables) | |
def _get_missing_variables_message(reference_set, compared_set, compared_path): | |
missing_variables_set = reference_set - compared_set | |
if len(missing_variables_set) == 0: | |
return None | |
missing_variables = list(missing_variables_set) | |
missing_variables.sort() | |
message = compared_path + ' is missing: \n\t' + "\n\t".join( | |
missing_variables) | |
return message | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment