Skip to content

Instantly share code, notes, and snippets.

@camillobruni
Last active November 3, 2016 11:16
Show Gist options
  • Save camillobruni/c4bebc6b70c481694fd28d632eacb433 to your computer and use it in GitHub Desktop.
Save camillobruni/c4bebc6b70c481694fd28d632eacb433 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import subprocess
import datetime
class Commit:
@classmethod
def parse(cls, hash, repository):
return Commit(repository, hash)
def __init__(self, repository, hash):
self.repository = repository
self.hash = hash
self.load()
def load(self):
result = self.repository.command(
"show --no-patch --format='%H%n%an %ae%n%ct%n%s%n%b' " + self.hash)
result = result.splitlines()
author = result[1].split(" ")
self.author_email = author[-1:][0]
self.author_name = " ".join(author[:-1])
self.commit_date = datetime.datetime.utcfromtimestamp(int(result[2]))
self.subject= result[3]
self.bug_id = 0
self.review_id = 0
self.body= result[4:]
for line in self.body:
if line.startswith("BUG="):
self.bug_id = line.split("=")[1]
elif line.startswith("Review-Url:"):
self.review_id = line.split("/")[-1]
self.body = "\n".join(self.body)
def done(self):
self.repository.reset_to(self)
def printOn(self, out):
pass
def print(self):
print("%s %s" % (self.hash[:7], self.commit_date.strftime("%Y-%m-%d %H:%M")))
print(self.author_email)
string = self.subject
while string :
print(string[:24])
if 25 < len(string) and string[24] == " ":
string = string[25:]
else:
string = string[24:]
if self.bug_id:
print("BUG=%s" % self.bug_id)
print("crrev.com/%s" % self.review_id)
print("")
def __str__(self):
return "%s %s %s %s" % (self.hash[:8], self.commit_date, self.author, self.subject)
class Repository:
def __init__(self, path):
self.path = path
self.tracking_branch = 'track'
self.remote = 'master'
def update(self):
pass
def command(self, cmd):
return subprocess.getoutput(
"git --git-dir='%s' %s" % (self.path, cmd))
def pending_commits(self):
self.update()
result = self.command(
"log --format=%%H %s..%s" % (
self.tracking_branch, self.remote))
result = result.splitlines()
result.reverse()
return [ Commit.parse(line, self) for line in result ]
def reset_to(self, commit):
self.command("reset --soft %s" % commit.hash)
class Printer:
""" Basic implementation of the DPN-233 / 833 / 8233 / 8333 Series printer protocol """
LINE_CHAR_WIDTH = 24
LINE_DOT_WIDTH = 144
def __init__(self):
pass
def write(self, string):
pass
def byte(self, byte):
assert(0 <= byte <= 0xff)
self.write(chr(byte))
def two_byte(self, number):
self.byte(number & 0xFF)
self.byte(number >> 8)
def bool(self, value):
self.byte(1 if value else 0)
def reset(self):
self.write('@')
def lf(self):
self.write("\n")
def cr(self):
self.write("\r")
def ff(self):
self.byte(0x0C)
def blank_lines_or_chars(self, m=0, n=0):
self.esc('f')
self.byte(m)
self.byte(n)
def blank_lines(self, n=1):
self.blank_lines_or_chars(1, n)
def blank_chars(self, n=1):
self.blank_lines_or_chars(0, n)
def scale_width(self, n=1):
assert(1 <= n <= 4)
self.esc('U')
self.byte(n)
def scale_height(self, n=1):
assert(1 <= n <= 4)
self.esc('V')
self.byte(n)
def scale(self, n=1):
assert(1 <= n <= 4)
self.esc('W')
self.byte(n)
def underline(self, on=True):
self.esc('-')
self.bool(on)
def overscore(self, on=True):
self.esc('+')
self.bool(on)
def font1(self):
self.esc('6')
def font2(self):
self.esc('7')
def double_width(self, on=True):
if on:
self.byte(0x0E)
else:
self.byte(0x14)
def invert(self, on=True):
self.esc('i')
self.bool(on)
def reverse(self, on=True):
"""Print upside down"""
self.esc('c')
self.bool(on)
def byte_image(data):
self.esc('K')
self.two_byte(len(data))
for byte in data: self.byte(byte)
def image(data):
""" Data is an array of strings, each non-space string will
be printed as a black pixel:
[
' ***** ',
' * *'
' * *'
' ***** '
]
"""
width = max([len(line) for line in data])
assert(width <= LINE_DOT_WIDTH)
# Combine 8 lines into a single image command
while len(data) > 0:
byte_data = [0x00] * width
bit_offset = 0
for line in data[:8]:
for column in range(len(line)):
bit = line[column] != ' '
byte_data[column] += bit << bit_offset
bit_offset += 1
self.byte_image(byte_data)
# Use the next 8 lines.
data = data[8:]
def dot_line_feed(n=1):
assert(1 <= n)
self.esc('J')
self.byte(n)
def set_line_spacing(self, n=0):
self.esc('1')
self.byte(n)
def esc(self, char=None):
self.write("\0")
if value != None:
self.write(char)
class ReversePrint(Printer):
def __initialize__(self, printer):
self.printer = printer
self.line_buffer = ""
self.buffer = []
def push_line_buffer(self):
if len(self.line_buffer) > 0:
self.buffer.append(self.line_buffer)
def write(string):
self.line_buffer += string
assert(len(self.line_buffer) <= LINE_CHAR_WIDTH)
def lf(self):
self.push_line_buffer()
self.line_buffer = "\n"
def __finalize__(self):
self.push_line_buffer()
self.buffer.reverse()
self.printer.reverse()
for line in self.buffer:
self.printer.write(line)
if __name__ == '__main__':
repository = Repository("v8/.git")
commits = repository.pending_commits()
for commit in commits:
commit.print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment