#!/Library/Frameworks/Python.framework/Versions/3.4/bin/python3 __author__ = 'sknop' import sys import os import os.path from collections import OrderedDict # expect # P4PORT # P4ROOT # P4CLIENT # P4CLIENT root P4CONFIG='.p4' P4ROOT='.' def update_config(filename, params): read_params = OrderedDict() with open(filename) as f: # understand and preserve comments? for line in f: (k,v) = line.split('=') read_params[k] = v read_params.update(params) create_config(filename, read_params) def create_config(filename, params): with open(filename, "w") as f: for (k,v) in params.items(): print("{}={}".format(k,v),file=f) def process_config(params): p4config = params.pop('P4CONFIG', P4CONFIG) p4root = params.pop('P4ROOT', P4ROOT) filename = os.path.join(p4root, p4config) if os.path.exists(filename): update_config(filename, params) else: # ensure the directory exists # note - for Python 2 remove exist_ok and replace with try - catch(FileExistError) block os.makedirs(p4root, exist_ok=True) create_config(filename, params) if __name__ == '__main__': params = OrderedDict((a.split('=') for a in sys.argv[1:])) print(params) process_config(params)
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 15832 | Sven Erik Knop | Updated with comment about Python 2 | ||
#2 | 15831 | Sven Erik Knop | Little tool to create test files | ||
#1 | 15653 | Sven Erik Knop |
Simple tool to populate a config file. Usage: CreateP4Config [var=value]* Example: CreateP4Config P4PORT=1666 P4USER=sknop P4CLIENT=my-client In addition, two special keywords are supported P4CONFIG (default .p4) P4ROOT (default .) |