#!/usr/bin/env python # # Script to generate P4CONFIG files # # Input: # Workspace root # P4PORT # P4CLIENT # P4USER from __future__ import print_function import sys import os, os.path from subprocess import Popen, PIPE import re P4="p4" class ConfigGenerator: def __init__(self, root, port, client, user): self.root = root self.port = port self.client = client self.user = user self.dict = {} self.comments = [] self.updated = False def create(self): if not os.path.isdir(self.root): os.makedirs(self.root) os.chdir(self.root) print("Creating P4CONFIG at %s with \n\tPort\t= %s\n\tClient\t= %s\n\tUser\t= %s" % (self.root, self.port, self.client, self.user)) configFile, configPath = self.findConfig() if not configPath or configPath == "noconfig": # config file does not exist yet configPath = os.path.join(self.root, configFile) else: print("Found %s" % configPath) if os.path.exists(configPath): with open(configPath) as f: for line in f: if line.startswith('#'): self.comments.append(line) elif "=" in line: key,value = line.split('=') self.dict[key]=value.rstrip() self.set("P4PORT", self.port) self.set("P4CLIENT", self.client) self.set("P4USER", self.user) if self.updated: with open(configPath, 'w') as f: for comment in self.comments: f.write(comment) for key, value in self.dict.iteritems(): f.write(key+'='+value + "\n") print("Created or updated %s" % configPath) def set(self, key, value): if not key in self.dict or self.dict[key] != value: self.dict[key] = value self.updated = True def findConfig(self): with Popen([P4, "-d", self.root, "set", "P4CONFIG"], stdout = PIPE).stdout as s: output = s.read() # match either # P4CONFIG=configfile\n # or # P4CONFIG=configfile (config 'configpath')\n r = re.compile("P4CONFIG=([\w.]+)(\s*\(\w+\)\s*)?( \(config '(.*)'\))?%s" % os.linesep) m = r.match(output) if m: config = m.group(1) path = m.group(4) return config, path else: print("Cannot extract config file from >%s<" % output) sys.exit(1) if __name__ == '__main__': if len(sys.argv) < 4: print("Usage: ConfigGenerator ws_root P4PORT P4CLIENT P4USER") print("Got parameters: ", sys.argv) sys.exit(1) root = sys.argv[1] port = sys.argv[2] client = sys.argv[3] user = sys.argv[4] c = ConfigGenerator(root, port, client, user) c.create()
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 8808 | Robert Cowham | Branching using cowhamr.sven.utils | ||
//guest/sven_erik_knop/P4Pythonlib/scripts/ConfigGenerator.py | |||||
#2 | 8183 | Sven Erik Knop |
Fixed problems on Windows: P4="p4" usually sufficient (overwrite if p4 not in path) os.linesep instead of "\n" in the regular expression (should have rstrip'd the output instead) |
||
#1 | 8182 | Sven Erik Knop |
ConfigGenerator.py Mark 1 A simple Python tool for P4V that takes four parameters: root PORT CLIENT USER and generates a config file out of it. Requires that P4CONFIG is set either in the environment or registry and accessible from P4V (Mac users need to use p4 set) Also requires a simple custom tool with these parameters: Application: /path/to/ConfigGenerator.py Arguments = $r $p $c $u Run in terminal window recommended for debugging purposes Tested with Python 2.7, but should run with at least 2.6 and 3.X (not tested) |