Skip to content

Commit ee86a64

Browse files
committed
more config file fixes
1 parent 5b03305 commit ee86a64

File tree

3 files changed

+27
-60
lines changed

3 files changed

+27
-60
lines changed

dev/pyRevitLabs/pyRevitLabs.Common/CommonUtils.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,6 @@ public static bool VerifyFile(string filePath) {
2626
return false;
2727
}
2828

29-
// https://stackoverflow.com/a/937558/2350244
30-
public static bool IsFileLocked(FileInfo file) {
31-
try {
32-
using (FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None)) {
33-
stream.Close();
34-
}
35-
}
36-
catch (IOException) {
37-
//the file is unavailable because it is:
38-
//still being written to
39-
//or being processed by another thread
40-
//or does not exist (has already been processed)
41-
return true;
42-
}
43-
44-
//file is not locked
45-
return false;
46-
}
47-
48-
public static void VerifyFileAccessible(string filePath) {
49-
// make sure file is accessible,
50-
// try multiple times and wait a little in between
51-
// fail after trying
52-
var finfo = new FileInfo(filePath);
53-
54-
uint tries = 3;
55-
do {
56-
if (IsFileLocked(finfo)) {
57-
Thread.Sleep(200);
58-
tries--;
59-
}
60-
else
61-
return;
62-
} while (tries > 0);
63-
64-
throw new PyRevitException("File is not accessible");
65-
}
66-
6729
public static bool VerifyPath(string path) {
6830
if (path != null && path != string.Empty)
6931
return Directory.Exists(path);

dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitConfig.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public PyRevitConfig(string cfgFilePath, bool adminMode = false) {
2323

2424
if (CommonUtils.VerifyFile(cfgFilePath))
2525
{
26-
CommonUtils.VerifyFileAccessible(cfgFilePath);
2726
ConfigFilePath = cfgFilePath;
2827

2928
// INI formatting
@@ -36,7 +35,7 @@ public PyRevitConfig(string cfgFilePath, bool adminMode = false) {
3635
_adminMode = adminMode;
3736
}
3837
else
39-
throw new PyRevitException("Can not access config file path.");
38+
throw new PyRevitException($"Can not access config file at {cfgFilePath}");
4039
}
4140

4241
// save config file to standard location

dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitConfigs.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,20 @@ public static PyRevitConfig GetConfigFile()
4646
string userConfig = PyRevitConsts.ConfigFilePath;
4747
string adminConfig = PyRevitConsts.AdminConfigFilePath;
4848

49-
if (!CommonUtils.VerifyFile(userConfig)
50-
&& CommonUtils.VerifyFile(adminConfig)) {
51-
FileInfo fi = new FileInfo(adminConfig);
52-
if (fi.IsReadOnly)
53-
return new PyRevitConfig(PyRevitConsts.AdminConfigFilePath, adminMode: true);
49+
if (!CommonUtils.VerifyFile(userConfig))
50+
{
51+
if (CommonUtils.VerifyFile(adminConfig))
52+
{
53+
if (new FileInfo(adminConfig).IsReadOnly)
54+
return new PyRevitConfig(adminConfig, adminMode: true);
55+
else
56+
SetupConfig(adminConfig);
57+
}
5458
else
55-
SetupConfig(adminConfig);
59+
SetupConfig();
5660
}
5761

58-
return new PyRevitConfig(PyRevitConsts.ConfigFilePath);
62+
return new PyRevitConfig(userConfig);
5963
}
6064

6165
// deletes config file
@@ -116,24 +120,26 @@ public static void SeedConfig(bool lockSeedConfig = false)
116120
// if admin config file exists, create initial config file from seed config
117121
public static void SetupConfig(string templateConfigFilePath = null)
118122
{
119-
string sourceFile = templateConfigFilePath ?? PyRevitConsts.AdminConfigFilePath;
123+
string sourceFile = templateConfigFilePath;
120124
string targetFile = PyRevitConsts.ConfigFilePath;
121125

122-
logger.Debug("Seeding config file \"{0}\" to \"{1}\"", sourceFile, targetFile);
123-
124-
try
126+
if (sourceFile is string)
125127
{
126-
if (File.Exists(sourceFile))
128+
logger.Debug("Seeding config file \"{0}\" to \"{1}\"", sourceFile, targetFile);
129+
130+
try
131+
{
127132
File.WriteAllText(targetFile, File.ReadAllText(sourceFile));
128-
else
129-
CommonUtils.EnsureFile(PyRevitConsts.ConfigFilePath);
130-
}
131-
catch (Exception ex)
132-
{
133-
throw new PyRevitException(
134-
$"Failed configuring config file from template at {sourceFile} | {ex.Message}"
135-
);
133+
}
134+
catch (Exception ex)
135+
{
136+
throw new PyRevitException(
137+
$"Failed configuring config file from template at {sourceFile} | {ex.Message}"
138+
);
139+
}
136140
}
141+
else
142+
CommonUtils.EnsureFile(targetFile);
137143
}
138144

139145
// specific configuration public access ======================================================================

0 commit comments

Comments
 (0)