File: parsegz.py

package info (click to toggle)
aptoncd 0.1-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,572 kB
  • ctags: 544
  • sloc: python: 3,125; xml: 1,216; makefile: 125; sh: 6
file content (84 lines) | stat: -rw-r--r-- 3,184 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
######################################################
##
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published
#  by the Free Software Foundation; version 2 only.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
##
######################################################
## 
#  Project: AptOnCd
#  File: parsegz.py
#  Author: Laudeci Oliveira <laudeci@gmail.com>
#  Creation: 16/11/2006
#  Changed: 
#  Purpose: Class that will control repositoy downloads
##
######################################################
import gzip
import re
(PACKAGE, VERSION, FILENAME, SIZE, DEBFILENAME,REMOTEFILEPATH,SECTION) = range(7)
class ParseFile:
    def __init__(self,strParse):
        self.ParseString = strParse.split('\n')
        self.Lines = []

    def Parse(self):
        LineNumber = -1
        # this procedure will return a list in the following format
        # [Package, Version, Filename, Size]
        Package  = re.compile(r'(Package:\s)(.*)',re.IGNORECASE| re.MULTILINE| re.DOTALL)
        Version  = re.compile(r'(Version:\s)(.*)',re.IGNORECASE| re.MULTILINE| re.DOTALL)
        Filename = re.compile(r'(Filename:\s)(.*)',re.IGNORECASE| re.MULTILINE| re.DOTALL)
        Section = re.compile(r'(Section::\s)(.*)',re.IGNORECASE| re.MULTILINE| re.DOTALL)
        Size =re.compile(r'(Size:\s)(.*)',re.IGNORECASE| re.MULTILINE| re.DOTALL)
        
        if len(self.ParseString) <=0:
            return []
    
        for line in self.ParseString:
            #get package name
            match = Package.search(line)
            if match:
                #(PACKAGE, VERSION, FILENAME, SIZE, DEBFILENAME,REMOTEFILEPATH,ARCHITECTURE) = range(7)
                self.Lines.append([match.group(2),'','','','','',''])
                LineNumber +=1
    
            #get package version
            matchv = Version.search(line)
            if matchv:
                self.Lines[LineNumber][1] = matchv.group(2)
            
            matcha = Section.search(line)
            if matcha:
                self.Lines[LineNumber][6] = matcha.group(2)
                
            #get package file
            matchn = Filename.search(line)
            if matchn:
                fullFilePath = matchn.group(2)
                filename = fullFilePath.split('/')[-1] #filename without path
                filePath = fullFilePath.replace(filename,'')#PATH without filename
                
                self.Lines[LineNumber][2] = fullFilePath
                self.Lines[LineNumber][4] = filename 
                self.Lines[LineNumber][5] = filePath 
            
            #get package size
            matchs = Size.search(line)
            if matchs:
                self.Lines[LineNumber][3] = matchs.group(2)

        return self.Lines
    
def openGZ(obj):
    inF = gzip.GzipFile(obj, 'rb')
    s=inF.read()
    inF.close()
    return s