1105 try: |
1105 try: |
1106 os.makedirs(path) |
1106 os.makedirs(path) |
1107 except OSError as ex: |
1107 except OSError as ex: |
1108 self.warning('error while creating %s directory: %s', path, ex) |
1108 self.warning('error while creating %s directory: %s', path, ex) |
1109 return |
1109 return |
|
1110 self.ensure_uid(path) |
|
1111 |
|
1112 def get_uid(self): |
1110 if self['uid']: |
1113 if self['uid']: |
1111 try: |
1114 try: |
1112 uid = int(self['uid']) |
1115 uid = int(self['uid']) |
1113 except ValueError: |
1116 except ValueError: |
1114 from pwd import getpwnam |
1117 from pwd import getpwnam |
1116 else: |
1119 else: |
1117 try: |
1120 try: |
1118 uid = os.getuid() |
1121 uid = os.getuid() |
1119 except AttributeError: # we are on windows |
1122 except AttributeError: # we are on windows |
1120 return |
1123 return |
|
1124 return uid |
|
1125 |
|
1126 def ensure_uid(self, path, enforce_write=False): |
|
1127 if not exists(path): |
|
1128 return |
|
1129 uid = self.get_uid() |
|
1130 if uid is None: |
|
1131 return |
1121 fstat = os.stat(path) |
1132 fstat = os.stat(path) |
1122 if fstat.st_uid != uid: |
1133 if fstat.st_uid != uid: |
1123 self.info('giving ownership of %s directory to %s', path, self['uid']) |
1134 self.info('giving ownership of %s to %s', path, self['uid']) |
1124 try: |
1135 try: |
1125 os.chown(path, uid, os.getgid()) |
1136 os.chown(path, uid, os.getgid()) |
1126 except OSError as ex: |
1137 except OSError as ex: |
1127 self.warning('error while giving ownership of %s directory to %s: %s', |
1138 self.warning('error while giving ownership of %s to %s: %s', |
1128 path, self['uid'], ex) |
1139 path, self['uid'], ex) |
1129 if not (fstat.st_mode & stat.S_IWUSR): |
1140 |
1130 self.info('forcing write permission on directory %s', path) |
1141 if enforce_write and not (fstat.st_mode & stat.S_IWUSR): |
|
1142 self.info('forcing write permission on %s', path) |
1131 try: |
1143 try: |
1132 os.chmod(path, fstat.st_mode | stat.S_IWUSR) |
1144 os.chmod(path, fstat.st_mode | stat.S_IWUSR) |
1133 except OSError as ex: |
1145 except OSError as ex: |
1134 self.warning('error while forcing write permission on directory %s: %s', |
1146 self.warning('error while forcing write permission on %s: %s', |
1135 path, ex) |
1147 path, ex) |
1136 return |
1148 |
|
1149 def ensure_uid_directory(self, path, enforce_write=False): |
|
1150 self.check_writeable_uid_directory(path) |
|
1151 for dirpath, dirnames, filenames in os.walk(path): |
|
1152 for name in filenames: |
|
1153 self.ensure_uid(join(dirpath, name), enforce_write) |
|
1154 return path |
1137 |
1155 |
1138 @cached |
1156 @cached |
1139 def instance_md5_version(self): |
1157 def instance_md5_version(self): |
1140 from hashlib import md5 # pylint: disable=E0611 |
1158 from hashlib import md5 # pylint: disable=E0611 |
1141 infos = [] |
1159 infos = [] |