From 594bc29f9e96fa9b90f79ce4d37646d66b3c4f8f Mon Sep 17 00:00:00 2001
From: Konstantin Baierer <unixprog@gmail.com>
Date: Wed, 16 Oct 2019 20:48:54 +0200
Subject: [PATCH] wip

---
 kwalitee.py          |  4 ----
 kwalitee/cli.py      | 36 ++++++++++++++++++++++++++++++++++++
 kwalitee/config.yml  | 18 ++++++++++++++++++
 kwalitee/kwalitee.py |  8 ++++++++
 kwalitee/repo.py     | 30 ++++++++++++++++++++++++++++++
 requirements.txt     |  3 ++-
 setup.py             |  6 +++---
 7 files changed, 97 insertions(+), 8 deletions(-)
 delete mode 100644 kwalitee.py
 create mode 100644 kwalitee/cli.py
 create mode 100644 kwalitee/config.yml
 create mode 100644 kwalitee/kwalitee.py
 create mode 100644 kwalitee/repo.py

diff --git a/kwalitee.py b/kwalitee.py
deleted file mode 100644
index e04c58e..0000000
--- a/kwalitee.py
+++ /dev/null
@@ -1,4 +0,0 @@
-from github import Github
-
-def analyze_kwalitee(url):
-    ret = {}
diff --git a/kwalitee/cli.py b/kwalitee/cli.py
new file mode 100644
index 0000000..d373bf1
--- /dev/null
+++ b/kwalitee/cli.py
@@ -0,0 +1,36 @@
+import click
+from ocrd.decorators import ocrd_loglevel
+from ocrd_utils import getLogger
+from yaml import safe_load
+from pkg_resources import resource_filename
+
+from .repo import Repo
+
+LOG = getLogger('kwalitee.cli')
+
+class CliCtx():
+    def __init__(self, config_file):
+        with open(config_file, 'r') as f_config_file:
+            self.config = safe_load(f_config_file.read())
+            self.repos = [Repo(self.config, url) for url in self.config['repolist']]
+pass_ctx = click.make_pass_decorator(CliCtx)
+
+@click.group()
+@click.option('-c', '--config-file', help="", default=resource_filename(__name__, 'config.yml'))
+@ocrd_loglevel
+@click.pass_context
+def cli(ctx, config_file, **kwargs): # pylint: disable=unused-argument
+    ctx.obj = CliCtx(config_file)
+
+@cli.command('generate-json', help='''
+
+    Generate JSON
+
+''')
+@pass_ctx
+def generate_json(ctx):
+    for repo in ctx.repos:
+        print("# %s" % repo.name)
+        print("\thas_ocrd_tool_json: %s" % repo.has_ocrd_tool_json())
+        #  print('%s %s -> %s' % (repo.path.is_dir(), repo.url, repo.path))
+        repo.clone()
diff --git a/kwalitee/config.yml b/kwalitee/config.yml
new file mode 100644
index 0000000..002fd04
--- /dev/null
+++ b/kwalitee/config.yml
@@ -0,0 +1,18 @@
+# Base dir to clone repos to
+repodir: /data/monorepo
+# repos to clone and process
+repolist:
+    - https://github.com/OCR-D/ocrd_calamari
+    - https://github.com/OCR-D/ocrd_im6convert
+    - https://github.com/OCR-D/ocrd_keraslm
+    - https://github.com/OCR-D/ocrd_kraken
+    - https://github.com/OCR-D/ocrd_ocropy
+    - https://github.com/OCR-D/ocrd_olena
+    - https://github.com/OCR-D/ocrd_segment
+    - https://github.com/OCR-D/ocrd_tesserocr
+    - https://github.com/mjenckel/LAYoutERkennung
+    - https://github.com/ocr-d-modul-2-segmentierung/segmentation-runner
+    - https://github.com/ASVLeipzig/cor-asv-fst
+    - https://github.com/ASVLeipzig/cor-asv-ann
+    - https://github.com/cisocrgroup/ocrd-postcorrection
+    - https://github.com/seuretm/ocrd_typegroups_classifier
diff --git a/kwalitee/kwalitee.py b/kwalitee/kwalitee.py
new file mode 100644
index 0000000..284b0e7
--- /dev/null
+++ b/kwalitee/kwalitee.py
@@ -0,0 +1,8 @@
+from ocrd_utils import pushd_popd
+def assess_kwalitee(repo):
+    ret = {}
+    # ocrd-tool.json
+    with open('ocrd-tool.json')
+    ret['ocrd-tool'] = 
+    # TODO has docker file?
+    print("TODO")
diff --git a/kwalitee/repo.py b/kwalitee/repo.py
new file mode 100644
index 0000000..f276e5b
--- /dev/null
+++ b/kwalitee/repo.py
@@ -0,0 +1,30 @@
+from pathlib import Path
+import json
+from subprocess import run
+from ocrd_utils import pushd_popd, getLogger
+
+LOG = getLogger('kwalitee.repo')
+
+class Repo():
+
+    def __init__(self, config, url):
+        self.url = url
+        self.config = config
+        self.name = Path(url).name
+        self.path = Path(self.config['repodir'], self.name)
+        self.ocrd_tool_path = Path(self.path, 'ocrd-tool.json')
+
+    def clone(self):
+        if self.path.is_dir():
+            LOG.debug("Already cloned: %s" % self.path)
+            return
+        with pushd_popd(self.config['repodir']):
+            LOG.debug("Cloning %s" % self.url)
+            result = run('git clone --depth 1'.split(' ') + [self.url])
+            LOG.debug("Result: %s" % result)
+
+    def has_ocrd_tool_json(self):
+        return self.ocrd_tool_path.is_file()
+
+    def get_ocrd_tool_json(self):
+        return json.load(self.ocrd_tool_path)
diff --git a/requirements.txt b/requirements.txt
index 2d0f665..e2ea807 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,4 @@
 click >=7
+ocrd
+pyyaml
 requests
-PyGithub
diff --git a/setup.py b/setup.py
index 13ce21e..fa88e69 100644
--- a/setup.py
+++ b/setup.py
@@ -13,17 +13,17 @@ setup(
     long_description_content_type='text/markdown',
     author='Konstantin Baierer',
     author_email='unixprog@gmail.com',
-    url='https://github.com/OCR-D/core',
+    url='https://github.com/OCR-D/kwalitee',
     license='Apache License 2.0',
     packages=find_packages(exclude=('tests', 'docs')),
     include_package_data=True,
     install_requires=install_requires,
     package_data={
-        '': ['*.json', '*.yml', '*.yaml', '*.bash', '*.xml'],
+        '': ['*.json', '*.yml', '*.yaml', '*.list', '*.xml'],
     },
     entry_points={
         'console_scripts': [
-            'ocrd=ocrd.cli:cli',
+            'ocrd-kwalitee=kwalitee.cli:cli',
         ]
     },
 )
-- 
GitLab