Dakota  Version
Explore and Predict with Confidence
Static Public Member Functions | Private Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
WorkdirHelper Class Reference

Static Public Member Functions

static void initialize ()
 initialize (at runtime) cached values for paths and environment More...
 
static const std::string & startup_pwd ()
 Query for dakota's startup $PWD.
 
static void change_directory (const bfs::path &new_dir)
 change current directory
 
static void prepend_preferred_env_path (const std::string &extra_path)
 Prepend cached preferredEnvPath with extra_path and update $PATH environment variable. More...
 
static void set_environment (const std::string &env_name, const std::string &env_val, bool overwrite_flag=true)
 Set an environment variable.
 
static bfs::path which (const std::string &driver_name)
 Returns the bfs::path for the analysis driver, supporting typical windows filename extensions, or empty if not found. More...
 
static bfs::path rel_to_abs (const bfs::path &subdir_path)
 get a valid absolute bfs::path to a subdirectory relative to rundir
 
static StringArray tokenize_driver (const String &user_an_driver)
 tokenize a white-space separated analysis driver, respecting escapes and nested quotes
 
static bool resolve_driver_path (String &an_driver)
 parse off the first whitespace-separated entry in the user's analysis_driver, and convert it to an absolute path if it begins with ./ or ../, replacing the passed string if needed. Returns true if the first token was modified.
 
static void split_wildcard (const std::string &path_with_wc, bfs::path &search_dir, bfs::path &wild_card)
 given a string with an optional path and a wildcard, e.g., /tmp/D*.?pp, parse it into the search path /tmp (default .) and the wildcard D*.?pp. Return wild_card as path to reduce wstring conversions More...
 
static bfs::path concat_path (const bfs::path &p_in, const String &tag)
 concatenate a string onto the end of a path More...
 
static bfs::path system_tmp_file (const std::string &prefix)
 generate a valid temporary file name prefix_%%%%%%%%
 
static bfs::path system_tmp_path ()
 get the system tmp path, e.g., /tmp or C:\temp
 
static bool create_directory (const bfs::path &dir_path, short mkdir_option)
 Create a directory, with options for remove or error. More...
 
static void recursive_remove (const bfs::path &rm_path, short fileop_option)
 Remove a path (file, directory, or symlink) without regard to its type. Only error if existed and there's an error in the remove.
 
static void rename (const bfs::path &old_path, const bfs::path &new_path, short fileop_option)
 Rename a file, catching any errors and optionally warning/erroring.
 
static void link_items (const StringArray &source_itemss, const bfs::path &dest_dir, bool overwrite)
 top-level link a list of source_paths (files, directories, symlinks), potentially including wildcards, from destination_dir, which must exist More...
 
static void copy_items (const StringArray &source_items, const bfs::path &dest_dir, bool overwrite)
 copy a list of source_paths (files, directories, symlinks), potentially including wildcards into destination_dir, which must exist More...
 
static void prepend_path_items (const StringArray &source_items)
 prepend any directories (including wildcards) found in source_items to the preferred environment path; this will update cached preferred path and PATH
 
static bool check_equivalent_dest (const StringArray &source_items, const bfs::path &dest_dir)
 check whether any of the passed source items are filesystem equivalent to the destination path, return true if any one is equivalent to dest
 
static bool find_driver (const StringArray &source_items, const bfs::path &search_driver)
 check whether the any of the passed source items (possibly including wildcards to be expanded) matches the passed search driver
 
static bool link (const bfs::path &src_path, const bfs::path &dest_dir, bool overwrite)
 create link from dest_dir/src_path.filename() to a single path (file, dir, link) in source directory More...
 
static bool recursive_copy (const bfs::path &src_path, const bfs::path &dest_dir, bool overwrite)
 Recrusive copy of src_path into dest_dir, with optional top-level overwrite (remove/recreate) of dest_dir/src_path.filename() More...
 
static bool prepend_path_item (const bfs::path &src_path, const bfs::path &dest_dir, bool overwrite)
 prepend the preferred env path with source path if it's a directory; this will update cached preferred path and manipulate PATH More...
 
static bool check_equivalent (const bfs::path &src_path, const bfs::path &dest_dir, bool overwrite)
 return true if the src and dest are filesystem equivalent
 
static bool find_file (const bfs::path &src_path, const bfs::path &search_file, bool overwrite)
 return true if the src_path is a regular file and has same filename as search_file
 
static bool file_op_items (const file_op_function &file_op, const StringArray &source_paths, const bfs::path &dest_dir, bool overwrite)
 recursively perform file_op (copy, path adjust, etc.) on a list of source_paths (files, directories, symlinks), which potentially include wildcards, w.r.t. destination_dir More...
 
static void set_preferred_path ()
 set/reset PATH to dakPreferredEnvPath
 
