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