Source code for ccpn.util.Register

"""Module Documentation here

"""
#=========================================================================================
# Licence, Reference and Credits
#=========================================================================================
__copyright__ = "Copyright (C) CCPN project (http://www.ccpn.ac.uk) 2014 - 2020"
__credits__ = ("Ed Brooksbank, Luca Mureddu, Timothy J Ragan & Geerten W Vuister")
__licence__ = ("CCPN licence. See http://www.ccpn.ac.uk/v3-software/downloads/license")
__reference__ = ("Skinner, S.P., Fogh, R.H., Boucher, W., Ragan, T.J., Mureddu, L.G., & Vuister, G.W.",
                 "CcpNmr AnalysisAssign: a flexible platform for integrated NMR analysis",
                 "J.Biomol.Nmr (2016), 66, 111-124, http://doi.org/10.1007/s10858-016-0060-y")
#=========================================================================================
# Last code modification
#=========================================================================================
__modifiedBy__ = "$modifiedBy: Ed Brooksbank $"
__dateModified__ = "$dateModified: 2020-08-24 18:19:29 +0100 (Mon, August 24, 2020) $"
__version__ = "$Revision: 3.0.1 $"
#=========================================================================================
# Created
#=========================================================================================
__author__ = "$Author: CCPN $"
__date__ = "$Date: 2017-04-07 10:28:41 +0000 (Fri, April 07, 2017) $"
#=========================================================================================
# Start of code
#=========================================================================================

import ast
import hashlib
import os
import sys
import uuid
import datetime
from ccpn.util import Logging
from ccpn.util import Url
import json
import platform
import re, uuid

userAttributes = ('name', 'organisation', 'email')


def _registrationPath():
    return os.path.expanduser('~/.ccpn/register.txt')


def _registrationServerScript():
    from ccpn.framework.PathsAndUrls import ccpn2Url

    return ccpn2Url + '/cgi-bin/register/updateRegistrationV3'


def _checkRegistrationServerScript():
    from ccpn.framework.PathsAndUrls import ccpn2Url

    return ccpn2Url + '/cgi-bin/register/checkRegistrationV3'


