1074 try: |
1074 try: |
1075 os.makedirs(path) |
1075 os.makedirs(path) |
1076 except OSError as ex: |
1076 except OSError as ex: |
1077 self.warning('error while creating %s directory: %s', path, ex) |
1077 self.warning('error while creating %s directory: %s', path, ex) |
1078 return |
1078 return |
|
1079 self.ensure_uid(path) |
|
1080 |
|
1081 def get_uid(self): |
1079 if self['uid']: |
1082 if self['uid']: |
1080 try: |
1083 try: |
1081 uid = int(self['uid']) |
1084 uid = int(self['uid']) |
1082 except ValueError: |
1085 except ValueError: |
1083 from pwd import getpwnam |
1086 from pwd import getpwnam |
1085 else: |
1088 else: |
1086 try: |
1089 try: |
1087 uid = os.getuid() |
1090 uid = os.getuid() |
1088 except AttributeError: # we are on windows |
1091 except AttributeError: # we are on windows |
1089 return |
1092 return |
|
1093 return uid |
|
1094 |
|
1095 def ensure_uid(self, path, enforce_write=False): |
|
1096 if not exists(path): |
|
1097 return |
|
1098 uid = self.get_uid() |
|
1099 if uid is None: |
|
1100 return |
1090 fstat = os.stat(path) |
1101 fstat = os.stat(path) |
1091 if fstat.st_uid != uid: |
1102 if fstat.st_uid != uid: |
1092 self.info('giving ownership of %s directory to %s', path, self['uid']) |
1103 self.info('giving ownership of %s to %s', path, self['uid']) |
1093 try: |
1104 try: |
1094 os.chown(path, uid, os.getgid()) |
1105 os.chown(path, uid, os.getgid()) |
1095 except OSError as ex: |
1106 except OSError as ex: |
1096 self.warning('error while giving ownership of %s directory to %s: %s', |
1107 self.warning('error while giving ownership of %s to %s: %s', |
1097 path, self['uid'], ex) |
1108 path, self['uid'], ex) |
1098 if not (fstat.st_mode & stat.S_IWUSR): |
1109 |
1099 self.info('forcing write permission on directory %s', path) |
1110 if enforce_write and not (fstat.st_mode & stat.S_IWUSR): |
|
1111 self.info('forcing write permission on %s', path) |
1100 try: |
1112 try: |
1101 os.chmod(path, fstat.st_mode | stat.S_IWUSR) |
1113 os.chmod(path, fstat.st_mode | stat.S_IWUSR) |
1102 except OSError as ex: |
1114 except OSError as ex: |
1103 self.warning('error while forcing write permission on directory %s: %s', |
1115 self.warning('error while forcing write permission on %s: %s', |
1104 path, ex) |
1116 path, ex) |
1105 return |
1117 |
1106 |
1118 |
1107 @cached |
1119 @cached |
1108 def instance_md5_version(self): |
1120 def instance_md5_version(self): |
1109 from hashlib import md5 # pylint: disable=E0611 |
1121 from hashlib import md5 # pylint: disable=E0611 |
1110 infos = [] |
1122 infos = [] |