LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
Classes | Functions | Variables
lsst.base.packages Namespace Reference

Classes

class  Packages
 

Functions

def getVersionFromPythonModule
 
def getPythonPackages
 
def getEnvironmentPackages
 

Variables

list __all__ = ["getVersionFromPythonModule", "getPythonPackages", "getEnvironmentPackages", "Packages"]
 
tuple BUILDTIME = set(["boost", "eigen", "tmv"])
 
tuple PYTHON = set(["galsim"])
 
tuple ENVIRONMENT = set(["astrometry_net", "astrometry_net_data", "minuit2", "xpa"])
 
 _eups = None
 

Detailed Description

Determine which packages are being used in the system and their versions

There are a few different types of packages, and their versions are collected in different ways:
1. Run-time libraries (e.g., cfitsio, fftw): we get their version from interrogating the dynamic library
2. Python modules (e.g., afw, numpy; galsim is also in this group even though we only use it through the
   library, because no version information is currently provided through the library): we get their version
   from the __version__ module variable. Note that this means that we're only aware of modules that have
   already been imported.
3. Other packages provide no run-time accessible version information (e.g., astrometry_net): we get their
   version from interrogating the environment. Currently, that means EUPS; if EUPS is replaced or dropped then
   we'll need to consider an alternative means of getting this version information.
4. Local versions of packages (a non-installed EUPS package, selected with "setup -r /path/to/package"): we
   identify these through the environment (EUPS again) and use as a version the path supplemented with the
   git SHA and, if the git repo isn't clean, an MD5 of the diff.

These package versions are collected and stored in a Packages object, which provides useful comparison and
persistence features.

Example usage:

from lsst.base import Packages
pkgs = Packages.fromSystem()
print "Current packages:", pkgs
old = Packages.read("/path/to/packages.pickle")
print "Old packages:", old
print "Missing packages compared to before:", pkgs.missing(old)
print "Extra packages compared to before:", pkgs.extra(old)
print "Different packages: ", pkgs.difference(old)
old.update(pkgs)  # Include any new packages in the old
old.write("/path/to/packages.pickle")

Function Documentation

def lsst.base.packages.getEnvironmentPackages ( )
Provide a dict of products and their versions from the environment

We use EUPS to determine the version of certain products (those that don't provide
a means to determine the version any other way) and to check if uninstalled packages
are being used. We only report the product/version for these packages.

Definition at line 132 of file packages.py.

134  """Provide a dict of products and their versions from the environment
135 
136  We use EUPS to determine the version of certain products (those that don't provide
137  a means to determine the version any other way) and to check if uninstalled packages
138  are being used. We only report the product/version for these packages.
139  """
140  try:
141  from eups import Eups
142  from eups.Product import Product
143  except:
144  from lsst.pex.logging import getDefaultLog
145  getDefaultLog().warn("Unable to import eups, so cannot determine package versions from environment")
146  return {}
147 
148  # Cache eups object since creating it can take a while
149  global _eups
150  if not _eups:
151  _eups = Eups()
152  products = _eups.findProducts(tags=["setup"])
153 
154  # Get versions for things we can't determine via runtime mechanisms
155  # XXX Should we just grab everything we can, rather than just a predetermined set?
156  packages = {prod.name: prod.version for prod in products if prod in ENVIRONMENT}
157 
158  # The string 'LOCAL:' (the value of Product.LocalVersionPrefix) in the version name indicates uninstalled
159  # code, so the version could be different than what's being reported by the runtime environment (because
160  # we don't tend to run "scons" every time we update some python file, and even if we did sconsUtils
161  # probably doesn't check to see if the repo is clean).
162  for prod in products:
163  if not prod.version.startswith(Product.LocalVersionPrefix):
164  continue
165  ver = prod.version
166 
167  gitDir = os.path.join(prod.dir, ".git")
168  if os.path.exists(gitDir):
169  # get the git revision and an indication if the working copy is clean
170  revCmd = ["git", "--git-dir=" + gitDir, "--work-tree=" + prod.dir, "rev-parse", "HEAD"]
171  diffCmd = ["git", "--no-pager", "--git-dir=" + gitDir, "--work-tree=" + prod.dir, "diff",
172  "--patch"]
173  try:
174  rev = subprocess.check_output(revCmd).decode().strip()
175  diff = subprocess.check_output(diffCmd)
176  except:
177  ver += "@GIT_ERROR"
178  else:
179  ver += "@" + rev
180  if diff:
181  ver += "+" + hashlib.md5(diff).hexdigest()
182  else:
183  ver += "@NO_GIT"
184 
185  packages[prod.name] = ver
186  return packages
187 
def getEnvironmentPackages
Definition: packages.py:132
def warn
Definition: log.py:99
def lsst.base.packages.getPythonPackages ( )
Return a dict of imported python packages and their versions

