Skip to content

Commit

Permalink
Many updates (#34)
Browse files Browse the repository at this point in the history
* Possible fix for #21

* Fix uninstalling not handling multiple versions

* Fix uninstalling not handling multiple versions

* possible fix for #23

* possible fix for #23

* possible fix for #24

* Fixes #25

* Fixes argument handling bug

* possible fix for #28

* Fixes #29

* possible fix for #31

* print error if lock file is corrupted

* small bug which produced unwanted print

* possible fix for #32

* possible fix for #24

* fix build

* run error [build-win] [docs]

* update for test output

* update for test output

* update for test output

* update for test output

* try with old version

* try with old version

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* try again with setup commands

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* added a debug print

* added debug print

* added debug print

* revert changes

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* init where needed

* possible fix for recording bug

* couple small details

* Bump certifi from 2022.9.24 to 2022.12.7 (#33)

Bumps [certifi](https://github.com/certifi/python-certifi) from 2022.9.24 to 2022.12.7.
- [Release notes](https://github.com/certifi/python-certifi/releases)
- [Commits](certifi/python-certifi@2022.09.24...2022.12.07)

---
updated-dependencies:
- dependency-name: certifi
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* couple small details

* couple small details

* turn recording flags back on

* bump version to make sure testing with latest dev branch

* Possible fix for #36

* possible fix for #35

* changed "unsafe" message

* Possible fix for #32

* possible fix for #38

* Added support for new aimodels.json structure

* fix #40

* Possible fix for #27

* removed lower()

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Rayrsn <25773909+Rayrsn@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 11, 2022
1 parent c4cb6ca commit cdf1d8f
Show file tree
Hide file tree
Showing 23 changed files with 300 additions and 193 deletions.
8 changes: 4 additions & 4 deletions aimm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
WEBSITE = "https://aimodels.org"
API_SERVER = "https://api.aimodels.org"
GITHUB_REPO = "visioninit/aimm"
VERSION = "0.4.0"
VERSION = "0.5.0"


def init():
for arg in sys.argv:
if arg in ('--version', '-v'):
if ('--version') in arg or ('-v') in arg:
print(f"AIMM Version: {VERSION}")
sys.exit(0)
if arg in ('--licenses'):
if ('--licenses') in arg:
if '--verbose' in sys.argv:
aimmApp.show_licenses(verbose=True)
sys.exit(0)
else:
aimmApp.show_licenses()
sys.exit(0)
if arg in ('--check-update'):
if ('--check-update') in arg:
aimmApp.check_for_updates(GITHUB_REPO, VERSION)
sys.exit(0)
if len(sys.argv) == 1 or sys.argv[1] == "--help": # if no arguments are passed or if the first argument is "--help", print help
Expand Down
29 changes: 17 additions & 12 deletions cli/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def add(name_version: str, mut_path: bool = typer.Option(False, "--unsafe-url"))
"""
# if model not installed, install it
# adds name as key and version as value to aimodels.json
name, version = base_funcs.extract_name_version(name_version)
name, version = base_funcs.lowercase_name_version(name_version)
if version is None:
version = base_funcs.get_last_version(name)

Expand All @@ -25,11 +25,11 @@ def add(name_version: str, mut_path: bool = typer.Option(False, "--unsafe-url"))

if base_funcs.should_install(name, version):
install.install(name_version,mut_path=mut_path)
else:
# add to aimodels-lock.json
save_path = os.path.join(aimmApp.main_dir, name, version)
base_funcs.update_ai_models_lock(name, version, save_path)
typer.echo(f"Already in aimodels.json: {name}:{version}")
if install.header == True:
sys.exit(1)
# add to aimodels-lock.json
save_path = os.path.join(aimmApp.main_dir, name, version)
base_funcs.update_ai_models_lock(name, version, save_path, "add")

# parse aimodels.json as a json
try:
Expand All @@ -42,13 +42,18 @@ def add(name_version: str, mut_path: bool = typer.Option(False, "--unsafe-url"))
# append name and version to aimodels.json if not already there
add_it = True
for package_name, package_version in aimodels.items():
if package_name.lower() == name.lower() and package_version == version:
typer.echo(f"Already in aimodels.json: {name}:{version}")
add_it = False
return
if package_name.lower() == name:
for v in package_version:
if v.lower() == version:
typer.echo(f"Already in aimodels.json: {name}:{version}")
add_it = False
return
if add_it:
aimodels.update({name: version})

# if the name is already there then add the version to the version list
if name in aimodels:
aimodels[name].append(version)
else:
aimodels[name] = [version]
try:
with open("aimodels.json", "w") as f:
# prettify json
Expand Down
13 changes: 8 additions & 5 deletions cli/aimmApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ def check_for_updates(repo, current_version):
r = requests.get(url, timeout=10)
if r.status_code == 200:
latest_version = r.json()["tag_name"]
latest_version_list = latest_version.split(".")
current_version_list = current_version.split(".")
if (latest_version_list[0] > current_version_list[0]) or (latest_version_list[1] > current_version_list[1]) or (latest_version_list[2] > current_version_list[2]):
if latest_version != current_version:
typer.echo(f"Update available: {current_version}{latest_version}")
typer.echo(" Latest version: https://github.com/visioninit/aimm/releases/latest")
typer.echo(f" Latest version: https://github.com/{repo}/releases/latest")
else:
typer.echo("No updates available.")
else:
Expand All @@ -96,8 +94,13 @@ def show_licenses(verbose: bool = False):
# check if running as a pyinstaller executable
if getattr(sys, 'frozen', False):
path = sys._MEIPASS
else:
# check if running as a python script
elif __file__:
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# is running as pynsist
else:
path = sys.executable
path = os.path.dirname(path)
if sys.platform == "win32":
temp = os.getenv("TEMP")
elif sys.platform in ("linux", "darwin"):
Expand Down
156 changes: 93 additions & 63 deletions cli/base_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ def extract_name_version(name_version):
sys.exit(1)
return name, version

def lowercase_name_version(name_version):
name, version = extract_name_version(name_version)
if version is None:
return name.lower(), version
else:
return name.lower(), version.lower()

def get_last_version(name):
url = f"{aimm.API_SERVER}/api/models?filters[$and][0][model_name][$eqi]={name}&publicationState=live&populate=deep"
# parse api as a json
Expand Down Expand Up @@ -122,85 +129,109 @@ def get_domain_from_url(download_url) -> str:


def get_model_path(name_version):
name, version = extract_name_version(name_version)
name, version = lowercase_name_version(name_version)
if version is None:
version = get_last_version(name)
for package in aimmApp.installed["packages"]:
if package["name"].lower() == name.lower() and package["version"] == version:
if package["name"].lower() == name and package["version"] == version:
return package["paths"]
else:
typer.echo(f"Error: {name}:{version} not found")
return None


# a function that updates aimodels-lock.json
def update_ai_models_lock(name, version, path):
# get current working directory
cwd = os.getcwd()
aimodels_lock_file = os.path.join(cwd, "aimodels-lock.json")
# get a list of files in path
files = os.listdir(path)
# path should use forward slashes instead of backslashes
path = path.replace("\\", "/")
# check if aimodels-lock.json exists
if not os.path.exists(aimodels_lock_file):
# create aimodels-lock.json
aimodels_lock = {
"packages": {
f"{name}:{version}": {
"path": path,
"files": files
},
},
"credentials": {}
}
# write aimodels-lock.json
with open("aimodels-lock.json", "w") as f:
json.dump(aimodels_lock, f, indent=4)
else:
def update_ai_models_lock(name, version, path, operation):
# make name and version case insensitive
name = name.lower()
version = version.lower()

if operation == "remove":
# read aimodels-lock.json
with open("aimodels-lock.json", "r") as f:
aimodels_lock = json.load(f)
# check if name and version already exist if not append
if f"{name}:{version}" not in aimodels_lock["packages"]:
try:
aimodels_lock["packages"][f"{name}:{version}"] = {
"path": path,
"files": files
}
except:
typer.echo("Error: aimodels-lock.json is corrupted")
sys.exit(1)
try:
with open("aimodels-lock.json", "r") as f:
aimodels_lock = json.load(f)
except json.JSONDecodeError:
typer.echo("Error: aimodels-lock.json is corrupted")
sys.exit(1)
# check if name and version already exist if not
if f"{name}:{version}" in aimodels_lock["packages"]:
# remove package from aimodels-lock.json
del aimodels_lock["packages"][f"{name}:{version}"]
# write aimodels-lock.json
try:
with open("aimodels-lock.json", "w") as f:
json.dump(aimodels_lock, f, indent=4)
except Exception as e:
typer.echo(f"Error: {e}")
sys.exit(1)

with open("aimodels-lock.json", "w") as f:
json.dump(aimodels_lock, f, indent=4)
elif operation == "add":
# get current working directory
cwd = os.getcwd()
aimodels_lock_file = os.path.join(cwd, "aimodels-lock.json")
# get a list of files in path
files = os.listdir(path)
# path should use forward slashes instead of backslashes
path = path.replace("\\", "/")
# check if aimodels-lock.json exists
if not os.path.exists(aimodels_lock_file):
# create aimodels-lock.json
aimodels_lock = {
"packages": {
f"{name}:{version}": {
"path": path,
"files": files
},
},
"credentials": {}
}
# write aimodels-lock.json
with open("aimodels-lock.json", "w") as f:
json.dump(aimodels_lock, f, indent=4)
else:
# read aimodels-lock.json
try:
aimodels_lock["packages"][f"{name}:{version}"] = {
"path": path,
"files": files
}
except:
with open("aimodels-lock.json", "r") as f:
aimodels_lock = json.load(f)
except json.JSONDecodeError:
typer.echo("Error: aimodels-lock.json is corrupted")
sys.exit(1)
# add to .gitignore
gitignore_file = os.path.join(cwd, ".gitignore")
gitignore_text = "# For local aimodels packages\naimodels-lock.json\n"
if not os.path.exists(gitignore_file):
with open(".gitignore", "w") as f:
f.write(gitignore_text)
else:
with open(".gitignore", "r") as f:
gitignore = f.read()
if "aimodels-lock.json" not in gitignore:
# append to end
with open(".gitignore", "a") as f:
# check if name and version already exist if not append
if f"{name}:{version}" not in aimodels_lock["packages"]:
try:
aimodels_lock["packages"][f"{name}:{version}"] = {
"path": path,
"files": files
}
except:
typer.echo("Error: aimodels-lock.json is corrupted")
sys.exit(1)
# write aimodels-lock.json
try:
with open("aimodels-lock.json", "w") as f:
json.dump(aimodels_lock, f, indent=4)
except Exception as e:
typer.echo(f"Error: {e}")
sys.exit(1)

else:
try:
aimodels_lock["packages"][f"{name}:{version}"] = {
"path": path,
"files": files
}
except:
typer.echo("Error: aimodels-lock.json is corrupted")
sys.exit(1)
# add to .gitignore
gitignore_file = os.path.join(cwd, ".gitignore")
gitignore_text = "# For local aimodels packages\naimodels-lock.json\n"
if not os.path.exists(gitignore_file):
with open(".gitignore", "w") as f:
f.write(gitignore_text)
else:
with open(".gitignore", "r") as f:
gitignore = f.read()
if "aimodels-lock.json" not in gitignore:
# append to end
with open(".gitignore", "a") as f:
f.write(gitignore_text)

def check_for_creds(download_url):
domain = get_domain_from_url(download_url)
Expand All @@ -227,4 +258,3 @@ def get_creds(download_url):
aimodels_lock = json.load(f)
# return username and password
return aimodels_lock["credentials"][domain]["username"], aimodels_lock["credentials"][domain]["password"]

1 change: 1 addition & 0 deletions cli/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def info(name: str, fetch : bool = typer.Option(False, "--fetch", help="Fetch th
"""
Show information about a model.
"""
name = name.lower()
if not base_funcs.is_valid(name,None):
typer.echo(f"Error: {name} not valid")
sys.exit(1)
Expand Down
Loading

0 comments on commit cdf1d8f

Please sign in to comment.