Skip to content
Snippets Groups Projects
Commit d412a16a authored by Seblu's avatar Seblu
Browse files

Rewrite command cat

parent 3d5606a9
No related branches found
No related tags found
No related merge requests found
...@@ -98,17 +98,13 @@ def c_build(parser, args): ...@@ -98,17 +98,13 @@ def c_build(parser, args):
def c_cat(parser, args): def c_cat(parser, args):
''' '''
Display image's file(s) Display image's file
''' '''
# looks if arguments is a file or image name # looks if arguments is a file or image name
if istools.isfile(args.image) and os.path.isfile(args.image): repoman = load_repositories(args)
pkg = PackageImage(istools.abspath(args.image)) img, repo = select_image(args.image, repoman)
elif PackageImage.check_image_name(args.image): for filename in args.file:
# get image package img.cat(filename)
repoman = load_repositories(args)
pkg = repoman.get(args.image, args.image_version)
for filename in args.files:
pkg.cat(filename)
def c_clean(parser, args): def c_clean(parser, args):
''' '''
...@@ -340,10 +336,8 @@ p_build.set_defaults(func=c_build) ...@@ -340,10 +336,8 @@ p_build.set_defaults(func=c_build)
# cat command parser # cat command parser
p_cat = subparsers.add_parser("cat", help=c_cat.__doc__.lower()) p_cat = subparsers.add_parser("cat", help=c_cat.__doc__.lower())
p_cat.add_argument("-v", "--image-version", type=int, default=None, p_cat.add_argument("image")
help="image version") p_cat.add_argument("file", nargs="+", help="file to cat (glob allowed)")
p_cat.add_argument("image", help="image (path or name)")
p_cat.add_argument("files", nargs="+", help="files to cat")
p_cat.set_defaults(func=c_cat) p_cat.set_defaults(func=c_cat)
# clean command parser # clean command parser
......
...@@ -17,7 +17,6 @@ import re ...@@ -17,7 +17,6 @@ import re
import cStringIO import cStringIO
import shutil import shutil
import gzip import gzip
import fnmatch
import installsystems.template as istemplate import installsystems.template as istemplate
import installsystems.tools as istools import installsystems.tools as istools
from installsystems.printer import * from installsystems.printer import *
...@@ -458,12 +457,9 @@ class PackageImage(Image): ...@@ -458,12 +457,9 @@ class PackageImage(Image):
''' '''
Display filename in the tarball Display filename in the tarball
''' '''
for filename in fnmatch.filter(self._tarball.getnames(), filename): for filename in self._tarball.getnames(glob_pattern=filename):
fd = self._tarball.extractfile(filename) arrow(filename)
if fd is not None: out(self._tarball.get_str(filename))
arrow(filename)
out(fd.read())
fd.close()
def run_parser(self, **kwargs): def run_parser(self, **kwargs):
''' '''
...@@ -484,7 +480,7 @@ class PackageImage(Image): ...@@ -484,7 +480,7 @@ class PackageImage(Image):
arrow("Run %s scripts" % directory) arrow("Run %s scripts" % directory)
arrowlevel(1) arrowlevel(1)
# get list of parser scripts # get list of parser scripts
l_scripts = self._tarball.getnames("%s/.*\.py" % directory) l_scripts = self._tarball.getnames(re_pattern="%s/.*\.py" % directory)
# order matter! # order matter!
l_scripts.sort() l_scripts.sort()
# run scripts # run scripts
......
...@@ -11,6 +11,7 @@ import time ...@@ -11,6 +11,7 @@ import time
import tarfile import tarfile
import StringIO import StringIO
import re import re
import fnmatch
class Tarball(tarfile.TarFile): class Tarball(tarfile.TarFile):
def add_str(self, name, content, ftype, mode): def add_str(self, name, content, ftype, mode):
...@@ -31,15 +32,19 @@ class Tarball(tarfile.TarFile): ...@@ -31,15 +32,19 @@ class Tarball(tarfile.TarFile):
Return a string from a filename in a tarball Return a string from a filename in a tarball
''' '''
ti = self.getmember(name) ti = self.getmember(name)
return self.extractfile(ti).read() fd = self.extractfile(ti)
return fd.read() if fd is not None else ""
def getnames(self, reg_pattern=None): def getnames(self, re_pattern=None, glob_pattern=None):
lorig = super(Tarball, self).getnames() lorig = super(Tarball, self).getnames()
if reg_pattern is None: # regexp matching
return lorig if re_pattern is not None:
else:
return [ tpname for tpname in lorig return [ tpname for tpname in lorig
if re.match(reg_pattern, tpname) ] if re.match(reg_pattern, tpname) ]
# globbing matching
if glob_pattern is not None:
return fnmatch.filter(lorig, glob_pattern)
return lorig
def size(self): def size(self):
''' '''
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment