# Path: common/utils.py
# def read_file(filename):
# def write_file(filename, text):
# def save_obj(filename, data):
# def load_obj(filename):
# def sha1(filename):
# def write_sha1(sha1, filename):
# def url_opener():
# def copy_file_from_url(url, out_file, expect_sha1 = None, max_retry = 4):
# def compress_file_data(out_filename, data, compress_type):
# def compress_file(out_filename, in_filename, compress_type):
# def uncompress_file(filename, compress_type):
# def _retry_on_eintr(func, *args):
# def safe_read(fd):
# def safe_write(fd, text):
# def print_traceback(*kwargs):
# def readline_backward(filename, buf_size=8192):
# 
# Path: common/qstat.py
# def running_jobs(job_base_name):
# 
# Path: common/db.py
# def user_db_prefix():
# def database_name(domain, family):
# def use_db(conn, domain, family, cursor_class = None):
# def create_conn(**kwargs):
#     def __init__(self, db_name):
#     def open(self):
#     def close(self):
# def connection(db_obj):
# class UserDb(object):


import sys
import os
import json
import hashlib
import time
import subprocess
import re
import collections
from common import utils
from common import qstat
from common import db
            new_state = 'running'
        except:
            utils.print_traceback("sge failure to exec job: %d" % r['job_id'], text)
            new_state = 'sge_fail'


        # Now we can really update the job state, see comment above.
        with db.connection(self):
            q = 'UPDATE job SET job_state=%s, sge_jobnumber=%s WHERE job_id=%s'
            self.cursor.execute(q, [ new_state, sge_job_nr, r['job_id'] ])

    def run_batch(self, nr_running, limit = 16):
        max_to_run = max(min(limit - nr_running, limit), 0)
        if max_to_run:
            for r in self.pending_request(max_to_run):
                print "starting:", r
                self.exec_request(r)

    def _exec_check(self, request):
        q = 'UPDATE job SET job_state="accounting" WHERE job_id=%s'
        self.cursor.execute(q, [ request['job_id'] ])

        q = 'INSERT into accounting (job_id, sge_jobnumber) VALUE (%s, %s)'
        self.cursor.execute(q, [ request['job_id'], request['sge_jobnumber'] ])

        self.conn.commit()

    def check_running(self):
        sge_running = qstat.running_jobs('')
        if sge_running:

# Path: common/utils.py
# def read_file(filename):
# def write_file(filename, text):
# def save_obj(filename, data):
# def load_obj(filename):
# def sha1(filename):
# def write_sha1(sha1, filename):
# def url_opener():
# def copy_file_from_url(url, out_file, expect_sha1 = None, max_retry = 4):
# def compress_file_data(out_filename, data, compress_type):
# def compress_file(out_filename, in_filename, compress_type):
# def uncompress_file(filename, compress_type):
# def _retry_on_eintr(func, *args):
# def safe_read(fd):
# def safe_write(fd, text):
# def print_traceback(*kwargs):
# def readline_backward(filename, buf_size=8192):
# 
# Path: common/qstat.py
# def running_jobs(job_base_name):
# 
# Path: common/db.py
# def user_db_prefix():
# def database_name(domain, family):
# def use_db(conn, domain, family, cursor_class = None):
# def create_conn(**kwargs):
#     def __init__(self, db_name):
#     def open(self):
#     def close(self):
# def connection(db_obj):
# class UserDb(object):


# Path: jobs/sge_jobs.py
import sys
import os
import json
import hashlib
import time
import subprocess
import re
import collections
from common import utils
from common import qstat
from common import db

            new_state = 'running'
        except:
            utils.print_traceback("sge failure to exec job: %d" % r['job_id'], text)
            new_state = 'sge_fail'


        # Now we can really update the job state, see comment above.
        with db.connection(self):
            q = 'UPDATE job SET job_state=%s, sge_jobnumber=%s WHERE job_id=%s'
            self.cursor.execute(q, [ new_state, sge_job_nr, r['job_id'] ])

    def run_batch(self, nr_running, limit = 16):
        max_to_run = max(min(limit - nr_running, limit), 0)
        if max_to_run:
            for r in self.pending_request(max_to_run):
                print "starting:", r
                self.exec_request(r)

    def _exec_check(self, request):
        q = 'UPDATE job SET job_state="accounting" WHERE job_id=%s'
        self.cursor.execute(q, [ request['job_id'] ])

        q = 'INSERT into accounting (job_id, sge_jobnumber) VALUE (%s, %s)'
        self.cursor.execute(q, [ request['job_id'], request['sge_jobnumber'] ])

        self.conn.commit()

    def check_running(self):
        sge_running = qstat.running_jobs('')
        if sge_running:

            with db.connection(self):