Python's get the user's name
1 #!/usr/local/bin/python
2
3 import posix
4 import string
5
6 uid = `posix.getuid()`
7 passwd = open('/etc/passwd')
8 for line in passwd.readlines():
9 rec = string.splitfields(line, ':')
10 if rec[2] == uid:
11 print 'hello', rec[0],
12 print 'mind if we call you bruce?'
13 break
14 else:
15 print "I can't find you in /etc/passwd"
Perl's Get the User name
1 #!/usr/bin/perl -wUsing Python's FTP library
2
3 $user = getpwent();
4 print "hello $user, mind if I call you bruce?\n";
#!/usr/local/bin/pythonUsing Perl's FTP Libraryfrom ftplib import FTP
ftp = FTP('ftp.python.org') # connect to host
ftp.login() # login anonymous
ftp.cwd('pub/python/doc') # change directory
ftp.retrlines('LIST') # list python/doc
F = open('python.FAQ', 'w') # file: python.FAQ
ftp.retrbinary('RETR FAQ', F.write, 1024)
ftp.quit()
$ftp = Net::FTP->new("some.host.name");
$ftp->login("anonymous","me@here.there");
$ftp->cwd("/pub");
$ftp->get("that.file");
$ftp->quit;
A File Comparor in Python
#!/usr/contrib/newest/bin/pythonfrom sys import *
from os import *
from posix import *# Define a function which "roots" a path reference, if
# it's not already rooted.
def root_file_reference(file_reference):
if file_reference[0] == '/':
return file_reference
else:
return getcwd() + "/" + file_referencedef usage():
print "Usage: compare_archive TARFILE DIRECTORY"
exit(1)if len(argv) != 3:
usage()source_archive = root_file_reference(argv[1])
root_of_altered_sources = root_file_reference(argv[2])tempdir = "/tmp/comparison." + str(getpid())
report = ""
count = 0
mkdir(tempdir)
chdir(tempdir)
system("tar xf " + source_archive)
for file in popen("find . -type f -print").readlines():
# Chop off the terminal newline.
file = file[:-1]
difference = popen("diff %s %s/./%s" %
(file, root_of_altered_sources, file)).read()
if difference != "":
report = report + "-------------------------------\n"
report = report + "Modifications in " + file
report = report + ":\n" + difference
count = count + 1system("rm -rf " + tempdir)
print "A total of %s sources have been changed." % count
print report
If you want to try compare_archive.py for yourself, simply install
Python on a Unix host, save the source into a file named compare_archive.py,
set its execution bit, and launch it from the command line. You'll see
a
result that looks something like:
A total of 2 sources have been changed.
-------------------------------
Modifications in ./src/first:
1a2
> This is a new line.
-------------------------------
Modifications in ./lib/ar/mine.c:
2a3,5
> #include
> #include
> #include