Skip to content

Commit da9a5b4

Browse files
committed
Add update
1 parent 9bc5ccf commit da9a5b4

File tree

8 files changed

+58
-11
lines changed

8 files changed

+58
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ cpc [file_path] [options]
3535
| `-t` | `--time` | To show the time for the script to run |
3636
| `-v` | `--version` | To show the version of this interpreter |
3737
| `-ne` | `--no-error` | To remove all error messages |
38+
| `-u` | `--update` | To update the version (only useful when using a version equal or greater than `0.1.2` and installed by git) |
3839

3940
### 常见问题
4041
#### 出现 `Import Error`

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.2

main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from src.parse import *
88
import src.options as options
99
import src.global_var as global_var
10+
from src.history import HOME_PATH
1011
from src.quit import quit
1112

1213
from sys import argv, exit
@@ -20,7 +21,7 @@
2021

2122
preline = '>'
2223
multi_preline = '.'
23-
home_path = os.path.dirname(__file__)
24+
home_path = HOME_PATH
2425

2526
# 清除注释以及多余字符
2627
def remove_comment(text: str):

src/history.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
def parent_path(p):
1010
return os.path.dirname(p)
1111

12+
HOME_PATH = parent_path(parent_path(__file__))
13+
1214
class Cmd:
13-
def __init__(self, home_path=parent_path(parent_path(__file__)), save_path='.history', history_size=1000):
15+
def __init__(self, home_path=HOME_PATH, save_path='.history', history_size=1000):
1416
self.home_path = home_path
1517
self.save_path = save_path
1618
self.history_size = history_size

src/options.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from sys import exit
22
import platform
3+
from .update import VERSION, update
34

4-
VERSION = '0.1.1'
55
PLATFORM = f'[ {platform.python_implementation()} {platform.python_version()} ] on {platform.system()}'
66

77
options_dict = {
@@ -75,12 +75,17 @@ def show_keywords():
7575
def remove_error():
7676
options_dict['show_error'] = False
7777

78+
def update_version():
79+
update()
80+
quit(0)
81+
7882
arguments = [ # 输入参数: (参数简写, 参数全称, 运行函数, 描述)
7983
('-gt', '--get-tree', get_tree, 'To show the tree of the program after being parsed'),
8084
('-v', '--version', version, 'To show the version of this interpreter'),
8185
('-h', '--help', help, 'To show this help page'),
8286
('-p', '--parse', open_parse_info, 'To show parse information during running'),
8387
('-t', '--time', get_time, 'To show the time for the script to run'),
8488
('-k', '--keywords', show_keywords, 'To show all the keywords'),
85-
('-ne', '--no-error', remove_error, 'To remove all error messages')
89+
('-ne', '--no-error', remove_error, 'To remove all error messages'),
90+
('-u', '--update', update_version, 'To check or update the version (only work if you are installing with git)')
8691
]

src/quit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from os import _exit
22
from .global_var import output_error, console
33

4-
def quit(code):
4+
def quit(code=0):
55
output_error()
66
console.postloop()
77
_exit(code)

src/requirements.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
requirements = [
2-
"ply",
3-
"chardet",
2+
('ply', 'ply'),
3+
('chardet', 'chardet'),
4+
('GitPython', 'git')
45
]
56

67
def config():
78
import importlib
89
import pip
9-
for require in requirements:
10+
for package_name, import_name in requirements:
1011
try:
11-
importlib.import_module(require)
12+
importlib.import_module(import_name)
1213
except:
13-
print(f'\033[1mMissing Important Dependence `{require}`\nTrying to Install for You...\033[0m')
14-
pip.main(['install', require])
14+
print(f'\033[1mMissing Important Dependence `{package_name}`\nTrying to Install for You...\033[0m')
15+
pip.main(['install', package_name])

src/update.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from .history import HOME_PATH
2+
import os
3+
import git
4+
5+
VERSION = ''
6+
7+
with open(os.path.join(HOME_PATH, 'VERSION'), 'r') as f:
8+
VERSION = f.read().strip()
9+
10+
flag = False
11+
12+
def check_update(repo: git.Repo, remote: git.Remote):
13+
global flag
14+
remote.fetch()
15+
status = repo.is_dirty()
16+
flag = True
17+
return status
18+
19+
def update():
20+
repo = git.Repo(HOME_PATH)
21+
remote = repo.remote()
22+
23+
print('Checking Update...', end='\r')
24+
if check_update(repo, remote):
25+
# 询问是否更新
26+
u = input('There is a new version of the program. Do you want to update it? [y/N] ').strip().lower()
27+
if u == 'y':
28+
try:
29+
remote.pull()
30+
print('\033[1mUpdate Successful\033[0m')
31+
except Exception as e:
32+
print(f'\033[1;31mFailed to Update\033[0m\n\t{e}')
33+
else:
34+
print('Stop Updating')
35+
else:
36+
print('You are using the latest version!')

0 commit comments

Comments
 (0)