All pastes #619193 Raw Edit

Untitled

public text v1 · immutable
·published 2007-07-14 14:59 UTC
rendered paste body
#!/usr/bin/python
# Copyright 2007 Ryan Kavanagh

import sys
import os
import deb822
import subprocess
import string
import gzip
import shutil

### START SETTING VARIABLES ###
home = '/home/ryan/'
incomming = home + 'pbuilder/incomming/'
queued = home + 'pbuilder/queued/'
processed = home + 'pbuilder/processed/'
log_file = processed + 'log/'
logs = '/var/www/blog.ryanak.ca/web/logs/'
pid = home + 'pbuilder/.checkinc.pid'
### END CONFIG ###

def get_filenames(f):
    """ Produces a list of the three required files to build a debian source
    package, takes either a '.dsc' or a '.changes' as input

    """
    base = os.path.dirname(f)
    if f.endswith('.dsc'):
        d = deb822.Dsc(file(f))
    elif f.endswith('.changes'):
        d = deb822.Changes(file(f))
    return [os.path.join(base, elt['name']) for elt in d['Files']] + [f]

def number_directory(queued_directory):
    """ Number the queued sumdirectories, takes the Queues directory as
    input.

    """
    # What are the existing queued directories?
    existing_directories = os.listdir(queued_directory)
    # Let's sort them so we can find the most recent one
    existing_directories.sort()
    # What was the number of the most recent directory?
    latest_dir = existing_directories[-1].split('-')[0]
    # Let's add one to the most recent directory's number so that the
    # upcomming directory is processed afterwards.
    return int(latest_dir) + 1

def check_and_create_queued(queued_directory, filename):
    """ Checks 'queued_dir' to see if the subdirectory exists. Create the
    subdirectory following format if it doesn't:
    QueueNumber-package/
    example: 3-aoeui/

    """
    # Let's start off saying that the subdirectory doesn't exist, because
    # because it shouldn't!
    exists = False
    # Let's get the name of the source package.
    base = filename.split('_')[0]
    # Let's assign the number to the subdirectory since we will be using it
    # several times
    numbered_directory = str(number_directory(queued_directory)) + '-' + base
    # Let's iterate threw the existing subdirectories and check if the
    # one we want to create already exists.
    for item in os.listdir(queued_directory):
        if item == numbered_directory:
            exists = True
    # If it doesn't exist, let's create it
    if exists == False:
        new_dir = queued + numbered_directory
        os.mkdir(new_dir)

def determine_upcomming_build(queued_directory):
    """ Checks 'queued_directory' and returns the upcomming file to be
    built

    """
    subdirectories = os.listdir(queued_directory)
    subdirectories.sort()
    upcomming_subdirectory = subdirectory[0]
    # Let's return upcomming's .dsc
    for file in os.listdir(upcomming_subdirectory):
        if file.endswith('.dsc'):
            upcomming_dsc = queued + subdirectory[0] + file
            return upcomming_dsc

def check_if_we_are_building(pid):
    """ Checks 'pid' to see if it exists. If it does, we are running
    and return True.

    """
    return os.access(pid, 1)

def done_building(subdirectory, distro):
    """ Moves the subdirectory from queued to processed once built,
    It also moves the build log to the webserver.

    """

    os.rename(log_file, log + log_file)
    os.rename(subdirectory, processed + subdirectory)
    result = home + 'pbuilder/' + distro + '/result/'
    base = subdirectory.split('-', 1)[-1]
    os.rename(result + base + '*', processed + subdirectory)
    os.remove

def build_if_not_running(upcomming_build):
    """ Build the upcomming dsc if we are not already running """
    # Create the pid file
    open(pid, 'a').close()
    distro = get_distro(upcomming_build)
    log_file +=
    p = subprocess.Popen('pbuilder-' + distro + ' build' + dsc, stdout=subprocess.PIPE)
    p.wait()
    print p.stdout.readlines()

def get_distro(dsc):
    "Runs sed on the .diff.gz to find what pbuilder to run."

    diff_filename = dsc.rsplit('.', 1)[0] + '.diff.gz'

    changes_file = diff_filename.gzip.open(file, 'r')
    for line in changes_file.readlines():
        if line.split(';')[0].split()[-1] == True:
            return line.split(';')[0].split()[-1]


# What files do we have in the incomming directory?
incomming_files = os.listdir(incomming)

files_to_move_to_queue = []
# Let's find the .dsc file, and create a list of files to move into
# it's respective queued subdirectory
for filename in incomming_files:
    if filename.endswith('.dsc'):
        files_to_move_to_queue = get_filenames(filename)

# We haven't done anything yet, so the subdirectory doesn't exist.
exists = False

for file in files_to_move_to_queue:
    # Move files to subdirectory in queued
    incomming_file = incomming + file
    # The directory doesn't exist yet, we don't think, but we'll check anyways
    if exists != True:
        check_and_create_queued(queued, file)
        # Logic dictates that it now exists
        exists = True

    # What are the current subdirectories
    queued_dir_list = os.listdir(queued)
    # What's the subdirectory? It will be queued_dir_list[-1]
    queued_dir_list.sort()
    # We will move the file from incomming to the following:
    queued_file = queued + queued_dir_list[-1] + '/' + file
    # And let's finally move the file
    os.rename(incomming_file, queued_file)

#p = subprocess.Popen('pbuilder-' + distro + ' build' + dsc, stdout=subprocess.PIPE)
#p.wait()
#print p.stdout.readlines()