summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorArch Librarian <arch@canonical.com>2004-11-24 10:00:17 +0000
committerArch Librarian <arch@canonical.com>2004-11-24 10:00:17 +0000
commit507d25595a7e989139b9c425b39f02ee48fa033e (patch)
treeb6d9a27db1078d62df8efe68a853165f324bfb60 /doc
downloadpython-apt-507d25595a7e989139b9c425b39f02ee48fa033e.tar.gz
Initial revision
Author: jgg Date: 2001-02-20 06:32:01 GMT Initial revision
Diffstat (limited to 'doc')
-rwxr-xr-xdoc/examples/config.py55
-rwxr-xr-xdoc/examples/configisc.py39
-rwxr-xr-xdoc/examples/tagfile.py8
-rwxr-xr-xdoc/examples/versiontest.py36
4 files changed, 138 insertions, 0 deletions
diff --git a/doc/examples/config.py b/doc/examples/config.py
new file mode 100755
index 00000000..222c1331
--- /dev/null
+++ b/doc/examples/config.py
@@ -0,0 +1,55 @@
+#!/usr/bin/python
+# Example demonstrating how to use the configuration/commandline system
+# for configuration.
+# Some valid command lines..
+# config.py -h --help ; Turn on help
+# config.py -no-h --no-help --help=no ; Turn off help
+# config.py -qqq -q=3 ; verbosity to 3
+# config.py -c /etc/apt/apt.conf ; include that config file]
+# config.py -o help=true ; Turn on help by giving a config file string
+# config.py -no-h -- -help ; Turn off help, specify the file '-help'
+# -c and -o are standard APT-program options.
+
+# This shows how to use the system for configuration and option control.
+# The other varient is for ISC object config files. See configisc.py.
+import apt_pkg,sys,posixpath;
+
+# Create a new empty Configuration object - there is also the system global
+# configuration object apt_pkg.Config which is used interally by apt-pkg
+# routines to control unusual situations. I recommend using the sytem global
+# whenever possible..
+Cnf = apt_pkg.newConfiguration();
+
+print "Command line is",sys.argv
+
+# Load the default configuration file, InitConfig() does this better..
+Cnf.Set("config-file","/etc/apt/apt.conf"); # or Cnf["config-file"] = "..";
+if posixpath.exists(Cnf.FindFile("config-file")):
+ apt_pkg.ReadConfigFile(Cnf,"/etc/apt/apt.conf");
+
+# Merge the command line arguments into the configuration space
+Arguments = [('h',"help","help"),
+ ('v',"version","version"),
+ ('q',"quiet","quiet","IntLevel"),
+ ('c',"config-file","","ConfigFile"),
+ ('o',"option","","ArbItem")]
+print "FileNames",apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
+
+print "Quiet level selected is",Cnf.FindI("quiet",0);
+
+# Do some stuff with it
+if Cnf.FindB("version",0) == 1:
+ print "Version selected - 1.1";
+
+if Cnf.FindB("help",0) == 1:
+ print apt_pkg.Package,apt_pkg.Version,"for",apt_pkg.Architecture, \
+ "compiled on",apt_pkg.Date,apt_pkg.Time;
+ print "Hi, I am the help text for this program";
+ sys.exit(0);
+
+print "No help for you, try -h";
+
+# Print the configuration space
+print "The Configuration space looks like:";
+for I in Cnf.keys():
+ print "%s \"%s\";"%(I,Cnf[I]);
diff --git a/doc/examples/configisc.py b/doc/examples/configisc.py
new file mode 100755
index 00000000..1773a919
--- /dev/null
+++ b/doc/examples/configisc.py
@@ -0,0 +1,39 @@
+#!/usr/bin/python
+# Example demonstrating how to use the configuration/commandline system
+# for object setup.
+
+# This parses the given config file in 'ISC' style where the sections
+# represent object instances and shows how to iterate over the sections.
+# Pass it the sample apt-ftparchive configuration,
+# doc/examples/ftp-archive.conf
+# or a bind8 config file..
+
+import apt_pkg,sys,posixpath;
+
+ConfigFile = apt_pkg.ParseCommandLine(apt_pkg.Config,[],sys.argv);
+
+if len(ConfigFile) != 1:
+ print "Must have exactly 1 file name";
+ sys.exit(0);
+
+Cnf = apt_pkg.newConfiguration();
+apt_pkg.ReadConfigFileISC(Cnf,ConfigFile[0]);
+
+# Print the configuration space
+#print "The Configuration space looks like:";
+#for I in Cnf.keys():
+# print "%s \"%s\";"%(I,Cnf[I]);
+
+# bind8 config file..
+if Cnf.has_key("Zone"):
+ print "Zones: ",Cnf.SubTree("zone").List();
+ for I in Cnf.List("zone"):
+ SubCnf = Cnf.SubTree(I);
+ if SubCnf.Find("type") == "slave":
+ print "Masters for %s: %s"%(SubCnf.MyTag(),SubCnf.ValueList("masters"));
+else:
+ print "Tree definitions:";
+ for I in Cnf.List("tree"):
+ SubCnf = Cnf.SubTree(I);
+ # This could use Find which would eliminate the possibility of exceptions.
+ print "Subtree %s with sections '%s' and architectures '%s'"%(SubCnf.MyTag(),SubCnf["Sections"],SubCnf["Architectures"]);
diff --git a/doc/examples/tagfile.py b/doc/examples/tagfile.py
new file mode 100755
index 00000000..01fed2cc
--- /dev/null
+++ b/doc/examples/tagfile.py
@@ -0,0 +1,8 @@
+#!/usr/bin/env python
+import apt_pkg
+
+Parse = apt_pkg.ParseTagFile(open("apt/lists/_org_ftp.debian.org_ftp_dists_potato_main_binary-i386_Packages","r"));
+
+while Parse.Step() == 1:
+ print Parse.Section.get("Package");
+ print apt_pkg.ParseDepends(Parse.Section.get("Depends",""));
diff --git a/doc/examples/versiontest.py b/doc/examples/versiontest.py
new file mode 100755
index 00000000..95f887f2
--- /dev/null
+++ b/doc/examples/versiontest.py
@@ -0,0 +1,36 @@
+#!/usr/bin/python
+
+# This is a simple clone of tests/versiontest.cc
+import apt_pkg,sys,re,string;
+apt_pkg.InitConfig();
+apt_pkg.InitSystem();
+
+TestFile = apt_pkg.ParseCommandLine(apt_pkg.Config,[],sys.argv);
+if len(TestFile) != 1:
+ print "Must have exactly 1 file name";
+ sys.exit(0);
+
+# Go over the file..
+List = open(TestFile[0],"r");
+CurLine = 0;
+while(1):
+ Line = List.readline();
+ CurLine = CurLine + 1;
+ if Line == "":
+ break;
+ Line = string.strip(Line);
+ if len(Line) == 0 or Line[0] == '#':
+ continue;
+
+ Split = re.split("[ \n]",Line);
+
+ # Check forward
+ if apt_pkg.VersionCompare(Split[0],Split[1]) != int(Split[2]):
+ print "Comparision failed on line %u. '%s' ? '%s' %i != %i"%(CurLine,
+ Split[0],Split[1],apt_pkg.VersionCompare(Split[0],Split[1]),
+ int(Split[2]));
+ # Check reverse
+ if apt_pkg.VersionCompare(Split[1],Split[0]) != -1*int(Split[2]):
+ print "Comparision failed on line %u. '%s' ? '%s' %i != %i"%(CurLine,
+ Split[1],Split[0],apt_pkg.VersionCompare(Split[1],Split[0]),
+ -1*int(Split[2]));