Skip to content

Commit

Permalink
more config file fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eirannejad committed Feb 9, 2022
1 parent 5b03305 commit ee86a64
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 60 deletions.
38 changes: 0 additions & 38 deletions dev/pyRevitLabs/pyRevitLabs.Common/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,6 @@ public static bool VerifyFile(string filePath) {
return false;
}

// https://stackoverflow.com/a/937558/2350244
public static bool IsFileLocked(FileInfo file) {
try {
using (FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None)) {
stream.Close();
}
}
catch (IOException) {
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}

//file is not locked
return false;
}

public static void VerifyFileAccessible(string filePath) {
// make sure file is accessible,
// try multiple times and wait a little in between
// fail after trying
var finfo = new FileInfo(filePath);

uint tries = 3;
do {
if (IsFileLocked(finfo)) {
Thread.Sleep(200);
tries--;
}
else
return;
} while (tries > 0);

throw new PyRevitException("File is not accessible");
}

public static bool VerifyPath(string path) {
if (path != null && path != string.Empty)
return Directory.Exists(path);
Expand Down
3 changes: 1 addition & 2 deletions dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public PyRevitConfig(string cfgFilePath, bool adminMode = false) {

if (CommonUtils.VerifyFile(cfgFilePath))
{
CommonUtils.VerifyFileAccessible(cfgFilePath);
ConfigFilePath = cfgFilePath;

// INI formatting
Expand All @@ -36,7 +35,7 @@ public PyRevitConfig(string cfgFilePath, bool adminMode = false) {
_adminMode = adminMode;
}
else
throw new PyRevitException("Can not access config file path.");
throw new PyRevitException($"Can not access config file at {cfgFilePath}");
}

// save config file to standard location
Expand Down
46 changes: 26 additions & 20 deletions dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitConfigs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,20 @@ public static PyRevitConfig GetConfigFile()
string userConfig = PyRevitConsts.ConfigFilePath;
string adminConfig = PyRevitConsts.AdminConfigFilePath;

if (!CommonUtils.VerifyFile(userConfig)
&& CommonUtils.VerifyFile(adminConfig)) {
FileInfo fi = new FileInfo(adminConfig);
if (fi.IsReadOnly)
return new PyRevitConfig(PyRevitConsts.AdminConfigFilePath, adminMode: true);
if (!CommonUtils.VerifyFile(userConfig))
{
if (CommonUtils.VerifyFile(adminConfig))
{
if (new FileInfo(adminConfig).IsReadOnly)
return new PyRevitConfig(adminConfig, adminMode: true);
else
SetupConfig(adminConfig);
}
else
SetupConfig(adminConfig);
SetupConfig();
}

return new PyRevitConfig(PyRevitConsts.ConfigFilePath);
return new PyRevitConfig(userConfig);
}

// deletes config file
Expand Down Expand Up @@ -116,24 +120,26 @@ public static void SeedConfig(bool lockSeedConfig = false)
// if admin config file exists, create initial config file from seed config
public static void SetupConfig(string templateConfigFilePath = null)
{
string sourceFile = templateConfigFilePath ?? PyRevitConsts.AdminConfigFilePath;
string sourceFile = templateConfigFilePath;
string targetFile = PyRevitConsts.ConfigFilePath;

logger.Debug("Seeding config file \"{0}\" to \"{1}\"", sourceFile, targetFile);

try
if (sourceFile is string)
{
if (File.Exists(sourceFile))
logger.Debug("Seeding config file \"{0}\" to \"{1}\"", sourceFile, targetFile);

try
{
File.WriteAllText(targetFile, File.ReadAllText(sourceFile));
else
CommonUtils.EnsureFile(PyRevitConsts.ConfigFilePath);
}
catch (Exception ex)
{
throw new PyRevitException(
$"Failed configuring config file from template at {sourceFile} | {ex.Message}"
);
}
catch (Exception ex)
{
throw new PyRevitException(
$"Failed configuring config file from template at {sourceFile} | {ex.Message}"
);
}
}
else
CommonUtils.EnsureFile(targetFile);
}

// specific configuration public access ======================================================================
Expand Down

0 comments on commit ee86a64

Please sign in to comment.