We wade through sys.modules and attempt to determine the version for each
module.  Note, therefore, that we can only report on modules that have
*already* been imported.

We don't include any module for which we cannot determine a version.

Definition at line 82 of file packages.py.

82 
83 def getPythonPackages():
84  """Return a dict of imported python packages and their versions
85 
86  We wade through sys.modules and attempt to determine the version for each
87  module. Note, therefore, that we can only report on modules that have
88  *already* been imported.
89 
90  We don't include any module for which we cannot determine a version.
91  """
92  # Attempt to import libraries that only report their version in python
93  for module in PYTHON:
94  try:
95  importlib.import_module(module)
96  except:
97  pass # It's not available, so don't care
98 
99  packages = {"python": sys.version}
100  # Not iterating with sys.modules.iteritems() because it's not atomic and subject to race conditions
101  moduleNames = list(sys.modules.keys())
102  for name in moduleNames:
103  module = sys.modules[name]
104  try:
105  ver = getVersionFromPythonModule(module)
106  except:
107  continue # Can't get a version from it, don't care
108 
109  # Remove "foo.bar.version" in favor of "foo.bar"
110  # This prevents duplication when the __init__.py includes "from .version import *"
111  found = False
112  for ending in (".version", "._version"):
113  if name.endswith(ending):
114  name = name[:-len(ending)]
115  if name in packages:
116  assert ver == packages[name]
117  found = True
118  elif name in packages:
119  assert ver == packages[name]
120 
121  # Use LSST package names instead of python module names
122  # This matches the names we get from the environment (i.e., EUPS) so we can clobber these build-time
123  # versions if the environment reveals that we're not using the packages as-built.
124  if "lsst" in name:
125  name = name.replace("lsst.", "").replace(".", "_")
126 
127  packages[name] = ver
128 
129  return packages
130 
def getVersionFromPythonModule
Definition: packages.py:62
def lsst.base.packages.getVersionFromPythonModule (   module)
Determine the version of a python module

Will raise AttributeError if the __version__ attribute is not set.

We supplement the version with information from the __dependency_versions__
(a specific variable set by LSST's sconsUtils at build time) only for packages
that are typically used only at build-time.

Definition at line 62 of file packages.py.

62 
63 def getVersionFromPythonModule(module):
64  """Determine the version of a python module
65 
66  Will raise AttributeError if the __version__ attribute is not set.
67 
68  We supplement the version with information from the __dependency_versions__
69  (a specific variable set by LSST's sconsUtils at build time) only for packages
70  that are typically used only at build-time.
71  """
72  version = module.__version__
73  if hasattr(module, "__dependency_versions__"):
74  # Add build-time dependencies
75  deps = module.__dependency_versions__
76  buildtime = BUILDTIME & set(deps.keys())
77  if buildtime:
78  version += " with " + " ".join("%s=%s" % (pkg, deps[pkg])
79  for pkg in sorted(buildtime))
80  return version
81 
def getVersionFromPythonModule
Definition: packages.py:62

Variable Documentation

list lsst.base.packages.__all__ = ["getVersionFromPythonModule", "getPythonPackages", "getEnvironmentPackages", "Packages"]

Definition at line 47 of file packages.py.

lsst.base.packages._eups = None

Definition at line 131 of file packages.py.

tuple lsst.base.packages.BUILDTIME = set(["boost", "eigen", "tmv"])

Definition at line 51 of file packages.py.

tuple lsst.base.packages.ENVIRONMENT = set(["astrometry_net", "astrometry_net_data", "minuit2", "xpa"])

Definition at line 59 of file packages.py.

tuple lsst.base.packages.PYTHON = set(["galsim"])

Definition at line 55 of file packages.py.