#!/usr/bin/env python

import os
import sys
import json
import urllib2
import shlex
import subprocess

class Plz(object):
  def __init__(self, arg):
    self.host = 'http://slackware.cs.utah.edu/pub'
    self.api  = 'http://slackware-packages.herokuapp.com/packages'
    self.target_dir = '/boot/extra'

    self.arg      = arg
    self.command  = arg[1]
    self.name     = arg[2]
    self.version  = arg[3]
    self.pkg      = ''
    self.x64      = sys.maxsize > 2 ** 32

  def install(self):
    self._download()
    self._installpkg()


  # Private methods

  def _installpkg(self):
    if not self._installed():
      print "Installing {0}".format(self.pkg['file_name'])
      file = self._path([self.target_dir, self.pkg['package_name']])

      FNULL = open(os.devnull, 'w')
      subprocess.call(['upgradepkg', '--install-new', file], stdout=FNULL, stderr=subprocess.STDOUT)
    else:
      print "Using {0}".format(self.pkg['package_name'])

  def _download(self):
    api = self._path([self.api, self.name])
    pkg = self._get(api)

    versions = [v for v in pkg['versions'] if v['x64'] == self.x64]
    versions = [v for v in versions if v['version'] == self.version]

    if not versions:
      sys.exit("! No match for {0} ({1}) for your architecture.".format(self.name, self.version))

    self.pkg = versions[-1]

    local_pkg  = self._path([self.target_dir, self.pkg['package_name']])

    if not os.path.isfile(local_pkg):
      self._wget(self._path([self.host, self.pkg['path']]))

  def _installed(self):
    return self.pkg['file_name'] in self._installed_packages()

  def _installed_packages(self):
    packages_list = os.listdir('/var/log/packages/')
    files_list = [f[:-4] for f in os.listdir('/boot/extra')]
    return self._common(files_list, packages_list)

  def _wget(self, url):
    cmd = 'wget -qP {0} {1}'.format(self.target_dir, url)
    subprocess.call(shlex.split(cmd))

  def _get(self, url):
    response = urllib2.urlopen(url)
    return json.load(response)

  def _path(self, list):
    return ('/').join(list)

  def _common(self, a, b):
    b = set(b)
    return [aa for aa in a if aa in b]

  def _diff(self, a, b):
    b = set(b)
    return [aa for aa in a if aa not in b]

# CLI
args = sys.argv
if len(args) == 4:
  plz = Plz(args)
  plz.install()
else:
  exit("Usage: plz install NAME VERSION")

@@ -406,31 +406,8 @@
 %5B3%5D%0A
-    self.pkg      = ''%0A
     
@@ -443,16 +443,57 @@
  2 ** 32
+%0A    self.pkg      = self._package_data()
 %0A%0A  def 
@@ -511,29 +511,8 @@
 f):%0A
-    self._download()%0A
     
@@ -531,17 +531,16 @@
 lpkg()%0A%0A
-%0A
   # Priv
@@ -665,16 +665,40 @@
 _name'%5D)
+%0A%0A      self._download()
 %0A      f
@@ -824,35 +824,18 @@
 l(%5B'
-upgradepkg', '--
 install
--new
+pkg
 ', f
@@ -967,24 +967,224 @@
 load(self):%0A
+    local_pkg  = self._path(%5Bself.target_dir, self.pkg%5B'package_name'%5D%5D)%0A%0A    if not os.path.isfile(local_pkg):%0A      self._wget(self._path(%5Bself.host, self.pkg%5B'path'%5D%5D))%0A%0A  def _package_data(self):%0A
     api = se
@@ -1507,18 +1507,14 @@
     
-self.pkg =
+return
  ver
@@ -1528,181 +1528,8 @@
 1%5D%0A%0A
-    local_pkg  = self._path(%5Bself.target_dir, self.pkg%5B'package_name'%5D%5D)%0A%0A    if not os.path.isfile(local_pkg):%0A      self._wget(self._path(%5Bself.host, self.pkg%5B'path'%5D%5D))%0A%0A
   de

Fix installing with plz