Skip to content

Commit c1ba93e

Browse files
authored
Merge pull request #10 from DeepCodeAI/refactoring
Refactoring
2 parents 8cf7950 + da9eb75 commit c1ba93e

10 files changed

Lines changed: 55 additions & 94 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
## [0.0.7] - 2019-01-31
2-
- Bugfixes
1+
## [0.0.8] - 2019-02-02
2+
- Removed confirmation step for uploading folders
3+
- Simplified logging
34

45
## [0.0.7] - 2019-01-16
56
- Added support of very large folders
7+
- Bugfixes
68

79
## [0.0.5] - 2019-01-15
810
- Hotfixes

‎deepcode/src/constants/config_constants.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
DEEPCODE_PACKAGE_NAME = 'deepcode'
2-
DEEPCODE_PACKAGE_VERSION = '0.0.7'
2+
DEEPCODE_PACKAGE_VERSION = '0.0.8'
33
DEEPCODE_CONFIG_FILENAME = '.deepcodeconfig'
44
DEEPCODE_SOURCE_NAME = {'source': 'cli'}
55
DEEPCODE_BACKEND_HOST = 'https://www.deepcode.ai'
@@ -19,13 +19,11 @@
1919
DEEPCODE_CONFIG_NAMES = {
2020
'backend_host': 'BACKEND_HOST',
2121
'token': 'TOKEN',
22-
'is_upload_confirmed': 'IS_UPLOAD_CONFIRMED',
2322
}
2423

2524
DEEPCODE_DEFAULT_CONFIG_FIELDS = {
2625
DEEPCODE_CONFIG_NAMES['backend_host']: DEEPCODE_BACKEND_HOST,
27-
DEEPCODE_CONFIG_NAMES['token']: None,
28-
DEEPCODE_CONFIG_NAMES['is_upload_confirmed']: False,
26+
DEEPCODE_CONFIG_NAMES['token']: None
2927
}
3028

3129
CURRENT_FOLDER_PATH = '.'

‎deepcode/src/helpers/cli_helpers.py‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,12 @@
5858
'Backend host for DeepCode CLI has been successfully updated\nNow {new_host} is used.'.format(new_host=new_host))
5959
}
6060

61-
CONFRIM_UPLOAD_HELPERS = {
62-
'confirm': text__with_colors['blue']('To analyze code, confirm remote analysis by DeepCode. Confirm? (y/n): '),
63-
'success': text__with_colors['green']('Remote analysis is successfully confirmed.'),
64-
'fail': text__with_colors['red']('Remote analysis is not confirmed.')
65-
}
66-
6761
LOGIN_HELPERS = {
6862
'url': lambda login_url:
6963
text__with_colors['blue']('The deepCode login page {} is opened in browser.\nWaiting for login...'.format(
7064
text_decorations['bold'](login_url))),
7165
'login_success': text__with_colors['green']('Login has been successfull'),
7266
'logout': text__with_colors['green']('Logout has been successfull'),
73-
'login_without_confirm': text__with_colors['red']('Already logged in, but remote analysis is not confirmed'),
7467
'already_login': text__with_colors['green']('Already logged in'),
7568
'not_logged_in': text__with_colors['red']('Not logged in'),
7669
}
@@ -83,8 +76,8 @@
8376
}
8477

8578
BUNDLE_HELPERS = {
86-
'creating': lambda path='': text__with_colors['blue']('Creating {} bundle...'.format(path)),
87-
'uploading': lambda path='': text__with_colors['blue']('Uploading {} bundle...'.format(path)),
88-
'empty': lambda path='': text__with_colors['blue']('Bundle {} is empty, nothing to analyze.'.format(path)),
79+
'creating': text__with_colors['blue']('Creating bundle...'),
80+
'uploading': text__with_colors['blue']('Uploading bundle...'),
81+
'empty': text__with_colors['blue']('Bundle is empty, nothing to analyze.'),
8982
'creating_diff': 'Creating bundles...'
9083
}

‎deepcode/src/modules/analyzer.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def analyze(self, bundle_id, show_progressbar=True):
7272
def fetch_analysis_from_server(self, route, bundle_id, progress_bar=None,):
7373
# deepcode ignore replace~range~list: using iterator inside forloop is good
7474
for _ in range(MAX_POLLS_LIMIT):
75+
# print('before http get fetch_analysis_from_server to route --> ', route)
7576
analysis_response = self.http.get(route, response_to_json=False)
7677
analysis_results = analysis_response.json()
7778
if not validate_analysis_response(analysis_results):

‎deepcode/src/modules/bundler.py‎

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ def create_repo_bundle(self, bundle_path):
3232

3333
@DeepCodeErrorHandler.backend_error_decorator
3434
def create_server_remote_bundle(self, path_dict_to_remote_bundle):
35-
remote_bundle = self.http.post(DEEPCODE_API_ROUTES['create_bundle'], {
36-
'data':
37-
{key: path_dict_to_remote_bundle[key]
38-
for key in path_dict_to_remote_bundle if path_dict_to_remote_bundle[key] is not None},
39-
})
35+
data = {
36+
key: path_dict_to_remote_bundle[key]
37+
for key in path_dict_to_remote_bundle
38+
if path_dict_to_remote_bundle[key] is not None
39+
}
40+
# print('create_server_remote_bundle http post with data --> ', data)
41+
remote_bundle = self.http.post(DEEPCODE_API_ROUTES['create_bundle'], {'data': data})
4042
if not validate_remote_bundle_response(remote_bundle):
4143
DeepCodeErrorHandler.raise_backend_error('invalid_bundle_response',
4244
err_details=DeepCodeErrorHandler.construct_backend_error_for_report(
@@ -60,23 +62,27 @@ def create_files_bundle(self, bundle_path, show_progressbar=True):
6062
# create hashes bundle
6163
self.hashes_bundles[abs_path] = self.create_hashes_bundle(
6264
abs_path, show_progressbar=show_progressbar)
65+
# print('hashes_bundles --> ', self.hashes_bundles)
6366
# create remote bundle on server
6467
server_start_bundle = self.create_files_server_bundle(
6568
self.hashes_bundles[abs_path])
69+
# print('server_start_bundle --> ', server_start_bundle)
6670
# check for missing files and upload missing files
6771
self.server_bundles[abs_path] = self.handle_server_bundle_missing_files(
6872
server_start_bundle,
6973
self.hashes_bundles[abs_path],
70-
abs_path, show_progressbar=show_progressbar)
74+
show_progressbar=show_progressbar)
75+
# print('server_bundles --> ', self.server_bundles)
7176
return self.server_bundles[abs_path]
7277

7378
@DeepCodeErrorHandler.backend_error_decorator
7479
def create_files_filters_from_server(self):
75-
return self.http.get(DEEPCODE_API_ROUTES['files_filters'])
80+
res = self.http.get(DEEPCODE_API_ROUTES['files_filters'])
81+
return res
7682

7783
@DeepCodeErrorHandler.bundle_path_error_decorator
7884
def create_abs_bundle_path(self, bundle_path):
79-
is_current_path = bundle_path is CURRENT_FOLDER_PATH
85+
# is_current_path = bundle_path is CURRENT_FOLDER_PATH
8086
result_path = os.path.join(os.path.sep, os.path.realpath(bundle_path))
8187
if not os.path.exists(result_path):
8288
DeepCodeErrorHandler.raise_path_error('no_path')
@@ -90,16 +96,19 @@ def create_hashes_bundle(self, bundle_path, show_progressbar=True):
9096
self.files_filters,
9197
show_progressbar=show_progressbar,
9298
progress_iterator=progress_iterator(
93-
prefix=BUNDLE_HELPERS['creating'](bundle_path))
99+
prefix=BUNDLE_HELPERS['creating'])
94100
)
95101
if not len(hashes_bundle):
96102
DeepCodeErrorHandler.raise_files_bundle_error('empty_bundle')
97103
return hashes_bundle
98104

99105
@DeepCodeErrorHandler.backend_error_decorator
100106
def create_files_server_bundle(self, hashes_bundle):
101-
server_bundle = self.http.post(DEEPCODE_API_ROUTES['create_bundle'], {
102-
'data': {'files': hashes_bundle}})
107+
# print('create_files_server_bundle. hashes_bundle --> ', hashes_bundle)
108+
server_bundle = self.http.post(
109+
DEEPCODE_API_ROUTES['create_bundle'],
110+
{'data': {'files': hashes_bundle}}
111+
)
103112
if not validate_remote_bundle_response(server_bundle):
104113
DeepCodeErrorHandler.raise_backend_error('invalid_bundle_response',
105114
err_details=DeepCodeErrorHandler.construct_backend_error_for_report(
@@ -111,7 +120,7 @@ def check_server_bundle_on_server(self, bundle_id):
111120
return self.http.get(DEEPCODE_API_ROUTES['check_bundle'](bundle_id))
112121

113122
@DeepCodeErrorHandler.backend_error_decorator
114-
def handle_server_bundle_missing_files(self, server_bundle, hashes_bundle, bundle_path, show_progressbar=True):
123+
def handle_server_bundle_missing_files(self, server_bundle, hashes_bundle, show_progressbar=True):
115124
def _iterate_func(progress_bar=None):
116125
bundle_to_check = server_bundle
117126
# deepcode ignore replace~range~list: Iterator in for-loop is considered good
@@ -124,14 +133,13 @@ def _iterate_func(progress_bar=None):
124133
if progress_bar:
125134
progress_bar.update(MAX_PROGRESS_VALUE)
126135
break
127-
self.upload_missing_files(
128-
bundle_to_check, hashes_bundle, bundle_path, show_progressbar)
136+
self.upload_missing_files(bundle_to_check, hashes_bundle, show_progressbar)
129137
bundle_to_check = self.check_server_bundle_on_server(
130138
bundle_to_check['bundleId'])
131139
return bundle_to_check
132140

133141
if show_progressbar:
134-
with construct_progress(prefix=BUNDLE_HELPERS['uploading'](bundle_path)) as progress:
142+
with construct_progress(prefix=BUNDLE_HELPERS['uploading']) as progress:
135143
return _iterate_func(progress_bar=progress)
136144
else:
137145
return _iterate_func()
@@ -141,8 +149,8 @@ def missing_files_in_server_bundle(self, bundle):
141149
return missingFiles in bundle and len(bundle[missingFiles])
142150

143151
@DeepCodeErrorHandler.backend_error_decorator
144-
def upload_missing_files(self, server_bundle, hashes_bundle, bundle_path, show_progressbar):
145-
152+
def upload_missing_files(self, server_bundle, hashes_bundle, show_progressbar):
153+
# print('!!! started upload_missing_files !!!')
146154
def _upload_missing_to_server(batch):
147155
bundle_id = server_bundle['bundleId']
148156
return self.http.post(DEEPCODE_API_ROUTES['upload_files'](bundle_id), {
@@ -151,16 +159,15 @@ def _upload_missing_to_server(batch):
151159
}, response_to_json=False)
152160

153161
missing_files_batch = self.create_missing_files_batch(
154-
server_bundle['missingFiles'], hashes_bundle, bundle_path)
155-
162+
server_bundle['missingFiles'], hashes_bundle)
156163
# split into smaller batches if necessary
157164
separate_batches = self.split_missing_files_into_batches(
158165
missing_files_batch)
159166
i = 1
160167

161168
multi_bundle_progress_bar = None
162169
if show_progressbar:
163-
multi_bundle_progress_bar = construct_progress(prefix=BUNDLE_HELPERS['uploading']("split"), min_value=0, max_value=len(separate_batches))
170+
multi_bundle_progress_bar = construct_progress(prefix=BUNDLE_HELPERS['uploading'], min_value=0, max_value=len(separate_batches))
164171

165172

166173
for batch in separate_batches:
@@ -183,13 +190,13 @@ def compute_files_batch_list_size(self, batch):
183190
return total_size
184191

185192
@DeepCodeErrorHandler.files_bundle_error_decorator
186-
def create_missing_files_batch(self, missing_files, hashes_bundle, abs_bundle_path):
193+
def create_missing_files_batch(self, missing_files, hashes_bundle):
187194
missing_files_batch = []
188195
for file_path in missing_files:
189-
p = file_path[1:] if file_path[0] == '/' else file_path
196+
# p = file_path[1:] if file_path[0] == '/' else file_path
197+
p = file_path
190198
file_hash = hashes_bundle[p]
191-
file_content = file_contents_as_string(
192-
os.path.join(abs_bundle_path, p))
199+
file_content = file_contents_as_string(p)
193200
if file_content is not None:
194201
missing_files_batch.append(
195202
{'fileHash': file_hash, 'fileContent': file_content})

‎deepcode/src/modules/config.py‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ def update_config(self, new_fields={}):
5151

5252
def delete_user_config(self, update_file=True):
5353
self.current_config[DEEPCODE_CONFIG_NAMES['token']] = ''
54-
self.current_config[DEEPCODE_CONFIG_NAMES['is_upload_confirmed']] = False
5554
if update_file:
5655
self.update_config_file(self.current_config)
5756

@@ -62,27 +61,15 @@ def update_backend_host(self, new_host):
6261
def is_current_backend_host_is_default(self):
6362
return self.current_config[DEEPCODE_CONFIG_NAMES['backend_host']] == DEEPCODE_BACKEND_HOST
6463

65-
def set_user_login_config(self, token, upload_confrm):
64+
def set_user_login_config(self, token):
6665
logged_in_config = {
6766
'token': token,
6867
}
69-
if upload_confrm:
70-
logged_in_config['is_upload_confirmed'] = True
7168
self.update_config(logged_in_config)
7269

7370
def is_user_logged_in(self):
7471
return bool(self.current_config[DEEPCODE_CONFIG_NAMES['token']])
7572

76-
def is_code_upload_confirmed(self):
77-
return self.current_config[DEEPCODE_CONFIG_NAMES['is_upload_confirmed']]
78-
79-
def check_login_and_confirm(self):
80-
return [self.is_user_logged_in(), self.is_code_upload_confirmed()]
81-
82-
def activate_code_upload(self):
83-
self.current_config[DEEPCODE_CONFIG_NAMES['is_upload_confirmed']] = True
84-
self.update_config_file(self.current_config)
85-
8673
def configure_cli(self):
8774
new_backend_host = input(
8875
CONFIG_SETTINGS_MESSAGES['configure_backend_host'])

‎deepcode/src/modules/deepcode_main.py‎

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,10 @@ def cli_config_actions(self, cli_args_dict):
5757

5858
# handle cli login command
5959
def cli_login_actions(self):
60-
is_user_logged_in, is_upload_confirmed = self.config.check_login_and_confirm()
60+
is_user_logged_in = self.config.is_user_logged_in()
6161
# if user is already logged in
6262
if is_user_logged_in:
63-
if not is_upload_confirmed:
64-
print(LOGIN_HELPERS['login_without_confirm'])
65-
self.confirm_upload_common_actions()
66-
else:
67-
print(LOGIN_HELPERS['already_login'])
63+
print(LOGIN_HELPERS['already_login'])
6864
return
6965
# login user flow
7066
logged_in = self.user.login()
@@ -73,15 +69,7 @@ def cli_login_actions(self):
7369
print(LOGIN_HELPERS['url'](login_url))
7470
self.user.check_login(token)
7571
print(LOGIN_HELPERS['login_success'])
76-
confirm_response = self.user.confirm_code_upload()
77-
self.config.set_user_login_config(
78-
token, confirm_response)
79-
80-
# confirm uploading code
81-
def confirm_upload_common_actions(self):
82-
confirm_response = self.user.confirm_code_upload()
83-
if confirm_response:
84-
self.config.activate_code_upload()
72+
self.config.set_user_login_config(token)
8573

8674
# logout of user (cleans up user config data)
8775
def cli_logout_actions(self):
@@ -97,14 +85,11 @@ def cli_analyze_actions(self, analyze_options):
9785
self.cli_analysis_display_actions(analysis_results, analyze_options)
9886

9987
def cli_pre_analyze_actions(self, analyze_options):
100-
is_user_logged_in, is_upload_confirmed = self.config.check_login_and_confirm()
88+
is_user_logged_in = self.config.is_user_logged_in()
10189
if not is_user_logged_in:
10290
print(LOGIN_HELPERS['not_logged_in'])
10391
return
104-
if not is_upload_confirmed:
105-
print(LOGIN_HELPERS['login_without_confirm'])
106-
self.confirm_upload_common_actions()
107-
return
92+
10893
single_path, two_paths = (1, 2)
10994
paths = analyze_options['path']
11095
# paths_count = len(paths)
@@ -151,7 +136,7 @@ def cli_analysis_display_actions(self, analysis_results, analyze_options):
151136
# analyze func for module mode
152137
@DeepCodeErrorHandler.module_mode_error_decorator
153138
def module_analyze_actions(self, paths, is_repo=False):
154-
is_user_logged_in = self.config.check_login_and_confirm()[0]
139+
is_user_logged_in = self.config.is_user_logged_in()
155140
if not is_user_logged_in:
156141
DeepCodeErrorHandler.raise_backend_error('token')
157142

‎deepcode/src/modules/user.py‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from deepcode.src.constants.backend_constants import MAX_POLLS_LIMIT, POLLING_INTERVAL, BACKEND_STATUS_CODES
88
from deepcode.src.constants.config_constants import DEEPCODE_API_ROUTES, DEEPCODE_SOURCE_NAME
9-
from deepcode.src.helpers.cli_helpers import CONFRIM_UPLOAD_HELPERS
109

1110

1211
class DeepCodeUser:
@@ -33,11 +32,3 @@ def check_login(self, token):
3332
return True
3433
time.sleep(POLLING_INTERVAL)
3534
return False
36-
37-
def confirm_code_upload(self):
38-
positive_response_types = ('y', 'Y')
39-
confirm_upload_user_response = input(CONFRIM_UPLOAD_HELPERS['confirm'])
40-
is_positive_user_response = confirm_upload_user_response in positive_response_types
41-
print(
42-
CONFRIM_UPLOAD_HELPERS['success' if is_positive_user_response else 'fail'])
43-
return is_positive_user_response

‎deepcode/src/utils/analysis_utils.py‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ def hash_files(path, max_file_size, filters_dict, show_progressbar=True, progres
4949
# filtering files
5050
for f in files:
5151
file_path = os.path.join(root, f)
52-
rel_path = os.path.relpath(file_path, path)
53-
if pass_filter(rel_path, filters_dict):
54-
paths.append((file_path, rel_path))
52+
if pass_filter(file_path, filters_dict):
53+
paths.append(file_path)
5554
result = {}
5655
# creations of hashes
5756
for path in paths:
@@ -62,14 +61,12 @@ def hash_files(path, max_file_size, filters_dict, show_progressbar=True, progres
6261
return result
6362

6463

65-
def create_file_hash_with_path(max_file_size, path_list):
66-
abs_path_, rel_path_ = path_list
67-
file_content = file_contents_as_string(abs_path_, max_file_size)
64+
def create_file_hash_with_path(max_file_size, file_path):
65+
file_content = file_contents_as_string(file_path, max_file_size)
6866
if not file_content:
6967
return None
70-
file_hash = hash_file_content(file_content)
71-
72-
return rel_path_, file_hash
68+
69+
return file_path, hash_file_content(file_content)
7370

7471

7572
def file_contents_as_string(path, max_file_size=MAX_FILE_SIZE):

‎test_package.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ To test the released cli package in a clean environment, run:
1616
pip install deepcode
1717
deepcode login
1818

19-
After interactively performing the login and remote transfer confirmation,
19+
After interactively performing the login,
2020
positive cli output and a proper ~/.deepcodeconfig should be generated
2121
(version 0.0.4 fails with a JSON error instead).

0 commit comments

Comments
 (0)