This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.py
134 lines (117 loc) · 4.2 KB
/
mock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python2
# -*- coding:utf-8 -*-
# Copyright 2019 Jonkimi
# All rights reserved.
# license that can be found in the LICENSE file.
from __future__ import print_function
# from builtins import input
from sys import stdout
import json
import os.path
import urllib2
import urlparse
import shutil
import argparse
import shlex
import ConfigParser
# test cookie 'koa.sid=7QC71YKGHTaOZUbeMHirZjicr0MN8-fX; koa.sid.sig=h0Q0zcHLVyJ_qQVxfwD_9pvXSLE'
server = 'https://127.0.0.1:8088'
repo_url = '{0}/repository/get?id={1}'
mock_url_prefix = '{0}/app/mock/{1}/'
# 脚本所在目录
mock_dir = os.path.split(os.path.realpath(__file__))[0]
example_script = u'test'
example_json = u'test.json'
def send_req(url, cookie):
'''
向 rap2-delos 发送请求
:param url:
:param cookie:
:return:
'''
print('send request: ', url)
req = urllib2.Request(url)
req.add_header('cookie', cookie)
try:
response = urllib2.urlopen(req, timeout=60)
except urllib2.HTTPError as err:
print("HTTP error({0}): {1}".format(err.errno, err.strerror))
response_str = None
except IOError as err:
print("IO error({0}): {1}, please check your repo id and cookie.".format(err.errno, err.strerror))
response_str = None
else:
response_str = response.read()
return response_str
def mock_repo(repo_id, cookie):
repo_str = send_req(repo_url.format(server, repo_id), cookie)
if repo_str is None:
return
parsed_json = json.loads(repo_str)
data = parsed_json['data']
if data is not None:
interfaces = [y for x in data['modules'] for y in x['interfaces']]
# print json.dumps(interfaces)
ws_interfaces = filter(lambda i: i['name'].startswith('ws'), interfaces)
# print json.dumps(ws_interfaces)
for ws in ws_interfaces:
print('----------------------------------------')
ws_url = ws['url']
if ws_url.endswith('/'):
ws_url = ws_url[:len(ws_url) - 1]
name = os.path.basename(ws_url)
if ws_url.startswith('/'):
ws_url = ws_url[1: len(ws_url)]
path = os.path.dirname(os.path.join(mock_dir, ws_url))
print('path:', ws_url)
# 创建目录
try:
if not os.path.exists(path):
os.makedirs(path)
except OSError as err:
print("OSError ({0}): {1}".format(err.errno, err.strerror))
else:
# 复制脚本
shutil.copy(os.path.join(mock_dir, example_script), os.path.join(path, name))
# 写入脚本配置
with open(os.path.join(path, name + '.conf'), 'w') as mock_url:
mock_url.write(urlparse.urljoin(mock_url_prefix.format(server, repo_id), ws_url))
print('mock {0} OK '.format(ws_url))
def handle():
while True:
stdout.flush()
command = raw_input()
if command is None or not command.startswith('mock '):
print(APP_DESC)
else:
try:
args = parser.parse_args(shlex.split(command[5:]))
except SystemExit :
# 将命令行解析失败或打印帮助信息后的退出 忽略
pass
else:
# print(args)
if args.cookie is None or args.repo_id is None:
print(APP_DESC)
else:
mock_repo(args.repo_id, args.cookie)
if __name__ == '__main__':
APP_DESC = """usage: mock [-h] [-i repo-id] [-c cookie]
optional arguments:
-h, --help show this help message and exit
-i, --repo-id set repository id
-c, --cookie set auth cookie
"""
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--repo-id', type=int, help="")
parser.add_argument('-c', '--cookie', type=str, help="")
# 读取服务器配置
config_path = os.path.join(mock_dir, "mock.conf")
if not os.path.exists(config_path):
print("config file mock.conf not exists.")
else:
cf = ConfigParser.ConfigParser()
cf.read(config_path)
server = cf.get('main', 'server')
print(APP_DESC)
handle()