Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate

Advertising

Untitled
Saturday, July 14th, 2007 at 2:59:15pm UTC 

  1. #!/usr/bin/python
  2. # Copyright 2007 Ryan Kavanagh
  3.  
  4. import sys
  5. import os
  6. import deb822
  7. import subprocess
  8. import string
  9. import gzip
  10. import shutil
  11.  
  12. ### START SETTING VARIABLES ###
  13. home = '/home/ryan/'
  14. incomming = home + 'pbuilder/incomming/'
  15. queued = home + 'pbuilder/queued/'
  16. processed = home + 'pbuilder/processed/'
  17. log_file = processed + 'log/'
  18. logs = '/var/www/blog.ryanak.ca/web/logs/'
  19. pid = home + 'pbuilder/.checkinc.pid'
  20. ### END CONFIG ###
  21.  
  22. def get_filenames(f):
  23.     """ Produces a list of the three required files to build a debian source
  24.     package, takes either a '.dsc' or a '.changes' as input
  25.  
  26.     """
  27.     base = os.path.dirname(f)
  28.     if f.endswith('.dsc'):
  29.         d = deb822.Dsc(file(f))
  30.     elif f.endswith('.changes'):
  31.         d = deb822.Changes(file(f))
  32.     return [os.path.join(base, elt['name']) for elt in d['Files']] + [f]
  33.  
  34. def number_directory(queued_directory):
  35.     """ Number the queued sumdirectories, takes the Queues directory as
  36.     input.
  37.  
  38.     """
  39.     # What are the existing queued directories?
  40.     existing_directories = os.listdir(queued_directory)
  41.     # Let's sort them so we can find the most recent one
  42.     existing_directories.sort()
  43.     # What was the number of the most recent directory?
  44.     latest_dir = existing_directories[-1].split('-')[0]
  45.     # Let's add one to the most recent directory's number so that the
  46.     # upcomming directory is processed afterwards.
  47.     return int(latest_dir) + 1
  48.  
  49. def check_and_create_queued(queued_directory, filename):
  50.     """ Checks 'queued_dir' to see if the subdirectory exists. Create the
  51.     subdirectory following format if it doesn't:
  52.     QueueNumber-package/
  53.     example: 3-aoeui/
  54.  
  55.     """
  56.     # Let's start off saying that the subdirectory doesn't exist, because
  57.     # because it shouldn't!
  58.     exists = False
  59.     # Let's get the name of the source package.
  60.     base = filename.split('_')[0]
  61.     # Let's assign the number to the subdirectory since we will be using it
  62.     # several times
  63.     numbered_directory = str(number_directory(queued_directory)) + '-' + base
  64.     # Let's iterate threw the existing subdirectories and check if the
  65.     # one we want to create already exists.
  66.     for item in os.listdir(queued_directory):
  67.         if item == numbered_directory:
  68.             exists = True
  69.     # If it doesn't exist, let's create it
  70.     if exists == False:
  71.         new_dir = queued + numbered_directory
  72.         os.mkdir(new_dir)
  73.  
  74. def determine_upcomming_build(queued_directory):
  75.     """ Checks 'queued_directory' and returns the upcomming file to be
  76.     built
  77.  
  78.     """
  79.     subdirectories = os.listdir(queued_directory)
  80.     subdirectories.sort()
  81.     upcomming_subdirectory = subdirectory[0]
  82.     # Let's return upcomming's .dsc
  83.     for file in os.listdir(upcomming_subdirectory):
  84.         if file.endswith('.dsc'):
  85.             upcomming_dsc = queued + subdirectory[0] + file
  86.             return upcomming_dsc
  87.  
  88. def check_if_we_are_building(pid):
  89.     """ Checks 'pid' to see if it exists. If it does, we are running
  90.     and return True.
  91.  
  92.     """
  93.     return os.access(pid, 1)
  94.  
  95. def done_building(subdirectory, distro):
  96.     """ Moves the subdirectory from queued to processed once built,
  97.     It also moves the build log to the webserver.
  98.  
  99.     """
  100.  
  101.     os.rename(log_file, log + log_file)
  102.     os.rename(subdirectory, processed + subdirectory)
  103.     result = home + 'pbuilder/' + distro + '/result/'
  104.     base = subdirectory.split('-', 1)[-1]
  105.     os.rename(result + base + '*', processed + subdirectory)
  106.     os.remove
  107.  
  108. def build_if_not_running(upcomming_build):
  109.     """ Build the upcomming dsc if we are not already running """
  110.     # Create the pid file
  111.     open(pid, 'a').close()
  112.     distro = get_distro(upcomming_build)
  113.     log_file +=
  114.     p = subprocess.Popen('pbuilder-' + distro + ' build' + dsc, stdout=subprocess.PIPE)
  115.     p.wait()
  116.     print p.stdout.readlines()
  117.  
  118. def get_distro(dsc):
  119.     "Runs sed on the .diff.gz to find what pbuilder to run."
  120.  
  121.     diff_filename = dsc.rsplit('.', 1)[0] + '.diff.gz'
  122.  
  123.     changes_file = diff_filename.gzip.open(file, 'r')
  124.     for line in changes_file.readlines():
  125.         if line.split(';')[0].split()[-1] == True:
  126.             return line.split(';')[0].split()[-1]
  127.  
  128.  
  129. # What files do we have in the incomming directory?
  130. incomming_files = os.listdir(incomming)
  131.  
  132. files_to_move_to_queue = []
  133. # Let's find the .dsc file, and create a list of files to move into
  134. # it's respective queued subdirectory
  135. for filename in incomming_files:
  136.     if filename.endswith('.dsc'):
  137.         files_to_move_to_queue = get_filenames(filename)
  138.  
  139. # We haven't done anything yet, so the subdirectory doesn't exist.
  140. exists = False
  141.  
  142. for file in files_to_move_to_queue:
  143.     # Move files to subdirectory in queued
  144.     incomming_file = incomming + file
  145.     # The directory doesn't exist yet, we don't think, but we'll check anyways
  146.     if exists != True:
  147.         check_and_create_queued(queued, file)
  148.         # Logic dictates that it now exists
  149.         exists = True
  150.  
  151.     # What are the current subdirectories
  152.     queued_dir_list = os.listdir(queued)
  153.     # What's the subdirectory? It will be queued_dir_list[-1]
  154.     queued_dir_list.sort()
  155.     # We will move the file from incomming to the following:
  156.     queued_file = queued + queued_dir_list[-1] + '/' + file
  157.     # And let's finally move the file
  158.     os.rename(incomming_file, queued_file)
  159.  
  160. #p = subprocess.Popen('pbuilder-' + distro + ' build' + dsc, stdout=subprocess.PIPE)
  161. #p.wait()
  162. #print p.stdout.readlines()

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will not expire by default. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right