diff --git a/adopt-queue.py b/adopt-queue.py index 602f3b4..95a1056 100644 --- a/adopt-queue.py +++ b/adopt-queue.py @@ -1,15 +1,17 @@ -import ConfigParser +import configparser import sqlite3 import os -import urllib2 +import urllib import sys +import re +import github class Adopt(): def __init__(self, argv): self.examplemode = False - print "Starting up." + print("Starting up.") self.args = sys.argv @@ -26,49 +28,56 @@ def __init__(self, argv): self.queuefile = open("exampleadopts/local-queue", 'r') else: if os.path.isfile("queue.list"): - print "existing file" + print("existing file") self.queuefile = open("queue.list", 'r') queuelines = self.queuefile.readlines() self.fileid = 1 if os.path.getsize("queue.list") == 0: - print "No projects in the queue to process." + print("No projects in the queue to process.") else: - print "Processing .adopt files..." + print("Processing .adopt files...") # Let's gather the lines of .adopts that will need to be removed lines_to_rem = [] for l in queuelines: - print "\t * " + str(l) + l = l.rstrip() + print("\t * " + str(l)) if self.examplemode == True: self.process_file("./exampleadopts/" + l) else: f = open("current-adopt.tmp", 'wb') - url = urllib2.urlopen(l) + url = urllib.request.urlopen(l) f.write(url.read()) f.close() - if self.process_file("current-adopt.tmp") == False: - lines_to_rem.append(l) + + u = urllib.parse.urlparse(l) + path = u.path.split("/") + if path[-1] == "ADOPTME.md": + self.process_github("current-adopt.tmp", path[-4],path[-3]) + else: + if self.process_file("current-adopt.tmp") == False: + lines_to_rem.append(l) self.fileid = self.fileid + 1 - print "...done." + print("...done.") # Now let's remove .adopts not required # This happens when the status has changed from 'no' to 'yes' f = open("queue.list","w") - print "Removing .adopts that are now maintained:" + print("Removing .adopts that are now maintained:") for line in queuelines: match = False for i in lines_to_rem: if line == i: - print "\t * " + str(line) + print("\t * " + str(line)) match = True if match == False: @@ -76,14 +85,14 @@ def __init__(self, argv): f.close() - print "...done." + print("...done.") self.queuefile.close() def setup_db(self): """Remove a pre-existing database and create a new database and schema.""" - print "Setting up the database..." + print("Setting up the database...") self.db = sqlite3.connect("db.sql") self.db.execute("CREATE TABLE projects (ID INT PRIMARY KEY NOT NULL, \ NAME TEXT NOT NULL, \ @@ -96,12 +105,12 @@ def setup_db(self): EMAIL TEXT NOT NULL \ )") - print "...done." + print("...done.") def process_file(self, f): """Process an individual .adopt file and add it to the database.""" - config = ConfigParser.ConfigParser() + config = configparser.ConfigParser() config.read(f.strip()) status = config.get('Project', 'maintained') @@ -116,22 +125,53 @@ def process_file(self, f): contact = config.get('Contact', 'name') email = config.get('Contact', 'email') - query = "INSERT INTO projects(ID, NAME, DESCRIPTION, CATEGORY, REPO, DISCUSSION, LANGUAGES, CONTACT, EMAIL) VALUES(" \ - + str(self.fileid) + ", " \ - + "'" + name + "', " \ - + "'" + description + "', " \ - + "'" + category + "', " \ - + "'" + repo + "', " \ - + "'" + discussion + "', " \ - + "'" + languages + "', " \ - + "'" + contact + "', " \ - + "'" + email + "')" - self.db.execute(query) - self.db.commit() + self.insert_project(name, description, category, repo, discussion, languages, contact, email) return True else: return False + def process_github(self, f, user, project): + """Process an ADOPTME.md file on GitHub""" + + for line in open(f, "r").readlines(): + match = re.search("Category:(.*)", line) + if match: + category = match.groups()[0].strip() + + match = re.search("Contact:(.*)<(.*)>", line) + if match: + contact = match.groups()[0].strip() + email = match.groups()[1].strip() + + g = github.Github() + + repository = g.get_repo(user + "/" + project) + + name = repository.name + description = repository.description + + github_base_url = "https://github.com/" + user + "/" + project + + repo = github_base_url + discussion = github_base_url + "/issues" + languages = repository.language + + self.insert_project(name, description, category, repo, discussion, languages, contact, email) + + def insert_project(self, name, description, category, repo, discussion, languages, contact, email): + query = "INSERT INTO projects(ID, NAME, DESCRIPTION, CATEGORY, REPO, DISCUSSION, LANGUAGES, CONTACT, EMAIL) VALUES(" \ + + str(self.fileid) + ", " \ + + "'" + name + "', " \ + + "'" + description + "', " \ + + "'" + category + "', " \ + + "'" + repo + "', " \ + + "'" + discussion + "', " \ + + "'" + languages + "', " \ + + "'" + contact + "', " \ + + "'" + email + "')" + self.db.execute(query) + self.db.commit() + if __name__ == '__main__': a = Adopt(sys.argv) diff --git a/website.py b/website.py index cbe4030..363d218 100644 --- a/website.py +++ b/website.py @@ -1,8 +1,8 @@ -import ConfigParser +import configparser import string import sqlite3 import cherrypy -import urllib2 +import urllib3 import os class AdoptSite(object): @@ -86,11 +86,9 @@ def add(self): def add_project(self, project): """Add the project to the queue.list.""" - print "FOOOO" - # Warning: no error checking f = open("current-adopt-add.tmp", 'wb') - url = urllib2.urlopen(project) + url = urllib3.urlopen(project) f.write(url.read()) f.close()