diff --git a/README.md b/README.md index bc57b4d..a9eea39 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,15 @@ It runs the entire file if `filepath` is provided, otherwise, it enters playgrou - `recursion-limit(rl)` all integer number as the recursion depth limit of the interpreter. +- `integrity-protection` + + - `true`: Enable integrity protection. + - `false`: Disable integrity protection. + + > Integrity Protection prevent any accidental or malicious modification of the interpreter. + + > This protection will be automatically disabled in developer mode. + - `dev` - `true`: Enable developer mode. diff --git a/README_zh.md b/README_zh.md index 84e1d53..1446436 100644 --- a/README_zh.md +++ b/README_zh.md @@ -101,6 +101,15 @@ cpc [file_paths] [options] - `true`: 启用开发者模式。 - `false`: 关闭开发者模式。 +- `integrity-protection` + + - `true`: 启用完整性保护。 + - `false`: 禁用完整性保护。 + + > 完整性保护将会阻止任何意外或恶意的解释器修改。 + + > 此保护将在开发者模式下被自动禁用。 + - 开发者选项 - `dev.simulate-update` diff --git a/main.py b/main.py index 2459a07..35c4396 100755 --- a/main.py +++ b/main.py @@ -22,6 +22,7 @@ from src.line_commands import run_command from src.update import update from src.update import update_expired +from src.update import integrity_protection import sys import os @@ -193,6 +194,9 @@ def main(input_=None, output_=None, addition_file_name=None): if addition_file_name: file_paths.add(addition_file_name) + if not config.get_config('dev') and config.get_config('integrity-protection'): + integrity_protection() + #自动更新 if config.get_config('dev.simulate-update') or (config.get_config('auto-update') and not config.get_config('dev') and update_expired()): update() diff --git a/src/config.py b/src/config.py index fd0c460..b715484 100644 --- a/src/config.py +++ b/src/config.py @@ -25,6 +25,7 @@ def __init__(self, config_file_name=".cpc_config.json"): self.config = { 'remote': _Config('remote', 'https://github.com/iewnfod/CAIE_Code.git', remote_update), 'dev': _Config('dev', False, dev_mod), + 'integrity-protection': _Config('integrity-protection', True, integrity_protection), 'branch': _Config('branch', 'stable', branch_update), 'rl': _Config('recursion-limit', 1000, recursive_limit), 'dev.simulate-update': _Config('dev.simulate-update', False, simulate_update), diff --git a/src/config_funcs.py b/src/config_funcs.py index c5797c4..b18ef5b 100644 --- a/src/config_funcs.py +++ b/src/config_funcs.py @@ -43,6 +43,8 @@ def update(self, obj, val): dev_mod = DictConfig({'true': True, 'false': False}) +integrity_protection = DictConfig({'true': True, 'false': False}) + branch_update = SetConfig({'stable', 'nightly', 'dev'}) simulate_update = DictConfig({'true': True, 'false': False}) diff --git a/src/options.py b/src/options.py index e9518b5..d7573cd 100644 --- a/src/options.py +++ b/src/options.py @@ -22,10 +22,11 @@ def get_value(value): return options_dict[value] def standard_output(): + import datetime print(f'CAIE Pseudocode Interpreter v{VERSION} ({get_current_branch()}/{get_commit_hash_msg()[0]})') print(f'Using {PLATFORM}') print('Repository at \033[4mhttps://github.com/iewnfod/CAIE_Code/\33[0m') - print('Copyright (c) 2023 Iewnfod. ') + print(f'Copyright © {datetime.datetime.now().year} Iewnfod. ') print('All Rights Reserved. ') def open_parse_info(): diff --git a/src/update.py b/src/update.py index 2a71675..1997e21 100644 --- a/src/update.py +++ b/src/update.py @@ -121,6 +121,21 @@ def show_notification(_branch): else: print("🙁No developer notification available.") +def integrity_protection(): + repo = git.Repo(HOME_PATH) + if not os.environ.get('CODESPACES'): + current_branch = get_current_branch() + local_commit = repo.head.commit + remote_branch = repo.remote().refs[current_branch] + remote_commit = remote_branch.commit + diff = local_commit.diff(remote_commit) + + if diff or repo.is_dirty(): + repo.git.reset('--hard', remote_commit) + print("❗INTEGRITY WARNING❗") + print("Changes have been discarded") + print("---------------------------------") + def update(): from .global_var import config if os.getenv('CODESPACES'):