Skip to content

Commit

Permalink
searching packages; error checking; handling args in reqts file
Browse files Browse the repository at this point in the history
  • Loading branch information
mobeets committed Jan 15, 2017
1 parent 5a6ffcd commit d3d9743
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 16 deletions.
17 changes: 15 additions & 2 deletions find_package.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import urllib2
from lxml import etree
from github import Github, GitRelease
import imp

try:
imp.find_module('github')
from github import Github
except ImportError:
# dummy constructor
Github = lambda: True

def find_github_repo(query, handle=None, release_tag=None):
"""
Expand All @@ -9,6 +17,9 @@ def find_github_repo(query, handle=None, release_tag=None):
"""
if handle is None:
handle = Github()
if not hasattr(handle, 'search_repositories'):
# PyGithub not installed
return
rs = handle.search_repositories(query + 'language:matlab')
try:
repo = rs[0]
Expand All @@ -18,7 +29,9 @@ def find_github_repo(query, handle=None, release_tag=None):
# find release with matching tag
rels = [t for t in repo.get_tags() if t.name == release_tag]
if rels:
return get_zipball_from_url(repo, rels[0].url)
return rels[0].zipball_url
else:
return
try:
# find latest release
return get_zipball_from_url(repo, repo.url + '/releases/latest')
Expand Down
110 changes: 96 additions & 14 deletions mpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import urllib2
import shutil
import tempfile
from subprocess import call
from StringIO import StringIO
from zipfile import ZipFile
from find_package import find_github_repo, find_matlabcentral_repo

HOMEDIR = os.path.expanduser("~")
MATLABDIR = os.path.join(HOMEDIR, 'Documents', 'MATLAB')
Expand Down Expand Up @@ -66,7 +68,34 @@ def unzip(url, outdir, allow_nesting):
copytree(tmppath, outdir)
return False

def main(url, name, outdir, force, allow_nesting):
def find_package(name, githubfirst=False, version=None):
if version is not None:
return find_github_repo(name, release_tag=version)
if githubfirst:
url = find_github_repo(name)
if url is None:
url = find_matlabcentral_repo(name)
else:
url = find_matlabcentral_repo(name)
if url is None:
url = find_github_repo(name)
return url

def main(url, name, outdir, force, allow_nesting, internaldir, searchonly, githubfirst, version):
if url is None:
url = find_package(name, githubfirst, version)
if url is None:
if version is None:
print "Could not find any package named '{0}' on Github or Matlab FileExchange.".format(name)
else:
print "Could not find any package named '{0}' on Github with version {1}.".format(name, version)
return
if searchonly:
print 'Package "{0}" found at "{1}". Not installing.'.format(name, url)
return
else:
print 'Package "{0}" found at "{1}".'.format(name, url)
return
url = url.strip()
name = name.strip()
if not os.path.exists(outdir):
Expand All @@ -81,26 +110,79 @@ def main(url, name, outdir, force, allow_nesting):
else:
print 'ERROR: Could not install "{0}"'.format(name)

def load_from_file(infile, outdir, force, allow_nesting):
def check_args_in_file(args, i):
msgs = []
if args.count('-s') or args.count('--searchonly'):
arg = '-s' if args.count('-s') else '--searchonly'
msg = 'Ignoring {0} option in line {1}'.format(arg, i)
msgs.append(msg)
if args.count('-f') or args.count('--force'):
arg = '-f' if args.count('-f') else '--force'
msg = 'Ignoring {0} option in line {1}'.format(arg, i)
msgs.append(msg)
if args.count('-i') or args.count('--installdir'):
arg = '-i' if args.count('-i') else '--installdir'
msg = 'Ignoring {0} option in line {1}'.format(arg, i)
msgs.append(msg)
return msgs

def load_from_file(infile, outdir, force, searchonly):
curdir = os.path.dirname(os.path.abspath(__file__))
mpmpath = os.path.join(curdir, 'mpm.py')
extra_args = ''
if outdir:
extra_args += ' --installdir ' + outdir
if force:
extra_args += ' --force'
if searchonly:
extra_args += ' --searchonly'
with open(infile) as f:
for line in f.readlines():
name, url = line.split()
main(url, name, outdir, force, allow_nesting)
for i, line in enumerate(f.readlines()):
args = line.split()
msgs = check_args_in_file(args, i+1)
for msg in msgs:
print 'WARNING: ' + msg
args += extra_args.split()
print ' '.join(args)
continue
call(['python', mpmpath] + args)

def check_args(args):
msgs = []
if not args.reqsfile and not args.name:
msg = "Must provide a package name"
msgs.append(msg)
if args.reqsfile and args.version:
msg = "Specifying a version is not allowed when loading from requirements file."
msgs.append(msg)
if args.reqsfile and args.internaldir:
msg = "Specifying an internaldir is illegal when loading from requirements file."
msgs.append(msg)
if args.reqsfile and args.githubfirst:
msg = "Specifying to search githubfirst is illegal when loading from requirements file."
msgs.append(msg)
return msgs

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("name", nargs="?", type=str, default=None, help="name of package")
parser.add_argument("url", nargs="?", type=str, default=None, help="url of package as zip")
parser.add_argument("-r", "--reqsfile", type=str, required=False, default=None, help="path to requirements file")
parser.add_argument("-o", "--installdir", type=str, default=MATLABDIR, help="installation directory")
parser.add_argument("-i", "--installdir", type=str, default=MATLABDIR, help="installation directory")
parser.add_argument("-f", "--force", action='store_true', default=False, help="overwrite if package already exists")
parser.add_argument("--allow-nesting", action='store_true', default=False, help="prevent trying to un-nest packages")
parser.add_argument("-n", "--internaldir", type=str, required=False, default=None, help="add internal directory to path instead")
parser.add_argument("-s", "--searchonly", action='store_true', default=False, help="search only (no install)")
parser.add_argument("-g", "--githubfirst", action='store_true', default=False, help="check github before matlab fileexchange")
parser.add_argument("-v", "--version", type=str, required=False, default=None, help="attempt t find particular release version on Github")
args = parser.parse_args()
if args.reqsfile:
load_from_file(args.reqsfile, args.installdir, args.force, args.allow_nesting)
else:
if not args.url and not args.name:
parser.print_help()
print "Must provide -r, or a name and -e"
sys.exit(1)
main(args.url, args.name, args.installdir, args.force, args.allow_nesting)
msgs = check_args(args)
if msgs:
parser.print_help()
for msg in msgs:
print msg
sys.exit(1)
if args.reqsfile:
load_from_file(args.reqsfile, args.installdir, args.force, args.searchonly)
else:
main(args.url, args.name, args.installdir, args.force, args.allow_nesting, args.internaldir, args.searchonly, args.githubfirst, args.version)

0 comments on commit d3d9743

Please sign in to comment.