static void set_preferred_path (const boost::filesystem::path &extra_path)
 set PATH to absolute(extra_path):dakPreferredEnvPath, without changing cached preferred PATH More...
 
static void reset ()
 Resets the working directory "state" to its initial state when DAKOTA was launched.
 

Private Member Functions

 WorkdirHelper ()
 default constructor
 
 WorkdirHelper (const WorkdirHelper &)
 copy constructor
 
 ~WorkdirHelper ()
 destructor
 
WorkdirHelperoperator= (const WorkdirHelper &)
 assignment operator
 

Static Private Member Functions

static bfs::path po_which (const std::string &driver_name)
 Returns the bfs::path for the analysis driver - POSIX-style implementation, returns empty if not found. More...
 
static std::string init_startup_path ()
 Initializes class member, startupPATH. More...
 
static std::string init_preferred_env_path ()
 Initializes class member, dakPreferredEnvPath. More...
 
static std::vector< std::string > tokenize_env_path (const std::string &path)
 Tokenizes $PATH environment variable into a "list" of directories. More...
 

Static Private Attributes

static std::string startupPWD = "."
 Value of $PWD var upon entry to dakota main()
 
static std::string startupPATH = "."
 Value of $PATH (PATH% on windows) var upon entry to dakota main(), omitting any leading PATH= or Path=.
 
static std::string dakPreferredEnvPath = "."
 Dakota preferred search PATH/Path = ".:startupPWD:startupPATH", omitting any leading PATH= or Path=.
 

Detailed Description

Utility class for cross-platform management of environment and paths. Including directory and file operations. On initialization, this class does not manipulate the present working directory, nor the PATH environment variable, but stores context to manipulate them later.

Member Function Documentation

◆ initialize()

void initialize ( )
static

initialize (at runtime) cached values for paths and environment

