From 3005978b51c628f929461009d06bed80d72fd63e Mon Sep 17 00:00:00 2001 From: M Pacer Date: Sun, 8 Apr 2018 15:56:48 -0700 Subject: [PATCH] format docstrings, remove prefix, create target_set, remove print remove unused imports --- procbuild/builder.py | 18 ++++++---- procbuild/listener.py | 81 +++++++++++++++++++----------------------- procbuild/submitter.py | 30 ++++++++-------- 3 files changed, 62 insertions(+), 67 deletions(-) diff --git a/procbuild/builder.py b/procbuild/builder.py index 3fdaf44..dbdef61 100644 --- a/procbuild/builder.py +++ b/procbuild/builder.py @@ -78,12 +78,18 @@ class BuildManager: Parameters ---------- - user: str; name of GitHub user - branch: str; name of git branch on user's PR - target: str; string representation of integer for which paper to build - cache: str; cache directory in which the build tools and final paper live - master_branch: str; git branch for build tools - log: function; logging function + user : str + name of GitHub user + branch : str + name of git branch on user's PR + target : str + string representation of integer for which paper to build + cache : str + cache directory in which the build tools and final paper live + master_branch : str, optional + git branch for build tools, defaults to "master" + log : function + logging function """ def __init__(self, diff --git a/procbuild/listener.py b/procbuild/listener.py index 8389a32..612eb9a 100644 --- a/procbuild/listener.py +++ b/procbuild/listener.py @@ -1,10 +1,8 @@ import json import io import codecs -import time import asyncio -from multiprocessing import Process from concurrent.futures import ThreadPoolExecutor import zmq @@ -20,48 +18,42 @@ class Listener: """ Listener class for defining zmq sockets and maintaining a build queue. - Attributes: + Attributes ------------ - ctx: zmq.asyncio.Context, the main context for the listener class - - prefix: str, the prefix listened for by zmq sockets - - socket: zmq.socket, the socket for listening to - - queue: asyncio.Queue, the queue for holding the builds - - dont_build: set, unique collection of PRs currently in self.queue + ctx : zmq.asyncio.Context + main context for the listener class + socket : zmq.socket + the socket for listening to + queue : asyncio.Queue + the queue for holding the builds + dont_build : set + unique collection of PRs currently in self.queue Note: Only modify self.dont_build within synchronous blocks. - """ - def __init__(self, prefix='build_queue'): - """ - Parameters: - ------------ - build_queue: str, the prefix that will be checked for by the zmq socket - """ + def __init__(self): self.ctx = Context.instance() - self.prefix = prefix + target_set = {'build_queue'} self.socket = self.ctx.socket(zmq.SUB) self.socket.connect(OUT) - self.socket.setsockopt(zmq.SUBSCRIBE, self.prefix.encode('utf-8')) + for target in target_set: + self.socket.setsockopt(zmq.SUBSCRIBE, target.encode('utf-8')) self.queue = asyncio.Queue() self.dont_build = set() async def listen(self): """Listener method, containing while loop for checking socket + """ while True: msg = await self.socket.recv_multipart() target, raw_payload = msg payload = json.loads(raw_payload.decode('utf-8')) - print('received', payload) paper_to_build = payload.get('build_paper', None) - if self.check_age_and_queue(paper_to_build): + if self.check_age(paper_to_build) or self.check_queue(paper_to_build): continue self.dont_build.add(paper_to_build) await self.queue.put(paper_to_build) @@ -69,9 +61,10 @@ async def listen(self): def check_age(self, nr): """Check the age of a PR's status_file based on its number. - Parameters: + Parameters ------------ - nr: int, the number of the PR in order of receipt + nr : int + the number of the PR in order of receipt """ age = file_age(status_file(nr)) min_wait = 0.5 @@ -84,9 +77,10 @@ def check_age(self, nr): def check_queue(self, nr): """Check whether the queue currently contains a build request for a PR. - Parameters: + Parameters ------------ - nr: int, the number of the PR to check + nr : int + the number of the PR to check """ in_queue = False if nr in self.dont_build: @@ -94,21 +88,13 @@ def check_queue(self, nr): in_queue = True return in_queue - def check_age_and_queue(self, nr): - """Check whether the PR is old enough or whether it is already in queue. - - Parameters: - ------------ - nr: int, the number of the PR to check - """ - return self.check_age(nr) or self.check_queue(nr) - def report_status(self, nr): """prints status notification from status_file for paper `nr` - Parameters: + Parameters ------------ - nr: int, the number of the PR to check + nr : int + the number of the PR to check """ with io.open(status_file(nr), 'r') as f: status = json.load(f)['status'] @@ -122,7 +108,8 @@ def report_status(self, nr): async def queue_builder(self, loop=None): """Manage queue and trigger builds, report results. - loop: asyncio.loop, the loop on which to be running these tasks + loop : asyncio.loop + the loop on which to be running these tasks """ while True: # await an item from the queue @@ -136,10 +123,12 @@ async def queue_builder(self, loop=None): def paper_log(self, nr, record): """Writes status to PR's log file - Parameters: - ------------ - nr: int, the number of the PR to check - record: dict, the dictionary content to be written to the log + Parameters + ------------ + nr : int + the number of the PR to check + record : dict + the dictionary content to be written to the log """ status_log = status_file(nr) with io.open(status_log, 'wb') as f: @@ -148,9 +137,10 @@ def paper_log(self, nr, record): def build_and_log(self, nr): """Builds paper for PR number and logs the resulting status - Parameters: + Parameters ------------ - nr: int, the number of the PR to check + nr : int + the number of the PR to check """ pr_info = get_pr_info() pr = pr_info[int(nr)] @@ -171,6 +161,7 @@ def build_and_log(self, nr): status = build_manager.build_paper() self.paper_log(nr, status) + if __name__ == "__main__": print('Listening for incoming messages...') diff --git a/procbuild/submitter.py b/procbuild/submitter.py index dc64ebb..2f70c98 100644 --- a/procbuild/submitter.py +++ b/procbuild/submitter.py @@ -7,22 +7,18 @@ class BuildRequestSubmitter: """Class for submitting build requests to zmq socket. - Attributes: + Parameters ------------ - - socket: zmq.socket, socket for pushing messages out - - verbose: bool, whether to print a message when submitting with builder + verbose : bool, optional + whether to print a message when submitting with builder (default is False) + + Attributes + ------------ + verbose + socket : zmq.socket, socket for pushing messages out """ def __init__(self, verbose=False): - """ - - Parameters: - ------------ - verbose: bool, whether to print a message when submitting with builder - defaults to False - """ ctx = zmq.Context() self.socket = ctx.socket(zmq.PUSH) self.socket.connect(IN) @@ -31,18 +27,20 @@ def __init__(self, verbose=False): def construct_message(self, nr): """Creates message to be sent on zmq socket. - Parameters: + Parameters ------------ - nr: int, the number of the PR in order of receipt + nr : int + the number of the PR in order of receipt """ return ['build_queue', json.dumps({'build_paper': nr})] def submit(self, nr): """Submits message to zmq socket - Parameters: + Parameters ------------ - nr: int, the number of the PR in order of receipt + nr : int + the number of the PR in order of receipt """ message = self.construct_message(nr) if self.verbose: