str_list_push: remove unused function
[oweals/opkg-lede.git] / tests / regress / opk.py
1 import tarfile, os
2 import cfg
3
4 class Opk:
5         valid_control_fields = ["Package", "Version", "Depends", "Provides",\
6                         "Replaces", "Conflicts", "Suggests", "Recommends",\
7                         "Section", "Architecture", "Maintainer", "MD5Sum",\
8                         "Size", "InstalledSize", "Filename", "Source",\
9                         "Description", "OE", "Homepage", "Priority",\
10                         "Conffiles"]
11
12         def __init__(self, **control):
13                 for k in control.keys():
14                         if k not in self.valid_control_fields:
15                                 raise Exception("Invalid control field: "
16                                                 "{}".format(k))
17                 self.control = control
18
19         def write(self, tar_not_ar=False, data_files=None):
20                 filename = "{Package}_{Version}_{Architecture}.opk"\
21                                                 .format(**self.control)
22                 if os.path.exists(filename):
23                         os.unlink(filename)
24                 if os.path.exists("control"):
25                         os.unlink("control")
26                 if os.path.exists("control.tar.gz"):
27                         os.unlink("control.tar.gz")
28                 if os.path.exists("data.tar.gz"):
29                         os.unlink("data.tar.gz")
30
31                 f = open("control", "w")
32                 for k in self.control.keys():
33                         f.write("{}: {}\n".format(k, self.control[k]))
34                 f.close()
35
36                 tar = tarfile.open("control.tar.gz", "w:gz")
37                 tar.add("control")
38                 tar.close()
39
40                 tar = tarfile.open("data.tar.gz", "w:gz")
41                 if data_files:
42                         for df in data_files:
43                                 tar.add(df)
44                 tar.close()
45
46
47                 if tar_not_ar:
48                         tar = tarfile.open(filename, "w:gz")
49                         tar.add("control.tar.gz")
50                         tar.add("data.tar.gz")
51                         tar.close()
52                 else:
53                         os.system("ar q {} control.tar.gz data.tar.gz \
54                                         2>/dev/null".format(filename))
55
56                 os.unlink("control")
57                 os.unlink("control.tar.gz")
58                 os.unlink("data.tar.gz")
59
60 class OpkGroup:
61         def __init__(self):
62                 self.opk_list = []
63
64         def add(self, **control):
65                 self.opk_list.append(Opk(**control))
66         
67         def write_opk(self, tar_not_ar=False):
68                 for o in self.opk_list:
69                         o.write(tar_not_ar)
70
71         def write_list(self, filename="Packages"):
72                 f = open(filename, "w")
73                 for opk in self.opk_list:
74                         for k in opk.control.keys():
75                                 f.write("{}: {}\n".format(k, opk.control[k]))
76                         f.write("Filename: {Package}_{Version}_{Architecture}"
77                                         ".opk\n".format(**opk.control))
78                         f.write("\n")
79                 f.close()
80
81
82 def regress_init():
83         """
84         Initialisation and sanity checking.
85         """
86
87         if not os.access(cfg.opkgcl, os.X_OK):
88                 print("Cannot exec {}".format(cfg.opkgcl))
89                 exit(False)
90
91         os.chdir(cfg.opkdir)
92
93         os.system("rm -fr {}".format(cfg.offline_root))
94
95         os.makedirs("{}/usr/lib/opkg".format(cfg.offline_root))
96         os.makedirs("{}/etc/opkg".format(cfg.offline_root))
97
98         f = open("{}/etc/opkg/opkg.conf".format(cfg.offline_root), "w")
99         f.write("arch all 1\n")
100         f.write("src test file:{}\n".format(cfg.opkdir))
101         f.close()