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
=== modified file 'example.py'
--- example.py 2010-08-11 04:46:25 +0000
+++ example.py 2010-08-13 17:44:39 +0000
@@ -29,7 +29,8 @@
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")
3030
31# Configure the apt client31# Configure the apt client
32apt.set_architecture("amd64")32#apt.set_architecture("amd64")
33apt.set_architecture("i386")
3334
34apt.set_status("/var/lib/dpkg/status")35apt.set_status("/var/lib/dpkg/status")
3536
3637
=== modified file 'unwrapt/definitions/aptdef/__init__.py'
--- unwrapt/definitions/aptdef/__init__.py 2010-08-11 07:02:50 +0000
+++ unwrapt/definitions/aptdef/__init__.py 2010-08-13 17:44:39 +0000
@@ -112,7 +112,7 @@
112def to_filename(directory, url):112def to_filename(directory, url):
113 """113 """
114 Forms a full filename from a directory and url.114 Forms a full filename from a directory and url.
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
116 underscores, and appends it to directory.116 underscores, and appends it to directory.
117 """117 """
118 return os.path.join(directory, url.split("//")[1].replace("/", "_"))118 return os.path.join(directory, url.split("//")[1].replace("/", "_"))
@@ -226,13 +226,22 @@
226 """226 """
227 This is a missing docstring ZOMG!227 This is a missing docstring ZOMG!
228 """228 """
229
230 if download:
231 self.on_download_lists(reporthook)
232
233 # Read the newly-downloaded lists.
234 self.on_read_lists()
235
236
237 def on_download_lists(self, reporthook=None):
229 238
230 directory = os.path.join(self.download_directory, "lists")239 directory = os.path.join(self.download_directory, "lists")
231240
232 #TODO: This function obviously needs to be split up and modularized :)241 # If the download directory does not exist, create it
242 if not os.path.exists(directory):
243 os.makedirs(directory)
233244
234 # This is a list of files we downloaded and now need to parse
235 downloaded = []
236 for repo in self.__iter_repositories():245 for repo in self.__iter_repositories():
237246
238 # Build the strings247 # Build the strings
@@ -240,31 +249,31 @@
240 filename = to_filename(directory, url)249 filename = to_filename(directory, url)
241 display_name = "Repository => %s / %s" % (repo["dist"], repo["section"])250 display_name = "Repository => %s / %s" % (repo["dist"], repo["section"])
242251
243 # If the download directory does not exist, create it
244 if not os.path.exists(directory):
245 os.makedirs(directory)
246
247 # Download252 # Download
248 #TODO: pass proxy information and catch exceptions253 #TODO: pass proxy information and catch exceptions
249 #TODO: Support bz2 and unarchived Packages files254 #TODO: Support bz2 and unarchived Packages files
250 filename = "%s.gz" % filename255 filename = "%s.gz" % filename
251 if download:256 download_url("%s.gz" % url, filename, display_name, proxy=self.proxy["proxy"], username=self.proxy["user"], password=self.proxy["pass"])
252 download_url("%s.gz" % url, filename, display_name, proxy=self.proxy["proxy"], username=self.proxy["user"], password=self.proxy["pass"])257
253 downloaded.append((repo, filename))258
254 259 def on_read_lists(self):
255 #TODO: Improve this. For now we are just opening local files in 260
256 # unextracted format (what you find in /var/lib/apt/lists) since261 directory = os.path.join(self.download_directory, "lists")
257 # that's an easy way to do things. This won't open the gz files262
258 # that Unwrapt downloads however263 lists = []
259 else: # Files that are pre-downloaded264 for repo in self.__iter_repositories():
260 downloaded.append((repo, filename[:-3]))265
261 266 # Build the strings
267 url = to_url(repo, self.architecture, "Packages")
268 filename = to_filename(directory, url)
269 filename = "%s.gz" % filename # Works only if the index files are gz
270 lists.append((repo, filename))
262 271
263 self.packages = {}272 self.packages = {}
264 273
265 total = len(downloaded)274 total = len(lists)
266 # Now parse each file, extracting as necessary275 # Now parse each file, extracting as necessary
267 for i, value in enumerate(downloaded):276 for i, value in enumerate(lists):
268 repo, filename = value277 repo, filename = value
269278
270 # Display percent read 279 # Display percent read
@@ -272,17 +281,19 @@
272 sys.stdout.write("\rReading package lists... %3i%%" % frac)281 sys.stdout.write("\rReading package lists... %3i%%" % frac)
273 sys.stdout.flush()282 sys.stdout.flush()
274283
275 # Parse packages into dictionary284 # Attempt to open the package list.
276 try:285 try:
277 if filename.endswith(".gz"):286 if filename.endswith(".gz"):
278 f = gzip.open(filename, "rb")287 f = gzip.open(filename, "rb")
279 else:288 else:
280 f = open(filename, "rb") 289 f = open(filename, "rb")
281 290 except: #FIXME: specify exception.
282 self.__parse(repo, f)291 logging.error("\nPackage list does not exist: %s" % filename)
283 f.close()292 continue
284 except:293
285 logging.error("\nPackage list does not exist: %s" % filename)294 # Parse packages into dictionary
295 self.__parse(repo, f)
296 f.close()
286 297
287 #TODO: Insert items into database298 #TODO: Insert items into database
288299
@@ -290,6 +301,7 @@
290 sys.stdout.write("\n")301 sys.stdout.write("\n")
291 302
292 logging.info("%i packages available" % len(self.packages))303 logging.info("%i packages available" % len(self.packages))
304
293305
294 def __parse(self, repo, f):306 def __parse(self, repo, f):
295 """307 """
@@ -438,9 +450,9 @@
438 #TODO: This function obviously needs to be split up and modularized :)450 #TODO: This function obviously needs to be split up and modularized :)
439451
440 # First check if the package is installed already?452 # First check if the package is installed already?
441 if metadata["Package"] in self.status:453 status = self.on_get_package_status(metadata["Package"])
442 raise AttributeError, "Package already set to status: %s" % \454 if status != "not installed":
443 self.status[metadata["Package"]]["Status"]455 raise AttributeError, "Package already set to status: %s" % status
444 456
445 # Mark the package itself457 # Mark the package itself
446 if not dependency: metadata["Status"] = "to be downloaded"458 if not dependency: metadata["Status"] = "to be downloaded"
@@ -449,15 +461,10 @@
449461
450 logging.info("Finding dependencies for %s..." % metadata["Package"])462 logging.info("Finding dependencies for %s..." % metadata["Package"])
451463
452 # Build a string of the necessary sections we need464 depends = self.on_get_package_dependencies(metadata)
453 depends = []
454 for section in self.binary_dependencies:
455 if section in metadata:
456 depends.append(metadata[section])
457 depends = ", ".join(depends)
458 465
459 # Do the dependency calculations466 # Do the dependency calculations
460 for dep in depends.split(", "):467 for dep in depends:
461 468
462 # In case we have some ORs469 # In case we have some ORs
463 options = dep.split(" | ")470 options = dep.split(" | ")
@@ -478,7 +485,7 @@
478 # Test for compatible version just in case485 # Test for compatible version just in case
479 if len(details) > 1:486 if len(details) > 1:
480 comparison = details[1][1:] # strip the '('487 comparison = details[1][1:] # strip the '('
481 version = details [2][:-1] # strip the ')'488 version = details[2][:-1] # strip the ')'
482 489
483 satisfied = DpkgVersion(self.status[name]["Version"]).compare_string(comparison, version)490 satisfied = DpkgVersion(self.status[name]["Version"]).compare_string(comparison, version)
484 491
@@ -501,8 +508,19 @@
501 # Mark sub-dependencies as well508 # Mark sub-dependencies as well
502 if pkg:509 if pkg:
503 self.on_mark_package(pkg, dependency=True)510 self.on_mark_package(pkg, dependency=True)
504 511
505 512
513 def on_get_package_dependencies(self, metadata):
514
515 # Build a string of the necessary sections we need
516 depends = []
517 for section in self.binary_dependencies:
518 if section in metadata:
519 depends += metadata[section].split(", ")
520
521 return depends
522
523
506 def on_apply_changes(self):524 def on_apply_changes(self):
507 525
508 directory = os.path.join(self.download_directory, "packages")526 directory = os.path.join(self.download_directory, "packages")
@@ -628,9 +646,7 @@
628 # Call apt-get install with the packages646 # Call apt-get install with the packages
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"]
630 648
631 #FIXME: apt-get update will fail when installing on an offline machine.649 subprocess.call("apt-gcache gencaches", shell=True)
632 # `apt-cache gencaches` should be used.
633 subprocess.call("apt-get update", shell=True)
634 subprocess.call("apt-get -y install %s" % " ".join(packages), shell=True)650 subprocess.call("apt-get -y install %s" % " ".join(packages), shell=True)
635 651
636 652

Subscribers

People subscribed via source and target branches