Repurpose the installtion app for it's original intent which was let apps report on their dependencies and if they were met. This commit removes two more Python dependencies: GitPython and psutil.

This commit is contained in:
Roberto Rosario
2015-04-01 13:19:39 -04:00
parent 3f8fe90787
commit 57fd943c9d
8 changed files with 49 additions and 164 deletions

View File

@@ -56,6 +56,8 @@ Add new static media::
Remove unused dependencies::
$ pip uninstall South
$ pip uninstall GitPython
$ pip uninstall psutil
The upgrade procedure is now complete.

View File

@@ -0,0 +1 @@
from .classes import PropertyNamespace # NOQA

View File

@@ -7,7 +7,7 @@ from common.utils import encapsulate
from navigation.api import register_links, register_model_list_columns
from project_tools.api import register_tool
from .classes import Property, PropertyNamespace
from .classes import Property, PropertyNamespace, PIPNotFound, VirtualEnv
from .links import link_menu_link, link_namespace_details, link_namespace_list
@@ -41,3 +41,13 @@ class InstallationApp(apps.AppConfig):
register_links(PropertyNamespace, [link_namespace_details])
register_links(['installation:namespace_list', PropertyNamespace], [link_namespace_list], menu_name='secondary_menu')
register_tool(link_menu_link)
# Virtualenv
namespace = PropertyNamespace('venv', _('VirtualEnv'))
try:
venv = VirtualEnv()
except PIPNotFound:
namespace.add_property('pip', 'pip', _('pip not found.'), report=True)
else:
for item, version, result in venv.get_results():
namespace.add_property(item, '%s (%s)' % (item, version), result, report=True)

View File

@@ -7,9 +7,10 @@ import sh
try:
pip = sh.Command('pip')
PIP = True
except sh.CommandNotFound:
PIP = False
else:
PIP = True
class PIPNotFound(Exception):

View File

