Skip to content

Commit

Permalink
Make changes to test scripts and add more search paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshit Bansal committed Jul 8, 2019
1 parent 455aad4 commit a19f4ad
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
16 changes: 13 additions & 3 deletions recipes/lib/edit_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,18 @@ def edit_json(script):
Note: Name of script is the dataset name.
"""
json_file = script.replace('-', '_') + '.json'
search_paths = ['scripts', './', '../scripts', '../']
file_path = None
for search_path in search_paths:
if os.path.exists(os.path.join(search_path, json_file)):
file_path = search_path
break
if file_path is None:
print("Script not found.")
return
try:
contents = json.load(
open(os.path.join('scripts', json_file), 'r'))
open(os.path.join(file_path, json_file), 'r'))
except (IOError, OSError):
print("Script not found.")
return
Expand All @@ -180,14 +189,15 @@ def edit_json(script):

file_name = contents['name'] + ".json"
file_name = file_name.replace('-', '_')
with open(os.path.join('scripts', file_name), 'w') as output_file:

with open(os.path.join(file_path, file_name), 'w') as output_file:
json.dump(contents,
output_file,
sort_keys=True,
indent=4,
separators=(',', ': '))
output_file.write('\n')
print("\nScript written to " + os.path.join('scripts', file_name))
print("\nScript written to " + os.path.join(file_path, file_name))
output_file.close()


Expand Down
2 changes: 1 addition & 1 deletion recipes/lib/get_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

script_list = []

script_version_list = get_script_version()
script_version_list = get_script_version(['scripts', './', '../scripts', '../'])
for script in script_version_list:
script = script.split(',')[0]
if script.endswith('.json'):
Expand Down
44 changes: 22 additions & 22 deletions recipes/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,30 @@ def read_py(script_name, search_path):
return None


def get_script_version():
def get_script_version(search_paths=['scripts']):
"""This function gets the version number of the scripts and returns them in array form."""
search_path = 'scripts'
loaded_files = []
scripts = []
if exists(search_path):
data_packages = [file_i for file_i in os.listdir(search_path) if file_i.endswith(".json")]
for script in data_packages:
script_name = '.'.join(script.split('.')[:-1])
script_version = read_json(os.path.join(search_path, script))
if script_name not in loaded_files and script_version:
scripts.append(','.join([script, str(script_version)]))
loaded_files.append(script_name)

files = [file for file in os.listdir(search_path)
if file[-3:] == ".py" and file[0] != "_" and
('#retriever' in
' '.join(open_fr(join(search_path, file), encoding=ENCODING).readlines()[:2]).lower())
]
for script in files:
script_name = '.'.join(script.split('.')[:-1])
script_version = read_py(script_name, search_path)
if script_name not in loaded_files and script_version:
scripts.append(','.join([script, str(script_version)]))
loaded_files.append(script_name)
for search_path in search_paths:
if exists(search_path):
data_packages = [file_i for file_i in os.listdir(search_path) if file_i.endswith(".json")]
for script in data_packages:
script_name = '.'.join(script.split('.')[:-1])
script_version = read_json(os.path.join(search_path, script))
if script_name not in loaded_files and script_version:
scripts.append(','.join([script, str(script_version)]))
loaded_files.append(script_name)

files = [file for file in os.listdir(search_path)
if file[-3:] == ".py" and file[0] != "_" and
('#retriever' in
' '.join(open_fr(join(search_path, file), encoding=ENCODING).readlines()[:2]).lower())
]
for script in files:
script_name = '.'.join(script.split('.')[:-1])
script_version = read_py(script_name, search_path)
if script_name not in loaded_files and script_version:
scripts.append(','.join([script, str(script_version)]))
loaded_files.append(script_name)
scripts = sorted(scripts, key=str.lower)
return scripts
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

current_platform = platform.system().lower()

if os.path.exists(".git/hooks"): # check if we are in git repo
os.system("cp hooks/pre-commit .git/hooks/pre-commit")
os.system("chmod +x .git/hooks/pre-commit")

def read(*names, **kwargs):
return open(
os.path.join(os.path.dirname(__file__), *names),
Expand Down
2 changes: 1 addition & 1 deletion test/test_modified_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from distutils.version import LooseVersion

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from src.utils import get_script_version
from recipes.lib.utils import get_script_version


file_location = os.path.dirname(os.path.realpath(__file__))
Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import os

from src.utils import get_script_version
from recipes.lib.utils import get_script_version


def write_version_file(scripts):
Expand Down

0 comments on commit a19f4ad

Please sign in to comment.