Skip to content

Commit

Permalink
Hardware Profile: Now works python2 and python3
Browse files Browse the repository at this point in the history
New packages required:
    python-uritools
    python3-uritools
    python-mailer
    python3-mailer
    python-simplejson
    python3-simplejson
    python3-mysqldb # required for bindings too
    python3-future  # required for bindings too
  • Loading branch information
Bill Meek committed Nov 15, 2019
1 parent 871accb commit 1df343e
Show file tree
Hide file tree
Showing 25 changed files with 484 additions and 351 deletions.
9 changes: 6 additions & 3 deletions mythtv/configure
Expand Up @@ -6991,11 +6991,14 @@ fi

# Check for python dependencies
if enabled bindings_python; then
is_python3 && python=python2
check_python || disable_bindings_python "Python 2.6"
check_python || disable_bindings_python "Python 2.6 or greater"
check_py_lib MySQLdb || disable_bindings_python "MySQLdb"
check_py_lib lxml || disable_bindings_python "lxml"
check_py_lib urlgrabber || disable_bindings_python "urlgrabber"
check_py_lib requests || disable_bindings_python "requests"
check_py_lib uritools || disable_bindings_python "uritools"
check_py_lib email || disable_bindings_python "email"
check_py_lib simplejson || disable_bindings_python "simplejson"
check_py_lib future || disable_bindings_python "future"
fi

# Check for perl dependencies
Expand Down
57 changes: 30 additions & 27 deletions mythtv/programs/scripts/hardwareprofile/MultipartPostHandler.py
Expand Up @@ -3,18 +3,18 @@

####
# 02/2006 Will Holcomb <wholcomb@gmail.com>
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# 7/26/07 Slightly modified by Brian Schneider
# 7/26/07 Slightly modified by Brian Schneider
# in order to support unicode files ( multipart_encode function )
"""
Usage:
Expand All @@ -40,60 +40,63 @@
The main function of this file is a sample which downloads a page and
then uploads it to the W3C validator.
"""

import urllib
import urllib2
import mimetools, mimetypes
from __future__ import print_function

from future import standard_library
standard_library.install_aliases()
from builtins import object
import urllib.request, urllib.error, urllib.parse
from email.generator import _make_boundary
import mimetypes
import os, stat
from cStringIO import StringIO
import sys
from io import StringIO

class Callable:
class Callable(object):
def __init__(self, anycallable):
self.__call__ = anycallable

# Controls how sequences are uncoded. If true, elements may be given multiple values by
# assigning a sequence.
doseq = 1

class MultipartPostHandler(urllib2.BaseHandler):
handler_order = urllib2.HTTPHandler.handler_order - 10 # needs to run first
class MultipartPostHandler(urllib.request.BaseHandler):
handler_order = urllib.request.HTTPHandler.handler_order - 10 # needs to run first

def http_request(self, request):
data = request.get_data()
data = request.data
if data is not None and type(data) != str:
v_files = []
v_vars = []
try:
for(key, value) in data.items():
if type(value) == file:
v_files.append((key, value))
else:
v_vars.append((key, value))
for(key, value) in list(data.items()):
# 'file' type doesn't exist in python3 and we didn't use it. Removed if:
v_vars.append((key, value))
except TypeError:
systype, value, traceback = sys.exc_info()
raise TypeError, "not a valid non-string sequence or mapping object", traceback
raise TypeError("not a valid non-string sequence or mapping object", traceback)

if len(v_files) == 0:
data = urllib.urlencode(v_vars, doseq)
data = urllib.parse.urlencode(v_vars, doseq)
else:
boundary, data = self.multipart_encode(v_vars, v_files)

