Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix for pyinstaller dummy site.py #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions xicam/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ def __init__(self):
# Places to look for plugins
self.plugindirs = [user_plugin_dir,
site_plugin_dir] \
+ list(xicam.__path__)
+ list(xicam.__path__)
if getattr(sys,'frozen',False):
self.plugindirs.append(os.path.join(os.path.dirname(sys.argv[0]),'_plugins'))

self.setPluginPlaces(self.plugindirs)
msg.logMessage('plugindirectories:', *self.plugindirs)

Expand Down Expand Up @@ -150,7 +153,10 @@ def collectPlugins(self, paths=None):

Overloaded to add callback.
"""
self.setPluginPlaces(self.plugindirs + (paths or []))
dirs = self.plugindirs
if venvs.current_environment:
dirs.append(venvs.current_environment)
self.setPluginPlaces(dirs)

self.locatePlugins()

Expand Down Expand Up @@ -205,7 +211,6 @@ def showLoading(self, plugininfo: PluginInfo):
msg.logMessage(f'Loading {name} from {plugininfo.path}')

def venvChanged(self):
self.setPluginPlaces([venvs.current_environment])
self.collectPlugins()

def __getitem__(self, item: str):
Expand Down Expand Up @@ -242,6 +247,9 @@ def loadPlugins(self, callback=None):

initial_len = len(self.loadqueue)

if not len(self.loadqueue):
return []

for candidate_infofile, candidate_filepath, plugin_info in iter(self.loadqueue.popleft, (None, None, None)):
# if a callback exists, call it before attempting to load
# the plugin so that a message can be displayed to the
Expand Down
106 changes: 81 additions & 25 deletions xicam/plugins/cammart.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,64 @@
import json
from urllib import parse
import os, sys

import pkg_resources
import requests
import yaml
from appdirs import user_config_dir, site_config_dir, user_cache_dir
import platform
import subprocess
from xicam.core import msg
from importlib import reload
import pip

from . import manager
from . import venvs


def pipmain(args):
subprocess.call([sys.executable, "-m", "pip", *args])
old_env = os.environ.copy()
os.environ["PYTHONPATH"] = os.path.join(
venvs.current_environment, "Lib/site-packages"
)
os.environ["PYTHONBASE"] = venvs.current_environment
old_prefix = sys.prefix
sys.prefix = venvs.current_environment

r = 1 # (error code)
try:
# Install the bundled software

try:
r = pip.main(args)
except AttributeError:
from pip._internal import main

# try:
# from pip._internal import main as pipmain
# except ModuleNotFoundError:
# from pip import main as pipmain
r = main(args)
except Exception as ex:
msg.logError(ex)

from . import manager
from . import venvs
finally:
os.environ = old_env
sys.prefix = old_prefix
return r

# subprocess.call([os.path.join(venvs.current_environment, 'Scripts/pip.exe'), *args], env=env)
# return 0 # Above subprocess typicall returns error code even though it succeeds


op_sys = platform.system()
if op_sys == 'Darwin': # User config dir incompatible with venv on darwin (space in path name conflicts)
user_package_registry = os.path.join(user_cache_dir(appname='xicam'),'packages.yml')
if (
op_sys == "Darwin"
): # User config dir incompatible with venv on darwin (space in path name conflicts)
user_package_registry = os.path.join(
user_cache_dir(appname="xicam"), "packages.yml"
)
else:
user_package_registry = os.path.join(user_config_dir(appname='xicam'),'packages.yml')
site_package_registry = os.path.join(site_config_dir(appname='xicam'),'packages.yml')
user_package_registry = os.path.join(
user_config_dir(appname="xicam"), "packages.yml"
)
site_package_registry = os.path.join(site_config_dir(appname="xicam"), "packages.yml")


def install(name: str):
"""
Expand All @@ -45,21 +76,42 @@ def install(name: str):
# TODO: check if package is in repo

# Get install plugin package information from cam-mart repository
o = requests.get(f'http://cam.lbl.gov:5000/pluginpackages?where={{"name":"{name}"}}')
o = requests.get(
f'http://cam.lbl.gov:5000/pluginpackages?where={{"name":"{name}"}}'
)

# Get the uri from the plugin package information
uri = parse.urlparse(json.loads(o.content)['_items'][0]["installuri"])
uri = parse.urlparse(json.loads(o.content)["_items"][0]["installuri"])

failure = True

print('Installing to:', venvs.current_environment)
# import setuptools

# print("Updating setuptools...")
# print("version:", setuptools.__version__)
# pipmain(["install", "--upgrade", "--ignore-installed", "setuptools"])
# setuptools = reload(setuptools)

print("Installing to:", venvs.current_environment)

# Install from the uri
if uri.scheme == 'pipgit': # Clones a git repo and installs with pip
failure = pipmain(['install', f'--target={venvs.current_environment}', 'git+https://' + ''.join(uri[1:])])
elif uri.scheme == 'pip':
failure = pipmain(['install', f'--target={venvs.current_environment}', ''.join(uri[1:])])
elif uri.scheme == 'conda':
if uri.scheme == "pipgit": # Clones a git repo and installs with pip
failure = pipmain(
[
"install",
# f'--target={os.path.join(venvs.current_environment,"Lib/site-packages")}',
"git+https://" + "".join(uri[1:]),
]
)
elif uri.scheme == "pip":
failure = pipmain(
[
"install",
# f'--target={os.path.join(venvs.current_environment,"Lib/site-packages")}',
"".join(uri[1:]),
]
)
elif uri.scheme == "conda":
raise NotImplementedError

if not failure:
Expand All @@ -69,12 +121,15 @@ def install(name: str):


def uninstall(name: str):
# Note: pkg_resources keeps a "working_set" that won't normally be updated when venv changes...
# An update may need to be forcefully triggered in the future
pkg_resources._initialize_master_working_set()
failure = True
if name in pkg_registry:
scheme = pkg_registry[name]
if scheme in ['pipgit', 'pip']:
failure = pipmain(['uninstall', '-y', name])
elif scheme == 'conda':
if scheme in ["pipgit", "pip"]:
failure = pipmain(["uninstall", "-y", name])
elif scheme == "conda":
raise NotImplementedError
else:
# TODO: Handle if name is not in the registry
Expand All @@ -85,6 +140,7 @@ def uninstall(name: str):

return not failure


class pkg_registry(collections.MutableMapping):
def __init__(self):
self._store = dict()
Expand Down Expand Up @@ -114,13 +170,13 @@ def __keytransform__(self, key):

def load(self):
try:
with open(user_package_registry, 'r') as f:
with open(user_package_registry, "r") as f:
self._store = yaml.load(f.read())
except FileNotFoundError:
pass

def save(self):
with open(user_package_registry, 'w') as f:
with open(user_package_registry, "w") as f:
f.write(yaml.dump(self._store))


Expand Down
Loading