diff --git a/.travis.yml b/.travis.yml index 535b557..0e494bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,13 @@ language: python python: + - "3.8" - "3.7" - "3.6" - - "3.5" - - "2.7" install: - python setup.py install + - pip install httpretty script: - - python setup.py test + - python pynux/tests/test_utils.py #before_script: # - git config --global user.name "Teracy" # Configure your git user.name here diff --git a/pynux/nx_recompute_quota.py b/pynux/nx_recompute_quota.py new file mode 100755 index 0000000..afc314c --- /dev/null +++ b/pynux/nx_recompute_quota.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +from __future__ import print_function +import sys +import argparse +import os +import importlib +import itertools +from pynux import utils +from pynux.utils import utf8_arg +#from icecream import ic + + +def main(argv=None): + parser = argparse.ArgumentParser(description='recompute quota statistics on specific path') + parser.add_argument( + 'path', nargs=1, help='nuxeo document path', type=utf8_arg) + utils.get_common_options(parser) + if argv is None: + argv = parser.parse_args() + + nx = utils.Nuxeo(rcfile=argv.rcfile, loglevel=argv.loglevel.upper()) + + nuxeo = nx.nuxeo_client + + # x = nuxeo.operations.operations.keys() + # x.sort() + # ic(x) + + if nuxeo.operations.operations['Quotas.RecomputeStatistics']: + operation = nuxeo.operations.new('Quotas.RecomputeStatistics') + operation.params = {'path': argv.path[0]} + operation.execute() + else: + print('Quota Module not installed on Nuxeo server') + exit(1) + + +# main() idiom for importing into REPL for debugging +if __name__ == "__main__": + sys.exit(main()) +""" +Copyright © 2019, Regents of the University of California +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +- Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. +- Neither the name of the University of California nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +""" diff --git a/pynux/tests/test_utils.py b/pynux/tests/test_utils.py index bfe2979..a9e8935 100644 --- a/pynux/tests/test_utils.py +++ b/pynux/tests/test_utils.py @@ -12,7 +12,7 @@ class TestNuxeoREST(unittest.TestCase): def setUp(self): self.nx = utils.Nuxeo({ - 'api': 'http://mockme/r', + 'api': 'http://localhost:8080/nuxeo/', }, rcfile=io.BytesIO(bytes())) @httpretty.activate diff --git a/pynux/utils.py b/pynux/utils.py index 3962668..9773f0c 100644 --- a/pynux/utils.py +++ b/pynux/utils.py @@ -12,7 +12,6 @@ from future import standard_library standard_library.install_aliases() from builtins import object -import requests import json import sys import os @@ -23,6 +22,8 @@ from os.path import expanduser import codecs import urllib.parse +import nuxeo.client +from nuxeo.auth import TokenAuth # set the output to utf8 in py2 or py3 UTF8Writer = codecs.getwriter('utf8') @@ -79,8 +80,8 @@ def __init__(self, conf={}, rcfile=None, loglevel=_loglevel_): password = Administrator [rest_api] -base = http://localhost:8080/nuxeo/site/api/v1 -X-NXDocumentProperties = dublincore +baseURL = http://localhost:8080/nuxeo/ +restPath = site/api/v1/ """ config = configparser.ConfigParser() # first level of defaults hardcoded above @@ -108,13 +109,14 @@ def __init__(self, conf={}, rcfile=None, loglevel=_loglevel_): config.get('nuxeo_account', 'user'), "password": config.get('nuxeo_account', 'password'), - "api": - config.get('rest_api', 'base'), - "X-NXDocumentProperties": - config.get('rest_api', 'X-NXDocumentProperties'), + "baseURL": + config.get('rest_api', 'baseURL'), + "restPath": + config.get('rest_api', 'restPath'), "X-Authentication-Token": token, } + defaults['api'] = u'{}{}'.format(defaults['baseURL'], defaults['restPath']).strip('/') self.conf = {} self.conf.update(defaults) # override the defaults based on conf pased in by caller @@ -133,15 +135,8 @@ def __init__(self, conf={}, rcfile=None, loglevel=_loglevel_): } # auth and headers for the request object - self.document_property_headers = { - 'X-NXDocumentProperties': self.conf['X-NXDocumentProperties'] - } if self.conf['auth_method'] == 'token': - self.document_property_headers.update({ - 'X-Authentication-Token': - self.conf['X-Authentication-Token'] - }) - self.auth = None + self.auth = TokenAuth(self.conf['X-Authentication-Token']) else: self.auth = (self.conf["user"], self.conf["password"]) @@ -157,6 +152,8 @@ def __init__(self, conf={}, rcfile=None, loglevel=_loglevel_): redacted = self.conf redacted.update({'password': '...redacted...'}) self.logger.debug(redacted) + self.nuxeo_client = nuxeo.client.Nuxeo(host=self.conf['baseURL'], auth=self.auth) + self.request = self.nuxeo_client.client.request ## Python generator for paged API resource # based on http://stackoverflow.com/questions/17702785/ @@ -173,12 +170,8 @@ def _get_page(self, url, params, current_page_index): :returns: json from nuxeo """ params.update({'currentPageIndex': current_page_index}) - res = requests.get( - url, - headers=self.document_property_headers, - params=params, - auth=self.auth) - res.raise_for_status() + path = url.split(self.nuxeo_client.client.host,1)[1] + res = self.request('GET', path, params=params) self.logger.debug(res.content) return json.loads(res.content.decode('utf-8')) @@ -249,9 +242,8 @@ def get_uid(self, path): :rtype: string """ url = u'/'.join([self.conf['api'], "path", escape_path(path).strip('/')]) - res = requests.get( - url, headers=self.document_property_headers, auth=self.auth) - res.raise_for_status() + path = url.split(self.nuxeo_client.client.host,1)[1] + res = self.request('GET', path) return json.loads(res.content.decode('utf-8'))['uid'] def get_metadata(self, **documentid): @@ -268,9 +260,8 @@ def get_metadata(self, **documentid): elif 'uid' in documentid: uid = documentid['uid'] url = u'/'.join([self.conf['api'], "id", uid]) - res = requests.get( - url, headers=self.document_property_headers, auth=self.auth) - res.raise_for_status() + path = url.split(self.nuxeo_client.client.host,1)[1] + res = self.request('GET', path) return json.loads(res.content.decode('utf-8')) def update_nuxeo_properties(self, data, **documentid): @@ -297,11 +288,9 @@ def update_nuxeo_properties(self, data, **documentid): payload['uid'] = uid payload['entity-type'] = data.get('entity-type', 'document') payload['properties'] = data['properties'] - res = requests.put( - url, data=json.dumps(payload), auth=self.auth, headers=headers) - res.raise_for_status() - r2 = requests.get(url, auth=self.auth, headers=headers) - r2.raise_for_status() + path = url.split(self.nuxeo_client.client.host,1)[1] + res = self.request('PUT', path, data=json.dumps(payload)) + r2 = self.request('GET', path) return json.loads(r2.content) def print_document_summary(self, documents): diff --git a/setup.py b/setup.py index 77bdf10..f0530cd 100644 --- a/setup.py +++ b/setup.py @@ -2,13 +2,13 @@ from setuptools import setup, find_packages setup( name='pynux', - version = "1.0.9", + version = "1.1.0-beta", packages = find_packages(), install_requires = [ - 'requests', 'configparser', 'future', 'EZID @ https://github.com/ucldc/ezid/archive/v0.4.2.tar.gz', + 'nuxeo', ], test_suite = 'pynux.tests.test_utils', tests_require = ['httpretty'],