Skip to content

Commit cc1239a

Browse files
eosfactory data dir
1 parent 882c521 commit cc1239a

File tree

3 files changed

+106
-82
lines changed

3 files changed

+106
-82
lines changed

docs/tutorials/01.InstallingEOSFactory.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Then
164164
```bash
165165
./uninstall.sh
166166
```
167-
or
167+
Or, anywhere in the system, do
168168
```bash
169169
pip3 uninstall eosfactory-tokenika
170170
```

eosfactory/core/config.py

Lines changed: 98 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
import subprocess
77

8+
import eosfactory
89
import eosfactory.core.errors as errors
910
import eosfactory.core.logger as logger
1011
import eosfactory.core.utils as utils
@@ -15,12 +16,7 @@
1516
EOSIO_CDT_VERSION = "1.6.1"
1617
PYTHON_VERSION = "3.5 or higher"
1718
EOSFACTORY_DIR = "eosfactory/"
18-
APP_DATA_DIR_USER = (
19-
os.path.expandvars("${HOME}/.local/" + EOSFACTORY_DIR),
20-
os.path.expandvars("${HOME}")
21-
)
22-
APP_DATA_DIR_SUDO = ("/usr/local/" + EOSFACTORY_DIR, "/usr/local/")
23-
APP_CWD_DIR = "/tmp/eosfactory/"
19+
TMP = "/tmp/eosfactory/"
2420
SETUPTOOLS_NAME = "eosfactory_tokenika"
2521

2622
LOCALHOST_HTTP_ADDRESS = "127.0.0.1:8888"
@@ -32,6 +28,10 @@
3228
TEMPLATE_DIR = ("TEMPLATE_DIR", "templates/contracts")
3329
EOSIO_CPP_DIR = "/usr/opt/eosio.cdt/0.0.0/"
3430

31+
eosfactory_data_ = ("EOSFACTORY_DATA_DIR",
32+
[os.path.expandvars("${HOME}/.local/" + EOSFACTORY_DIR),\
33+
"/usr/local/" + EOSFACTORY_DIR,],
34+
[])
3535
node_address_ = ("LOCAL_NODE_ADDRESS", [LOCALHOST_HTTP_ADDRESS])
3636
wallet_address_ = ("WALLET_MANAGER_ADDRESS", [LOCALHOST_HTTP_ADDRESS])
3737
genesis_json_ = ("EOSIO_GENESIS_JSON",
@@ -74,54 +74,78 @@
7474
"EOSIO_CONTRACT_WORKSPACE", [CONTRACTS_DIR])
7575

7676

77-
def get_app_data_dir():
78-
if APP_DATA_DIR_SUDO[1] in __file__:
79-
app_data_dir = APP_DATA_DIR_SUDO[0]
80-
elif os.path.expandvars(APP_DATA_DIR_USER[1]) in __file__:
81-
app_data_dir = APP_DATA_DIR_USER[0]
82-
else:
83-
app_data_dir = eosf_dir()
84-
85-
if app_data_dir and os.path.exists(app_data_dir):
86-
return app_data_dir
77+
def eosfactory_data():
78+
'''Data directory.
8779
88-
raise errors.Error('''
89-
Cannot determine the directory of application data. Tried:
90-
{}
91-
{}
92-
{}
93-
The path '__file__' is
94-
{}
95-
The chosen path is
80+
For developer's installation, data is in the root of the installation.
81+
.: wsl_root.sh
82+
config: config.ini, config.json, genesis.json, ...
83+
contracts: eosio_token, hello_world, tic_tac_toe, ...
84+
templates: contracts, ...
85+
includes: eoside, ...
86+
libs: ...
87+
'''
88+
tested = []
89+
is_not_linked = is_site_package()
90+
91+
if not is_not_linked:
92+
path = eosf_dir()
93+
tested.append(path)
94+
if os.path.exists(os.path.join(path, "config", "config.ini")):
95+
return path
96+
elif is_not_linked == 1:
97+
for path in eosfactory_data_[1]:
98+
tested.append(path)
99+
if os.path.exists(os.path.join(path, "config", "config.ini")):
100+
return path
101+
elif is_not_linked == 2:
102+
for path in eosfactory_data_[2]:
103+
tested.append(path)
104+
if os.path.exists(os.path.join(path, "config", "config.ini")):
105+
return path
106+
107+
msg = "Cannot determine the directory of application data. Tried:"
108+
for path in tested:
109+
msg = '''{}
96110
{}
97-
but it does not exist, seemingly.
98-
'''.format(
99-
APP_DATA_DIR_SUDO[0], APP_DATA_DIR_USER[0], eosf_dir(),
100-
__file__,
101-
app_data_dir),
102-
translate=False)
111+
'''.format(msg, path)
112+
113+
raise errors.Error(msg, translate=False)
114+
115+
116+
def is_site_package():
117+
is_local_or_system = -1
118+
eosfactory_path = eosfactory.__path__
119+
for item in eosfactory_path:
120+
if "site-packages" in item:
121+
if "local" in item:
122+
is_local_or_system = 1
123+
else:
124+
is_local_or_system = 2
103125