@@ -1,154 +0,0 @@
from __future__ import unicode_literals
import os
import sys
import platform
import uuid
import time
from git import Repo
import psutil
import sh
try:
from sh import lsb_release, uname
except ImportError:
LSB = False
else:
LSB = True
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.datastructures import SortedDict
from django.conf import settings
from common.utils import pretty_size
from mayan import __version__ as mayan_version
from ocr.settings import PDFTOTEXT_PATH, TESSERACT_PATH, UNPAPER_PATH
from .classes import PIPNotFound, PropertyNamespace, VirtualEnv
class Installation(object):
_properties = SortedDict()
uuid = models.CharField(max_length=48, blank=True, default=lambda: unicode(uuid.uuid4()))
def add_property(self, property_instance):
self._properties[property_instance.name] = property_instance
def get_properties(self):
self.set_properties()
return self._properties.values()
def os_properties(self):
namespace = PropertyNamespace('os', _('Operating system'))
if LSB:
namespace.add_property('is_lsb', _('LSB OS'), True, True)
namespace.add_property('distributor_id', _('Distributor ID'), lsb_release('-i', '-s'), True)
namespace.add_property('description', _('Description'), lsb_release('-d', '-s'), True)
namespace.add_property('release', _('Release'), lsb_release('-r', '-s'), True)
namespace.add_property('codename', _('Codename'), lsb_release('-c', '-s'), True)
namespace.add_property('sysinfo', _('System info'), uname('-a'), True)
else:
namespace.add_property('is_lsb', _('LSB OS'), False)
namespace.add_property('architecture', _('OS architecture'), platform.architecture(), report=True)
namespace.add_property('python_version', _('Python version'), platform.python_version(), report=True)
namespace.add_property('hostname', _('Hostname'), platform.node())
namespace.add_property('platform', _('Platform'), sys.platform, report=True)
namespace.add_property('machine', _('Machine'), platform.machine(), report=True)
namespace.add_property('processor', _('Processor'), platform.processor(), report=True)
namespace.add_property('cpus', _('Number of CPUs'), psutil.cpu_count(), report=True)
namespace.add_property('total_phymem', _('Total physical memory'), pretty_size(psutil.virtual_memory().total), report=True)
namespace.add_property('disk_partitions', _('Disk partitions'), '; '.join(['%s %s %s %s' % (partition.device, partition.mountpoint, partition.fstype, partition.opts) for partition in psutil.disk_partitions()]))
def binary_dependencies(self):
namespace = PropertyNamespace('bins', _('Binary dependencies'))
try:
tesseract = sh.Command(TESSERACT_PATH)
except sh.CommandNotFound:
namespace.add_property('tesseract', _('tesseract version'), _('not found'), report=True)
except Exception:
namespace.add_property('tesseract', _('tesseract version'), _('error getting version'), report=True)
else:
namespace.add_property('tesseract', _('tesseract version'), tesseract('-v').stderr, report=True)
try:
unpaper = sh.Command(UNPAPER_PATH)
except sh.CommandNotFound:
namespace.add_property('unpaper', _('unpaper version'), _('not found'), report=True)
except Exception:
namespace.add_property('unpaper', _('unpaper version'), _('error getting version'), report=True)
else:
namespace.add_property('unpaper', _('unpaper version'), unpaper('-V').stdout, report=True)
try:
pdftotext = sh.Command(PDFTOTEXT_PATH)
except sh.CommandNotFound:
namespace.add_property('pdftotext', _('pdftotext version'), _('not found'), report=True)
except Exception:
namespace.add_property('pdftotext', _('pdftotext version'), _('error getting version'), report=True)
else:
namespace.add_property('pdftotext', _('pdftotext version'), pdftotext('-v').stderr, report=True)
def mayan_properties(self):
namespace = PropertyNamespace('mayan', _('Mayan EDMS'))
namespace.add_property('uuid', _('UUID'), self.uuid, report=True)
namespace.add_property('mayan_version', _('Mayan EDMS version'), mayan_version, report=True)
def git_properties(self):
namespace = PropertyNamespace('git', _('Git repository'))
try:
repo = Repo(os.path.abspath(settings.BASE_DIR))
except:
namespace.add_property('is_git_repo', _('Running from a Git repository'), False)
else:
try:
repo.config_reader()
headcommit = repo.active_branch.commit
namespace.add_property('is_git_repo', _('Running from a Git repository'), True)
namespace.add_property('repo_remotes', _('Repository remotes'), ', '.join([unicode(remote) for remote in repo.remotes]), report=True)
namespace.add_property('repo_remotes_urls', _('Repository remotes URLs'), ', '.join([unicode(remote.url) for remote in repo.remotes]), report=True)
namespace.add_property('repo_head_reference', _('Branch'), repo.head.reference, report=True)
namespace.add_property('headcommit_hexsha', _('HEAD commit hex SHA'), headcommit.hexsha, report=True)
namespace.add_property('headcommit_author', _('HEAD commit author'), headcommit.author)
namespace.add_property('headcommit_authored_date', _('HEAD commit authored date'), time.asctime(time.gmtime(headcommit.authored_date)), report=True)
namespace.add_property('headcommit_committer', _('HEAD commit committer'), headcommit.committer)
namespace.add_property('headcommit_committed_date', _('HEAD commit committed date'), time.asctime(time.gmtime(headcommit.committed_date)), report=True)
namespace.add_property('headcommit_message', _('HEAD commit message'), headcommit.message, report=True)
except:
# Error getting git information, leave blank
pass
def virtualenv_properties(self):
namespace = PropertyNamespace('venv', _('VirtualEnv'))
try:
venv = VirtualEnv()
except PIPNotFound:
namespace.add_property('pip', 'pip', _('pip not found.'), report=True)
else:
for item, version, result in venv.get_results():
namespace.add_property(item, '%s (%s)' % (item, version), result, report=True)
def set_properties(self):
self._properties = SortedDict()
self.os_properties()
self.binary_dependencies()
self.mayan_properties()
self.git_properties()
self.virtualenv_properties()
def __getattr__(self, name):
self.set_properties()
try:
return self._properties[name].value
except KeyError:
raise AttributeError
class Meta:
verbose_name = verbose_name_plural = _('Installation details')

