| 1 | ======================= |
|---|
| 2 | Architecture overview |
|---|
| 3 | ======================= |
|---|
| 4 | |
|---|
| 5 | If you want to add a check or frontend to pkgcore-checks you should |
|---|
| 6 | probably read this thing first. If you only want to add a check you |
|---|
| 7 | should only read the first section ("For everyone"). After that is |
|---|
| 8 | extra documentation on adding feed types and some of the internals. |
|---|
| 9 | |
|---|
| 10 | For everyone |
|---|
| 11 | ============ |
|---|
| 12 | |
|---|
| 13 | Addons |
|---|
| 14 | ------ |
|---|
| 15 | |
|---|
| 16 | Most interesting objects are addons. The interface is defined in |
|---|
| 17 | base.py. They can have "dependencies" on other addons (by class): all |
|---|
| 18 | addons referenced by the required_addons class attribute of an active |
|---|
| 19 | addon are also active (and this applies recursively, of course). |
|---|
| 20 | |
|---|
| 21 | Before they are instantiated the class (or static) methods |
|---|
| 22 | mangle_option_parser and check_values are used to modify the optparse |
|---|
| 23 | process. At this point all available checks (and their dependencies) |
|---|
| 24 | are active addons. After the commandline is parsed all checks that end |
|---|
| 25 | up being active after parsing the commandline are active addons, and |
|---|
| 26 | those are instantiated. They receive the optparse values instance |
|---|
| 27 | followed by the (instantiated) addons they depend on as positional |
|---|
| 28 | arguments to __init__. |
|---|
| 29 | |
|---|
| 30 | (The reason for the two "phases" here (optparse mangling before |
|---|
| 31 | instantiation) is we want the addons to influence the way options are |
|---|
| 32 | parsed while we cannot instantiate them before options are parsed |
|---|
| 33 | (since we want to pass the values object and since we only want to |
|---|
| 34 | instantiate the ones that end up "selected" according to commandline |
|---|
| 35 | settings). The reason for the dependency system used here is to |
|---|
| 36 | provide a place to put options and state shared between multiple |
|---|
| 37 | checkers (without putting everything on a single global object).) |
|---|
| 38 | |
|---|
| 39 | Checkers, feeds |
|---|
| 40 | --------------- |
|---|
| 41 | |
|---|
| 42 | What a checker actually *is* is not well defined at the moment, but |
|---|
| 43 | everything subclassing base.Template and setting a feed_type attribute |
|---|
| 44 | is definitely a checker. Template subclasses Addon, so they are all |
|---|
| 45 | also addons. Their feed_type attribute should be set to one of the |
|---|
| 46 | feed types defined in base.py. Their feed method will be called with |
|---|
| 47 | an iterator producing values of a type depending on their feed_type. |
|---|
| 48 | Currently available are versioned_feed which feeds single package |
|---|
| 49 | objects and package_feed, category_feed and repository_feed which |
|---|
| 50 | produce sequences of packages. |
|---|
| 51 | |
|---|
| 52 | Whatever your feed type is, the first thing you should do with |
|---|
| 53 | everything you get out of the feed is "yield" it. The "feeder" and all |
|---|
| 54 | the checks are chained together, each yielding objects to the next. So |
|---|
| 55 | none of them should modify the data passed to them, and all of them |
|---|
| 56 | should yield *everything* to the next checker. |
|---|
| 57 | |
|---|
| 58 | The second argument to their feed method is a Reporter instance (again |
|---|
| 59 | defined in base.py) to pass Report instances on to. |
|---|
| 60 | |
|---|
| 61 | Scope |
|---|
| 62 | ----- |
|---|
| 63 | |
|---|
| 64 | An extra feature available to the feeders is their "scope" attribute. |
|---|
| 65 | This is somewhat similar to their feed_type. The difference is roughly |
|---|
| 66 | that feed_type must match *exactly* and scope indicates a *minimum* |
|---|
| 67 | requirement. This is mainly used by the transforms, but for certain |
|---|
| 68 | checks it is also useful to get "fed" single versions per iteration |
|---|
| 69 | but only if the check is run on the entire repository. Using a |
|---|
| 70 | repository feed would have the same effect of only running your check |
|---|
| 71 | if the entire repository is being checked, but requires building a |
|---|
| 72 | huge sequence containing all packages in memory before your check |
|---|
| 73 | runs. So if you can operate on fewer packages at a time, use a |
|---|
| 74 | "smaller" feed and scope. |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | Checker discovery |
|---|
| 78 | ----------------- |
|---|
| 79 | |
|---|
| 80 | Checkers are picked up by using pkgcore's plugin mechanism. For |
|---|
| 81 | writing a simple custom checker the easiest thing to do is putting the |
|---|
| 82 | entire thing in a single file including the pkgcore_plugins |
|---|
| 83 | registration dictionary. See core_checks.py for what such a dictionary |
|---|
| 84 | looks like. Then put it in a directory called pkgcore_checks/plugins/ |
|---|
| 85 | on your PYTHONPATH. So if you have a system-wide pkgcore-checks |
|---|
| 86 | installation you can put your own plugin in |
|---|
| 87 | ~/lib/python/pkgcore_checks/plugins/mycheck.py and run |
|---|
| 88 | PYTHONPATH=$HOME/lib/python pcheck ... to use the check (without |
|---|
| 89 | having to install a local copy of pkgcore-checks and putting the check |
|---|
| 90 | inside it). |
|---|
| 91 | |
|---|
| 92 | For those who need more feed types |
|---|
| 93 | ================================== |
|---|
| 94 | |
|---|
| 95 | Transforms |
|---|
| 96 | ---------- |
|---|
| 97 | |
|---|
| 98 | Usually the checks should run in a single "pipeline": looping over the |
|---|
| 99 | packages once uses various caches (not just inside pkgcore-checks but |
|---|
| 100 | also the os disk cache) more efficiently. This is accomplished by |
|---|
| 101 | applying "transforms" to the package iterator which change the feed |
|---|
| 102 | type. They are very similar to checks, but they do not yield the same |
|---|
| 103 | thing they receive. Most (currently all) of them are defined in |
|---|
| 104 | feeds.py. |
|---|
| 105 | |
|---|
| 106 | As you can see the way they set their input is a bit different from a |
|---|
| 107 | check. Because some simple operations like "Yield the first thing of |
|---|
| 108 | every value handed in" can be used for more than one "transformation" |
|---|
| 109 | they have more than one source and target feed type. For each of these |
|---|
| 110 | they can set their required scope and an integer indicating the "cost" |
|---|
| 111 | of this operation. These are currently set to mostly random values, |
|---|
| 112 | but the idea is they will allow the plugger to do a better job once |
|---|
| 113 | the number of feed types, sources and transforms grows. |
|---|
| 114 | |
|---|
| 115 | There is no way to change the scope. The scope is assumed to be |
|---|
| 116 | constant for the entire pipeline. |
|---|
| 117 | |
|---|
| 118 | Performance pitfalls |
|---|
| 119 | -------------------- |
|---|
| 120 | |
|---|
| 121 | As indicated earlier the checks should run in a single pipeline. This |
|---|
| 122 | pipeline is really a *line*: it is not possible to "fork" the iterator |
|---|
| 123 | without using potentially unlimited temporary storage. This is a |
|---|
| 124 | deficiency of the way iterators are used here. This makes it very |
|---|
| 125 | important that there are transforms available from all possible feeds |
|---|
| 126 | *and* back. |
|---|
| 127 | |
|---|
| 128 | An example: if the only available source is one generating a |
|---|
| 129 | versioned_feed (single package objects), there are transforms from |
|---|
| 130 | that to an ebuild_feed producing ebuild source lines and a |
|---|
| 131 | package_feed producing sequences of package objects for all versions |
|---|
| 132 | in a package, and checks of all those feed types are active, then the |
|---|
| 133 | entire repository will be looped over twice: once for the |
|---|
| 134 | versioned_feed checkers and either the package_feed or ebuild_feed |
|---|
| 135 | checkers, once for the remaining feed type. To avoid the second loop |
|---|
| 136 | register a transform back from package_feed and ebuild_feed to |
|---|
| 137 | versioned_feed. |
|---|
| 138 | |
|---|
| 139 | Sources |
|---|
| 140 | ------- |
|---|
| 141 | |
|---|
| 142 | Currently there is only one source, defined in feeds.py. |
|---|
| 143 | |
|---|
| 144 | Registration |
|---|
| 145 | ------------ |
|---|
| 146 | |
|---|
| 147 | Transforms are discovered the same way as checks are: the pkgcore |
|---|
| 148 | plugin system. See core_checks.py. The single available feed is |
|---|
| 149 | currently hardcoded. This will probably change in the future, but |
|---|
| 150 | exactly how remains to be seen. |
|---|
| 151 | |
|---|
| 152 | For pkgcore-checks internals hackers |
|---|
| 153 | ==================================== |
|---|
| 154 | |
|---|
| 155 | Commandline frontend |
|---|
| 156 | -------------------- |
|---|
| 157 | |
|---|
| 158 | The frontend code uses pkgcore's commandline utilities module and |
|---|
| 159 | lives in pcheck.py. It is pretty straightforward, although how control |
|---|
| 160 | flows through this module is not obvious without knowing how pkgcore's |
|---|
| 161 | commandline utils are used: |
|---|
| 162 | |
|---|
| 163 | - pkgcore's commandline glue instantiates the OptionParser |
|---|
| 164 | - it pulls up all available checks and transforms through the plugin system |
|---|
| 165 | - grab all addon dependencies too |
|---|
| 166 | - give them a chance to mangle the parser |
|---|
| 167 | - the commandline glue parses options, triggering various optparse |
|---|
| 168 | callbacks (options with a callback action and check_values, which |
|---|
| 169 | calls check_values on all addon classes). |
|---|
| 170 | - if option parsing succeeded the commandline glue calls main |
|---|
| 171 | - main instantiates all active addons and sources |
|---|
| 172 | - the autoplugger builds one or more pipelines |
|---|
| 173 | - main runs the pipelines |
|---|
| 174 | |
|---|
| 175 | Autoplugger |
|---|
| 176 | ----------- |
|---|
| 177 | |
|---|
| 178 | The autoplugger gets handed a bunch of "sink", transform and source |
|---|
| 179 | instances and builds pipelines from them. It is a hack that relies on |
|---|
| 180 | a fair amount of brute force to do its job, but so far it has been |
|---|
| 181 | sufficient. It is still a moving target, so its design (if it has one) |
|---|
| 182 | is not documented here. Use the source and do not forget about the |
|---|
| 183 | tests (it does not have as many as it should but there are a bunch, |
|---|
| 184 | and running the tests with debug mode forced (hacked) on should give |
|---|
| 185 | some idea of what's what). |
|---|