126+
break
127+
128+
if "eosfactory/eosfactory" in item:
129+
is_local_or_system = 0
104130

105-
def is_linked_package():
106-
is_linked = os.path.exists(os.path.join(eosf_dir(), CONFIG_DIR))
107-
is_installed_package = not is_linked and get_app_data_dir()
131+
# No EOSFactory:
132+
# import eosfactory
133+
# Traceback (most recent call last):
134+
# File "<stdin>", line 1, in <module>
135+
# ModuleNotFoundError: No module named 'eosfactory'
108136

109-
if (not is_linked) and (not is_installed_package):
137+
# developer's installation:
138+
# >>> import eosfactory
139+
# >>> print(eosfactory.__path__)
140+
# ['/mnt/c/Workspaces/EOS/eosfactory/eosfactory']
141+
142+
if is_local_or_system == -1:
110143
raise errors.Error('''
111-
Cannot determine the configuration directory.
112-
{}
144+
Cannot determine the configuration directory. 'eosfactory.__path__' is
113145
{}
114-
{}
115-
'''.format(
116-
os.path.join(eosf_dir(), CONFIG_DIR),
117-
os.path.join(APP_DATA_DIR_USER[0], CONFIG_DIR),
118-
os.path.join(APP_DATA_DIR_SUDO[0], CONFIG_DIR)
119-
), translate=False)
120-
121-
if is_linked and is_installed_package:
122-
is_linked = True
146+
'''.format(eosfactory_path), translate=False)
123147

124-
return is_linked
148+
return is_local_or_system
125149

126150

127151
def set_contract_workspace_dir(contract_workspace_dir=None, is_set=False):
@@ -155,7 +179,7 @@ def set(contract_workspace_dir):
155179
if contract_workspace_dir_[0] in map:
156180
contract_workspace_dir = map[contract_workspace_dir_[0]]
157181
else:
158-
contract_workspace_dir = os.path.join(APP_CWD_DIR, CONTRACTS_DIR)
182+
contract_workspace_dir = os.path.join(TMP, CONTRACTS_DIR)
159183

160184
new_dir = tilde(input(utils.heredoc('''
161185
Where do you prefer to keep your smart-contract projects?
@@ -183,7 +207,7 @@ def set(contract_workspace_dir):
183207

184208

