diff options
Diffstat (limited to 'repo')
-rwxr-xr-x | repo | 89 |
1 files changed, 89 insertions, 0 deletions
@@ -246,6 +246,7 @@ GITC_FS_ROOT_DIR = '/gitc/manifest-rw/' | |||
246 | 246 | ||
247 | import collections | 247 | import collections |
248 | import errno | 248 | import errno |
249 | import json | ||
249 | import optparse | 250 | import optparse |
250 | import re | 251 | import re |
251 | import shutil | 252 | import shutil |
@@ -1035,6 +1036,90 @@ def _ParseArguments(args): | |||
1035 | return cmd, opt, arg | 1036 | return cmd, opt, arg |
1036 | 1037 | ||
1037 | 1038 | ||
1039 | class Requirements(object): | ||
1040 | """Helper for checking repo's system requirements.""" | ||
1041 | |||
1042 | REQUIREMENTS_NAME = 'requirements.json' | ||
1043 | |||
1044 | def __init__(self, requirements): | ||
1045 | """Initialize. | ||
1046 | |||
1047 | Args: | ||
1048 | requirements: A dictionary of settings. | ||
1049 | """ | ||
1050 | self.requirements = requirements | ||
1051 | |||
1052 | @classmethod | ||
1053 | def from_dir(cls, path): | ||
1054 | return cls.from_file(os.path.join(path, cls.REQUIREMENTS_NAME)) | ||
1055 | |||
1056 | @classmethod | ||
1057 | def from_file(cls, path): | ||
1058 | try: | ||
1059 | with open(path, 'rb') as f: | ||
1060 | data = f.read() | ||
1061 | except EnvironmentError: | ||
1062 | # NB: EnvironmentError is used for Python 2 & 3 compatibility. | ||
1063 | # If we couldn't open the file, assume it's an old source tree. | ||
1064 | return None | ||
1065 | |||
1066 | return cls.from_data(data) | ||
1067 | |||
1068 | @classmethod | ||
1069 | def from_data(cls, data): | ||
1070 | comment_line = re.compile(br'^ *#') | ||
1071 | strip_data = b''.join(x for x in data.splitlines() if not comment_line.match(x)) | ||
1072 | try: | ||
1073 | json_data = json.loads(strip_data) | ||
1074 | except Exception: # pylint: disable=broad-except | ||
1075 | # If we couldn't parse it, assume it's incompatible. | ||
1076 | return None | ||
1077 | |||
1078 | return cls(json_data) | ||
1079 | |||
1080 | def _get_soft_ver(self, pkg): | ||
1081 | """Return the soft version for |pkg| if it exists.""" | ||
1082 | return self.requirements.get(pkg, {}).get('soft', ()) | ||
1083 | |||
1084 | def _get_hard_ver(self, pkg): | ||
1085 | """Return the hard version for |pkg| if it exists.""" | ||
1086 | return self.requirements.get(pkg, {}).get('hard', ()) | ||
1087 | |||
1088 | @staticmethod | ||
1089 | def _format_ver(ver): | ||
1090 | """Return a dotted version from |ver|.""" | ||
1091 | return '.'.join(str(x) for x in ver) | ||
1092 | |||
1093 | def assert_ver(self, pkg, curr_ver): | ||
1094 | """Verify |pkg|'s |curr_ver| is new enough.""" | ||
1095 | curr_ver = tuple(curr_ver) | ||
1096 | soft_ver = tuple(self._get_soft_ver(pkg)) | ||
1097 | hard_ver = tuple(self._get_hard_ver(pkg)) | ||
1098 | if curr_ver < hard_ver: | ||
1099 | print('repo: error: Your version of "%s" (%s) is unsupported; ' | ||
1100 | 'Please upgrade to at least version %s to continue.' % | ||
1101 | (pkg, self._format_ver(curr_ver), self._format_ver(soft_ver)), | ||
1102 | file=sys.stderr) | ||
1103 | sys.exit(1) | ||
1104 | |||
1105 | if curr_ver < soft_ver: | ||
1106 | print('repo: warning: Your version of "%s" (%s) is no longer supported; ' | ||
1107 | 'Please upgrade to at least version %s to avoid breakage.' % | ||
1108 | (pkg, self._format_ver(curr_ver), self._format_ver(soft_ver)), | ||
1109 | file=sys.stderr) | ||
1110 | |||
1111 | def assert_all(self): | ||
1112 | """Assert all of the requirements are satisified.""" | ||
1113 | # See if we need a repo launcher upgrade first. | ||
1114 | self.assert_ver('repo', VERSION) | ||
1115 | |||
1116 | # Check python before we try to import the repo code. | ||
1117 | self.assert_ver('python', sys.version_info) | ||
1118 | |||
1119 | # Check git while we're at it. | ||
1120 | self.assert_ver('git', ParseGitVersion()) | ||
1121 | |||
1122 | |||
1038 | def _Usage(): | 1123 | def _Usage(): |
1039 | gitc_usage = "" | 1124 | gitc_usage = "" |
1040 | if get_gitc_manifest_dir(): | 1125 | if get_gitc_manifest_dir(): |
@@ -1192,6 +1277,10 @@ def main(orig_args): | |||
1192 | print("fatal: unable to find repo entry point", file=sys.stderr) | 1277 | print("fatal: unable to find repo entry point", file=sys.stderr) |
1193 | sys.exit(1) | 1278 | sys.exit(1) |
1194 | 1279 | ||
1280 | reqs = Requirements.from_dir(os.path.dirname(repo_main)) | ||
1281 | if reqs: | ||
1282 | reqs.assert_all() | ||
1283 | |||
1195 | ver_str = '.'.join(map(str, VERSION)) | 1284 | ver_str = '.'.join(map(str, VERSION)) |
1196 | me = [sys.executable, repo_main, | 1285 | me = [sys.executable, repo_main, |
1197 | '--repo-dir=%s' % rel_repo_dir, | 1286 | '--repo-dir=%s' % rel_repo_dir, |