Skip to content

Commit 6a31397

Browse files
committed
added force arg to command line, print_log_messages takes a list of messages, silent on normal updates, improved docs
1 parent e4696d0 commit 6a31397

8 files changed

+251
-127
lines changed

casaconfig/__main__.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# get_argparser supplies configfile, noconfig, nositeconfig
88
# add measurespath, pull-data, data-update, measures-update, update-all, reference-testing, current-data
99

10+
parser.prog = "casaconfig"
11+
1012
# measurespath will default to the value in config if not set here
1113
parser.add_argument( "--measurespath", dest='measurespath', default=None,
1214
help="location of casarundata")
@@ -23,6 +25,8 @@
2325
help="set measurespath to the casarundata when this version was produced, used for testing purposes")
2426
parser.add_argument( "--current-data", dest='currentdata', action='store_const', const=True, default=False,
2527
help="print out a summary of the current casarundata and measures data installed in measurespath and then exit")
28+
parser.add_argument("--force", dest='force', action='store_const', const=True, default=False,
29+
help="force an update using the force=True option to update_all, data_update, and measures_update")
2630

2731
# initialize the configuration to be used
2832
flags,args = parser.parse_known_args(sys.argv)
@@ -65,8 +69,8 @@
6569

6670
# measures
6771
measuresInfo = dataInfo['measures']
68-
if measuresInfo is None or casarunInfo['version'] == "invalid":
69-
print("No measures data found (missing or unexpected readme.txt, not obviously legaca measures data).")
72+
if measuresInfo is None or measuresInfo['version'] == "invalid":
73+
print("No measures data found (missing or unexpected readme.txt, not obviously legacy measures data).")
7074
elif measuresInfo['version'] == "unknown":
7175
print("measures version is unknown (probably legacy measures data not maintained by casaconfig).")
7276
else:
@@ -83,17 +87,18 @@
8387
# ignore all other options
8488
else:
8589
# the update options, update all does everything, no need to invoke anything else
90+
print("Checking for updates into %s" % measurespath)
8691
if flags.updateall:
87-
casaconfig.update_all(measurespath)
92+
casaconfig.update_all(measurespath,force=flags.force)
8893
else:
8994
# do any pull_update first
9095
if flags.pulldata:
9196
casaconfig.pull_data(measurespath)
9297
# then data_update, not necessary if pull_data just happened
9398
if flags.dataupdate and not flags.pulldata:
94-
casaconfig.data_update(measurespath)
99+
casaconfig.data_update(measurespath, force=flags.force)
95100
# then measures_update
96101
if flags.measuresupdate:
97-
casaconfig.measures_update(measurespath)
102+
casaconfig.measures_update(measurespath, force=flags.force)
98103

99104
sys.exit(0)

casaconfig/config.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,9 @@
124124
if globals()[__v] is not None:
125125
globals()[__v] = __os.path.abspath(__os.path.expanduser(globals()[__v]))
126126
else:
127-
# debugging for now
128-
print("None value seen while expanding path-like fields for config parameter %s" % __v)
129-
print("__loaded_config_files : ")
130-
for __f in __loaded_config_files:
131-
print(" %s" % __f)
132-
print("__config_files : ")
133-
for __f in __config_files:
134-
print(" %s" % __f)
127+
pass
128+
# debugging
129+
# print("None value seen while expanding path-like fields for config parameter %s" % __v)
135130

136131
def load_success( ):
137132
return __loaded_config_files

casaconfig/private/data_update.py

+66-28
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def data_update(path=None, version=None, force=False, logger=None, auto_update_r
2020
Check for updates to the installed casarundata and install the update or change to
2121
the requested version when appropriate.
2222
23+
If no update is necessary then this function will silently return.
24+
2325
The path must contain a previously installed version of casarundata.
2426
Use pull_data to install casarundata into a new path (empty or does not exist).
2527
@@ -108,6 +110,7 @@ def data_update(path=None, version=None, force=False, logger=None, auto_update_r
108110
from .data_available import data_available
109111
from .print_log_messages import print_log_messages
110112
from .get_data_lock import get_data_lock
113+
from .pull_data import pull_data
111114
from .do_pull_data import do_pull_data
112115
from .get_data_info import get_data_info
113116

@@ -120,6 +123,11 @@ def data_update(path=None, version=None, force=False, logger=None, auto_update_r
120123
print_log_messages('path is None and has not been set in config.measurespath (probably casasiteconfig.py). Provide a valid path and retry.', logger, True)
121124
return
122125

126+
# when a specific version is requested then the measures readme.txt that is part of that version
127+
# will get a timestamp of now so that default measures updates won't happen for a day unless the
128+
# force argument is used for measures_update
129+
namedVersion = version is not None
130+
123131
path = os.path.expanduser(path)
124132
readme_path = os.path.join(path, 'readme.txt')
125133

@@ -131,12 +139,19 @@ def data_update(path=None, version=None, force=False, logger=None, auto_update_r
131139
print_log_messages('force must be False when auto_update_rules is True', logger, True)
132140
return
133141
if (not os.path.isdir(path)) or (os.stat(path).st_uid != os.getuid()):
134-
print_log_messages('path must exist as a directory and it must be owned by the user when auto_update_rules is True', logger, True)
142+
msgs = []
143+
msgs.append("Warning: path must exist as a directory and it must be owned by the user, path = %s" % path)
144+
msgs.append("Warning: no data updates are possible on this path by this user.")
145+
print_log_messages(msgs, logger, False)
135146
return
136147

137148
if not os.path.exists(readme_path):
138-
print_log_messages('No readme.txt file found at path. Nothing updated or checked.', logger, True);
139-
return
149+
# path must exist and it must be empty in order to continue
150+
if not os.path.exists(path) or (len(os.listdir(path)) > 0):
151+
print_log_messages('No readme.txt file found at path. Nothing updated or checked.', logger, True);
152+
return
153+
# ok to install a fresh copy, use pull_data directly
154+
return pull_data(path,version,force,logger)
140155

141156
# path must be writable with execute bit set
142157
if (not os.access(path, os.W_OK | os.X_OK)) :
@@ -153,8 +168,10 @@ def data_update(path=None, version=None, force=False, logger=None, auto_update_r
153168
dataReadmeInfo = get_data_info(path, logger, type='casarundata')
154169

155170
if dataReadmeInfo is None or dataReadmeInfo['version'] == 'invalid':
156-
print_log_messages('The readme.txt file at path could not be read as expected', logger, True)
157-
print_log_messages('choose a different path or empty this path and try again using pull_data', logger, True)
171+
msgs = []
172+
msgs.append('The readme.txt file at path could not be read as expected')
173+
msgs.append('choose a different path or empty this path and try again using pull_data')
174+
print_log_messages(msgs, logger, True)
158175
# no lock has been set yet, safe to simply return here
159176
return
160177

@@ -165,20 +182,25 @@ def data_update(path=None, version=None, force=False, logger=None, auto_update_r
165182
ageRecent = dataReadmeInfo['age'] < 1.0
166183

167184
if currentVersion is 'unknown':
168-
print_log_messages('The data update path appears to be casarundata but no readme.txt file was found', logger, False)
169-
print_log_messages('A data update is not possible but CASA use of this data may be OK.', logger, False)
170-
print_log_messages('casaconfig must first install the casarundata in path for data_update to run as expected on that path', logger, False)
185+
msgs = []
186+
msgs.append('The data update path appears to be casarundata but no readme.txt file was found')
187+
msgs.append('A data update is not possible but CASA use of this data may be OK.')
188+
msgs.append('casaconfig must first install the casarundata in path for data_update to run as expected on that path')
189+
print_log_messages(msgs, logger, True)
171190

172191
if (len(installed_files) == 0):
173192
# this shouldn't happen
174-
print_log_messages('The readme.txt file at path did not contain the expected list of installed files', logger, True)
175-
print_log_messages('choose a different path or empty this path and try again using pull_data', logger, True)
193+
msgs = []
194+
msgs.append('The readme.txt file at path did not contain the expected list of installed files')
195+
msgs.append('choose a different path or empty this path and try again using pull_data')
196+
print_log_messages(msgs, logger, True)
176197
# no lock has been set yet, safe to simply return here
177198
return
178199

179200
if version is None and force is False and ageRecent:
180201
# if version is None, the readme is less than 1 day old and force is False then return without checking for any newer versions
181-
print_log_messages('data_update latest version checked recently in %s, using version %s' % (path, currentVersion), logger)
202+
# normal use is silent, this line is useful during debugging
203+
# print_log_messages('data_update latest version checked recently in %s, using version %s' % (path, currentVersion), logger)
182204
# no lock has been set yet, safe to simply return here
183205
return
184206

@@ -195,7 +217,7 @@ def data_update(path=None, version=None, force=False, logger=None, auto_update_r
195217
# use the release version from get_data_info
196218
releaseInfo = get_data_info()['release']
197219
if releaseInfo is None:
198-
print_log_messages('No release info found, pull_data can not continue', logger, True)
220+
print_log_messages('No release info found, data_update can not continue', logger, True)
199221
return
200222
requestedVersion = releaseInfo['casarundata']
201223
expectedMeasuresVersion = releaseInfo['measures']
@@ -218,17 +240,20 @@ def data_update(path=None, version=None, force=False, logger=None, auto_update_r
218240
force = False
219241
# if measuresReadmeInfo is None then that's a problem and force remains True, this also catches 'invalid' and 'unknown' measures versions, which should not happen here
220242
if not force:
221-
print_log_messages('data_update requested "release" version of casarundata and measures are already installed.', logger)
243+
# normal use is silent, this line is useful during debugging
244+
# print_log_messages('data_update requested "release" version of casarundata and measures are already installed.', logger)
222245
# no lock has been set yet, safe to simply return here
223246
return
224247
else:
225248
# normal usage, ok to return now
249+
# normal use is silent, commented out lines are useful during debugging
226250
if latestVersion:
227-
print_log_messages('The latest version is already installed in %s, using version %s' % (path, currentVersion), logger)
251+
# print_log_messages('The latest version is already installed in %s, using version %s' % (path, currentVersion), logger)
228252
# touch the dates of the readme to prevent a future check on available data for the next 24 hours
229253
os.utime(readme_path)
230-
else:
231-
print_log_messages('Requested casarundata is installed in %s, using version %s' % (path, currentVersion), logger)
254+
#else:
255+
# print_log_messages('Requested casarundata is installed in %s, using version %s' % (path, currentVersion), logger)
256+
232257
# no lock has been set yet, safe to simply return here
233258
return
234259

@@ -241,10 +266,12 @@ def data_update(path=None, version=None, force=False, logger=None, auto_update_r
241266
lock_fd = get_data_lock(path, 'data_update')
242267
# if lock_fd is None it means the lock file was not empty - because we know that path exists at this point
243268
if lock_fd is None:
244-
print_log_messages('The lock file at %s is not empty.' % path, logger, True)
245-
print_log_messages('A previous attempt to update path may have failed or exited prematurely.', logger, True)
246-
print_log_messages('Remove the lock file and set force to True with the desired version (default to most recent).', logger, True)
247-
print_log_messages('It may be best to completely repopulate path using pull_data and measures_update.', logger, True)
269+
msgs = []
270+
msgs.append('The lock file at %s is not empty.' % path)
271+
msgs.append('A previous attempt to update path may have failed or exited prematurely.')
272+
msgs.append('Remove the lock file and set force to True with the desired version (default to most recent).')
273+
msgs.append('It may be best to completely repopulate path using pull_data and measures_update.')
274+
print_log_messages(msgs, logger, True)
248275
return
249276

250277
do_update = True
@@ -281,25 +308,36 @@ def data_update(path=None, version=None, force=False, logger=None, auto_update_r
281308
if len(installed_files) == 0:
282309
# this shouldn't happen, do not do an update
283310
do_update = False
284-
print_log_messages('The readme.txt file read at path did not contain the expected list of installed files', logger, True)
285-
print_log_messages('This should not happen unless multiple sessions are trying to update data at the same time and one experienced problems or was done out of sequence', logger, True)
286-
print_log_messages('Check for other updates in process or choose a different path or clear out this path and try again using pull_data or update_all', logger, True)
311+
msgs = []
312+
msgs.append('The readme.txt file read at path did not contain the expected list of installed files')
313+
msgs.append('This should not happen unless multiple sessions are trying to update data at the same time and one experienced problems or was done out of sequence')
314+
msgs.append('Check for other updates in process or choose a different path or clear out this path and try again using pull_data or update_all')
315+
print_log_messages(msgs, logger, True)
287316
else:
288317
# this shouldn't happen, do not do an update
289318
do_update = False
290-
print_log_messages('Unexpected problem reading readme.txt file during data_update, can not safely update to the requested version', logger, True)
291-
print_log_messages('This should not happen unless multiple sessions are trying to update at the same time and one experienced problems or was done out of sequence', logger, True)
292-
print_log_messages('Check for other updates in process or choose a different path or clear out this path and try again using pull_data or update_all', logger, True)
319+
msgs = []
320+
msgs.append('Unexpected problem reading readme.txt file during data_update, can not safely update to the requested version')
321+
msgs.append('This should not happen unless multiple sessions are trying to update at the same time and one experienced problems or was done out of sequence')
322+
msgs.append('Check for other updates in process or choose a different path or clear out this path and try again using pull_data or update_all')
323+
print_log_messages(msgs, logger, True)
293324

294325
if do_update:
295326
do_pull_data(path, requestedVersion, installed_files, currentVersion, currentDate, logger)
327+
if namedVersion is not None:
328+
# a specific version has been requested, set the times on the measures readme.txt to now to avoid
329+
# a default update of the measures data without using the force argument
330+
measuresReadmePath = os.path.join(path,'geodetic/readme.txt')
331+
os.utime(measuresReadmePath)
296332

297333
# truncate the lock file
298334
lock_fd.truncate(0)
299335

300336
except Exception as exc:
301-
print_log_messages('ERROR! : Unexpected exception while populating casarundata version %s to %s' % (requestedVersion, path), logger, True)
302-
print_log_messages('ERROR! : %s' % exc, logger, True)
337+
msgs = []
338+
msgs.append('ERROR! : Unexpected exception while populating casarundata version %s to %s' % (requestedVersion, path))
339+
msgs.append('ERROR! : %s' % exc)
340+
print_log_messages(msgs, logger, True)
303341
# leave the contents of the lock file as is to aid in debugging
304342
# import traceback
305343
# traceback.print_exc()

casaconfig/private/do_auto_updates.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,23 @@ def do_auto_updates(configDict, logger=None):
4545
from .print_log_messages import print_log_messages
4646
from .data_update import data_update
4747
from .measures_update import measures_update
48-
48+
4949
if configDict.measurespath is None:
5050
# likely still unset in casasiteconfig.py, suggest setting it in ~/.casa/config.py
5151
# or ask the site administrator (which may be the user) to edit casasiteconfig.py
5252
# point at the documentation
5353
# continue, because things still might work if there are measures in datapath
54-
print_log_messages('measurespath is None in config', logger, True)
55-
print_log_messages('this likely means a casasiteconfig.py was used and measurespath remains unset there.', logger, True)
56-
print_log_messages('Either set measurespath in your config file at ~/.casa/config.py', logger, True)
57-
print_log_messages('or ask the site manager to set that in casasiteconfig.py', logger, True)
58-
print_log_messages('visit https://casadocs.readthedocs.io/en/stable/notebooks/external-data.html for more information', logger, True)
54+
msgs = []
55+
msgs.append('measurespath is None in config')
56+
msgs.append('this likely means a casasiteconfig.py was used and measurespath remains unset there.')
57+
msgs.append('Either set measurespath in your config file at ~/.casa/config.py')
58+
msgs.append('or ask the site manager to set that in casasiteconfig.py')
59+
msgs.append('visit https://casadocs.readthedocs.io/en/stable/notebooks/external-data.html for more information')
5960

6061
if (configDict.measures_auto_update or configDict.data_auto_update):
61-
print('\nAuto updates of measures path are not possible because measurespath is not set, skipping auto updates', logger, True)
62+
msgs.append('Auto updates of measures path are not possible because measurespath is not set, skipping auto updates')
63+
64+
print_log_messages(msgs, logger, True)
6265

6366
return
6467

0 commit comments

Comments
 (0)