185209
def config_dir():
186-
dir = os.path.join(get_app_data_dir(), CONFIG_DIR)
210+
dir = os.path.join(eosfactory_data(), CONFIG_DIR)
187211
if not os.path.exists(dir):
188212
raise errors.Error('''
189213
Cannot find the configuration directory
@@ -193,7 +217,7 @@ def config_dir():
193217

194218

195219
def template_dir():
196-
dir = os.path.join(get_app_data_dir(), TEMPLATE_DIR[1])
220+
dir = os.path.join(eosfactory_data(), TEMPLATE_DIR[1])
197221
if not os.path.exists(dir):
198222
raise errors.Error('''
199223
Cannot find the template directory
@@ -211,7 +235,7 @@ def eoside_includes_dir():
211235
'''
212236
dir = includes_[1]
213237
if not os.path.isabs(dir):
214-
dir = os.path.join(get_app_data_dir(), includes_[1])
238+
dir = os.path.join(eosfactory_data(), includes_[1])
215239
if not os.path.exists(dir):
216240
dir = None
217241
return dir
@@ -226,7 +250,7 @@ def eoside_libs_dir():
226250
'''
227251
dir = libs_[1]
228252
if not os.path.isabs(dir):
229-
dir = os.path.join(get_app_data_dir(), libs_[1])
253+
dir = os.path.join(eosfactory_data(), libs_[1])
230254
if not os.path.exists(dir):
231255
dir = None
232256
return dir
@@ -242,7 +266,7 @@ def contract_workspace_dir(dont_set_workspace=False):
242266
subdirectory (typically *contracts/*) of the EOSFActory installation, if
243267
EOSFactory is installed from its GitHub repository, otherwise, they go to
244268
a directory specified as
245-
`join(.config.APP_CWD_DIR, .config.CONTRACTS_DIR)`.
269+
`join(.config.TMP, .config.CONTRACTS_DIR)`.
246270
247271
The setting may be changed with
248272
*EOSIO_CONTRACT_WORKSPACE* entry in the *config.json* file,
@@ -271,10 +295,10 @@ def contract_workspace_dir(dont_set_workspace=False):
271295
set as the contract workspace directory, does not exist.
272296
'''.format(path), translate=False)
273297
else:
274-
if is_linked_package():
298+
if not is_site_package():
275299
path = os.path.join(eosf_dir(), path)
276300
else:
277-
path = os.path.join(APP_CWD_DIR, path)
301+
path = os.path.join(TMP, path)
278302
if not os.path.exists(path):
279303
os.makedirs(path)
280304

@@ -307,20 +331,14 @@ def abi_file(contract_dir_hint):
307331
def eosf_dir():
308332
'''The absolute directory of the EOSFactory installation.
309333
'''
310-
try:
311-
path = os.path.realpath(os.path.join(
334+
path = os.path.realpath(os.path.join(
312335
os.path.realpath(__file__), FROM_HERE_TO_EOSF_DIR))
313-
except Exception as e:
314-
raise errors.Error('''
315-
Impossible error: __file__ path cannot be determined. The message is
316-
{}
317-
'''.format(str(e)))
318336

319337
if os.path.exists(path):
320338
return path
321339

322340
raise errors.Error('''
323-
Cannot determine the root directory of EOSFactory.
341+
Cannot determine the root directory of the EOSFactory installation.
324342
The path to the file 'config.py' is
325343
'{}'.
326344
The expected installation path, which is
@@ -426,7 +444,7 @@ def wsl_root():
426444
return ""
427445

428446
wsl_root_sh = "wsl_root.sh"
429-
wsl_root_sh = os.path.join(get_app_data_dir(), wsl_root_sh)
447+
wsl_root_sh = os.path.join(eosfactory_data(), wsl_root_sh)
430448

431449
if wsl_root_[1][0] is None:
432450
path = ""
@@ -882,9 +900,9 @@ def contract_dir(contract_dir_hint):
882900
return os.path.realpath(contract_dir_)
883901

884902
# ? the relative path to a contract directory, relative to
885-
# 'get_app_data_dir()/contracts'
903+
# 'eosfactory_data()/contracts'
886904
contract_dir_ = os.path.join(
887-
get_app_data_dir(), CONTRACTS_DIR, contract_dir_hint)
905+
eosfactory_data(), CONTRACTS_DIR, contract_dir_hint)
888906

889907
trace = trace + contract_dir_ + "\n"
890908
if os.path.isdir(contract_dir_):
@@ -1102,11 +1120,11 @@ def current_config(contract_dir=None, dont_set_workspace=False):
11021120
map = {}
11031121

11041122
map["CONFIG_FILE"] = config_file()
1105-
if is_linked_package():
1123+
if not is_site_package():
11061124
try:
1107-
map["EOS_FACTORY_DIR"] = eosf_dir()
1125+
map["EOSFACTORY_DIR"] = eosf_dir()
11081126
except:
1109-
map["EOS_FACTORY_DIR"] = None
1127+
map["EOSFACTORY_DIR"] = None
11101128
try:
11111129
map[node_address_[0]] = http_server_address()
11121130
except:
@@ -1185,10 +1203,15 @@ def current_config(contract_dir=None, dont_set_workspace=False):
11851203
map[libs_[0]] = eoside_libs_dir()
11861204
except:
11871205
map[libs_[0]] = None
1206+
try:
1207+
map[eosfactory_data_[0]] = eosfactory_data()
1208+
except:
1209+
map[eosfactory_data_[0]] = None
11881210
try:
11891211
map[TEMPLATE_DIR[0]] = template_dir()
11901212
except:
11911213
map[TEMPLATE_DIR[0]] = None
1214+
11921215

11931216
map["EOSIO_VERSION"] = eosio_version()
11941217
map["EOSIO_CDT_VERSION"] = eosio_cpp_version()
@@ -1226,22 +1249,21 @@ def config():
12261249
Python version {}
12271250
'''.format(VERSION, EOSIO_VERSION, EOSIO_CDT_VERSION, PYTHON_VERSION)
12281251
)
1229-
import pdb; pdb.set_trace()
1230-
if is_linked_package():
1252+
is_not_linked = is_site_package()
1253+
if not is_not_linked:
12311254
print(
1232-
'''
1233-
EOSFactory package is installed as a link to the directory:
1255+
'''EOSFactory package is installed as a link to the directory:
12341256
'{}'
12351257
'''.format(os.path.join(eosf_dir(), EOSFACTORY_DIR))
12361258
)
1237-
elif APP_DATA_DIR_USER[0] == get_app_data_dir():
1259+
elif is_not_linked == 1:
12381260
print(
1239-
'''EOSFactory is installed as a PyPi package locally.
1261+
'''EOSFactory is installed as a site package locally.
12401262
'''
12411263
)
1242-
elif APP_DATA_DIR_SUDO[0] == get_app_data_dir():
1264+
elif is_not_linked == 2:
12431265
print(
1244-
'''EOSFactory is installed as a PyPi package globally.
1266+
'''EOSFactory is installed as a site package globally.
12451267
'''
12461268
)
12471269

setup.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ def readme():
2525
with open('README.rst') as f:
2626
return f.read()
2727

28-
def data_files_(directory):
28+
def data_files_item(directory):
29+
'''Given a directory, return tuple the directory, its files.
30+
'''
2931
paths = []
3032
for (path, _directories, filenames) in os.walk(directory):
3133
for filename in filenames:
@@ -41,10 +43,10 @@ def data_files_(directory):
4143
(os.path.join(config.EOSFACTORY_DIR, "config"),
4244
["config/distributed/config.json", "config/config.ini",
4345
"config/genesis.json"])]
44-
DATA_FILES.extend(data_files_('templates'))
45-
DATA_FILES.extend(data_files_('contracts'))
46-
DATA_FILES.extend(data_files_('includes'))
47-
DATA_FILES.extend(data_files_('libs'))
46+
DATA_FILES.extend(data_files_item('templates'))
47+
DATA_FILES.extend(data_files_item('contracts'))
48+
DATA_FILES.extend(data_files_item('includes'))
49+
DATA_FILES.extend(data_files_item('libs'))
4850

4951

5052
setuptools.setup(

0 commit comments

Comments
 (0)