def setup(productName, version=None, prefTags=None, productRoot=None, eupsenv=None, fwd=True, tablefile=None, exact_version=False, postTags=[]): """ Return a set of shell commands which, when sourced, will setup a product. (If fwd is false, unset it up.) Note that if the first argument is found to be an instance of Eups, this function will assume that the old setup() signature is expected by the caller, and the parameters will be forwarded to osetup(). @param productName the name of the desired product to setup. @param version the desired version of the product. This can be either a string giving an explicit version or a Tag instance. @param prefTags the list of requested tags (n.b. the VRO already knows about them) @param postTags the list of requested post-tags (n.b. the VRO already knows about them) @param productRoot the root directory where the product is installed. If set, Eups will not consult its database for the product's location, but rather set it up as a "LOCAL" product. @param eupsenv the Eups instance to use to do the setup. If None, one will be created for it. @param fwd If False, actually do an unsetup. """ if isinstance(productName, Eups): # Note: this probably won't work if a mix of key-worded and # non-keyworded parameters are used. utils.deprecated("setup(): assuming deprecated function signature", productName.quiet) if productRoot is None: productRoot = True return osetup(productName, version, prefTags, productRoot, productName.setupType) if not eupsenv: eupsenv = Eups(readCache=False, exact_version=exact_version) if version: eupsenv.selectVRO(versionName=version) if isinstance(prefTags, str): prefTags = prefTags.split() elif isinstance(prefTags, Tag): prefTags = [prefTags] if prefTags is None: prefTags = [] if postTags is None: postTags = [] if prefTags: checkTagsList(eupsenv, prefTags) if postTags: checkTagsList(eupsenv, postTags) versionRequested = version ok, version, reason = eupsenv.setup(productName, version, fwd, productRoot=productRoot, tablefile=tablefile) cmds = [] if ok: # # Check that we got the desired tag # if eupsenv.quiet <= 0 and (prefTags or postTags) and not versionRequested: taggedVersion = None for tag in prefTags: taggedVersion = eupsenv.findTaggedProduct(productName, tag) if taggedVersion: break if not taggedVersion: for tag in postTags: taggedVersion = eupsenv.findTaggedProduct(productName, tag) if taggedVersion: break if taggedVersion: if version == taggedVersion.version: # OK, we got it pass elif productRoot: # they asked for a particular directory pass else: print >> utils.stderr, "Requested version tagged %s == \"%s\"; got version \"%s\"" % \ (",".join(prefTags + postTags), taggedVersion.version, version) else: if not re.search(r"^" + Product.Product.LocalVersionPrefix, version): if (fwd and eupsenv.verbose >= 0) or (not fwd and eupsenv.verbose > 0): extra = "" if os.path.isfile((prefTags + postTags)[0]): extra = " in" print >> utils.stdwarn, "No versions of %s are tagged%s %s; setup version is %s" % \ (productName, extra, ",".join(prefTags + postTags), version) # # Set new variables # for key, val in os.environ.items(): try: if val == eupsenv.oldEnviron[key]: continue except KeyError: pass if val and not re.search(r"^['\"].*['\"]$", val) and \ re.search(r"[\s<>|&;()]", val): # quote characters that the shell cares about val = "'%s'" % val if eupsenv.shell in ("sh", "zsh",): cmd = "export %s=%s" % (key, val) elif eupsenv.shell in ("csh",): cmd = "setenv %s %s" % (key, val) if eupsenv.noaction: if eupsenv.verbose < 2 and re.search(utils.setupEnvPrefix(), key): continue # these variables are an implementation detail cmd = "echo \"%s\"" % cmd cmds += [cmd] # # unset ones that have disappeared # for key in eupsenv.oldEnviron.keys(): if re.search(r"^EUPS_(DIR|PATH)$", key): # the world will break if we delete these continue if os.environ.has_key(key): continue if eupsenv.shell == "sh" or eupsenv.shell == "zsh": cmd = "unset %s" % (key) elif eupsenv.shell == "csh": cmd = "unsetenv %s" % (key) if eupsenv.noaction: if eupsenv.verbose < 2 and re.search(utils.setupEnvPrefix(), key): continue # an implementation detail cmd = "echo \"%s\"" % cmd cmds += [cmd] # # Now handle aliases # for key in eupsenv.aliases.keys(): value = eupsenv.aliases[key] try: if value == eupsenv.oldAliases[key]: continue except KeyError: pass if eupsenv.shell == "sh": cmd = "function %s { %s ; }; export -f %s" % (key, value, key) elif eupsenv.shell == "csh": value = re.sub(r'"?\$@"?', r"\!*", value) cmd = "alias %s \'%s\'" % (key, value) elif eupsenv.shell == "zsh": cmd = "%s() { %s ; }" % (key, value, key) if eupsenv.noaction: cmd = "echo \"%s\"" % re.sub(r"`", r"\`", cmd) cmds += [cmd] # # and unset ones that used to be present, but are now gone # for key in eupsenv.oldAliases.keys(): if eupsenv.aliases.has_key(key): continue if eupsenv.shell == "sh" or eupsenv.shell == "zsh": cmd = "unset %s" % (key) elif eupsenv.shell == "csh": cmd = "unalias %s" (key) if eupsenv.noaction: cmd = "echo \"%s\"" % cmd cmds += [cmd] elif fwd and version is None: print >> utils.stderr, \ "Unable to find an acceptable version of", productName if eupsenv.verbose and os.path.exists(productName): print >> utils.stderr, "(Did you mean setup -r %s?)" % productName cmds += ["false"] # as in /bin/false else: if fwd: versionName = version if eupsenv.isLegalRelativeVersion(versionName): versionName = "" if versionName: versionName = " " + versionName print >> utils.stderr, "Failed to setup %s%s: %s" % (productName, versionName, reason) else: print >> utils.stderr, "Failed to unsetup %s: %s" % (productName, reason) cmds += ["false"] # as in /bin/false return cmds
def setup(productName, version=None, prefTags=None, productRoot=None, eupsenv=None, fwd=True, tablefile=None, exact_version=False, postTags=[]): """ Return a set of shell commands which, when sourced, will setup a product. (If fwd is false, unset it up.) Note that if the first argument is found to be an instance of Eups, this function will assume that the old setup() signature is expected by the caller, and the parameters will be forwarded to osetup(). @param productName the name of the desired product to setup. @param version the desired version of the product. This can be either a string giving an explicit version or a Tag instance. @param prefTags the list of requested tags (n.b. the VRO already knows about them) @param postTags the list of requested post-tags (n.b. the VRO already knows about them) @param productRoot the root directory where the product is installed. If set, Eups will not consult its database for the product's location, but rather set it up as a "LOCAL" product. @param eupsenv the Eups instance to use to do the setup. If None, one will be created for it. @param fwd If False, actually do an unsetup. """ if isinstance(productName, Eups): # Note: this probably won't work if a mix of key-worded and # non-keyworded parameters are used. utils.deprecated("setup(): assuming deprecated function signature", productName.quiet) if productRoot is None: productRoot = True return osetup(productName, version, prefTags, productRoot, productName.setupType) if not eupsenv: eupsenv = Eups(readCache=False, exact_version=exact_version) if version: eupsenv.selectVRO(versionName=version) if isinstance(prefTags, str): prefTags = prefTags.split() elif isinstance(prefTags, Tag): prefTags = [prefTags] if prefTags is None: prefTags = [] if postTags is None: postTags = [] if prefTags: checkTagsList(eupsenv, prefTags) if postTags: checkTagsList(eupsenv, postTags) versionRequested = version ok, version, reason = eupsenv.setup(productName, version, fwd, productRoot=productRoot, tablefile=tablefile) cmds = [] if ok: # # Check that we got the desired tag # if eupsenv.quiet <= 0 and (prefTags or postTags) and not versionRequested: taggedVersion = None for tag in prefTags: taggedVersion = eupsenv.findTaggedProduct(productName, tag) if taggedVersion: break if not taggedVersion: for tag in postTags: taggedVersion = eupsenv.findTaggedProduct(productName, tag) if taggedVersion: break if taggedVersion: if version == taggedVersion.version: # OK, we got it pass elif productRoot: # they asked for a particular directory pass else: print >> utils.stderr, "Requested version tagged %s == \"%s\"; got version \"%s\"" % \ (",".join(prefTags + postTags), taggedVersion.version, version) else: if not re.search(r"^" + Product.Product.LocalVersionPrefix, version): if (fwd and eupsenv.verbose >= 0) or (not fwd and eupsenv.verbose > 0): extra = "" if os.path.isfile((prefTags + postTags)[0]): extra = " in" print >> utils.stdwarn, "No versions of %s are tagged%s %s; setup version is %s" % \ (productName, extra, ",".join(prefTags + postTags), version) # # Set new variables # for key, val in os.environ.items(): try: if val == eupsenv.oldEnviron[key]: continue except KeyError: pass if val and not re.search(r"^['\"].*['\"]$", val) and \ re.search(r"[\s<>|&;()]", val): # quote characters that the shell cares about val = "'%s'" % val if eupsenv.shell in ( "sh", "zsh", ): cmd = "export %s=%s" % (key, val) elif eupsenv.shell in ("csh", ): cmd = "setenv %s %s" % (key, val) if eupsenv.noaction: if eupsenv.verbose < 2 and re.search(utils.setupEnvPrefix(), key): continue # these variables are an implementation detail cmd = "echo \"%s\"" % cmd cmds += [cmd] # # Extra environment variables that EUPS uses # if not fwd and productName == "eups": for k in ( "EUPS_PATH", "EUPS_PKGROOT", "EUPS_SHELL", ): if k in os.environ: del os.environ[k] # # unset ones that have disappeared # for key in eupsenv.oldEnviron.keys(): if productName != "eups": # the world will break if we delete these if re.search(r"^EUPS_(DIR|PATH|PKGROOT|SHELL)$", key): continue if os.environ.has_key(key): continue if eupsenv.shell == "sh" or eupsenv.shell == "zsh": cmd = "unset %s" % (key) elif eupsenv.shell == "csh": cmd = "unsetenv %s" % (key) if eupsenv.noaction: if eupsenv.verbose < 2 and re.search(utils.setupEnvPrefix(), key): continue # an implementation detail cmd = "echo \"%s\"" % cmd cmds += [cmd] # # Now handle aliases # for key in eupsenv.aliases.keys(): value = eupsenv.aliases[key] try: if value == eupsenv.oldAliases[key]: continue except KeyError: pass if eupsenv.shell == "sh": cmd = "function %s { %s ; }; export -f %s" % (key, value, key) elif eupsenv.shell == "csh": value = re.sub(r'"?\$@"?', r"\!*", value) cmd = "alias %s \'%s\'" % (key, value) elif eupsenv.shell == "zsh": cmd = "%s() { %s ; }" % (key, value, key) if eupsenv.noaction: cmd = "echo \"%s\"" % re.sub(r"`", r"\`", cmd) cmds += [cmd] # # and unset ones that used to be present, but are now gone # for key in eupsenv.oldAliases.keys(): if eupsenv.aliases.has_key(key): continue if eupsenv.shell == "sh" or eupsenv.shell == "zsh": cmd = "unset %s" % (key) elif eupsenv.shell == "csh": cmd = "unalias %s" (key) if eupsenv.noaction: cmd = "echo \"%s\"" % cmd cmds += [cmd] elif fwd and version is None: print >> utils.stderr, \ "Unable to find an acceptable version of", productName if eupsenv.verbose and os.path.exists(productName): print >> utils.stderr, "(Did you mean setup -r %s?)" % productName cmds += ["false"] # as in /bin/false else: if fwd: versionName = version if eupsenv.isLegalRelativeVersion(versionName): versionName = "" if versionName: versionName = " " + versionName print >> utils.stderr, "Failed to setup %s%s: %s" % ( productName, versionName, reason) else: print >> utils.stderr, "Failed to unsetup %s: %s" % (productName, reason) cmds += ["false"] # as in /bin/false return cmds
def printProducts(ostrm, productName=None, versionName=None, eupsenv=None, tags=None, setup=False, tablefile=False, directory=False, dependencies=False, showVersion=False, showName=False, depth=None, productDir=None, topological=False, checkCycles=False, raw=False): """ print out a listing of products. Returned is the number of products listed. @param ostrm the output stream to send listing to @param productName restrict the listing to this product @param versionName restrict the listing to this version of the product. @param eupsenv the Eups instance to use; if None, a default will be created. @param tags restrict the listing to products with these tag names @param setup restrict the listing to products that are currently setup (or print actually setup versions with dependencies) @param tablefile include the path to each product's table file @param directory include each product's installation directory @param dependencies print the product's dependencies @param showVersion Only print the product{'s,s'} version[s] (e.g. eups list -V -s afw) @param showName Only print the product{'s,s'} name[s] (e.g. eups list --name -t rhl) @param depth a string giving an expression for determining whether a dependency of a certain depth should be included. This string can be a simple integer which will be taken to mean, print all depths <= that integer. An expression having just a a logical operator and an integer (e.g. "> 3") implies a comparison with the depth of each dependency (i.e. "depth > 3"). @param topological List dependencies in topological-sorted order @param checkCycles Raise RuntimeError if topological sort detects a cycle @param raw Generate "raw" output (suitable for further processing) """ if not eupsenv: eupsenv = Eups() if tags: if isinstance(tags, str): tags = tags.split() checkTagsList(eupsenv, tags) elif setup and not dependencies: tags = ["setup"] # If productDir is provided only list its dependencies; we do this by setting it up if productDir: if not dependencies: raise EupsException("-r only makes sense with --dependencies") if productDir: ups_dir = os.path.join(productDir, "ups") if not os.path.isdir(ups_dir): raise EupsException("Unable to guess product name as product has no ups directory") p = utils.guessProduct(ups_dir) if productName: if p != productName: raise EupsException("Guessed product %s from ups directory, but %s from path" % \ (productName, p)) else: productName = p if tags: tag = tags[0] else: tag = None eupsenv.setup(productName, versionName, productRoot=os.path.abspath(productDir)) setup = True # only list this version productNameIsGlob = productName and re.search(r"[\[\]?*]", productName) # is productName actually a glob? productList = eupsenv.findProducts(productName, versionName, tags) if not productList: if productName: msg = productName if versionName: msg += " %s" % versionName if tags: msg += " tagged \"%s\"" % ", ".join([Tag(t).name for t in tags]) raise ProductNotFound(productName, versionName, msg="Unable to find product %s" % msg) productList.sort(lambda a,b: cmp(a, b), lambda p: ":".join([p.name, p.version])) if dependencies: _msgs = {} # maintain list of printed dependencies recursionDepth, indent = 0, "" if len(productList) > 1: if setup: productList = eupsenv.getSetupProducts(productName) else: raise EupsException("Please choose the version you want listed (%s)" % (", ".join([p.version for p in productList]))) else: if topological: raise EupsException("--topological only makes sense with --dependencies") productTags = {} # list of tags indexed by product oinfo = None # previous value of "info"; used to suppress repetitions due to e.g. # listing directories when there's a NULL and Linux declaration if depth is None: depth = "True" else: try: depth = "<= %d" % int(depth) except ValueError: pass if not re.search(r"depth", depth): depth = "depth" + depth def includeProduct(recursionDepth): """Should we include a product at this recursionDepth in the listing?""" depthExpr = VersionParser(depth) depthExpr.define("depth", recursionDepth) return depthExpr.eval() if dependencies: recursionDepth = 0 product = productList[0] if includeProduct(recursionDepth) and not (checkCycles and not topological): if raw: fmt = "%s|%s" else: fmt = "%-40s %s" print fmt % (product.name, product.version) for product, optional, recursionDepth in eupsenv.getDependentProducts(product, setup, topological=topological, checkCycles=checkCycles): if not includeProduct(recursionDepth) or (checkCycles and not topological): continue if eupsenv.verbose or not _msgs.has_key(product.name): _msgs[product.name] = product.version if not re.search(r"==", depth): indent = "| " * (recursionDepth/2) if recursionDepth%2 == 1: indent += "|" if raw: print "%s|%s" % (product.name, product.version) else: print "%-40s %s" % (("%s%s" % (indent, product.name)), product.version) return 1 # # See if some tag appears more than once; if so, they are from different stacks # tagsSeen = {} for pi in productList: for t in pi.tags: if not tagsSeen.has_key(t): tagsSeen[t] = {} if not tagsSeen[t].has_key(pi.name): tagsSeen[t][pi.name] = 0 tagsSeen[t][pi.name] += 1 # # Actually list the products # nprod = len(productList) for pi in productList: name, version, root = pi.name, pi.version, pi.stackRoot() # for convenience if root == "none": root = " (none)" info = "" if setup: if not eupsenv.isSetup(pi.name, pi.version, pi.stackRoot()): continue else: if not pi._prodStack: # only found in the environment if False: continue # Exclude environment-only products if directory or tablefile: if eupsenv.verbose: if raw: if info: info += "|" info += version else: info += "%-10s" % (version) if directory: if pi.dir: if raw and info: info += "|" info += pi.dir else: info += "" if tablefile: if info: if raw: info += "|" else: info += "\t" if pi.tablefile: info += pi.tablefile else: info += "none" elif showName: if raw: if info: info += "|" info += name else: info += "%-10s" % (name) elif showVersion: info += "%-10s" % (version) else: if raw: if info: info += "|" info += name + "|" + version else: if productName and not productNameIsGlob: info += " " else: info += "%-21s " % (name) info += "%-10s " % (version) if eupsenv.verbose: if raw: if info: info += "|" if eupsenv.verbose > 1: if raw: info += pi.flavor + "|" else: info += "%-10s" % (pi.flavor) if raw: info += root + "|" + pi.dir else: info += "%-20s %-55s" % (root, pi.dir) extra = pi.tags else: extra = [] for t in pi.tags: if not eupsenv.verbose: t = Tag(t).name # get the bare tag name, not e.g. user:foo if tagsSeen.get(t) and tagsSeen[t].get(pi.name) > 1: t = "%s[%s]" % (t, root) extra.append(t) if eupsenv.isSetup(pi.name, pi.version, pi.stackRoot()): extra += ["setup"] if raw and info: info += "|" if extra: if raw: info += ":".join(extra) else: info += "\t" + " ".join(extra) if info: if info != oinfo: print info oinfo = info return nprod
def printProducts(ostrm, productName=None, versionName=None, eupsenv=None, tags=None, setup=False, tablefile=False, directory=False, dependencies=False, showVersion=False, showName=False, depth=None, productDir=None, topological=False, checkCycles=False, raw=False): """ print out a listing of products. Returned is the number of products listed. @param ostrm the output stream to send listing to @param productName restrict the listing to this product @param versionName restrict the listing to this version of the product. @param eupsenv the Eups instance to use; if None, a default will be created. @param tags restrict the listing to products with these tag names @param setup restrict the listing to products that are currently setup (or print actually setup versions with dependencies) @param tablefile include the path to each product's table file @param directory include each product's installation directory @param dependencies print the product's dependencies @param showVersion Only print the product{'s,s'} version[s] (e.g. eups list -V -s afw) @param showName Only print the product{'s,s'} name[s] (e.g. eups list --name -t rhl) @param depth a string giving an expression for determining whether a dependency of a certain depth should be included. This string can be a simple integer which will be taken to mean, print all depths <= that integer. An expression having just a a logical operator and an integer (e.g. "> 3") implies a comparison with the depth of each dependency (i.e. "depth > 3"). @param topological List dependencies in topological-sorted order @param checkCycles Raise RuntimeError if topological sort detects a cycle @param raw Generate "raw" output (suitable for further processing) """ if not eupsenv: eupsenv = Eups() if tags: if isinstance(tags, str): tags = tags.split() checkTagsList(eupsenv, tags) elif setup and not dependencies: tags = ["setup"] # If productDir is provided only list its dependencies; we do this by setting it up if productDir: if not dependencies: raise EupsException("-r only makes sense with --dependencies") if productDir: ups_dir = os.path.join(productDir, "ups") if not os.path.isdir(ups_dir): raise EupsException( "Unable to guess product name as product has no ups directory" ) p = utils.guessProduct(ups_dir) if productName: if p != productName: raise EupsException("Guessed product %s from ups directory, but %s from path" % \ (productName, p)) else: productName = p if tags: tag = tags[0] else: tag = None eupsenv.setup(productName, versionName, productRoot=os.path.abspath(productDir)) setup = True # only list this version productNameIsGlob = productName and re.search( r"[\[\]?*]", productName) # is productName actually a glob? productList = eupsenv.findProducts(productName, versionName, tags) if not productList: if productName: msg = productName if versionName: msg += " %s" % versionName if tags: msg += " tagged \"%s\"" % ", ".join( [Tag(t).name for t in tags]) raise ProductNotFound(productName, versionName, msg="Unable to find product %s" % msg) productList.sort(lambda a, b: cmp(a, b), lambda p: ":".join([p.name, p.version])) if dependencies: _msgs = {} # maintain list of printed dependencies recursionDepth, indent = 0, "" if len(productList) > 1: if setup: productList = eupsenv.getSetupProducts(productName) else: raise EupsException( "Please choose the version you want listed (%s)" % (", ".join([p.version for p in productList]))) else: if topological: raise EupsException( "--topological only makes sense with --dependencies") productTags = {} # list of tags indexed by product oinfo = None # previous value of "info"; used to suppress repetitions due to e.g. # listing directories when there's a NULL and Linux declaration if depth is None: depth = "True" else: try: depth = "<= %d" % int(depth) except ValueError: pass if not re.search(r"depth", depth): depth = "depth" + depth def includeProduct(recursionDepth): """Should we include a product at this recursionDepth in the listing?""" depthExpr = VersionParser(depth) depthExpr.define("depth", recursionDepth) return depthExpr.eval() if dependencies: recursionDepth = 0 product = productList[0] if includeProduct(recursionDepth) and not (checkCycles and not topological): if raw: fmt = "%s|%s" else: fmt = "%-40s %s" print fmt % (product.name, product.version) for product, optional, recursionDepth in eupsenv.getDependentProducts( product, setup, topological=topological, checkCycles=checkCycles): if not includeProduct(recursionDepth) or (checkCycles and not topological): continue if eupsenv.verbose or not _msgs.has_key(product.name): _msgs[product.name] = product.version if not re.search(r"==", depth): indent = "| " * (recursionDepth / 2) if recursionDepth % 2 == 1: indent += "|" if raw: print "%s|%s" % (product.name, product.version) else: print "%-40s %s" % ( ("%s%s" % (indent, product.name)), product.version) return 1 # # See if some tag appears more than once; if so, they are from different stacks # tagsSeen = {} for pi in productList: for t in pi.tags: if not tagsSeen.has_key(t): tagsSeen[t] = {} if not tagsSeen[t].has_key(pi.name): tagsSeen[t][pi.name] = 0 tagsSeen[t][pi.name] += 1 # # Actually list the products # nprod = len(productList) for pi in productList: name, version, root = pi.name, pi.version, pi.stackRoot( ) # for convenience if root == "none": root = " (none)" info = "" if setup: if not eupsenv.isSetup(pi.name, pi.version, pi.stackRoot()): continue else: if not pi._prodStack: # only found in the environment if False: continue # Exclude environment-only products if directory or tablefile: if eupsenv.verbose: if raw: if info: info += "|" info += version else: info += "%-10s" % (version) if directory: if pi.dir: if raw and info: info += "|" info += pi.dir else: info += "" if tablefile: if info: if raw: info += "|" else: info += "\t" if pi.tablefile: info += pi.tablefile else: info += "none" elif showName: if raw: if info: info += "|" info += name else: info += "%-10s" % (name) elif showVersion: info += "%-10s" % (version) else: if raw: if info: info += "|" info += name + "|" + version else: if productName and not productNameIsGlob: info += " " else: info += "%-21s " % (name) info += "%-10s " % (version) if eupsenv.verbose: if raw: if info: info += "|" if eupsenv.verbose > 1: if raw: info += pi.flavor + "|" else: info += "%-10s" % (pi.flavor) if raw: info += root + "|" + pi.dir else: info += "%-20s %-55s" % (root, pi.dir) extra = pi.tags else: extra = [] for t in pi.tags: if not eupsenv.verbose: t = Tag( t).name # get the bare tag name, not e.g. user:foo if tagsSeen.get(t) and tagsSeen[t].get(pi.name) > 1: t = "%s[%s]" % (t, root) extra.append(t) if eupsenv.isSetup(pi.name, pi.version, pi.stackRoot()): extra += ["setup"] if raw and info: info += "|" if extra: if raw: info += ":".join(extra) else: info += "\t" + " ".join(extra) if info: if info != oinfo: print info oinfo = info return nprod