From e569d6181cfc8c80c3651ef7c5f7e999514a0081 Mon Sep 17 00:00:00 2001 From: poikilos <7557867+poikilos@users.noreply.github.com> Date: Tue, 30 Nov 2021 17:28:07 -0500 Subject: [PATCH] Translate timestamps. --- utilities/enissue.py | 39 +++++++++++++++++++++++++++++++----- utilities/pyissuesyncd | 45 +++++++++++++++++++++++++++++++++++------- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/utilities/enissue.py b/utilities/enissue.py index 0f3c7ba..0251efe 100755 --- a/utilities/enissue.py +++ b/utilities/enissue.py @@ -27,9 +27,9 @@ Options: Partial API documentation: options keys: -- default_time_fmt: must be either a cross-platform (python) strftime - format (See https://strftime.org/) or the literal string - "unix_timestamp" to denote a unix timestamp. +- default_dt_parser: This must be a method that returns a python + datetime object by accepting a single argument, which is a string + from the style of the Repo's API. ''' from __future__ import print_function import sys @@ -102,6 +102,35 @@ if site_users_repos_meta is not None: if repository_id is None: repository_id = repo_meta.get('repository_id') ''' + + +def github_ts_to_dt(timestamp_s): + ''' + Use "%Y-%m-%dT%H:%M:%SZ" + GitHub example (Z for UTC): 2018-09-13T16:59:38Z + ''' + return datetime.strptime(timestamp_s, "%Y-%m-%dT%H:%M:%SZ") + + +def gitea_ts_to_dt(timestamp_s): + ''' + Use "%Y-%m-%dT%H:%M:%SZ" + Gitea example (last : must be removed): "2021-11-25T12:00:13-05:00" + ''' + example_s = "2021-11-25T12:00:13-05:00" + if len(timestamp_s) != len(example_s): + raise ValueError("Gitea {} is not like the expected {}" + "".format(timestamp_s, example_s)) + timestamp_s_raw = timestamp_s + timestamp_s = timestamp_s[:-3] + timestamp_s[-2:] + ''' + Remove the non-python-like : from %z + %z is "+0000 UTC offset in the form ±HHMM[SS[.ffffff]] (empty + string if the object is naive)." + - + ''' + return datetime.strptime(timestamp_s, "%Y-%m-%dT%H:%M:%S%z") + github_defaults = { 'repository_id': "80873867", 'instance_url': "https://api.github.com", @@ -123,7 +152,7 @@ github_defaults = { 'body': 'body', 'title': 'title', }, - 'default_time_fmt': "%Y-%m-%dT%H:%M:%SZ", # GitHub (Z for UTC): 2018-09-13T16:59:38Z + 'default_dt_parser': github_ts_to_dt, } gitea_defaults = { @@ -146,7 +175,7 @@ gitea_defaults = { 'body': 'body', 'title': 'title', }, - 'default_time_fmt': "unix_timestamp", + 'default_dt_parser': gitea_ts_to_dt, } # API documentation: diff --git a/utilities/pyissuesyncd b/utilities/pyissuesyncd index eaf4b9a..10365bc 100755 --- a/utilities/pyissuesyncd +++ b/utilities/pyissuesyncd @@ -46,6 +46,7 @@ pyissuesyncd --dst-repo https://example.com/git/repo import os import sys import json +from datetime import datetime #, timedelta # see def error(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) @@ -143,7 +144,9 @@ def start_issuesyncd(src_options, dst_options): non_issue = 1 issue_no = non_issue - 1 # while True: - while issue_no < non_issue: # for debug only + src_res_code = 0 + # while issue_no < non_issue: # for debug only + while src_res_code != 404: issue_no += 1 src_repo = Repo(src_options) src_issue, err = get_issue(src_repo, src_options, issue_no) @@ -153,23 +156,51 @@ def start_issuesyncd(src_options, dst_options): err.get('reason'))) continue - print("[pyissuesyncd] src_issue:") - print(json.dumps(src_issue, indent=2)) + # Example: ~/.cache/pyissuesyncd/source/issues/1.json + src_dt_parser = src_repo.options['default_dt_parser'] + src_created_dt_s = src_repo.getKnown(0, 'created_at') + src_updated_dt_s = src_repo.getKnown(0, 'updated_at') + src_updated_dt = src_dt_parser(src_updated_dt_s) + src_updated_ts = int(src_updated_dt.strftime("%s")) + # ^ See + ''' + print("* src_issue: {} updated: {} = {}" + "".format(issue_no, src_updated_ts, src_updated_dt)) + ''' + # print(json.dumps(src_issue, indent=2)) enissue.set_verbose(True) dst_repo = Repo(dst_options) dst_issue, err = get_issue(dst_repo, dst_options, issue_no) - print("[pyissuesyncd] dst_issue:") - print(json.dumps(dst_issue, indent=2)) + dst_dt_parser = dst_repo.options['default_dt_parser'] + dst_created_dt_s = dst_repo.getKnown(0, 'created_at') + dst_updated_dt_s = dst_repo.getKnown(0, 'updated_at') + dst_updated_dt = dst_dt_parser(dst_updated_dt_s) + dst_updated_ts = int(dst_updated_dt.strftime("%s")) + # ^ See + ''' + print("* dst_issue: {} updated: {} = {}" + "".format(issue_no, dst_updated_ts, dst_updated_dt)) + ''' + # Example: ~/.cache/pyissuesyncd/destination/issues/1.json + break # for debug only + continue # for debug only + # print(" * dst_issue:") + # print(json.dumps(dst_issue, indent=2)) if err is not None: if err.get('code') == 404: - dst_repo.create_issue(src_issue, src_repo) + # dst_repo.create_issue(src_issue, src_repo) continue error("Error accessing destination issue {}: {}: {}" "".format(issue_no, err.get('code'), err.get('reason'))) continue - dst_repo.update_issue(src_issue, src_repo) + # if issue_differs: # compare timestamp + if True: # for debug only + pass + # dst_repo.update_issue(src_issue, src_repo) def usage():