contenttype = 'multipart/form-data; boundary=%s' % boundary
if(request.has_header('Content-Type')
and request.get_header('Content-Type').find('multipart/form-data') != 0):
print "Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data')
print("Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data'))
request.add_unredirected_header('Content-Type', contenttype)

request.add_data(data)
request.data = bytearray(data, 'latin1')

return request

def multipart_encode(vars, files, boundary = None, buf = None):
def multipart_encode(self, these_vars, files, boundary = None, buf = None):
if boundary is None:
boundary = mimetools.choose_boundary()
boundary = _make_boundary(os.getuid + os.getpid())
if buf is None:
buf = StringIO()
for(key, value) in vars:
for(key, value) in these_vars:
buf.write('--%s\r\n' % boundary)
buf.write('Content-Disposition: form-data; name="%s"' % key)
buf.write('\r\n\r\n' + value + '\r\n')
Expand All @@ -118,15 +121,15 @@ def main():
import tempfile, sys

validatorURL = "http://validator.w3.org/check"
opener = urllib2.build_opener(MultipartPostHandler)
opener = urllib.request.build_opener(MultipartPostHandler)

def validateFile(url):
temp = tempfile.mkstemp(suffix=".html")
os.write(temp[0], opener.open(url).read())
params = { "ss" : "0", # show source
"doctype" : "Inline",
"uploaded_file" : open(temp[1], "rb") }
print opener.open(validatorURL, params).read()
print(opener.open(validatorURL, params).read())
os.remove(temp[1])

if len(sys.argv[1:]) > 0:
Expand Down
13 changes: 9 additions & 4 deletions mythtv/programs/scripts/hardwareprofile/config.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

import os,re
import commands
import os, re, sys
from subprocess import Popen, PIPE
import os_detect

SMOON_URL = "http://smolt.mythtv.org/"
Expand All @@ -10,7 +10,7 @@
from MythTV import MythDB
confdir = os.path.join(MythDB().dbconfig.confdir, 'HardwareProfile')
try:
os.mkdir(confdir, 0700)
os.mkdir(confdir, 0o700)
except OSError:
pass

Expand All @@ -27,8 +27,13 @@

FS_T_FILTER=False
FS_M_FILTER=True
FS_MOUNTS=commands.getoutput('rpm -ql filesystem').split('\n')

try:
p = Popen(['rpm', '-ql', 'filesystem'], stdout=PIPE)
p.wait()
FS_MOUNTS = p.stdout.read().strip()
except OSError:
FS_MOUNTS = ''

#This will attempt to find the distro.
#Uncomment any of the OS lines below to hardcode.
Expand Down
36 changes: 22 additions & 14 deletions mythtv/programs/scripts/hardwareprofile/deleteProfile.py 100644 → 100755
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python2

# smolt - Fedora hardware profiler
#
Expand All @@ -19,10 +19,11 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

from __future__ import print_function
from builtins import str
import sys
import urlgrabber.grabber
import requests
from optparse import OptionParser
from urlparse import urljoin


def main():
Expand All @@ -36,7 +37,7 @@ def main():
def serverMessage(page):
for line in page.split("\n"):
if 'ServerMessage:' in line:
error(_('Server Message: "%s"') % line.split('ServerMessage: ')[1])
error(_('Server Message: "%s"' % line.split('ServerMessage: ')[1]))
if 'Critical' in line:
sys.exit(3)

Expand Down Expand Up @@ -78,37 +79,44 @@ def serverMessage(page):
smolt.DEBUG = opts.DEBUG
smolt.hw_uuid_file = opts.uuidFile

grabber = urlgrabber.grabber.URLGrabber(user_agent=opts.user_agent, timeout=opts.timeout)

uuid = smolt.read_uuid()
delHostString = 'uuid=%s' % uuid

session = requests.Session()
session.headers.update({'USER-AGENT': opts.user_agent})
session.headers.update({'Content-type': 'application/x-www-form-urlencoded'})
session.headers.update({'Content-length': '%i' % len(delHostString)})

# Try retrieving current pub_uuid (from cache or remotely if necessary)
pub_uuid = None
try:
pub_uuid = smolt.read_pub_uuid(create_default_uuiddb(), uuid, silent=True)
except PubUUIDError:
pass

exceptions = (requests.exceptions.HTTPError,
requests.exceptions.URLRequired,
requests.exceptions.Timeout,
requests.exceptions.ConnectionError,
requests.exceptions.InvalidURL)

try:
o=grabber.urlopen(urljoin(opts.smoonURL + '/', '/client/delete'), data=delHostString, http_headers=(
('Content-length', '%i' % len(delHostString)),
('Content-type', 'application/x-www-form-urlencoded')))
except urlgrabber.grabber.URLGrabError, e:
o = session.post(opts.smoonURL + 'client/delete', data=delHostString,
timeout=opts.timeout)
except exceptions as e:
sys.stderr.write(_('Error contacting Server:'))
sys.stderr.write(str(e))
sys.stderr.write('\n')
sys.exit(1)
else:
serverMessage(o.read())
o.close()
serverMessage(o.text)
session.close()

if pub_uuid is None:
profile_url = urljoin(opts.smoonURL + '/', '/client/show?%s' % delHostString)
profile_url = opts.smoonURL + 'client/show?%s' % delHostString
else:
profile_url = get_profile_link(opts.smoonURL, pub_uuid)
print _('Profile removed, please verify at'), profile_url
print((_('Profile removed, please verify at'), profile_url))


if __name__ == '__main__':
Expand Down
7 changes: 4 additions & 3 deletions mythtv/programs/scripts/hardwareprofile/devicelist.py
Expand Up @@ -19,6 +19,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

from builtins import object
import os
from hwdata import DeviceMap

Expand Down Expand Up @@ -92,8 +93,8 @@
'subsystem_vendor',
'subsystem_device']

def cat(file):
fd = open(file, 'r')
def cat(file_name):
fd = open(file_name, 'r')
results = fd.readlines()
fd.close()
return results
Expand Down Expand Up @@ -160,7 +161,7 @@ def process(self):
desc = cat(PATH + 'product')[0].strip().decode('ASCII', errors='ignore')
except:
#The fedora python pkg is broken and doesn't like decode with options, so this is a fallback
desc = cat(PATH + 'product')[0].strip().decode()
desc = cat(PATH + 'product')[0].strip()
self.description = usb.device(self.vendorid,
self.deviceid,
alt=desc)
Expand Down
2 changes: 1 addition & 1 deletion mythtv/programs/scripts/hardwareprofile/distros/all.py
Expand Up @@ -17,7 +17,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

#from gentoo.main import create_gentoo
from mythtv_data.main import create_mythtv
from distros.mythtv_data.main import create_mythtv

def get():
#res = [create_gentoo(),create_mythtv()]
Expand Down
3 changes: 2 additions & 1 deletion mythtv/programs/scripts/hardwareprofile/distros/distro.py
Expand Up @@ -16,7 +16,8 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

class Distro:
from builtins import object
class Distro(object):
def _raise_abstract(self):
raise Exception('Function must be overwritten.')

Expand Down

0 comments on commit 1df343e

Please sign in to comment.