X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=cmframework%2Fsrc%2Fcmframework%2Futils%2Fcmflagfile.py;fp=cmframework%2Fsrc%2Fcmframework%2Futils%2Fcmflagfile.py;h=42d51f474efbdde5eb1ccf1b61548caa57ec55e1;hb=c389bdee7b3845b55f443dbf04c0ce4083a55886;hp=0000000000000000000000000000000000000000;hpb=5030f0c004701dd422c78c71c014ef60f48139fc;p=ta%2Fconfig-manager.git diff --git a/cmframework/src/cmframework/utils/cmflagfile.py b/cmframework/src/cmframework/utils/cmflagfile.py new file mode 100644 index 0000000..42d51f4 --- /dev/null +++ b/cmframework/src/cmframework/utils/cmflagfile.py @@ -0,0 +1,59 @@ +# Copyright 2019 Nokia + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import print_function +import logging +import os.path +import os +import stat + +from cmframework.apis import cmerror + + +class CMFlagFile(object): + CM_FLAGFILE_DIR = '/mnt/config-manager' + + def __init__(self, name): + logging.debug('CMFlagFile constructor called, name=%s', name) + + self._name = '{}/{}'.format(CMFlagFile.CM_FLAGFILE_DIR, name) + + def __nonzero__(self): + return self.is_set() + + def is_set(self): + logging.debug('is_set called') + + return os.path.exists(self._name) + + def set(self): + logging.debug('set called') + + if not self.is_set(): + try: + with open(self._name, 'w') as f: + os.chmod(self._name, stat.S_IRUSR | stat.S_IWUSR) + f.write('') + f.flush() + os.fsync(f.fileno()) + except IOError as exp: + raise cmerror.CMError(str(exp)) + + def unset(self): + logging.debug('unset called') + + if self.is_set(): + try: + os.remove(self._name) + except IOError as exp: + raise cmerror.CMError(str(exp))