Fedora Linux Support Community & Resources Center
  #1  
Old 28th January 2009, 09:52 PM
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 557
Thumbs up Yum Repo to Smart PM channel convertor script.

NOTE: Now the file upload feature is working, I have added the correctly indented php file as an attachment on post no: 3.

The attached php script will create any missing smart channel files from the equivalent yum repo files. Any new smart channel files will be placed in a /new-smart-channels subdir of the yum repos directory.


Installation

Copy the attached php CLI script below to a new file, called create-smart-channels.php.

Change the permissions to something like:

# chown root:root create-smart-channels.php
# chmod 755 create-smart-channels.php

Then place the script in the directory that contains your yum repo files, usually /etc/yum.repos.d/

If you have set any open_basedir restrictions in /etc/php.ini, you will also need to add '/etc/smart/channels/:' to the search paths to allow create-smart-channels.php to read the contents of the /etc/smart/channels/ directory.


Warning:

Do not allow php access to the whole of the /etc directory. This could be a security issue. Whatever access restrictions are set with open_basedir (if any), you should override these in your httpd.conf file, to restrict apache's php module to only allow access to php files under your apache's document root. To do this, use something like this in your httpd.conf file:

<Directory />
Options None
AllowOverride None
Order Deny,Allow
Deny from all

# these are the only directories which the apache PHP module can
# have access to. This setting overrides the settings in the
# global php.ini file, which applies to the CLI version of PHP.

php_admin_value open_basedir '/path/to/htdocs/:/path/to/php-manual/'
php_admin_flag display_errors OFF

</Directory>


Running the script

create-smart-channels.php requires two arguments, and also accepts an optional third argument. The invocation line for the script is:
./create-smart-channels.php <release-version> <basearch> [d]

<release-version> is a required parameter for the Fedora version number.

<basearch> is a required parameter for the target OS platform.

[d] is an optional parameter to turn on debugging output.
You can find the release-version and basearch for your particular machine, by comparing a yum repo file, with the equivalent existing smart channel file.

If we compare the yum fedora.repo file and the smart fedora.channel file:


yum fedora.repo file (1st section):

[fedora]
name=Fedora $releasever - $basearch
failovermethod=priority
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/releases/$releasever/Everything/$basearch/os
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=fedora-$releasever&arch=$basearch
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora file:///etc/pki/rpm-gpg/RPM-GPG-KEY


smart fedora.channel file (1st section):

[core]
name=Fedora 8 - i386
baseurl=http://download.fedora.redhat.com/pub/fedora/linux/releases/8/Everything/i386/os/
type=rpm-md


You can see that on my machine, $releasever equates to 8, and $basearch is equal to i386.

So the command to run create-smart-channels.php on my machine would be:

[root@localhost yum.repos.d]# ./create-smart-channels.php 8 i386

The script would run silently, and create any missing smart channels in /etc/yum.repos.d/new-smart-channels/ . If there are no missing Smart channels, the script will terminate.

Adding the optional [d] argument would cause the script to output alot of debugging info. This is handy for knowing how the script works, and what it's doing.


Installing new smart channels

To install the newly created smart channel files, shut down smart package manager GUI if it is running. Copy the new smart channel files to /etc/smart/channels/, and restart smart GUI.

Smart should now detect the new channel files, and ask whether to include those channels. Answer 'Yes' to include the new channel files.

If you copy the livna channel files to /etc/smart/channels, you will be asked if you want to replace the existing Livna channels.

On Fedora 8, selecting yes does not appear to create any problems, AFAICS.


Resetting Smart to it's default installation settings

Smart keeps some configuration files in /var/lib/smart. Channel data is kept here as well. When a new installation of Smart PM is done, this directory is empty.

When Smart PM finishes, it saves it's cache and config files in /var/lib/smart/cache & /var/lib/smart/config.

To reset Smart to it's default installation values:

Quit Smart PM.

Make some backup copies of /var/lib/smart directory, to something like:
/var/lib/smart-prev

Remove the contents of:

/var/lib/smart/channels and /var/lib/smart/packages, and the cache and
config file. That will leave two empty subdirs, /channels & /packages.

Restarting Smart after removing the contents of /var/lib/smart will cause Smart to re-read the channel information in /etc/smart/channels/

Last edited by CD-RW; 8th November 2009 at 10:05 PM.
Reply With Quote
  #2  
Old 28th January 2009, 10:20 PM
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 557
NOTE: Now the file upload feature is working, I have added the correctly indented php file as an attachment on post no: 3.

Here is the complete script:

#!/usr/bin/php

<?php

/**
************************************************** *******************
* create-smart-channels.php - license
*
* Copyright (c) 2009 CD-RW, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @author CD-RW
* @copyright 2009 CD-RW - http://forums.fedoraforum.org/member.php?u=81568
* @link http://forums.fedoraforum.org/showthread.php?t=211956
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*
************************************************** *******************
*/


