Merge lp:~mac9416/unwrapt/modularization into lp:unwrapt

Proposed by mac9416
Status: Merged
Merged at revision: 53
Proposed branch: lp:~mac9416/unwrapt/modularization
Merge into: lp:unwrapt
Diff against target: 207 lines (+62/-45)
2 files modified
example.py (+2/-1)
unwrapt/definitions/aptdef/__init__.py (+60/-44)
To merge this branch: bzr merge lp:~mac9416/unwrapt/modularization
Reviewer Review Type Date Requested Status
Keryx Admins Pending
Review via email: mp+32609@code.launchpad.net

This proposal supersedes a proposal from 2010-08-13.

Description of the change

I tried to modularize a couple of things, and in the process hacked the existing code to pieces. There's still a lot of work to do, so feel free to do the same to mine. :-)

To post a comment you must log in.
Revision history for this message
mac9416 (mac9416) wrote : Posted in a previous version of this proposal

lp:unwrapt was out of date. I've updated it to lp:~excid3/keryx/unwrapt. Resubmitting.

Revision history for this message
mac9416 (mac9416) wrote :

Forgive the little change to example.py. My test machine is 32-bit. :-P

Revision history for this message
Chris Oliver (excid3) wrote : Posted in a previous version of this proposal

Yeah I did not update it because I wanted to make sure recent changes were tested in my personal branch before pushing. I think it was all good and I forgot to push. :P

Revision history for this message
Chris Oliver (excid3) : Posted in a previous version of this proposal
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'example.py'
2--- example.py 2010-08-11 04:46:25 +0000
3+++ example.py 2010-08-13 17:44:39 +0000
4@@ -29,7 +29,8 @@
5 #apt.set_proxy({"http": "http://192.168.1.100:3128"}, "username", "password")
6
7 # Configure the apt client
8-apt.set_architecture("amd64")
9+#apt.set_architecture("amd64")
10+apt.set_architecture("i386")
11
12 apt.set_status("/var/lib/dpkg/status")
13
14
15=== modified file 'unwrapt/definitions/aptdef/__init__.py'
16--- unwrapt/definitions/aptdef/__init__.py 2010-08-11 07:02:50 +0000
17+++ unwrapt/definitions/aptdef/__init__.py 2010-08-13 17:44:39 +0000
18@@ -112,7 +112,7 @@
19 def to_filename(directory, url):
20 """
21 Forms a full filename from a directory and url.
22- i.e. Strips the url of the protocol prefix, replacse all slashes with
23+ i.e. Strips the url of the protocol prefix, replaces all slashes with
24 underscores, and appends it to directory.
25 """
26 return os.path.join(directory, url.split("//")[1].replace("/", "_"))
27@@ -226,13 +226,22 @@
28 """
29 This is a missing docstring ZOMG!
30 """
31+
32+ if download:
33+ self.on_download_lists(reporthook)
34+
35+ # Read the newly-downloaded lists.
36+ self.on_read_lists()
37+
38+
39+ def on_download_lists(self, reporthook=None):
40
41 directory = os.path.join(self.download_directory, "lists")
42
43- #TODO: This function obviously needs to be split up and modularized :)
44+ # If the download directory does not exist, create it
45+ if not os.path.exists(directory):
46+ os.makedirs(directory)
47
48- # This is a list of files we downloaded and now need to parse
49- downloaded = []
50 for repo in self.__iter_repositories():
51
52 # Build the strings
53@@ -240,31 +249,31 @@
54 filename = to_filename(directory, url)
55 display_name = "Repository => %s / %s" % (repo["dist"], repo["section"])
56
57- # If the download directory does not exist, create it
58- if not os.path.exists(directory):
59- os.makedirs(directory)
60-
61 # Download
62 #TODO: pass proxy information and catch exceptions
63 #TODO: Support bz2 and unarchived Packages files
64 filename = "%s.gz" % filename
65- if download:
66- download_url("%s.gz" % url, filename, display_name, proxy=self.proxy["proxy"], username=self.proxy["user"], password=self.proxy["pass"])
67- downloaded.append((repo, filename))
68-
69- #TODO: Improve this. For now we are just opening local files in
70- # unextracted format (what you find in /var/lib/apt/lists) since
71- # that's an easy way to do things. This won't open the gz files
72- # that Unwrapt downloads however
73- else: # Files that are pre-downloaded
74- downloaded.append((repo, filename[:-3]))
75-
76+ download_url("%s.gz" % url, filename, display_name, proxy=self.proxy["proxy"], username=self.proxy["user"], password=self.proxy["pass"])
77+
78+
79+ def on_read_lists(self):
80+
81+ directory = os.path.join(self.download_directory, "lists")
82+
83+ lists = []
84+ for repo in self.__iter_repositories():
85+
86+ # Build the strings
87+ url = to_url(repo, self.architecture, "Packages")
88+ filename = to_filename(directory, url)
89+ filename = "%s.gz" % filename # Works only if the index files are gz
90+ lists.append((repo, filename))
91
92 self.packages = {}
93
94- total = len(downloaded)
95+ total = len(lists)
96 # Now parse each file, extracting as necessary
97- for i, value in enumerate(downloaded):
98+ for i, value in enumerate(lists):
99 repo, filename = value
100
101 # Display percent read
102@@ -272,17 +281,19 @@
103 sys.stdout.write("\rReading package lists... %3i%%" % frac)
104 sys.stdout.flush()
105
106- # Parse packages into dictionary
107+ # Attempt to open the package list.
108 try:
109 if filename.endswith(".gz"):
110 f = gzip.open(filename, "rb")
111 else:
112- f = open(filename, "rb")
113-
114- self.__parse(repo, f)
115- f.close()
116- except:
117- logging.error("\nPackage list does not exist: %s" % filename)
118+ f = open(filename, "rb")
119+ except: #FIXME: specify exception.
120+ logging.error("\nPackage list does not exist: %s" % filename)
121+ continue
122+
123+ # Parse packages into dictionary
124+ self.__parse(repo, f)
125+ f.close()
126
127 #TODO: Insert items into database
128
129@@ -290,6 +301,7 @@
130 sys.stdout.write("\n")
131
132 logging.info("%i packages available" % len(self.packages))
133+
134
135 def __parse(self, repo, f):
136 """
137@@ -438,9 +450,9 @@
138 #TODO: This function obviously needs to be split up and modularized :)
139
140 # First check if the package is installed already?
141- if metadata["Package"] in self.status:
142- raise AttributeError, "Package already set to status: %s" % \
143- self.status[metadata["Package"]]["Status"]
144+ status = self.on_get_package_status(metadata["Package"])
145+ if status != "not installed":
146+ raise AttributeError, "Package already set to status: %s" % status
147
148 # Mark the package itself
149 if not dependency: metadata["Status"] = "to be downloaded"
150@@ -449,15 +461,10 @@
151
152 logging.info("Finding dependencies for %s..." % metadata["Package"])
153
154- # Build a string of the necessary sections we need
155- depends = []
156- for section in self.binary_dependencies:
157- if section in metadata:
158- depends.append(metadata[section])
159- depends = ", ".join(depends)
160+ depends = self.on_get_package_dependencies(metadata)
161
162 # Do the dependency calculations
163- for dep in depends.split(", "):
164+ for dep in depends:
165
166 # In case we have some ORs
167 options = dep.split(" | ")
168@@ -478,7 +485,7 @@
169 # Test for compatible version just in case
170 if len(details) > 1:
171 comparison = details[1][1:] # strip the '('
172- version = details [2][:-1] # strip the ')'
173+ version = details[2][:-1] # strip the ')'
174
175 satisfied = DpkgVersion(self.status[name]["Version"]).compare_string(comparison, version)
176
177@@ -501,8 +508,19 @@
178 # Mark sub-dependencies as well
179 if pkg:
180 self.on_mark_package(pkg, dependency=True)
181-
182-
183+
184+
185+ def on_get_package_dependencies(self, metadata):
186+
187+ # Build a string of the necessary sections we need
188+ depends = []
189+ for section in self.binary_dependencies:
190+ if section in metadata:
191+ depends += metadata[section].split(", ")
192+
193+ return depends
194+
195+
196 def on_apply_changes(self):
197
198 directory = os.path.join(self.download_directory, "packages")
199@@ -628,9 +646,7 @@
200 # Call apt-get install with the packages
201 packages = [value["Package"] for key, value in self.status.items() if value["Status"] == "to be installed"]
202
203- #FIXME: apt-get update will fail when installing on an offline machine.
204- # `apt-cache gencaches` should be used.
205- subprocess.call("apt-get update", shell=True)
206+ subprocess.call("apt-gcache gencaches", shell=True)
207 subprocess.call("apt-get -y install %s" % " ".join(packages), shell=True)
208
209

Subscribers

People subscribed via source and target branches