def got_file(ignored, filename, out_file, out_file_name): try: # If the requested file is the 'buildlog' compress it # using gzip before storing in Librarian. if file_sha1 == 'buildlog': out_file = open(out_file_name) filename += '.gz' out_file_name += '.gz' gz_file = gzip.GzipFile(out_file_name, mode='wb') copy_and_close(out_file, gz_file) os.remove(out_file_name.replace('.gz', '')) # Reopen the file, seek to its end position, count and seek # to beginning, ready for adding to the Librarian. out_file = open(out_file_name) out_file.seek(0, 2) bytes_written = out_file.tell() out_file.seek(0) library_file = getUtility(ILibraryFileAliasSet).create( filename, bytes_written, out_file, contentType=filenameToContentType(filename), restricted=private) finally: # Remove the temporary file. getFile() closes the file # object. os.remove(out_file_name) return library_file.id
def createUploadLog(self, content, filename=None): """Creates a file on the librarian for the upload log. :return: ILibraryFileAlias for the upload log file. """ # The given content is stored in the librarian, restricted as # necessary according to the targeted archive's privacy. The content # object's 'upload_log' attribute will point to the # `LibrarianFileAlias`. assert self.upload_log is None, ( "Upload log information already exists and cannot be overridden.") if filename is None: filename = 'upload_%s_log.txt' % self.id contentType = filenameToContentType(filename) if isinstance(content, unicode): content = content.encode('utf-8') file_size = len(content) file_content = StringIO(content) restricted = self.is_private return getUtility(ILibraryFileAliasSet).create( filename, file_size, file_content, contentType=contentType, restricted=restricted)
def upload_file(self, filepath): """Upload given file to Librarian. :param filepath: path to the file on disk that should be uploaded to Librarian. :raise: `LaunchpadScriptFailure` if the given filepath could not be opened. :return: the `LibraryFileAlias` record corresponding to the uploaded file. """ try: file = open(filepath) except IOError: raise LaunchpadScriptFailure('Could not open: %s' % filepath) flen = os.stat(filepath).st_size filename = os.path.basename(filepath) ftype = filenameToContentType(filename) library_file = getUtility(ILibraryFileAliasSet).create( filename, flen, file, contentType=ftype) return library_file
def process(self, build): """Process this upload, loading it into the database.""" self.logger.debug("Beginning processing.") for dirpath, _, filenames in scandir.walk(self.upload_path): if dirpath == self.upload_path: # All relevant files will be in a subdirectory. continue for livefs_file in sorted(filenames): livefs_path = os.path.join(dirpath, livefs_file) libraryfile = self.librarian.create( livefs_file, os.stat(livefs_path).st_size, open(livefs_path, "rb"), filenameToContentType(livefs_path), restricted=build.is_private) build.addFile(libraryfile) # The master verifies the status to confirm successful upload. self.logger.debug("Updating %s" % build.title) build.updateStatus(BuildStatus.FULLYBUILT) self.logger.debug("Finished upload.")