~dholbach/apps-brancher/1019300

« back to all changes in this revision

Viewing changes to brancher/tarball.py

  • Committer: Daniel Holbach
  • Date: 2012-07-08 13:20:49 UTC
  • Revision ID: daniel.holbach@canonical.com-20120708132049-e29lcka7kf6wj7bw
major refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from job import Action, LoggedActions
 
2
from . import defs
 
3
 
 
4
import os
 
5
import re
 
6
 
 
7
class Tarball(object):
 
8
    dsc_file = None
 
9
    def __init__(self, app_name, local_file, log):
 
10
        self.local_file = local_file
 
11
        self.log = log
 
12
        self.app_name = app_name
 
13
        self.extract_path = os.path.join(defs.get_path("tarballs"), 
 
14
                                      self.app_name)
 
15
        self.extract()
 
16
        self.find_dsc_file()
 
17
 
 
18
    def rename_tarball(self):
 
19
        if not self.local_file:
 
20
            return
 
21
        new_tarball = None
 
22
        if self.local_file.endswith(".tar_1.gz"):
 
23
            new_tarball = re.sub(".tar_1.gz", ".tar.gz", self.local_file)
 
24
        if self.local_file.endswith(".tgz"):
 
25
            new_tarball = re.sub(".tgz", ".tar.gz", self.local_file)
 
26
        if not new_tarball:
 
27
            return
 
28
        actions = LoggedActions()
 
29
        actions.add(Action(["mv", self.local_file, new_tarball], 
 
30
                            expected_retcode=0))
 
31
        self.log.add(logged_actions=actions)
 
32
        if not self.log.success:
 
33
            self.log.add(error="Renaming '%s' to '%s' failed." % \
 
34
                         (self.local_file, new_tarball))
 
35
        else:
 
36
            self.local_file = new_tarball
 
37
 
 
38
    def extract(self, log):
 
39
        if not self.local_file:
 
40
            return
 
41
        self.rename_tarball()
 
42
        if os.path.exists(self.extract_path):
 
43
            actions = LoggedActions()
 
44
            actions.add(Action(["rm", "-r", self.extract_path], 
 
45
                               expected_retcode=0))
 
46
        os.makedirs(self.extract_path)
 
47
        
 
48
        actions = LoggedActions()
 
49
        if self.local_file.endswith(".tar.gz"):
 
50
            cmd = ["tar", "xfz", self.files[0], "-C", self.extract_path]
 
51
        elif self.local_file.endswith(".tar"):
 
52
            cmd = ["tar", "xf", self.files[0], "-C", self.extract_path]
 
53
        elif self.local_file.endswith(".zip"):
 
54
            cmd = ["unzip", self.local_file, "-d", self.extract_path]
 
55
        else:
 
56
            return
 
57
        actions.add(Action(cmd, expected_retcode=0))
 
58
        self.log.add(logged_actions=actions)
 
59
        if not log.success:
 
60
            log.add(error="Unpacking of %s failed." % self.local_file)
 
61
            
 
62
    def find_dsc_file(self):
 
63
        for dirpath, dirnames, filenames in os.walk(self.extract_path):
 
64
            dsc_files = filter(lambda a: a.endswith(".dsc"), filenames)
 
65
            if dsc_files:
 
66
                self.dsc_file = os.path.join(dirpath, dsc_files[0])