[docs]def loadDict(): path = _registrationPath() registrationDict = {} try: if os.path.isfile(path): with open(path) as fp: data = fp.read() registrationDict = ast.literal_eval(data) except Exception as e: sys.stderr.write('Error loading registration: %s\n' % e) return registrationDict
[docs]def saveDict(registrationDict): path = _registrationPath() directory = os.path.dirname(path) try: if not os.path.exists(directory): os.makedirs(directory) with open(path, 'w') as fp: fp.write(str(registrationDict)) except Exception as e: sys.stderr.write('Error saving registration: %s\n' % e)
[docs]def getHashCode(registrationDict): m = hashlib.md5() for attrib in userAttributes: value = registrationDict.get(attrib, '') m.update(value.encode('utf-8')) return m.hexdigest()
[docs]def setHashCode(registrationDict): registrationDict['hashcode'] = getHashCode(registrationDict)
def _build(*chars): return ''.join([c for c in map(chr, chars)]) _bF = _build(98, 117, 105, 108, 100, 70, 111, 114) _hc = _build(104, 97, 115, 104, 99, 111, 100, 101) _cS = _build(99, 104, 101, 99, 107, 83, 117, 109) _nP = _build(110, 111, 110, 45, 112, 114, 111, 102, 105, 116) _r0 = _build(79, 75) _r1 = _build(78, 111, 116, 32, 70, 111, 117, 110, 100) _r2 = _build(68, 105, 102, 102, 101, 114, 101, 110, 116, 32, 76, 105, 99, 101, 110, 99, 101) _tc = _build(76, 73, 67, 69, 78, 83, 69, 46, 116, 120, 116) _otherAttributes = (_hc,) _serverResponses = (_r0, _r1, _r2) def _insertRegistration(registrationDict): val0, _, _, val1 = _getBuildID() return {_bF: val0, _cS: val1}
[docs]def isNewRegistration(registrationDict): for attrib in userAttributes: if not registrationDict.get(attrib): return True if _hc not in registrationDict: return True hashcode = getHashCode(registrationDict) return hashcode != registrationDict[_hc]
[docs]def updateServer(registrationDict, version='3'): url = _registrationServerScript() values = {} for attr in userAttributes + (_hc,): value = [] for c in registrationDict[attr]: value.append(c if 32 <= ord(c) < 128 else '_') values[attr] = ''.join(value) values['version'] = str(version) values['OSversion'] = platform.platform() # values['systemInfo'] = ';'.join(platform.uname()) _uname = platform.uname() values['systemInfo'] = ';'.join((_uname.system, _uname.machine, _uname.processor)) values['ID'] = ':'.join(re.findall('..', '%012x' % uuid.getnode())) values['compileVersion'] = _getCompileVersion() var0, var1, var2, _ = _getBuildID() values[_build(98, 117, 105, 108, 100, 70, 111, 114)] = var0 values[_build(108, 105, 99, 101, 110, 99, 101, 84, 121, 112, 101)] = var1 values[_build(108, 105, 99, 101, 110, 99, 101, 73, 68)] = var2 try: return Url.fetchUrl(url, values, timeout=2.0) except Exception as e: logger = Logging.getLogger() logger.warning('Could not update registration on server.')
def _getCompileVersion(): """Get the compileVersion information from the compileVersion file """ from ccpn.framework.PathsAndUrls import ccpnConfigPath COMPILEVERSIONFILE = 'compileVersion.txt' lfile = os.path.join(ccpnConfigPath, COMPILEVERSIONFILE) if not os.path.exists(lfile): return 'noCompileVersionInformation' with open(lfile, 'r', encoding='UTF-8') as fp: try: # return the first line of the compileVersion information file - should be created by ./buildDistribution return fp.readlines()[0] except: return 'badCompileVersionInformation' def _checkLicenceScript(): from ccpn.framework.PathsAndUrls import ccpn2Url return ccpn2Url + '/cgi-bin/register/checkLicence' def _getBuildID(): from ccpn.util import Data vals = [] for val in (_build(98, 117, 105, 108, 100, 70, 111, 114), _build(108, 105, 99, 101, 110, 99, 101, 84, 121, 112, 101), _build(108, 105, 99, 101, 110, 99, 101, 73, 68), _build(99, 104, 101, 99, 107, 83, 117, 109)): vals.append(getattr(Data, val, '')) return vals[0], vals[1], vals[2], vals[3]
[docs]def checkServer(registrationDict, version='3'): """Check the registration status on the server """ url = _checkRegistrationServerScript() values = {} for attr in userAttributes + (_hc,): value = [] if attr in registrationDict: for c in registrationDict[attr]: value.append(c if 32 <= ord(c) < 128 else '_') values[attr] = ''.join(value) values['version'] = str(version) values['OSversion'] = platform.platform() # values['systemInfo'] = ';'.join(platform.uname()) _uname = platform.uname() values['systemInfo'] = ';'.join((_uname.system, _uname.machine, _uname.processor)) values['ID'] = ':'.join(re.findall('..', '%012x' % uuid.getnode())) values['compileVersion'] = _getCompileVersion() var0, var1, var2, _ = _getBuildID() values[_build(98, 117, 105, 108, 100, 70, 111, 114)] = var0 values[_build(108, 105, 99, 101, 110, 99, 101, 84, 121, 112, 101)] = var1 values[_build(108, 105, 99, 101, 110, 99, 101, 73, 68)] = var2 try: found = Url.fetchUrl(url, values, timeout=2.0) if found.strip() in _serverResponses: return found.strip() == 'OK' except Exception as e: logger = Logging.getLogger() logger.warning('Could not check registration on server.', e) logger.exception(e)
[docs]def checkInternetConnection(): try: return Url.checkInternetConnection() except Exception as e: Logging.getLogger().exception(e) return False
def _fetchGraceFile(application): """ :return: grace filepath used as time stamp """ from ccpn.framework.PathsAndUrls import userPreferencesDirectory msg = 'If you are modifying this file it means you are a computer savvy! Please register and contribute to the project.' v = application.applicationVersion f = 'grace.json' path = os.path.join(userPreferencesDirectory, v + f) if not os.path.exists(path): with open(path, "w") as file: json.dump(msg, file) return path else: return path def _graceCounter(apath, d=5): """ :param path: a file which was created when the program (version) was started for the first time :param d: days of grace :return: days left """ today = datetime.datetime.today() modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(apath)) duration = today - modified_date return d - duration.days