[cwconfig] extract uid file setting code from check_writeable_uid_directory (related to #11298794) 3.22
authorDavid Douard <david.douard@logilab.fr>
Tue, 19 Jul 2016 19:26:20 +0200
branch3.22
changeset 11435 42578bc0b5ef
parent 11434 f6ba947c11ee
child 11436 dca6ae982ee5
[cwconfig] extract uid file setting code from check_writeable_uid_directory (related to #11298794) into an ensure_uid() method
cwconfig.py
--- a/cwconfig.py	Tue Jul 19 15:53:58 2016 +0200
+++ b/cwconfig.py	Tue Jul 19 19:26:20 2016 +0200
@@ -1076,6 +1076,9 @@
             except OSError as ex:
                 self.warning('error while creating %s directory: %s', path, ex)
                 return
+        self.ensure_uid(path)
+
+    def get_uid(self):
         if self['uid']:
             try:
                 uid = int(self['uid'])
@@ -1087,22 +1090,31 @@
                 uid = os.getuid()
             except AttributeError: # we are on windows
                 return
+        return uid
+
+    def ensure_uid(self, path, enforce_write=False):
+        if not exists(path):
+            return
+        uid = self.get_uid()
+        if uid is None:
+            return
         fstat = os.stat(path)
         if fstat.st_uid != uid:
-            self.info('giving ownership of %s directory to %s', path, self['uid'])
+            self.info('giving ownership of %s to %s', path, self['uid'])
             try:
                 os.chown(path, uid, os.getgid())
             except OSError as ex:
-                self.warning('error while giving ownership of %s directory to %s: %s',
+                self.warning('error while giving ownership of %s to %s: %s',
                              path, self['uid'], ex)
-        if not (fstat.st_mode & stat.S_IWUSR):
-            self.info('forcing write permission on directory %s', path)
+
+        if enforce_write and not (fstat.st_mode & stat.S_IWUSR):
+            self.info('forcing write permission on %s', path)
             try:
                 os.chmod(path, fstat.st_mode | stat.S_IWUSR)
             except OSError as ex:
-                self.warning('error while forcing write permission on directory %s: %s',
+                self.warning('error while forcing write permission on %s: %s',
                              path, ex)
-                return
+    
 
     @cached
     def instance_md5_version(self):