Merge lp:~mac9416/unwrapt/modularization into lp:unwrapt
- modularization
- Merge into stable
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 |
Related bugs: |
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.
Commit message
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 | # |
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 | 29 | #apt.set_proxy({"http": "http://192.168.1.100:3128"}, "username", "password") | 29 | #apt.set_proxy({"http": "http://192.168.1.100:3128"}, "username", "password") |
6 | 30 | 30 | ||
7 | 31 | # Configure the apt client | 31 | # Configure the apt client |
9 | 32 | apt.set_architecture("amd64") | 32 | #apt.set_architecture("amd64") |
10 | 33 | apt.set_architecture("i386") | ||
11 | 33 | 34 | ||
12 | 34 | apt.set_status("/var/lib/dpkg/status") | 35 | apt.set_status("/var/lib/dpkg/status") |
13 | 35 | 36 | ||
14 | 36 | 37 | ||
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 | 112 | def to_filename(directory, url): | 112 | def to_filename(directory, url): |
20 | 113 | """ | 113 | """ |
21 | 114 | Forms a full filename from a directory and url. | 114 | Forms a full filename from a directory and url. |
23 | 115 | i.e. Strips the url of the protocol prefix, replacse all slashes with | 115 | i.e. Strips the url of the protocol prefix, replaces all slashes with |
24 | 116 | underscores, and appends it to directory. | 116 | underscores, and appends it to directory. |
25 | 117 | """ | 117 | """ |
26 | 118 | return os.path.join(directory, url.split("//")[1].replace("/", "_")) | 118 | return os.path.join(directory, url.split("//")[1].replace("/", "_")) |
27 | @@ -226,13 +226,22 @@ | |||
28 | 226 | """ | 226 | """ |
29 | 227 | This is a missing docstring ZOMG! | 227 | This is a missing docstring ZOMG! |
30 | 228 | """ | 228 | """ |
31 | 229 | |||
32 | 230 | if download: | ||
33 | 231 | self.on_download_lists(reporthook) | ||
34 | 232 | |||
35 | 233 | # Read the newly-downloaded lists. | ||
36 | 234 | self.on_read_lists() | ||
37 | 235 | |||
38 | 236 | |||
39 | 237 | def on_download_lists(self, reporthook=None): | ||
40 | 229 | 238 | ||
41 | 230 | directory = os.path.join(self.download_directory, "lists") | 239 | directory = os.path.join(self.download_directory, "lists") |
42 | 231 | 240 | ||
44 | 232 | #TODO: This function obviously needs to be split up and modularized :) | 241 | # If the download directory does not exist, create it |
45 | 242 | if not os.path.exists(directory): | ||
46 | 243 | os.makedirs(directory) | ||
47 | 233 | 244 | ||
48 | 234 | # This is a list of files we downloaded and now need to parse | ||
49 | 235 | downloaded = [] | ||
50 | 236 | for repo in self.__iter_repositories(): | 245 | for repo in self.__iter_repositories(): |
51 | 237 | 246 | ||
52 | 238 | # Build the strings | 247 | # Build the strings |
53 | @@ -240,31 +249,31 @@ | |||
54 | 240 | filename = to_filename(directory, url) | 249 | filename = to_filename(directory, url) |
55 | 241 | display_name = "Repository => %s / %s" % (repo["dist"], repo["section"]) | 250 | display_name = "Repository => %s / %s" % (repo["dist"], repo["section"]) |
56 | 242 | 251 | ||
57 | 243 | # If the download directory does not exist, create it | ||
58 | 244 | if not os.path.exists(directory): | ||
59 | 245 | os.makedirs(directory) | ||
60 | 246 | |||
61 | 247 | # Download | 252 | # Download |
62 | 248 | #TODO: pass proxy information and catch exceptions | 253 | #TODO: pass proxy information and catch exceptions |
63 | 249 | #TODO: Support bz2 and unarchived Packages files | 254 | #TODO: Support bz2 and unarchived Packages files |
64 | 250 | filename = "%s.gz" % filename | 255 | filename = "%s.gz" % filename |
76 | 251 | if download: | 256 | download_url("%s.gz" % url, filename, display_name, proxy=self.proxy["proxy"], username=self.proxy["user"], password=self.proxy["pass"]) |
77 | 252 | download_url("%s.gz" % url, filename, display_name, proxy=self.proxy["proxy"], username=self.proxy["user"], password=self.proxy["pass"]) | 257 | |
78 | 253 | downloaded.append((repo, filename)) | 258 | |
79 | 254 | 259 | def on_read_lists(self): | |
80 | 255 | #TODO: Improve this. For now we are just opening local files in | 260 | |
81 | 256 | # unextracted format (what you find in /var/lib/apt/lists) since | 261 | directory = os.path.join(self.download_directory, "lists") |
82 | 257 | # that's an easy way to do things. This won't open the gz files | 262 | |
83 | 258 | # that Unwrapt downloads however | 263 | lists = [] |
84 | 259 | else: # Files that are pre-downloaded | 264 | for repo in self.__iter_repositories(): |
85 | 260 | downloaded.append((repo, filename[:-3])) | 265 | |
86 | 261 | 266 | # Build the strings | |
87 | 267 | url = to_url(repo, self.architecture, "Packages") | ||
88 | 268 | filename = to_filename(directory, url) | ||
89 | 269 | filename = "%s.gz" % filename # Works only if the index files are gz | ||
90 | 270 | lists.append((repo, filename)) | ||
91 | 262 | 271 | ||
92 | 263 | self.packages = {} | 272 | self.packages = {} |
93 | 264 | 273 | ||
95 | 265 | total = len(downloaded) | 274 | total = len(lists) |
96 | 266 | # Now parse each file, extracting as necessary | 275 | # Now parse each file, extracting as necessary |
98 | 267 | for i, value in enumerate(downloaded): | 276 | for i, value in enumerate(lists): |
99 | 268 | repo, filename = value | 277 | repo, filename = value |
100 | 269 | 278 | ||
101 | 270 | # Display percent read | 279 | # Display percent read |
102 | @@ -272,17 +281,19 @@ | |||
103 | 272 | sys.stdout.write("\rReading package lists... %3i%%" % frac) | 281 | sys.stdout.write("\rReading package lists... %3i%%" % frac) |
104 | 273 | sys.stdout.flush() | 282 | sys.stdout.flush() |
105 | 274 | 283 | ||
107 | 275 | # Parse packages into dictionary | 284 | # Attempt to open the package list. |
108 | 276 | try: | 285 | try: |
109 | 277 | if filename.endswith(".gz"): | 286 | if filename.endswith(".gz"): |
110 | 278 | f = gzip.open(filename, "rb") | 287 | f = gzip.open(filename, "rb") |
111 | 279 | else: | 288 | else: |
118 | 280 | f = open(filename, "rb") | 289 | f = open(filename, "rb") |
119 | 281 | 290 | except: #FIXME: specify exception. | |
120 | 282 | self.__parse(repo, f) | 291 | logging.error("\nPackage list does not exist: %s" % filename) |
121 | 283 | f.close() | 292 | continue |
122 | 284 | except: | 293 | |
123 | 285 | logging.error("\nPackage list does not exist: %s" % filename) | 294 | # Parse packages into dictionary |
124 | 295 | self.__parse(repo, f) | ||
125 | 296 | f.close() | ||
126 | 286 | 297 | ||
127 | 287 | #TODO: Insert items into database | 298 | #TODO: Insert items into database |
128 | 288 | 299 | ||
129 | @@ -290,6 +301,7 @@ | |||
130 | 290 | sys.stdout.write("\n") | 301 | sys.stdout.write("\n") |
131 | 291 | 302 | ||
132 | 292 | logging.info("%i packages available" % len(self.packages)) | 303 | logging.info("%i packages available" % len(self.packages)) |
133 | 304 | |||
134 | 293 | 305 | ||
135 | 294 | def __parse(self, repo, f): | 306 | def __parse(self, repo, f): |
136 | 295 | """ | 307 | """ |
137 | @@ -438,9 +450,9 @@ | |||
138 | 438 | #TODO: This function obviously needs to be split up and modularized :) | 450 | #TODO: This function obviously needs to be split up and modularized :) |
139 | 439 | 451 | ||
140 | 440 | # First check if the package is installed already? | 452 | # First check if the package is installed already? |
144 | 441 | if metadata["Package"] in self.status: | 453 | status = self.on_get_package_status(metadata["Package"]) |
145 | 442 | raise AttributeError, "Package already set to status: %s" % \ | 454 | if status != "not installed": |
146 | 443 | self.status[metadata["Package"]]["Status"] | 455 | raise AttributeError, "Package already set to status: %s" % status |
147 | 444 | 456 | ||
148 | 445 | # Mark the package itself | 457 | # Mark the package itself |
149 | 446 | if not dependency: metadata["Status"] = "to be downloaded" | 458 | if not dependency: metadata["Status"] = "to be downloaded" |
150 | @@ -449,15 +461,10 @@ | |||
151 | 449 | 461 | ||
152 | 450 | logging.info("Finding dependencies for %s..." % metadata["Package"]) | 462 | logging.info("Finding dependencies for %s..." % metadata["Package"]) |
153 | 451 | 463 | ||
160 | 452 | # Build a string of the necessary sections we need | 464 | depends = self.on_get_package_dependencies(metadata) |
155 | 453 | depends = [] | ||
156 | 454 | for section in self.binary_dependencies: | ||
157 | 455 | if section in metadata: | ||
158 | 456 | depends.append(metadata[section]) | ||
159 | 457 | depends = ", ".join(depends) | ||
161 | 458 | 465 | ||
162 | 459 | # Do the dependency calculations | 466 | # Do the dependency calculations |
164 | 460 | for dep in depends.split(", "): | 467 | for dep in depends: |
165 | 461 | 468 | ||
166 | 462 | # In case we have some ORs | 469 | # In case we have some ORs |
167 | 463 | options = dep.split(" | ") | 470 | options = dep.split(" | ") |
168 | @@ -478,7 +485,7 @@ | |||
169 | 478 | # Test for compatible version just in case | 485 | # Test for compatible version just in case |
170 | 479 | if len(details) > 1: | 486 | if len(details) > 1: |
171 | 480 | comparison = details[1][1:] # strip the '(' | 487 | comparison = details[1][1:] # strip the '(' |
173 | 481 | version = details [2][:-1] # strip the ')' | 488 | version = details[2][:-1] # strip the ')' |
174 | 482 | 489 | ||
175 | 483 | satisfied = DpkgVersion(self.status[name]["Version"]).compare_string(comparison, version) | 490 | satisfied = DpkgVersion(self.status[name]["Version"]).compare_string(comparison, version) |
176 | 484 | 491 | ||
177 | @@ -501,8 +508,19 @@ | |||
178 | 501 | # Mark sub-dependencies as well | 508 | # Mark sub-dependencies as well |
179 | 502 | if pkg: | 509 | if pkg: |
180 | 503 | self.on_mark_package(pkg, dependency=True) | 510 | self.on_mark_package(pkg, dependency=True) |
183 | 504 | 511 | ||
184 | 505 | 512 | ||
185 | 513 | def on_get_package_dependencies(self, metadata): | ||
186 | 514 | |||
187 | 515 | # Build a string of the necessary sections we need | ||
188 | 516 | depends = [] | ||
189 | 517 | for section in self.binary_dependencies: | ||
190 | 518 | if section in metadata: | ||
191 | 519 | depends += metadata[section].split(", ") | ||
192 | 520 | |||
193 | 521 | return depends | ||
194 | 522 | |||
195 | 523 | |||
196 | 506 | def on_apply_changes(self): | 524 | def on_apply_changes(self): |
197 | 507 | 525 | ||
198 | 508 | directory = os.path.join(self.download_directory, "packages") | 526 | directory = os.path.join(self.download_directory, "packages") |
199 | @@ -628,9 +646,7 @@ | |||
200 | 628 | # Call apt-get install with the packages | 646 | # Call apt-get install with the packages |
201 | 629 | packages = [value["Package"] for key, value in self.status.items() if value["Status"] == "to be installed"] | 647 | packages = [value["Package"] for key, value in self.status.items() if value["Status"] == "to be installed"] |
202 | 630 | 648 | ||
206 | 631 | #FIXME: apt-get update will fail when installing on an offline machine. | 649 | subprocess.call("apt-gcache gencaches", shell=True) |
204 | 632 | # `apt-cache gencaches` should be used. | ||
205 | 633 | subprocess.call("apt-get update", shell=True) | ||
207 | 634 | subprocess.call("apt-get -y install %s" % " ".join(packages), shell=True) | 650 | subprocess.call("apt-get -y install %s" % " ".join(packages), shell=True) |
208 | 635 | 651 | ||
209 | 636 | 652 |
lp:unwrapt was out of date. I've updated it to lp:~excid3/keryx/unwrapt. Resubmitting.