Skip to content

Commit

Permalink
2to3 transtion, part 6: more scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
R. Saravanan committed Jul 1, 2014
1 parent f5b0303 commit df6096c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
11 changes: 8 additions & 3 deletions graphterm/bin/gdownload
Expand Up @@ -5,6 +5,8 @@
gdownload: Download files or piped content; if multiple files, download zip archive
"""

from __future__ import absolute_import, print_function

import datetime
import mimetypes
import os
Expand Down Expand Up @@ -59,7 +61,7 @@ def populate_zipfile(zipbuf, source_files):
arcname = os.path.join(relpath, file)
zipbuf.write(filename, arcname)
else:
print >> sys.stderr, "Skipping inaccessible file", source_file
print("Skipping inaccessible file", source_file, file=sys.stderr)

usage = "usage: %prog [file] [file2|dir2] ..."
parser = OptionParser(usage=usage)
Expand All @@ -79,7 +81,10 @@ if not args:
download_name = options.name
content = None
try:
content = sys.stdin.read()
if sys.version_info[0] < 3:
content = sys.stdin.read()
else:
content = sys.stdin.buffer.read()
except (EOFError, KeyboardInterrupt):
content = None

Expand Down Expand Up @@ -108,7 +113,7 @@ elif os.path.isdir(args[0]) or len(args) > 1:
hostname = "download"
download_name = hostname + "-" + str(datetime.date.today()) + ".zip"

print >> sys.stderr, "Creating zip archive..."
print("Creating zip archive...", file=sys.stderr)
outbuf = gterm.BlobBytesIO("application/zip", max_bytes=options.max_bytes)
zipbuf = zipfile.ZipFile(outbuf, "w", zipfile.ZIP_DEFLATED)
populate_zipfile(zipbuf, args)
Expand Down
10 changes: 6 additions & 4 deletions graphterm/bin/gjs
Expand Up @@ -5,6 +5,8 @@
gjs: execute arbitrary Javascript in graphterm
"""

from __future__ import absolute_import, print_function

import sys

