Archive for the ‘how-to’ Category.

A few Gentoo tips

Mostly archived here for my benefit for when I break something with a reckless emerge -uDNAv world update again. *vbg*

  1. To install a binary version when something major's borked (like when you can't get anything to compile, including gcc, because you broke gcc…)
    • Go to tinderbox.dev.gentoo.org and find the correct directory for your machine. (For example, my AMD64 server uses binaries in the tinderbox.dev.gentoo.org/default/linux/amd64/ directory.)
    • Add this to your /etc/make.conf (or /etc/portage/make.conf), all on one line: PORTAGE_BINHOST="http://tinderbox.dev.gentoo.org/default-linux/amd64/"

      (Use the directory you chose in the previous step, obviously.)

    • To install the binary from tinderbox, use emerge -G $package. So, to install a gcc binary, you would type: emerge -G gcc
    • More info available here.
  2. To find out what a USE flag does:
    • euse -i $flag
    • So to find out what the multilib flag does, you would type euse -i multilib
    • Euse is part of the gentoolkit package, so you'll need to emerge that if you haven't already (emerge -uDNav gentoolkit).
  3. One way to fix the dreaded C++ preprocessor "/lib/cpp" fails sanity check error:
    • Update your active gcc version with a binary from tinderbox, as explained above. To see which gcc version that's installed on your machine the system is using as the default, type gcc-config -l. (That's an L for list.)
    • Make sure that gcc has the cxx USE flag enabled (in your /etc/portage/package.use or make.conf), so that it will build with support for C++ and won't break when rebuilt. (Ask me how I learned that flag was needed. Yeah. *vbg*)
    • Rebuild gcc (emerge -uDNav gcc).
  4. Typing emerge --info --verbose $package will tell you the system info and all the USE flags a package was built with, so you can see if the flags you needed enabled/disabled are.
  5. Older snapshots of portage are available if you are trying to update an older system that hasn't been updated in a while.
  6. List of USE flags
  7. Gentoo cheat sheet

What's your favorite tip or trick when dealing with an "oh crap, what the hell did I do?" moment?

Default (initial) value in Django's admin for a choice field

I was trying to set the initial value for a choice field in the admin like this:

from django import forms
from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from author.models import Author
from serials.constants import STATUS_CHOICES

class AuthorAdminForm(forms.ModelForm):
    class Meta:
        model = Author

class AuthorAdmin(MPTTModelAdmin):
    form = AuthorAdminForm(initial={'status': STATUS_CHOICES[0][0]})
admin.site.register(Author, AuthorAdmin)

Logically, I should have been able to do it this way, I thought, at least according to the docs. But instead it was giving me this error: TypeError: issubclass() arg 1 must be a class. (Uh, ok.) After a bit of searching, I eventually found this comment, and realized I must be trying to instantiate a form that the admin would instantiate automatically, had already been instantiated, or something like that pharmacieviagra.com.

After some more searching, I found the answer on the way to do what I needed here:

from django import forms
from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from author.models import Author
from serials.constants import STATUS_CHOICES

class AuthorAdminForm(forms.ModelForm):
    class Meta:
        model = Author

    def __init__(self, *args, **kwargs):
        super(AuthorAdminForm, self).__init__(*args, **kwargs)
        self.initial['status'] = STATUS_CHOICES[0][0]
            
class AuthorAdmin(MPTTModelAdmin):
    form = AuthorAdminForm
admin.site.register(Author, AuthorAdmin)

Yay! Hope this helps someone else who has the same issue. :)

Django trick: using named admin urls in templates

To add a link to an admin page on the user-side of your django site, you can use the named url patterns. (Seems like a simple and fairly straightforward thing to want to do, but I frequently find that things are not so simply found in most open source docs, nor are the examples always useful. But my usual open source docs rant is for another day. *g*)

It took a bit of digging, but I finally found that the admin url pattern options are listed here. From that, I was able to figure out that if I wanted to link to the edit (change action) page (parameter) for an article (model) in the app article, the pattern was:

{{ app_label }}_{{ model_name }}_action [parameters]

The namespace for the admin is, of course, admin, so putting that together using the {% url %} template tag, you get:

<a href="{% url 'admin:article_article_change' article.pk %}">{% trans 'Edit' %}</a>

That correctly creates the relative url to /admin/article/article/2. Yay!

Be sure to load the admin template tags in your template, too, or it won't work:

{% load admin_urls %}

Et voilà! *vbg* Hope this saves someone else a bit of time.

(This works for Django 1.4, btw.)

Convert flac to mp3 using ffmpeg, retain ID3 tags

Finally found the info I needed in the ffmpeg docs to keep the ID3 tags when converting from flac to mp3:

for FILE in *.flac;
do
    ffmpeg -i "$FILE" -ab 320k -map_metadata 0 "${FILE%.*}.mp3";
done

ETA 8/27/2012: updated "-map_meta_data 0:0" command to "-map_metadata 0" for the new version of ffmpeg.

Happy Earth Day!

Of course, I think every day should be Earth Day. If you make just one change today to change your impact on the world around you, that'd be a wonderful thing. *vbg*

Try something simple, one step at a time:

  • Use reusable bags every time you go to the store–any store: grocery, book, craft, etc. Tip: after you've used your bags to haul things into the house, immediately take them back out to your car or put them back in something that's always with you so you'll have them the next time you shop and don't fall back on the excuse that you forgot to bring them with you. In no time at all, it'll be second nature to grab the bags before you enter a store. :)
  • Take your reusable cup with you and refill it rather than using disposable cups.
  • Bring your own name tag with you to conferences and meetings.
  • Replace as many lightbulbs in your house with CFLs as you can shop.
  • Pack your or your kids' (if you have 'em) lunches in a lunch box or reusable containers rather than paper bags.
  • Recycle everything you can, even the annoying hard plastic packaging that so many things are wrapped in.
  • Compost non-meat-based waste. (Seriously, if I can do it, anybody can. *vbg*)

What's your favorite tip to give others a start on making the world around them a better place?

You can make a difference! (Really, it's not that hard to do. *vbg*)