LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
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 import lsst.pex.policy as Policy
27 import lsst.pex.exceptions as pexExcept
28 
29 import lsst.daf.base as dafBase
30 from lsst.daf.base import *
31 
32 import os
33 
34 ##
35 # @brief a determination of the various directory roots that can be
36 # used by a pipeline.
37 #
38 # This class takes a "dir" policy (found in platform policies or pipeline
39 # configuration policies) and a run identifier and converts the values into
40 # a set of directory paths that a pipeline is allowed to use. A typical
41 # use might be:
42 # @code
43 # lookup = lsst.daf.base.PropertySet()
44 # dirs = Directories(dirPolicy, "rlp0220")
45 # lookup = dirs.getDirs()
46 # @endcode
47 #
48 # The schema of the input policy is expected to have the following keys:
49 # @verbatim
50 # defaultRoot the default root directory all files read or
51 # written by pipelines deployed on this platform.
52 # This must be an absolute directory. This can be
53 # overriden by any of the "named role" directories
54 # below.
55 # runDirPattern: the pattern to use for setting the root directory
56 # for a production run. The result is a directory
57 # relative to the default root directory (set via
58 # defaultRoot). The format is a python formatting
59 # string using the following dictionary keys:
60 # runid the unique identifier for the
61 # production run
62 # work a named directory representing the working directory
63 # where pipeline policy files are deployed and the
64 # pipeline is started from
65 # input a named directory representing the directory to cache
66 # or find input data
67 # output a named directory representing the directory to write
68 # output data
69 # update a named directory where updatable data is deployed
70 # scratch a named directory for temporary files that may be
71 # deleted upon completion ofthe pipeline
72 # @endverbatim
73 class Directories(object):
74 
75  ##
76  # @brief determine the directories from the policy input
77  # @param dirPolicy the "dir" policy containing the
78  # @param shortName the short name of the pipeline
79  # @param runId the run ID for the pipeline run (default: "no-id")
80  def __init__(self, dirPolicy, shortName, runId="no-id"):
81  self.policy = dirPolicy
82  self.runid = runId
83  self.shortname = shortName
84  self.patdata = { "runid": self.runid, "shortname": self.shortname }
85  self.defroot = None
86 
87  ##
88  # @brief return the default root directory
89  def getDefaultRootDir(self):
90  if self.defroot is not None:
91  return self.defroot
92 
93  root = self.policy.getString("defaultRoot")
94  if root == ".":
95  root = os.environ["PWD"]
96  elif not os.path.isabs(root):
97  root = os.path.join(os.environ["PWD"], root)
98  self.defroot = root
99  return root
100 
101  ##
102  # @brief return the default run directory.
103  # This a subdirectory of the default root directory used specifically
104  # for the current run of the pipeline (given as an absolute path).
105  def getDefaultRunDir(self):
106  root = self.getDefaultRootDir()
107 
108  fmt = self.policy.getString("runDirPattern")
109  runDir = fmt % self.patdata
110 
111  if os.path.isabs(runDir):
112  runDir = os.path.splitdrive(runDir)[1]
113  if runDir[0] == os.sep:
114  runDir = runDir[1:]
115 
116  return os.path.join(root, runDir)
117 
118  ##
119  # @brief return the absolute path to "named" directory.
120  # A named directory is one that is intended for a particular role
121  # and accessible via a logical name. These include:
122  # @verbatim
123  # work the working directory (where the pipeline is started)
124  # input the directory to cache or find input data
125  # output the directory to write output data
126  # update the directory where updateable data is deployed
127  # scratch a directory for temporary files that may be
128  # deleted upon completion of the pipeline.
129  # @endverbatim
130  # This function does not check that the name is one of these, so other
131  # names are supported. If a name is give that was not specified in the
132  # policy file, the update directory is returned.
133  def getNamedDirectory(self, name):
134  try:
135  dir = self.policy.getString(name) % self.patdata
136  except pexExcept.exceptionsLib.Exception as e:
137  dir = self.policy.getString("update") % self.patdata
138 
139  if not os.path.isabs(dir):
140  dir = os.path.join(self.getDefaultRunDir(), dir)
141 
142  return dir
143 
144  ##
145  # return the absolute paths for the standard named directories as a
146  # PropertySet. The keys will include "work", "input", "output",
147  # "update", and "scratch".
148  def getDirs(self):
150  for name in "work input output update scratch".split():
151  out.set(name, self.getNamedDirectory(name))
152  return out
153 
def getDefaultRunDir
return the default run directory.
Definition: Directories.py:105
def getDirs
return the absolute paths for the standard named directories as a PropertySet.
Definition: Directories.py:148
def __init__
determine the directories from the policy input
Definition: Directories.py:80
def getNamedDirectory
return the absolute path to &quot;named&quot; directory.
Definition: Directories.py:133
def getDefaultRootDir
return the default root directory
Definition: Directories.py:89
a determination of the various directory roots that can be used by a pipeline.
Definition: Directories.py:73
Class for storing generic metadata.
Definition: PropertySet.h:82