from optparse import OptionParser
Expand All @@ -26,7 +28,7 @@ parser.add_option("-e", "--eval", dest="eval", default="",

if options.file:
if not args:
print >> sys.stderr, "Please provide filename"
print("Please provide filename", file=sys.stderr)
sys.exit(1)
with open(args[0], "r") as f:
js = "".join(f.readlines())
Expand All @@ -38,11 +40,11 @@ elif args:
else:
js = args[0] + "(" + ", ".join(arg if arg.isdigit() else '"'+arg+'"' for arg in args[1:]) + ")"
else:
print >> sys.stderr, parser.get_usage()
print(parser.get_usage(), file=sys.stderr)
sys.exit(1)

if options.verbose:
print js
print(js)

Headers = {"content_type": "text/plain"}
Headers["x_gterm_response"] = "eval_js"
Expand All @@ -53,7 +55,7 @@ gterm.wrap_write(js, headers=Headers, stderr=True)
try:
lines = sys.stdin.readlines()
if not sys.stdout.isatty():
print "".join(lines)
print("".join(lines))
except KeyboardInterrupt:
pass

Expand Down
8 changes: 5 additions & 3 deletions graphterm/bin/greveal
Expand Up @@ -7,6 +7,8 @@ greveal: reveal.js in graphterm
Type "bbb" to exit
"""

from __future__ import absolute_import, print_function

import base64
import json
import mimetypes
Expand Down Expand Up @@ -200,17 +202,17 @@ if args:
with open(args[0]) as f:
lines = f.readlines()
except Exception:
print >> sys.stderr, "Error in reading markdown content from file", args[0]
print("Error in reading markdown content from file", args[0], file=sys.stderr)
sys.exit(1)
else:
title = "stdin"
try:
lines = sys.stdin.readlines()
except (EOFError, KeyboardInterrupt):
print >> sys.stderr, "Error in reading markdown content from standard input"
print("Error in reading markdown content from standard input", file=sys.stderr)

if not lines:
print >> sys.stderr, "No content to process"
print("No content to process", file=sys.stderr)
sys.exit(1)

blobs = {}
Expand Down
32 changes: 17 additions & 15 deletions graphterm/bin/gupload
Expand Up @@ -14,6 +14,8 @@ To switch stdout and stderr:
gupload 3>&1 1>&2 2>&3-
"""

from __future__ import absolute_import, print_function

import base64
import json
import os
Expand Down Expand Up @@ -50,7 +52,7 @@ parser.add_option("", "--verbose",
(options, args) = parser.parse_args()

if len(args) > 1:
print >> sys.stderr, "To many arguments"
print("To many arguments", file=sys.stderr)
sys.exit(1)

assert sys.stdin.isatty()
Expand All @@ -69,25 +71,25 @@ if filepath:
if os.path.isdir(filepath):
to_dir = True
elif not options.write:
print >> sys.stderr, "Specify -w option to overwrite file %s" % filepath
print("Specify -w option to overwrite file %s" % filepath, file=sys.stderr)
sys.exit(1)
else:
head, tail = os.path.split(filepath)
if head:
if os.path.exists(head):
if not os.path.isdir(head):
print >> sys.stderr, "Cannot overwrite file %s" % head
print("Cannot overwrite file %s" % head, file=sys.stderr)
sys.exit(1)
else:
print >> sys.stderr, "Please create directory %s" % head
print("Please create directory %s" % head, file=sys.stderr)
sys.exit(1)

if options.loop and not to_dir:
sys.exit("Error: loop option inconsistent with non-directory destination")

while True:
if not options.quiet and not options.stdout:
print >> sys.stderr, "Uploading file..."
print("Uploading file...", file=sys.stderr)

params = {"display": "block", "current_directory": Work_dir}
html_headers = {"x_gterm_response": "upload_file",
Expand Down Expand Up @@ -120,7 +122,7 @@ while True:
break

if options.verbose:
print >> sys.stderr, "header=%s\n" % (header_line,)
print("header=%s\n" % (header_line,), file=sys.stderr)

# Process headers
if not header_line:
Expand All @@ -141,13 +143,13 @@ while True:
filename = headers["x_gterm_filepath"]
expect_length = headers["x_gterm_length"]
if options.verbose:
print >> sys.stderr, "file=%s, type=%s, expect_len=%s\n" % (filename, content_type, expect_length)
print("file=%s, type=%s, expect_len=%s\n" % (filename, content_type, expect_length), file=sys.stderr)

if expect_length:
count = expect_length
assert not (count % 4)
prefix = ""
content_list = []
content_list = bytearray()
while count > 0:
chunk = sys.stdin.read(min(count, CHUNK_BYTES))
assert chunk
Expand All @@ -159,10 +161,10 @@ while True:
prefix = line[-offset:]
line = line[:-offset]
if options.verbose:
print >> sys.stderr, "line(%d,%s)=%s" % (len(chunk), count, line,)
content_list.append(base64.b64decode(line))
print("line(%d,%s)=%s" % (len(chunk), count, line,), file=sys.stderr)
content_list.extend(base64.b64decode(line))
assert not prefix
content = "".join(content_list)
content = bytes(content_list)
else:
b64_content = ""
content = ""
Expand All @@ -178,10 +180,10 @@ while True:
tempath = os.path.join(filepath, filename)
else:
tempath = filepath
with open(tempath, "w") as f:
with open(tempath, "wb") as f:
f.write(content)
if not options.quiet:
print >> sys.stderr, "Uploaded file "+tempath
print("Uploaded file "+tempath, file=sys.stderr)

if not options.loop:
break
Expand All @@ -191,6 +193,6 @@ while True:

if Stdout_data is not None:
if options.stdout:
sys.stderr.write(content)
os.write(sys.stderr.fileno(), content)
else:
sys.stdout.write(content)
os.write(sys.stdout.fileno(), content)

0 comments on commit df6096c

Please sign in to comment.