/** * Creates or updates a package in the database * * @return object package object */ public function createPackage($project_name = null, $repo_id = null, $repo_name = null, $arch_name = null, $package_name = null, $file_name = null, $repo_arch_name = null) { if ($project_name && $repo_name && $arch_name && $package_name && $file_name) { // get fill package info via OBS API try { $extinfo = $this->api->getPackageWithFullInformation($project_name, $repo_name, $arch_name, $package_name, $file_name); } catch (RuntimeException $e) { $this->log(' [EXCEPTION] ' . $e->getMessage()); } } else { throw new RuntimeException('Not enough parameters to gather full package information'); return; } // get a com_meego_package instance $package = $this->getPackageByFileName($file_name, $repo_id); if ($package && $repo_id && $extinfo) { if (!$package->guid) { $package->repository = $repo_id; $package->filename = $extinfo->filename; } // deb or rpm $package->type = substr($package->filename, strrpos($package->filename, '.') + 1); $package->size = $extinfo->size; $package->name = $extinfo->name; $package->title = $extinfo->title; $package->parent = $package_name; $package->version = $extinfo->version; $summary_max_length = 100; if (array_key_exists('summary_max_length', $this->config)) { $summary_max_length = $this->config['summary_max_length']; } if (strlen($extinfo->summary)) { $package->summary = $this->generateAbstract($extinfo->summary, $summary_max_length); } else { $package->summary = $this->generateAbstract($extinfo->description, $summary_max_length); } $package->description = $extinfo->description; // if the package is a source package then the downloadurl is slightly different // also change the title a bit if ($repo_arch_name == 'src') { $package->title = $package->title . '-src'; } if ($repo_arch_name == 'armv7el') { // fix the inconsistency between the published repo and the API $repo_arch_name = 'armv7l'; if ($package->type == 'deb') { $repo_arch_name = $extinfo->arch; } } // direct download url $_uri = str_replace(':', ':/', $project_name) . '/' . str_replace(':', ':/', $repo_name) . '/' . $repo_arch_name . '/' . $file_name; $package->downloadurl = $this->download_repo_protocol . '://' . $this->download_repo_host . '/' . $_uri; // @todo $package->bugtracker = '* TODO *'; // for some info we need a special xray try { switch ($package->type) { case 'rpm': $xray = new RpmXray($this->download_repo_protocol, $this->download_repo_host, $_uri); break; case 'deb': $xray = new DebXray($this->download_repo_protocol, $this->download_repo_host, $_uri, $this->config['wget'], $this->config['wget_options'], $this->debug); break; default: throw new RuntimeException("Unknown file extension: " . $package->type . "(should be rpm or deb)."); } } catch (RuntimeException $e) { $this->log(' [EXCEPTION] ' . $e->getMessage()); // if there was a problem during xray (with code 999) // then it almost certainly means that the package no longer exists in the repository // so if the package exists in our database then remove it if ($package->guid && $e->getCode() == 999) { // if package deletion is OK then return immediately $result = $this->deletePackage($package, $project_name); } } if (is_object($xray)) { $package->license = $this->getLicense($xray->license, ''); $package->homepageurl = $xray->url; $package->category = $this->getCategory($xray->group); if (isset($xray->title) && strlen($xray->title)) { $package->title = $xray->title; } } // call the parent if ($package->guid) { $this->log(' update: ' . $package->filename . ' (title: ' . $package->title . ', guid: ' . $package->guid . ')'); $package->metadata->hidden = false; $package->update(); } else { if ($package->create()) { $this->log(' create: ' . $package->filename . ' (title: ' . $package->title . ', guid: ' . $package->guid . ')'); } else { $this->log(' failed to create: ' . $package->filename . ' (title: ' . $package->title . ')'); } } try { // if attachment creation failed then use the original OBS link $package->installfileurl = $this->api->getInstallFileURL($project_name, $repo_name, $arch_name, $package_name, $file_name); // get the file and store it locally // $fp might be a stream, or string if wget is in use $fp = $this->api->http->get_as_stream($this->api->getRelativeInstallPath($project_name, $repo_name, $arch_name, $package_name, $file_name)); if ($fp) { $attachment = $package->create_attachment($package_name . "_install.ymp", $package_name . "_install.ymp", "text/x-suse-ymp"); if ($attachment) { $blob = new midgard_blob($attachment); $handler = $blob->get_handler('wb'); if ($handler) { if (!$this->config['wget']) { $ymp = stream_get_contents($fp); } else { $ymp = $fp; } $origymp = $ymp; // replace name with the package name $ymp = self::replace_name_with_packagename($ymp, $package->name); if (!strlen($ymp)) { $this->log(' attempt to update package name in: ' . $attachment->name . ' would result in 0 byte long file; rollback, location: blobs/' . $attachment->location); $ymp = $origymp; } // write the attachment to the file system fwrite($handler, $ymp); if (!$this->config['wget']) { fclose($fp); } // close the attachment's handler fclose($handler); $attachment->update(); $this->log(' attachment created: ' . $attachment->name . ' (location: blobs/' . $attachment->location . ')'); } else { $this->log('Could not create attachment'); } } else { // could not create attachment, maybe we have it already $attachments = $package->list_attachments(); foreach ($attachments as $attachment) { if ($attachment->name == $package_name . "_install.ymp") { $blob = new midgard_blob($attachment); $handler = $blob->get_handler('rb+'); if ($handler) { $content = $blob->read_content(); $ymp = $content; if (strlen($content)) { $ymp = self::replace_name_with_packagename($content, $package->name); } if (!strlen($ymp)) { $this->log(' attempt to update package name in: ' . $attachment->name . ' would result in 0 byte long file; rollback, location: blobs/' . $attachment->location); $ymp = $content; } fwrite($handler, $ymp); fclose($handler); } else { $this->log(' failed to update attachment: ' . $attachment->name . ', location: blobs/' . $attachment->location); } break; } } } // set the install url field to the local attachment if (is_object($attachment)) { // write a local relative URL for the install file $package->installfileurl = '/mgd:attachment/' . $attachment->guid . '/' . $attachment->name; } // update because of the installfileurl stuff $package->update(); } } catch (RuntimeException $e) { $this->log(' [EXCEPTION] ' . $e->getMessage()); } // get the roles and create the necessary role objects $roles = $this->api->getPackageMeta($project_name, $package_name); foreach ($roles as $role => $userids) { foreach ($userids as $userid) { $this->log(' create role: ' . $userid . ' = ' . $role . ' (' . $package->guid . ')'); $this->createRole($package->guid, $userid, $role); } } // add relations by calling the parent class $this->addRelations($extinfo, $package); } return $package; }
/** * Opens the attachment for file IO, the semantics match the original * mgd_open_attachment call. Returns a filehandle that can be used with the * usual PHP file functions if successful, the handle has to be closed with * the close() method when you no longer need it, don't let it fall over * the end of the script. * * <b>Important Note:</b> It is important to use the close() member function of * this class to close the file handle, not just fclose(). Otherwise, the upgrade * notification switches will fail. * * @param string $mode The mode which should be used to open the attachment, same as * the mode parameter of the PHP fopen call. This defaults to write access (see * mgd_open_attachmentl for details). * @return resource A file handle to the attachment if successful, false on failure. */ function open() { if (!$this->id) { debug_add('Cannot open a non-persistent attachment..', MIDCOM_LOG_WARN); debug_print_r('Object state:', $this); return false; } if ($this->_open_handle !== null) { debug_add("Warning, the Attachment {$this->id} already had an open file handle, we close it implicitly.", MIDCOM_LOG_WARN); @fclose($this->_open_handle); $this->_open_handle = null; } switch (func_num_args()) { case 0: $mode = 'default'; $this->_open_write_mode = true; $blob = new midgard_blob($this->__object); $handle = $blob->get_handler(); break; case 1: $mode = func_get_arg(0); $this->_open_write_mode = $mode[0] != 'r'; /* WARNING, read mode not supported by midgard_blob! */ $blob = new midgard_blob($this->__object); $handle = @fopen($blob->get_path(), $mode); break; default: trigger_error('midcom_db_attachment takes either zero or one arguments.', E_USER_ERROR); // This should exit. } if (!$handle) { debug_add("Failed to open attachment with mode {$mode}, last Midgard error was: " . midcom_connection::get_error_string(), MIDCOM_LOG_WARN); } $this->_open_handle = $handle; return $handle; }
/** * Goes through a release specified by a URL that points to a Release file * * @param string OBS project name, e.g. home:feri * @param string optional; specify a concrete package to be imported * @param boolean optional; if true then only cleanup will be performed on the local database * otherwise full import happens * */ public function go($release_file_url = null, $specific_package_name = null, $cleanonly = false) { // get release information from the relase file switch ($this->protocol) { case 'http': case 'https': $content = $this->http->get($release_file_url); break; case 'file': # todo break; } $release = $this->parseReleaseFile($content); if (!array_key_exists('Suite', $release)) { throw new RuntimeException('The Release files does not specify a suite.' . $packages_file_url); return null; } // generate name and tile from release information $project_name = strtolower($release['Origin'] . '-' . $release['Suite'] . '-' . $release['Label']); $project_title = ucwords($release['Origin'] . ' ' . $release['Suite'] . ' ' . $release['Label']); // check if the project is already recorded in our database // this returns a com_meego_project object $project = $this->getProject($project_name); // set properties $project->name = $project_name; $project->title = $project_title; $project->description = "Debian repository from " . ucfirst($release['Origin']); if (!$cleanonly) { if ($project->guid) { $log = 'Update project record: ' . $project->name; $project->update(); } else { $log = 'Create project record: ' . $project->name; $project->create(); } $this->log($log . ' (' . $project->title . ', ' . $project->description . ')'); } if ($project->id) { $this->log("\nComponents in {$project->name}:"); // get all components (ie repositories in the database) this Suite contains $repositories = explode(' ', $release['Components']); // iterate through each and every published repository // and dig out the packages foreach ($repositories as $repo_name) { $this->log("\n -> " . $repo_name); $architectures = explode(' ', $release['Architectures']); // get all available architectures within this repository foreach ($architectures as $arch_name) { $this->log("\n -> " . $arch_name); // get a com_meego_repository object $repo = $this->getRepository($repo_name, $arch_name, $project->id); $repo_title = ucfirst($release['Suite']) . ' ' . ucfirst($release['Label']) . ' ' . ucfirst($repo_name) . ' (for ' . $arch_name . ')'; // fill in properties of the repo object $repo->name = strtolower($release['Suite'] . '_' . $release['Label'] . '_' . $repo_name); $repo->title = $repo_title; $repo->arch = $arch_name; $repo->project = $project->id; $repo->os = $release['Origin']; $repo->osversion = $release['Suite']; $repo->osgroup = $release['Label']; $repo->osux = $release['ux']; if (!$cleanonly) { if ($repo->guid) { $log = ' update: '; $repo->update(); } else { $log = ' create: '; $repo->create(); } $this->log($log . $repo->name . ' (id: ' . $repo->id . '; ' . $repo->os . ' ' . $repo->osversion . ', ' . $repo->osgroup . ', ' . $repo->osux . ')'); } $fulllist = array(); // determine the url of the binary Packages file $packages_file_url = preg_replace('|Release$|', '', $release_file_url) . '/' . $repo_name . '/binary-' . $arch_name . '/Packages'; // get an array of all available packages // this array is a special one, so study the method for details $packages = $this->parsePackagesFile($packages_file_url); if (!count($packages)) { continue; } foreach ($packages as $package_name => $versions) { // iterate through each versions foreach ($versions as $package_version => $details) { $this->log("\n -> package #" . ++$this->package_counter . ': ' . $package_name . ' ' . $package_version); if ($cleanonly) { // only cleanup is requested so we can go to the next package continue; } // creates or updates a package in the database $package = $this->createPackage($project->name, $repo->id, $repo_name, $arch_name, $details); if (!$package) { // we got no package object, usually because we had to delete // an existing one from database, so go to next binary package continue; } $fulllist[] = $package->filename; try { $image_names = array_filter($this->api->getPackageSourceFiles($project->name, $package_name), function ($name) { $retval = false; $_icon_marker = 'icon.png'; $_screenshot_marker = 'screenshot.png'; if (strpos($name, $_icon_marker) === strlen($name) - strlen($_icon_marker) || strpos($name, $_screenshot_marker) === strlen($name) - strlen($_screenshot_marker)) { $retval = true; } return $retval; }); } catch (RuntimeException $e) { $this->log(' [EXCEPTION] ' . $e->getMessage()); } foreach ($image_names as $name) { try { $fp = $this->api->getPackageSourceFile($project->name, $package_name, $name); } catch (RuntimeException $e) { $this->log(' [EXCEPTION] ' . $e->getMessage()); } if ($fp) { $attachment = $package->create_attachment($name, $name, "image/png"); if ($attachment) { $blob = new midgard_blob($attachment); $handler = $blob->get_handler('wb'); fwrite($handler, stream_get_contents($fp)); fclose($fp); fclose($handler); $attachment->update(); } } } } } // now cleanup all the packages from our database // that are not part of this OBS repository $this->cleanPackages($repo, $fulllist); } } } // if $project->id }