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
Directories.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 #
4 # LSST Data Management System
5 # Copyright 2008, 2009, 2010 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 
26 from builtins import object
27 import lsst.pex.exceptions as pexExcept
28 
29 import lsst.daf.base as dafBase
30 
31 import os
32 
33 
34 class Directories(object):
35  """Determine the various directory roots that can be used by a pipeline
36  This class takes a "dir" config and a run identifier and converts the
37  values into a set of directory paths that a pipeline is allowed to use.
38 
39  Parameters
40  ----------
41  dirConfig : str
42  the "dir" config
43  shortName : str
44  the short name of the pipeline
45  runId : str, optional
46  the run ID for the pipeline run (default: "no-id")
47 
48  Examples
49  --------
50  A typical use might be:
51 
52  lookup = lsst.daf.base.PropertySet()
53  dirs = Directories(dirConfig, "rlp0220")
54  lookup = dirs.getDirs()
55 
56  The schema of the input config is expected to have the following keys:
57 
58  defaultRoot : str
59  the default root directory all files read or
60  written by pipelines deployed on this platform.
61  This must be an absolute directory. This can be
62  overriden by any of the "named role" directories
63  below.
64  runDirPattern : str
65  the pattern to use for setting the root directory
66  for a production run. The result is a directory
67  relative to the default root directory (set via
68  defaultRoot). The format is a python formatting
69  string using the following dictionary keys:
70  runid : str
71  the unique identifier for the production run
72  workDir : str
73  a named directory representing the working directory
74  where pipeline config files are deployed and the
75  pipeline is started from
76  inputDir : str
77  a named directory representing the directory to cache
78  or find input data
79  outputDir : str
80  a named directory representing the directory to write
81  output data
82  updateDir : str
83  a named directory where updatable data is deployed
84  scratchDir : str
85  a named directory for temporary files that may be deleted upon completion ofthe pipeline
86  """
87  def __init__(self, dirConfig, shortName, runId="no-id"):
88  self.config = dirConfig
89 
90  self.runid = runId
91 
92  self.shortname = shortName
93 
94  # data pattern
95  self.patdata = {"runid": self.runid, "shortname": self.shortname}
96 
97  # default root directory
98  self.defroot = None
99 
100  def getDefaultRootDir(self):
101  """ accessor to get default root directory
102 
103  Returns
104  -------
105  root : `str`
106  name of the default root directory
107  """
108  if self.defroot is not None:
109  return self.defroot
110 
111  root = self.config.defaultRoot
112  if root == ".":
113  root = os.environ["PWD"]
114  elif not os.path.isabs(root):
115  root = os.path.join(os.environ["PWD"], root)
116  self.defroot = root
117  return root
118 
119  ##
120  # @brief return the default run directory.
121  def getDefaultRunDir(self):
122  """Accessor to get the default run directory
123  This a subdirectory of the default root directory used specifically
124  for the current run of the pipeline (given as an absolute path).
125 
126  Returns
127  ------
128  rundir : `str`
129  name of the run directory
130  """
131  root = self.getDefaultRootDir()
132 
133  fmt = self.config.runDirPattern
134  runDir = fmt % self.patdata
135 
136  if os.path.isabs(runDir):
137  runDir = os.path.splitdrive(runDir)[1]
138  if runDir[0] == os.sep:
139  runDir = runDir[1:]
140 
141  return os.path.join(root, runDir)
142 
143  def getNamedDirectory(self, name):
144  """Get the absolute path to "named" directory.
145 
146  Extended Summary
147  -----------------
148  A named directory is one that is intended for a particular role
149  and accessible via a logical name. These include:
150  work the working directory (where the pipeline is started)
151  input the directory to cache or find input data
152  output the directory to write output data
153  update the directory where updateable data is deployed
154  scratch a directory for temporary files that may be
155  deleted upon completion of the pipeline.
156  This function does not check that the name is one of these, so other
157  names are supported. If a name is give that was not specified in the
158  config, the update directory is returned.
159 
160  Returns
161  -------
162  dir : `str`
163  absolute directory path
164  """
165 
166  configDict = self.config.toDict()
167  try:
168  dir = configDict[name] % self.patdata
169  except pexExcept.Exception:
170  dir = configDict["updateDir"] % self.patdata
171 
172  if not os.path.isabs(dir):
173  dir = os.path.join(self.getDefaultRunDir(), dir)
174 
175  return dir
176 
177  def getDirs(self):
178  """Return the absolute paths for the standard named directories.
179 
180  Returns
181  -------
182  out : PropertySet
183  PropertySet whose keys consist of "work", "input", "output", "update", and "scratch".
184  """
185  out = dafBase.PropertySet()
186  for name in "workDir inputDir outputDir updateDir scratchDir".split():
187  out.set(name, self.getNamedDirectory(name))
188  return out
def getDefaultRunDir
return the default run directory.
Definition: Directories.py:121
Class for storing generic metadata.
Definition: PropertySet.h:82