Skip to content
Browse files

Rewrote slackware's convert_package_list to use awk

  • Loading branch information...
1 parent 501e23f commit 0d7e49870974d5a93aa960f4eecd24162d8e2535 noryb009 committed
Showing with 22 additions and 63 deletions.
  1. +22 −63 distro/slackware/convert_package_list
View
85 distro/slackware/convert_package_list
@@ -1,66 +1,25 @@
-#!/usr/bin/python
+#!/bin/sh
# usage: convert_package_list $package_list
-from sys import argv
-import re
-
-# the regular expression used to parse lines
-LINE_REGEX = re.compile("(.+):(.*)", re.S)
-
-class Package(object):
- def __init__(self, name):
- self.name = name
- self.description = None
-
- def __str__(self):
- return "%(name)s|%(version)s|%(revision)s|%(location)s/%(file_name)s|%(description)s" % { "name" : self.name,
- "version" : self.version,
- "revision" : self.revision,
- "location" : self.location,
- "file_name" : self.file_name,
- "description" : self.description
- }
-
-# read the file contents
-f = open(argv[1], "rb")
-contents = f.read().split("\n")
-f.close()
-
-# initialize the "package" variable
-package = None
-
-# parse the file
-for line in contents:
- match = LINE_REGEX.match(line)
- if match is None:
- continue
-
- # get the field name and value
- field = match.group(1)
- value = match.group(2).strip()
-
- if "PACKAGE NAME" == field:
- # split the file name and reverse it
- package_name_split = value.split("-")
- package_name_split.reverse()
-
- name = package_name_split[3:]
- name.reverse()
- package = Package("-".join(name))
-
- package.version = package_name_split[2]
- package.revision = package_name_split[0][:package_name_split[0].index(".")]
- package.file_name = value
-
- if "PACKAGE LOCATION" == field:
- package.location = value[2:]
-
- if package is not None:
- if package.name == field:
- if package.description is None:
- if " " in value:
- package.description = value[2 + value.index(" "):-1]
- else:
- package.description = value
- print package
+awk -F ":" '
+ function trimB(s) {sub(/^ */,"",s); return s} # trim beginning of string
+ function printPkg() {
+ namelen=split(filename,namearr,"-"); # split name by "-"
+ if(namelen>3) { # if enough fields
+ sub(/\..*/,"",namearr[namelen]); pkgrev=namearr[namelen] # remove ".t(gz/xz/...)" from pkgrev
+ pkgver=namearr[namelen-2] # get version
+ for(i=1;i<namelen-2;i++){pkgname=pkgname"-"namearr[i]} sub(/^-/, "", pkgname) # for each remaining field, add to name, then remove beginning "-"
+ printf("%s|%s|%s|%s/%s|%s|\n",pkgname,pkgver,pkgrev,pkgpath,filename,pkgdesc) # print
+ }
+ pkgname=""; pkgver=""; pkgrev=""; pkgpath=""; filename=""; pkgdesc=""; # clear vars
+ }
+ {
+ if($1 == "PACKAGE NAME"){filename=trimB($2);} # trim
+ if($1 == "PACKAGE LOCATION"){pkgpath=trimB($2); sub(/^\.\//,"",pkgpath)} # trim, remove "./"
+ if($1 == "PACKAGE DESCRIPTION"){getline pkgdesc; sub(/[^:]*:/,"",pkgdesc); pkgdesc=trimB(pkgdesc)} # remove pkgname:, trim
+ if($0 == "" && filename != "") {printPkg()} # empty line, print current package
+ }END{
+ if(filename != "") {printPkg()} # end of file, print package if one left
+ }
+' "$1"

0 comments on commit 0d7e498

Please sign in to comment.
Something went wrong with that request. Please try again.