Initialize defers calls to Boost filesystem utilities until runtime (required on some operating systems.

References WorkdirHelper::dakPreferredEnvPath, WorkdirHelper::init_preferred_env_path(), WorkdirHelper::init_startup_path(), WorkdirHelper::startupPATH, and WorkdirHelper::startupPWD.

Referenced by Environment::Environment().

◆ prepend_preferred_env_path()

void prepend_preferred_env_path ( const std::string &  extra_path)
static

Prepend cached preferredEnvPath with extra_path and update $PATH environment variable.

Overwrites $PATH with an additional directory prepended, typically for the purpose of ensuring templatedir is in the $PATH; updates cached preferred PATH and environment PATH, so exercise caution with repeated calls.

References WorkdirHelper::dakPreferredEnvPath, WorkdirHelper::set_environment(), and WorkdirHelper::startupPWD.

Referenced by WorkdirHelper::prepend_path_item().

◆ which()

bfs::path which ( const std::string &  driver_name)
static

Returns the bfs::path for the analysis driver, supporting typical windows filename extensions, or empty if not found.

Uses string representing $PATH to locate an analysis driver on the host computer. Returns the path to the driver (as a string)

This version is a wrapper over the "plain ol' which" implementation, allowing an array of windows, 3-letter extensions to be checked.

References Dakota::get_pathext(), and WorkdirHelper::po_which().

Referenced by NIDRProblemDescDB::check_driver().

◆ split_wildcard()

void split_wildcard ( const std::string &  path_with_wc,
bfs::path &  search_dir,
bfs::path &  wild_card 
)
static

given a string with an optional path and a wildcard, e.g., /tmp/D*.?pp, parse it into the search path /tmp (default .) and the wildcard D*.?pp. Return wild_card as path to reduce wstring conversions

Input: path_with_wc; Output: search_dir, wild_card

Referenced by WorkdirHelper::file_op_items().

◆ concat_path()

bfs::path concat_path ( const bfs::path &  p_in,
const String &  tag 
)
static

◆ create_directory()

bool create_directory ( const bfs::path &  dir_path,
short  mkdir_option 
)
static

Create a directory, with options for remove or error.

mkdir_option is DIR_CLEAN (remove and recreate), DIR_PERSIST (leave existing), or DIR_ERROR (don't allow existing) returns whether a new directory was created.

References Dakota::abort_handler(), and WorkdirHelper::recursive_remove().

Referenced by NonDMUQBayesCalibration::calibrate(), and ProcessApplicInterface::define_filenames().

◆ link_items()

void link_items ( const StringArray &  source_items,
const bfs::path &  dest_dir,
bool  overwrite 
)
static

top-level link a list of source_paths (files, directories, symlinks), potentially including wildcards, from destination_dir, which must exist

Iterate source items (paths or wildcards), linking each of them from the destination. If overwrite, remove and replace any existing destination target, otherwise, allow to persist

References WorkdirHelper::file_op_items(), and WorkdirHelper::link().

Referenced by ProcessApplicInterface::define_filenames().

◆ copy_items()

void copy_items ( const StringArray &  source_items,
const bfs::path &  dest_dir,
bool  overwrite 
)
static

copy a list of source_paths (files, directories, symlinks), potentially including wildcards into destination_dir, which must exist

Iterate source items (paths or wildcards), copying each of them into the destination. If overwrite, remove and replace any existing destination target, otherwise, allow to persist

References WorkdirHelper::file_op_items(), and WorkdirHelper::recursive_copy().

Referenced by ProcessApplicInterface::define_filenames().

◆ link()

bool link ( const bfs::path &  src_path,
const bfs::path &  dest_dir,
bool  overwrite 
)
static

create link from dest_dir/src_path.filename() to a single path (file, dir, link) in source directory

Assumes source file exists since it was iterated in the calling context. If overwrite, any existing file in dest_dir will be removed prior to creating the new link.

References Dakota::abort_handler().

Referenced by WorkdirHelper::link_items().

◆ recursive_copy()

bool recursive_copy ( const bfs::path &  src_path,
const bfs::path &  dest_dir,
bool  overwrite 
)
static

Recrusive copy of src_path into dest_dir, with optional top-level overwrite (remove/recreate) of dest_dir/src_path.filename()

note dest_dir is the containing folder for the src_path contents to be placed in for consistency with other convenience functions (may need to reconsider)

References Dakota::abort_handler().

Referenced by WorkdirHelper::copy_items().

◆ prepend_path_item()

bool prepend_path_item ( const bfs::path &  src_path,
const bfs::path &  dest_dir,
bool  overwrite 
)
static

prepend the preferred env path with source path if it's a directory; this will update cached preferred path and manipulate PATH

prepend the env path with source path if it's a directory or directory symlink

References Dakota::abort_handler(), and WorkdirHelper::prepend_preferred_env_path().

Referenced by WorkdirHelper::prepend_path_items().

◆ file_op_items()

bool file_op_items ( const file_op_function file_op,
const StringArray &  source_items,
const bfs::path &  dest_dir,
bool  overwrite 
)
static

recursively perform file_op (copy, path adjust, etc.) on a list of source_paths (files, directories, symlinks), which potentially include wildcards, w.r.t. destination_dir

Iterator implementation for copy, link, etc file operation. Iterate source items (paths or wildcards), performing file_op on each w.r.t. destination. If overwrite, remove and replace any existing destination target (at top-level), otherwise, allow to persist. Return code true indicates abnormal behavior.

References WorkdirHelper::split_wildcard(), and Dakota::strcontains().

Referenced by WorkdirHelper::check_equivalent_dest(), WorkdirHelper::copy_items(), WorkdirHelper::find_driver(), WorkdirHelper::link_items(), and WorkdirHelper::prepend_path_items().

◆ set_preferred_path()

void set_preferred_path ( const boost::filesystem::path &  extra_path)
static

set PATH to absolute(extra_path):dakPreferredEnvPath, without changing cached preferred PATH

If needed, convert the passed item to an absolute path (while could make sense to prepend a relative path, no current use cases) and prepend when setting environment. Does not update cached preferred path.

References WorkdirHelper::dakPreferredEnvPath, WorkdirHelper::rel_to_abs(), and WorkdirHelper::set_environment().

◆ po_which()

bfs::path po_which ( const std::string &  driver_name)
staticprivate

Returns the bfs::path for the analysis driver - POSIX-style implementation, returns empty if not found.

For absolute driver_name, validates that is regular file. For relative, uses string representing $PATH (preferred path) to locate an analysis driver on the host computer. Returns the path to the driver, or empty if not found.

This is the "plain ol' which" impl that worked well, historically, on POSIX.

References Dakota::contains(), WorkdirHelper::dakPreferredEnvPath, and WorkdirHelper::tokenize_env_path().

Referenced by WorkdirHelper::which().

◆ init_startup_path()

std::string init_startup_path ( )
staticprivate

Initializes class member, startupPATH.

Gets the $PATH (PATH% on windows) and returns the std::string value

Referenced by WorkdirHelper::initialize().

◆ init_preferred_env_path()

std::string init_preferred_env_path ( )
staticprivate

Initializes class member, dakPreferredEnvPath.

Prepends '.' and the startupPWD to the initial startup $PATH string so that analysis driver detection is more robust

References WorkdirHelper::startupPATH, and WorkdirHelper::startupPWD.

Referenced by WorkdirHelper::initialize().

◆ tokenize_env_path()

std::vector< std::string > tokenize_env_path ( const std::string &  env_path)
staticprivate

Tokenizes $PATH environment variable into a "list" of directories.

Creates a a vector of directories (as an aid to search) by breaking up the $PATH environment variable (passed in as a string argument)

Referenced by WorkdirHelper::po_which().


The documentation for this class was generated from the following files: