Skip to content

Commit

Permalink
Merge pull request #98 from ranman/bug/termination
Browse files Browse the repository at this point in the history
Fix OSX Termination and upgrade sql alchemy version
  • Loading branch information
gurgeh committed Jul 7, 2014
2 parents 2252cc9 + abafb8f commit 9414b9b
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 118 deletions.
2 changes: 1 addition & 1 deletion osx-requirements.txt
@@ -1,4 +1,4 @@
SQLAlchemy==0.7.6
SQLAlchemy==0.9.4
lockfile==0.9.1
pycrypto==2.5
pyobjc-core==2.5.1
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,4 +1,4 @@
SQLAlchemy==0.7.6
SQLAlchemy==0.9.4
lockfile==0.9.1
pycrypto==2.5
keyring==1.2.2
7 changes: 4 additions & 3 deletions selfspy/__init__.py
Expand Up @@ -34,6 +34,7 @@

from selfspy import config as cfg


def parse_config():
conf_parser = argparse.ArgumentParser(description=__doc__, add_help=False,
formatter_class=argparse.RawDescriptionHelpFormatter)
Expand All @@ -54,7 +55,7 @@ def parse_config():

parser.add_argument('-n', '--no-text', action='store_true', help='Do not store what you type. This will make your database smaller and less sensitive to security breaches. Process name, window titles, window geometry, mouse clicks, number of keys pressed and key timings will still be stored, but not the actual letters. Key timings are stored to enable activity calculation in selfstats. If this switch is used, you will never be asked for password.')
parser.add_argument('-r', '--no-repeat', action='store_true', help='Do not store special characters as repeated characters.')

parser.add_argument('--change-password', action="store_true", help='Change the password used to encrypt the keys columns and exit.')

return parser.parse_args()
Expand Down Expand Up @@ -83,7 +84,7 @@ def check_with_encrypter(password):
pass

lockname = os.path.join(args['data_dir'], cfg.LOCK_FILE)
cfg.LOCK = LockFile(lockname)
cfg.LOCK = LockFile(lockname)
if cfg.LOCK.is_locked():
print '%s is locked! I am probably already running.' % lockname
print 'If you can find no selfspy process running, it is a stale lock and you can safely remove it.'
Expand Down Expand Up @@ -125,7 +126,7 @@ def check_with_encrypter(password):
cfg.LOCK.acquire()
try:
astore.run()
except SystemExit:
except SystemExit:
astore.close()
except KeyboardInterrupt:
pass
Expand Down
40 changes: 25 additions & 15 deletions selfspy/activity_store.py
Expand Up @@ -77,14 +77,14 @@ def __init__(self, db_name, encrypter=None, store_text=True, repeat_char=True):

def trycommit(self):
self.last_commit = time.time()
for _ in xrange(1000):
for _ in range(1000):
try:
self.session.commit()
break
except sqlalchemy.exc.OperationalError:
time.sleep(1)
except:
self.session.rollback()
self.session.rollback()

def run(self):
self.session = self.session_maker()
Expand All @@ -98,23 +98,33 @@ def run(self):
self.sniffer.run()

def got_screen_change(self, process_name, window_name, win_x, win_y, win_width, win_height):
""" Receives a screen change and stores any changes. If the process or window has
changed it will also store any queued pressed keys.
process_name is the name of the process running the current window
window_name is the name of the window
win_x is the x position of the window
win_y is the y position of the window
win_width is the width of the window
win_height is the height of the window """
cur_process = self.session.query(Process).filter_by(name=process_name).scalar()
"""Receives a screen change and stores any changes.
If the process or window has changed it will also store any queued pressed keys.
Keyword arguments:
process_name -- the name of the process running the current window
window_name -- the name of the window
win_x -- the x position of the window
win_y -- the y position of the window
win_width -- the width of the window
win_height -- the height of the window"""

cur_process = self.session.query(
Process
).filter_by(
name=process_name
).scalar()
if not cur_process:
cur_process = Process(process_name)
self.session.add(cur_process)

cur_geometry = self.session.query(Geometry).filter_by(xpos=win_x,
ypos=win_y,
width=win_width,
height=win_height).scalar()
cur_geometry = self.session.query(
Geometry
).filter_by(
xpos=win_x,
ypos=win_y,
width=win_width,
height=win_height
).scalar()
if not cur_geometry:
cur_geometry = Geometry(win_x, win_y, win_width, win_height)
self.session.add(cur_geometry)
Expand Down
13 changes: 8 additions & 5 deletions selfspy/models.py
Expand Up @@ -22,7 +22,10 @@
import datetime

from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy import Index, Column, Boolean, Integer, Unicode, DateTime, Binary, ForeignKey, create_engine
from sqlalchemy import (
Index, Column, Boolean, Integer, Unicode, DateTime, Binary, ForeignKey,
create_engine
)
from sqlalchemy.orm import sessionmaker, relationship, backref


Expand Down Expand Up @@ -69,7 +72,7 @@ def __init__(self, title, process_id):
def __repr__(self):
return "<Window '%s'>" % (self.title)


class Geometry(SpookMixin, Base):
xpos = Column(Integer, nullable=False)
ypos = Column(Integer, nullable=False)
Expand All @@ -87,7 +90,7 @@ def __init__(self, x, y, width, height):
def __repr__(self):
return "<Geometry (%d, %d), (%d, %d)>" % (self.xpos, self.ypos, self.width, self.height)


class Click(SpookMixin, Base):
button = Column(Integer, nullable=False)
press = Column(Boolean, nullable=False)
Expand All @@ -100,7 +103,7 @@ class Click(SpookMixin, Base):

window_id = Column(Integer, ForeignKey('window.id'), nullable=False)
window = relationship("Window", backref=backref('clicks'))

geometry_id = Column(Integer, ForeignKey('geometry.id'), nullable=False)
geometry = relationship("Geometry", backref=backref('clicks'))

Expand All @@ -118,7 +121,7 @@ def __init__(self, button, press, x, y, nrmoves, process_id, window_id, geometry
def __repr__(self):
return "<Click (%d, %d), (%d, %d, %d)>" % (self.x, self.y, self.button, self.press, self.nrmoves)


def pad(s, padnum):
ls = len(s)
if ls % padnum == 0:
Expand Down
4 changes: 2 additions & 2 deletions selfspy/period.py
Expand Up @@ -27,7 +27,7 @@ def __init__(self, cutoff, maxtime):
def append(self, time):
ltimes = len(self.times)
end = min(time + self.cutoff, self.maxtime)

def check_in(i):
if self.times[i][0] <= time <= self.times[i][1]:
self.times[i] = (self.times[i][0], max(end, self.times[i][1]))
Expand All @@ -52,7 +52,7 @@ def maybe_merge(i):
else:
self.times.insert(i, (time, end))
maybe_merge(i)

def extend(self, times):
for time in times:
self.append(time)
Expand Down

0 comments on commit 9414b9b

Please sign in to comment.