From 4564cb6d15bde7ea5decc4fd30cdcbd9ccd5f6d3 Mon Sep 17 00:00:00 2001 From: Michael Murtaugh Date: Fri, 28 Nov 2014 11:34:01 +0100 Subject: [PATCH] last changes, start uuid based project --- cgi-bin/hype.cgi | 3 +- cgi-bin/make.cgi | 12 +++++--- cgi-bin/project.py | 11 +++++-- cgi-bin/settings.py | 3 +- cgi-bin/start.cgi | 71 +++++++++++++++++++++++++++++++++++++++++++++ cgi-bin/touch.cgi | 58 ++++++++++++++++++++++++++++++++++++ editor.css | 5 ++++ editor.js | 8 ++++- 8 files changed, 161 insertions(+), 10 deletions(-) create mode 100755 cgi-bin/start.cgi create mode 100755 cgi-bin/touch.cgi diff --git a/cgi-bin/hype.cgi b/cgi-bin/hype.cgi index 801844e..8cc0d8c 100755 --- a/cgi-bin/hype.cgi +++ b/cgi-bin/hype.cgi @@ -48,8 +48,7 @@ print """ - - + diff --git a/cgi-bin/make.cgi b/cgi-bin/make.cgi index 156c2b1..fce231c 100755 --- a/cgi-bin/make.cgi +++ b/cgi-bin/make.cgi @@ -3,7 +3,7 @@ import cgitb; cgitb.enable() import os, sys, cgi, json from subprocess import PIPE, Popen -from settings import PROJECT_PATH, PROJECT_URL, EDITOR_URL, MAKE +from settings import PROJECT_PATH, PROJECT_URL, EDITOR_URL, MAKE, MAKEFILE from project import Project method = os.environ.get("REQUEST_METHOD", "") @@ -14,10 +14,14 @@ dryrun = fs.getvalue("n", "") if path: proj = Project(project) + args = [MAKE] + if MAKEFILE: + args.append("-f") + args.append(MAKEFILE) if dryrun: - p = Popen([MAKE, "-n", path], stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=proj.fullpath) - else: - p = Popen([MAKE, path], stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=proj.fullpath) + args.append("-n") + args.append(path) + p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=proj.fullpath) stdout, stderr = p.communicate() ret = {} ret['stdout'] = stdout diff --git a/cgi-bin/project.py b/cgi-bin/project.py index 77fb0b5..04420ed 100644 --- a/cgi-bin/project.py +++ b/cgi-bin/project.py @@ -1,5 +1,5 @@ import os, operator, sys, re, subprocess, mimetypes, json -from settings import PROJECT_PATH, PROJECT_URL, MAKE +from settings import PROJECT_PATH, PROJECT_URL, MAKE, MAKEFILE from urlparse import urljoin from pprint import pprint from itertools import tee, izip @@ -87,7 +87,14 @@ class Project (object): Must remake target `...' ==> remake """ try: - output = subprocess.check_output([MAKE, "-n", "--debug=v"], cwd=self.fullpath) + args = [MAKE] + if MAKEFILE: + args.append("-f") + args.append(MAKEFILE) + args.append("-n") + args.append("--debug=v") + + output = subprocess.check_output(args, cwd=self.fullpath) missingpat = re.compile(r"^\s*File\ \`(.+?)\'\ does\ not\ exist\.\s*$", re.M) remakepat = re.compile(r"^\s*Must\ remake\ target\ \`(.+?)\'\.\s*$", re.M) missing = [x for x in missingpat.findall(output) if is_filename(x)] diff --git a/cgi-bin/settings.py b/cgi-bin/settings.py index 22d4d5c..af90310 100644 --- a/cgi-bin/settings.py +++ b/cgi-bin/settings.py @@ -1,8 +1,9 @@ PANDOC = "pandoc" EPUB_PATH = None MAKE = "make" - +MAKEFILE = "/home/murtaugh/projects/DPT/hybrideditor/cgi-bin/makefile" PROJECT_PATH = "./projects" PROJECT_URL = "/projects/" # must end with / EDITOR_URL = "/cgi-bin/hype.cgi" +SAMPLE_PROJECT_PATH = "projects/sample" \ No newline at end of file diff --git a/cgi-bin/start.cgi b/cgi-bin/start.cgi new file mode 100755 index 0000000..7212e1a --- /dev/null +++ b/cgi-bin/start.cgi @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +import cgitb; cgitb.enable() +import os, cgi, sys, operator +from settings import PROJECT_PATH, PROJECT_URL, EDITOR_URL, SAMPLE_PROJECT_PATH +from urlparse import urljoin +from urllib import urlencode +from project import Project +from uuid import uuid1 +from shutil import copytree + + +method = os.environ.get("REQUEST_METHOD", "") + +def redirect (url): + print """Content-type: text/html; charset=utf-8""" + print + print """ + + + + +""".format(url) + +errormsg = "" +if method == "POST": + fs = cgi.FieldStorage() + project = uuid1().hex + projpath = os.path.join(PROJECT_PATH, project) + try: + copytree(SAMPLE_PROJECT_PATH, projpath) + proj = Project(project, create=True) + redirect("{0}?{1}".format(EDITOR_URL, urlencode({'p': proj.path}))) + sys.exit(0) + except OSError, e: + errormsg = """An error occurred, check your project name (try without using special characters)
\n
({0})
""".format(e) + +projects = [] +for p in os.listdir(PROJECT_PATH): + fp = os.path.join(PROJECT_PATH, p) + if os.path.isdir(fp) and not p.startswith("."): + projects.append(Project(p)) +projects.sort(key=operator.attrgetter("path")) + + +print "Content-type:text/html;charset=utf-8" +print +print """ + + + +Hybrid Publishing Editor + + + +

Hybrid Publishing Editor

+""" +if len(errormsg): + print """

{0}

""".format(errormsg).encode("utf-8") +print """ +

Click the start project button below to start a new project.

+
+ +
+ + + +""" \ No newline at end of file diff --git a/cgi-bin/touch.cgi b/cgi-bin/touch.cgi new file mode 100755 index 0000000..7b814b0 --- /dev/null +++ b/cgi-bin/touch.cgi @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +import cgitb; cgitb.enable() +import os, sys, cgi, json +from subprocess import PIPE, Popen +from settings import PROJECT_PATH, PROJECT_URL, EDITOR_URL, MAKE +from project import Project +from time import sleep + +method = os.environ.get("REQUEST_METHOD", "") +fs = cgi.FieldStorage() +project = fs.getvalue("p", "") +paths = fs.getlist("f[]") + +ret = {} +passes = 0 +fails = 0 + +proj = Project(project) +if len(paths) == 0: + count = 1 + newname = "untitled_file.md" + fp = os.path.join(proj.fullpath, newname) + while os.path.exists(fp): + count += 1 + newname = "untitled_file_{0}.md".format(count) + fp = os.path.join(proj.fullpath, newname) + sleep(0.001) + + args = ["touch", fp] + p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=proj.fullpath) + stdout, stderr = p.communicate() + if p.returncode == 0: + passes += 1 + else: + fails += 1 +else: + for p in paths: + fp = os.path.join(proj.fullpath, p) + + # print >> sys.stderr, "*** rm", fp + try: + args = ["touch", fp] + p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=proj.fullpath) + stdout, stderr = p.communicate() + if p.returncode == 0: + passes += 1 + else: + fails += 1 + except OSError, e: + fails += 1 + +ret['passes'] = passes +ret['fails'] = fails + +print "Content-type: application/json" +print +print json.dumps(ret) diff --git a/editor.css b/editor.css index a446531..6018483 100644 --- a/editor.css +++ b/editor.css @@ -387,3 +387,8 @@ div.ace_print-margin{ /* Provides visible feedback when use drags a file over the drop zone: */ .fd-zone.over { border-color: maroon; background: #eee; } + + +.editorbody { + font-size: 18px !important; +} \ No newline at end of file diff --git a/editor.js b/editor.js index 5dafc6a..62048cc 100644 --- a/editor.js +++ b/editor.js @@ -10,7 +10,6 @@ and [Creating 010](http://creating010.com). */ - var editor_factory = aa_aceeditor(window.ace), editor = editor_factory.get_editor(), editor_elt = d3.select(editor.elt), @@ -327,6 +326,12 @@ function rename () { } +function new_file () { + d3.json(url_for("touch.cgi"), function (data) { + refresh_listing(); + }) +} + function refresh_listing(done) { d3.json(url_for("listing.cgi"), function (data) { // console.log("data", data); @@ -414,6 +419,7 @@ $("#listing_delete").click(delete_selection); $("#listing_download").click(download_selection); $("#listing_rename").click(rename); $("#listing_cancel").click(listing_cancel); +$("#listing_new_file").click(new_file); // $("#listing_select_all").click(select_all); /* File drop */