Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
first commit
  • Loading branch information
Jon Rifkin committed Sep 17, 2020
0 parents commit c667454
Show file tree
Hide file tree
Showing 3 changed files with 472 additions and 0 deletions.
51 changes: 51 additions & 0 deletions DB.py
@@ -0,0 +1,51 @@
# Provide stateful DB access accessible across functions
import sqlite3
class DB:
def __init__(self,dbpath):
self.db = sqlite3.connect(dbpath)
self.dbc = self.db.cursor()
def write(self,sqls):
# If sqls is a single string, convert to list of one
if not type(sqls) in ( type(()), type([]) ): sqls = [ sqls ]
for sql in sqls:
self.dbc.execute(sql)
self.db.commit()
def read(self,sql):
self.dbc.execute(sql)
return self.dbc.fetchall()
def close(self):
self.db.close()
# Only needed when creating the database
def create_tables(self):
CREATE_TABLE_SQLS = [
"""CREATE TABLE IF NOT EXISTS user_footprint (
datetime text,
userid integer,
user text,
rank integer,
count integer,
footprint integer,
primary key (datetime,user)
);
""",
"""CREATE TABLE IF NOT EXISTS homedir_footprint (
datetime text,
dir text,
rank integer,
count integer,
footprint integer,
primary key (datetime,dir)
);
""",
"""CREATE TABLE IF NOT EXISTS shareddir_footprint (
datetime text,
dir text,
rank integer,
count integer,
footprint integer,
primary key (datetime,dir)
);
"""
]
self.write(CREATE_TABLE_SQLS)

34 changes: 34 additions & 0 deletions User.py
@@ -0,0 +1,34 @@
#!/usr/bin/python

import subprocess
def _ncheck_output(cmds):
if type(cmds )!=type([]): cmds = [cmds.split()]
if type(cmds[0])!=type([]): cmds = [cmds]
ncmds = len(cmds)
ps = ncmds*[None]
for i,cmd in enumerate(cmds):
if i==0:
ps[i] = subprocess.Popen(cmds[i], stdout=subprocess.PIPE, shell=False)
else:
ps[i] = subprocess.Popen(cmds[i], stdin=ps[i-1].stdout, stdout=subprocess.PIPE, shell=False)
for i in range(ncmds-1):
ps[i].stdout.close()
return ps[-1].communicate()[0]

class User:
def __init__(self):
self.data = {}
self.is_loaded = False
def get_user(self,uid):
if not self.data:
self.load_data()
return self.data.get(int(uid),"N/A-" + str(uid))
def load_data(self):
for line in _ncheck_output('ssh head1 getent passwd').split("\n"):
sline = line.strip()
if not sline or sline[0]=='#': continue
fields = sline.split(":")
self.data[int(fields[2])] = fields[0]
self.is_loaded = True


0 comments on commit c667454

Please sign in to comment.