Skip to content

Commit 7d70d4f

Browse files
committed
Start using absolute path while fetching cache and config and saving files to output
1 parent f5df75e commit 7d70d4f

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Windows:
6767

6868
pip install requests
6969

70-
### Install python colorama module, [python-colorama module](http://pypi.python.org/pypi/colorama).
70+
### Install python colorama module, [python-colorama module](https://pypi.org/project/colorama/).
7171

7272
Debian/Ubuntu OS:
7373

_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '2.0.7'
1+
__version__ = '2.0.8'

cli/wrappers/cli_caller.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import datetime
77
from cli.cli_file_writer import CliFileWriter
88
from cli.formatter.cli_json_formatter import CliJsonFormatter
9+
from constants import CALLED_SCRIPT
910

1011

1112
class CliCaller:
@@ -96,9 +97,27 @@ def get_processed_output_path(self):
9697
if output_path.startswith('/') is True: # Given path is absolute
9798
final_output_path = output_path
9899
else:
99-
prepared_file_path = os.path.dirname(os.path.realpath(__file__)).split('/')[:-2] + [self.cli_output_folder]
100+
path_parts = os.path.dirname(os.path.realpath(__file__)).split('/')[:-2]
101+
called_script_dir = os.path.dirname(CALLED_SCRIPT)
102+
# It's about a case when user is calling script from not root directory.€
103+
if called_script_dir != 'vxapi.py':
104+
new_path_parts = []
105+
bad_parts = called_script_dir.split('/')
106+
for part in reversed(path_parts):
107+
if part in bad_parts:
108+
bad_parts.remove(part)
109+
continue
110+
new_path_parts.append(part)
111+
112+
new_path_parts.reverse()
113+
path_parts = new_path_parts
114+
115+
prepared_file_path = path_parts + [self.cli_output_folder]
100116
final_output_path = '/'.join(prepared_file_path)
101117

118+
if not final_output_path.startswith('/'):
119+
final_output_path = '/' + final_output_path
120+
102121
return final_output_path
103122

104123
def get_result_msg_for_files(self):
@@ -136,4 +155,4 @@ def save_files(self):
136155

137156
filename = '{}-{}-{}'.format(self.action_name, identifier, api_response.headers['Vx-Filename']) if identifier is not None else '{}-{}'.format(self.action_name, api_response.headers['Vx-Filename'])
138157

139-
return CliFileWriter.write(self.cli_output_folder, filename, api_response.content)
158+
return CliFileWriter.write(self.get_processed_output_path(), filename, api_response.content)

constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
import sys
3+
14
ACTION_SEARCH_HASH = 'search_hash'
25
ACTION_SEARCH_HASHES = 'search_hashes'
36
ACTION_SEARCH_STATES = 'search_states'
@@ -51,5 +54,7 @@
5154
ACTION_SCAN_URL_TO_FILE = 'scan_url_to_file'
5255

5356
MINIMAL_SUPPORTED_INSTANCE_VERSION = '8.2'
57+
CLI_BASE_PATH = os.path.dirname(os.path.realpath(__file__))
58+
CALLED_SCRIPT = sys.argv[0]
5459

5560
ACTION_WITH_MULTIPLE_CALL_SUPPORT = [ACTION_SUBMIT_FILE, ACTION_SCAN_FILE]

vxapi.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import time
4343
import os.path
4444
import json
45+
from importlib.machinery import SourceFileLoader
4546

4647
from collections import OrderedDict
4748

@@ -106,18 +107,19 @@ class CliManager:
106107
vxapi_cli_headers = {'User-agent': 'VxApi CLI Connector'}
107108
request_session = None
108109
loaded_action = None
109-
current_key_cache_path_template = 'cache/current_key_{}.json'
110+
current_key_cache_path_template = '{}/cache/current_key_{}.json'
110111
current_key_sess_cache_file_path = None
111112
cache_disabled = False
113+
config_path = '{}/config.py'.format(CLI_BASE_PATH)
112114

113115
def load_config(self):
114116
if is_test_env is True:
115117
config = json.loads(os.environ['VX_TEST_CONFIG'])
116-
elif os.path.exists('config.py'):
117-
from config import get_config
118-
config = get_config()
118+
elif os.path.exists(self.config_path):
119+
imported = SourceFileLoader('config', self.config_path).load_module()
120+
config = imported.get_config()
119121
else:
120-
raise MissingConfigurationError('Configuration is missing. Before running CLI, please copy the file \'config_tpl.py\' from current dir, rename it to \'config.py\', and fill')
122+
raise MissingConfigurationError('Configuration is missing (looked for: {}). Before running CLI, please copy the file \'config_tpl.py\' from current dir, rename it to \'config.py\', and fill'.format(self.config_path))
121123

122124
if 'server' not in config or 'api_key' not in config:
123125
raise ConfigError('Config does not contain all of required \'server\' and \'api_key\' keys')
@@ -135,7 +137,7 @@ def load_config(self):
135137
raise ConfigError('Your API Key is not compatible with API v2. Please regenerate it at your profile page or create the new one.')
136138

137139
self.config = config
138-
self.current_key_sess_cache_file_path = self.current_key_cache_path_template.format(self.config['api_key'])
140+
self.current_key_sess_cache_file_path = self.current_key_cache_path_template.format(CLI_BASE_PATH, self.config['api_key'])
139141

140142
self.cache_disabled = True if 'VX_DISABLE_CACHING' in os.environ and os.environ['VX_DISABLE_CACHING'] == '1' else False
141143

0 commit comments

Comments
 (0)