View File

@@ -8,14 +8,11 @@ from permissions.models import Permission
from .classes import PropertyNamespace
from .permissions import PERMISSION_INSTALLATION_DETAILS
from .models import Installation
def namespace_list(request):
Permission.objects.check_permissions(request.user, [PERMISSION_INSTALLATION_DETAILS])
Installation().get_properties()
return render_to_response('appearance/generic_list.html', {
'object_list': PropertyNamespace.get_all(),
'title': _('Installation property namespaces'),
@@ -26,8 +23,6 @@ def namespace_list(request):
def namespace_details(request, namespace_id):
Permission.objects.check_permissions(request.user, [PERMISSION_INSTALLATION_DETAILS])
Installation().get_properties()
namespace = PropertyNamespace.get(namespace_id)
object_list = namespace.get_properties()
title = _('Installation namespace details for: %s') % namespace.label

View File

@@ -2,6 +2,8 @@ from __future__ import unicode_literals
import logging
import sh
from django import apps
from django.utils.translation import ugettext_lazy as _
@@ -10,6 +12,7 @@ from common.utils import encapsulate
from documents.models import Document, DocumentVersion
from documents.signals import post_version_upload
from documents.widgets import document_link
from installation import PropertyNamespace
from main.api import register_maintenance_links
from navigation.api import register_links, register_model_list_columns
from navigation.links import link_spacer
@@ -24,6 +27,7 @@ from .links import (
)
from .models import DocumentVersionOCRError
from .permissions import PERMISSION_OCR_DOCUMENT
from .settings import PDFTOTEXT_PATH, TESSERACT_PATH, UNPAPER_PATH
from .tasks import task_do_ocr
logger = logging.getLogger(__name__)
@@ -79,3 +83,32 @@ class OCRApp(apps.AppConfig):
'name': _('Result'), 'attribute': 'result'
},
])
namespace = PropertyNamespace('ocr', _('OCR'))
try:
pdftotext = sh.Command(PDFTOTEXT_PATH)
except sh.CommandNotFound:
namespace.add_property('pdftotext', _('pdftotext version'), _('not found'), report=True)
except Exception:
namespace.add_property('pdftotext', _('pdftotext version'), _('error getting version'), report=True)
else:
namespace.add_property('pdftotext', _('pdftotext version'), pdftotext('-v').stderr, report=True)
try:
tesseract = sh.Command(TESSERACT_PATH)
except sh.CommandNotFound:
namespace.add_property('tesseract', _('tesseract version'), _('not found'), report=True)
except Exception:
namespace.add_property('tesseract', _('tesseract version'), _('error getting version'), report=True)
else:
namespace.add_property('tesseract', _('tesseract version'), tesseract('-v').stderr, report=True)
try:
unpaper = sh.Command(UNPAPER_PATH)
except sh.CommandNotFound:
namespace.add_property('unpaper', _('unpaper version'), _('not found'), report=True)
except Exception:
namespace.add_property('unpaper', _('unpaper version'), _('error getting version'), report=True)
else:
namespace.add_property('unpaper', _('unpaper version'), unpaper('-V').stdout, report=True)

View File

@@ -1,7 +1,5 @@
Django==1.7.7
GitPython==0.3.2.RC1
Pillow==2.7.0
celery==3.1.17
@@ -23,7 +21,6 @@ django-widget-tweaks==1.3
djangorestframework==2.4.4
pdfminer==20110227
psutil==2.2.1
pycountry==1.10
pytz==2015.2
python-dateutil==2.4.1