From f26ca45041589ade754331b63cc998fb97f5f597 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 16 Jul 2017 01:01:43 -0700 Subject: yet more cleanup; makefile --- Makefile | 14 ++++++++++++++ TODO.md | 23 +++++++---------------- divergence | 48 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..08398e0 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ + +INSTALL ?= install +PREFIX ?= /usr + +.PHONY: build +build: + echo "Nothing to build" + +.PHONY: install +install: + $(INSTALL) -t $(PREFIX)/bin divergence + $(INSTALL) -d $(PREFIX)/lib/divergence + $(INSTALL) -t $(PREFIX)/lib/divergence pandoc_confluence.lua -m 0644 + diff --git a/TODO.md b/TODO.md index 2211670..ce685c0 100644 --- a/TODO.md +++ b/TODO.md @@ -1,23 +1,14 @@ -program structure: -- check for pandoc existence -- render each file to pandoc format (checking for typos, etc) -- connect to server -- for each doc: - parse for metadata - read page to see if it exists - if no change, pass - else, upload - TODO: -- `--help` -- installation procedure -- extract space, title, and/or page ID from pandoc yaml header -- don't push if no change +- extract title, and/or page ID from pandoc yaml header. maybe even space? +- have space default to user home +- update checking doesn't actually work +- fuzzy page title matching bug +- optional header +- optional table of contents +- force update flag Nice to have, but unlikely to be implemented: - -- optional table of contents - more than just markdown (restructured text, html, etc) - configurable banner at the top of rendered pages ("this uploaded from...") - color output, one line per file diff --git a/divergence b/divergence index f72d4db..6017836 100755 --- a/divergence +++ b/divergence @@ -23,17 +23,26 @@ class DivergenceProgram: self.api.headers.update({'Content-Type': 'application/json'}) self.base_url = url self.space = space - - # TODO: find lua path? + self.pandoc_helper_path = None + for p in ('./pandoc_confluence.lua', + '/usr/local/lib/divergence/pandoc_confluence.lua', + '/usr/lib/divergence/pandoc_confluence.lua'): + if os.path.exists(p): + self.pandoc_helper_path = p + break + if self.pandoc_helper_path is None: + log.error("Could not find pandoc helper (pandoc_confluence.lua), bailing") + sys.exit(-1) def get_page(self, title): """ Returns None if not found, otherwise a dict with id, space, and body (in storage format) """ + # TODO: could remove the body_view stuff here resp = self.api.get(self.base_url + "/rest/api/content", params={"spaceKey": self.space, "title": title, - "expand": "body.storage,version,space", + "expand": "body.storage,body.view,version,space", "type": "page"}) log.debug(resp) @@ -49,7 +58,8 @@ class DivergenceProgram: return {"id": int(page['id']), "version": int(page['version']['number']), "space": page['space']['key'], - "body": page['body']['storage']['value']} + "body": page['body']['storage']['value'], + "body_view": page['body']['view']['value']} def create_page(self, title, body): resp = self.api.post(self.base_url + "/rest/api/content", @@ -83,7 +93,7 @@ class DivergenceProgram: return title def convert(self, f): - proc = subprocess.run(["pandoc", "-t", "pandoc_confluence.lua", f], + proc = subprocess.run(["pandoc", "-t", self.pandoc_helper_path, f], stdout=subprocess.PIPE) assert proc.returncode == 0 return proc.stdout.decode('UTF-8') @@ -101,6 +111,13 @@ class DivergenceProgram: print(f + ": created") else: if prev['body'] != body: + # TODO: too much changes in the diff here. Should do + # something like store the file sha1 in a comment, regex + # that out, and compare? + from difflib import Differ + sys.stdout.writelines(Differ().compare( + prev['body'].splitlines(keepends=True), + body.splitlines(keepends=True))) self.update_page(title, body, prev['id'], prev['version']) print(f + ": updated") else: @@ -108,11 +125,12 @@ class DivergenceProgram: def main(): parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter, description=""" Simple Markdown-to-Confluence uploader, using pandoc and the Confluence REST API. -Specify credentials and site URL with environment variables: +required environment variables: CONFLUENCE_USER CONFLUENCE_PASSWORD CONFLUENCE_URL @@ -123,7 +141,7 @@ Specify credentials and site URL with environment variables: default=0, help="Show more debugging statements (can be repeated)") parser.add_argument("-s", "--space-key", - required=True, + default=None, help='Confluence Space Key (usually like "PROJ" or "~username")') parser.add_argument("FILE", nargs='+') @@ -139,13 +157,27 @@ Specify credentials and site URL with environment variables: password = os.environ['CONFLUENCE_PASSWORD'] url = os.environ['CONFLUENCE_URL'] except KeyError: - parser.exit(-1, "Need to pass environment variable configs") + parser.exit(-1, "Need to pass environment variable configs\n") + + log.info("User: " + user) + log.info("URL: " + url) if url.endswith('/'): url = url[:-1] + if args.space_key is None: + args.space_key = "~" + user + log.warn("Defaulting to home space: %s" % args.space_key) + + try: + subprocess.check_output(['pandoc', '--version']) + except: + parser.exit(-1, "This script depends on 'pandoc', which doesn't " + "seem to be installed.\n") + dp = DivergenceProgram(user, password, url, args.space_key) dp.run(args.FILE) if __name__ == '__main__': main() + -- cgit v1.2.3