if ($argc < 3 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {

$message = <<<HDOC
Usage:

$argv[0] <release-version> <basearch> [d]

<release-version> is a required parameter for the Fedora version number.

<basearch> is a required parameter for the target OS platform.

[d] is an optional parameter to turn on debugging output.

HDOC;

echo $message;
exit("\n");
}


$RELEASE_VERSION = $argv[1];
$BASE_ARCH = $argv[2];


if (isset($argv[3]) && 'd' == $argv[3]) {
$DEBUG = true;
}
else {
$DEBUG = false;
}


# Path to yum repo dir - should be CWD.
$YUM_REPOS_DIR = './';

# Path to Smart Channels dir.
$SMART_CHANNELS_DIR = '/etc/smart/channels';


if ($handle = opendir($YUM_REPOS_DIR)) {
if ($DEBUG) {
echo "Directory handle: $handle\n\n";
echo "Contents of CWD:\n\n";
}

/* This is the correct way to loop over the directory. */
while (false !== ($dir_content = readdir($handle))) {
if ($DEBUG) {
echo "$dir_content\n";
}

# Filter the Yum repo filenames into an array.
# Does the filename end with .repo?
if (strpos($dir_content, '.repo')) {
$yum_repo_file[] = $dir_content;
}
}
closedir($handle);
}


# Sort the $yum_repo_file array into alphabetical order.
sort($yum_repo_file);



//------------------DEBUG code----------------------//
# Check the contents of $yum_repo_file[].
if ($DEBUG) {
echo "\nContents of \$yum_repo_file[] \n\n";
while (list($key, $value) = each($yum_repo_file)) {
echo "$key: $value \n";
}
}
//------------------DEBUG code----------------------//



# Convert the $yum_repo_file names
# to Smart channel names.
reset($yum_repo_file);


while (list($key, $value) = each($yum_repo_file)) {
$smart_channel_file = str_replace('.repo', '.channel', $yum_repo_file);
}



//------------------DEBUG code----------------------//
if ($DEBUG) {
reset($smart_channel_file);
echo "\nContents of \$smart_channel_file[] \n\n";
while (list($key, $value) = each($smart_channel_file)) {
echo "$key: $smart_channel_file[$key] \n";
}
}
//------------------DEBUG code----------------------//



# Don't create a new Smart Channel file for existing channels.

reset($smart_channel_file);

$no_missing_channels = true;

while (list($key, $value) = each($smart_channel_file)) {

# Is the Smart Channel file missing?
if (!is_file("$SMART_CHANNELS_DIR/$value")) {

$no_missing_channels = false;

# Build array of missing Smart channel files.
$missing_smart_channel_file[] = $value;

# Build array of missing Yum repo files.
$missing_yum_repo_file[] = str_replace('.channel', '.repo', $value);
}
}

if ($no_missing_channels) exit();


//------------------DEBUG code----------------------//
if ($DEBUG) {
reset($missing_smart_channel_file);

echo "\nContents of \$missing_smart_channel_file[] \n\n";
while (list($key, $value) = each($missing_smart_channel_file)) {
echo "$key: $missing_smart_channel_file[$key] \n";
}

reset($missing_yum_repo_file);

echo "\nContents of \$missing_yum_repo_file[] \n\n";
while (list($key, $value) = each($missing_yum_repo_file)) {
echo "$key: $missing_yum_repo_file[$key] \n";
}
echo "\n";
}
//------------------DEBUG code----------------------//



# Read the *contents* of each missing yum repo file
# into an array.

reset($missing_yum_repo_file);

while (list($key, $value) = each($missing_yum_repo_file)) {
$yum_repo_list[$value] = file($value);
}



//------------------DEBUG code----------------------//
# echo "\nContents of \$yum_repo_list[] \n\n";
# var_dump($yum_repo_list);
//------------------DEBUG code----------------------//



# Convert the contents of $yum_repo_list[]
# into Smart Package Manager format.

# Array of old text patterns to search for.
$yum_old_pattern = array(
'$releasever',
'$basearch',
"failovermethod=priority",
"#baseurl=",
"#mirrorlist=",
"enabled=1",
"enabled=0",
"gpgcheck=1",
"gpgcheck=0",
"gpgkey="
);

# New text patterns to replace old text patterns.
$smart_new_pattern = array(
"$RELEASE_VERSION",
"$BASE_ARCH",
"#failovermethod=priority",
"baseurl=",
"mirrorlist=",
"disabled=no",
"disabled=yes",
"#gpgcheck=1",
"#gpgcheck=0",
"#gpgkey="
);

# Loop over each repo file.
if ($DEBUG) {
echo "Contents of \$yum_repo_list[] & \$smart_channel_list[] \n";
}

while (list($key, $value) = each($yum_repo_list)) {
if ($DEBUG) {
echo "\n$key: $yum_repo_list[$key] \n";
}

# Build the smart channel list.
$sc_key = str_replace('.repo', '.channel', $key);
$smart_channel_list[$sc_key] = array();

# Loop over the lines of each yum repo file
# and convert into smart PM format.
while (list($line_num, $line_text) = each($yum_repo_list[$key])) {
if ($DEBUG) {
echo "\nYum repo: $line_num: $line_text";
}

$sc_text = str_replace($yum_old_pattern, $smart_new_pattern, $line_text);

if ($DEBUG) {
echo "Smart PM: $line_num: $sc_text";
}

# Modify the 'baseurl=' line if it's a Livna repository.
if (substr_count($key, 'livna')) {

# Skip any URL's of any type.
if (substr_count($sc_text, "\thttp://", 0)
|| substr_count($sc_text, "\tftp://", 0)) {
continue;
}

# tidy-up the 'baseurl=' parameter.
$res = substr_count($sc_text, "baseurl=\n", 0);

if (substr_count($sc_text, "baseurl=\n")) {

$new_baseurl = 'baseurl=';

# Get the next URL in the array,
# and use that for baseurl=.
$next_url = $yum_repo_list[$key][$line_num+1];

$res = str_replace($yum_old_pattern, $smart_new_pattern, $next_url);

$new_baseurl .= str_replace("\t", '', $res);

if ($DEBUG) {
echo "\$new_baseurl: $new_baseurl";
}

$smart_channel_list[$sc_key][$line_num]= $new_baseurl;
}
else {
$smart_channel_list[$sc_key][$line_num]= $sc_text;
}
}
else {
$smart_channel_list[$sc_key][$line_num]= $sc_text;
}
}
}


# Write each Smart channel file out to disk.

$NEW_SMART_CHANNELS_DIR = './new-smart-channels';

# Create the ./new-smart-channels directory
# if it doesn't already exist.

if (!is_dir($NEW_SMART_CHANNELS_DIR)) {
$res = mkdir($NEW_SMART_CHANNELS_DIR, 0755);
}


reset($smart_channel_list);

if ($DEBUG) {
echo "Contents of \$smart_channel_list[] \n";
}

while (list($key, $value) = each($smart_channel_list)) {
if ($DEBUG) {
echo "\n$key: $smart_channel_list[$key] \n";
}

# Create & open the new channel file for writing.
$file_handle = fopen("$NEW_SMART_CHANNELS_DIR/$key", "w");

# Write the Smart channel contents to the new file.
while (list($line_num, $line_text) = each($smart_channel_list[$key])) {
if ($DEBUG) {
echo "$line_num: $line_text";
}
fwrite($file_handle, $line_text);

# Output 'type=rpm-md' after the channel name.
if (substr_count($line_text, 'name=', 0)) {
fwrite($file_handle, "type=rpm-md \n");

if ($DEBUG) {
echo "type=rpm-md \n";
}
}
}

// Close the channel file.
fclose($file_handle);
}

?>

Last edited by CD-RW; 8th November 2009 at 10:01 PM.
Reply With Quote
  #3  
Old 6th November 2009, 12:35 AM
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 557
linuxkonqueror
Here is the actual php script. You will need to remove the .txt extension to run it.
Attached Files
File Type: txt create-smart-channels.php.txt (8.8 KB, 182 views)
Reply With Quote
  #4  
Old 30th July 2010, 12:59 PM
Sscheme Offline
Registered User
 
Join Date: Mar 2009
Posts: 18
linuxfedorafirefox
Re: Yum Repo to Smart PM channel convertor script.

Awesome script, thanx m8
Reply With Quote
  #5  
Old 30th July 2010, 07:14 PM
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 557
linuxfedorafirefox
Re: Yum Repo to Smart PM channel convertor script.

Thanks for the appreciation Sscheme - pleased you found it useful. It trips up when converting Yum repo files for rpmforge (IIRC) on Centos 5.5 I need to look into that, that's on a test machine where I installed Centos 5.5
Reply With Quote
  #6  
Old 27th January 2013, 09:34 PM
guitarplayer Offline
Registered User
 
Join Date: Jan 2011
Posts: 8
linuxfirefox
Re: Yum Repo to Smart PM channel convertor script.

Hi,
I just wanted to say Thank you for the script. It still works. I used it to convert RHEL 6.3 .repo files today with no issues.
Best regards,
gp
Reply With Quote
  #7  
Old 16th March 2013, 11:32 PM
CD-RW Offline
Registered User
 
Join Date: Nov 2006
Posts: 557
linuxfirefox
Re: Yum Repo to Smart PM channel convertor script.

Thanks for the appreciation GP. I'm still on Centos 5.8 for now. I had a go at installing 6.3 a while back, but I might have issues with the motherboard on my PC being to old for Centos 6.x
Reply With Quote
Reply

Tags
channel, convertor, repo, script, smart, yum

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Avi to DVD convertor for Fedora icymatrix Using Fedora 6 30th September 2009 06:39 AM
USB to com convertor Beralus Hardware & Laptops 1 15th July 2009 01:32 PM
Failed to install Fedora 8 Core channel on Smart (package manager) mhtrinh Using Fedora 4 20th April 2009 07:00 PM
SWF to MPEG or AVI convertor kp_forum Using Fedora 2 7th April 2008 04:40 PM
.doc to pdf convertor mailnicu Using Fedora 3 3rd April 2006 12:24 PM


Current GMT-time: 13:56 (Monday, 24-11-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
lAlfas del Pi - Eysines - Embalse Photos