blob: 66412890241cf427f93c9f62976842d14a3a7ad4 [file] [log] [blame]
mbligh462c0152008-03-13 15:37:10 +00001import os, sys, re, shutil, urlparse, urllib, pickle, random
mbligh15b2a112008-02-27 16:46:56 +00002from error import *
mbligh6231cd62008-02-02 19:18:33 +00003
mblighde0d47e2008-03-28 14:37:18 +00004
5def read_keyval(path):
6 """
7 Read a key-value pair format file into a dictionary, and return it.
8 Takes either a filename or directory name as input. If it's a
9 directory name, we assume you want the file to be called keyval.
10 """
11 if os.path.isdir(path):
12 path = os.path.join(path, 'keyval')
13 keyval = {}
14 for line in open(path):
15 line = re.sub('#.*', '', line.rstrip())
16 if not re.search(r'^\w+=', line):
17 raise ValueError('Invalid format line: %s' % line)
18 key, value = line.split('=', 1)
19 if re.search('^\d+$', value):
20 value = int(value)
21 elif re.search('^(\d+\.)?\d+$', value):
22 value = float(value)
23 keyval[key] = value
24 return keyval
25
26
mbligh6231cd62008-02-02 19:18:33 +000027def write_keyval(path, dictionary):
28 if os.path.isdir(path):
29 path = os.path.join(path, 'keyval')
30 keyval = open(path, 'a')
31 try:
32 for key, value in dictionary.iteritems():
33 if re.search(r'\W', key):
34 raise ValueError('Invalid key: %s' % key)
35 keyval.write('%s=%s\n' % (key, value))
36 finally:
37 keyval.close()
38
39
40def is_url(path):
41 """Return true if path looks like a URL"""
42 # for now, just handle http and ftp
43 url_parts = urlparse.urlparse(path)
44 return (url_parts[0] in ('http', 'ftp'))
45
46
47def get_file(src, dest, permissions=None):
48 """Get a file from src, which can be local or a remote URL"""
49 if (src == dest):
50 return
51 if (is_url(src)):
52 print 'PWD: ' + os.getcwd()
53 print 'Fetching \n\t', src, '\n\t->', dest
54 try:
55 urllib.urlretrieve(src, dest)
56 except IOError, e:
57 raise AutotestError('Unable to retrieve %s (to %s)'
58 % (src, dest), e)
59 else:
60 shutil.copyfile(src, dest)
61 if permissions:
62 os.chmod(dest, permissions)
63 return dest
64
65
66def unmap_url(srcdir, src, destdir='.'):
67 """
68 Receives either a path to a local file or a URL.
69 returns either the path to the local file, or the fetched URL
70
71 unmap_url('/usr/src', 'foo.tar', '/tmp')
72 = '/usr/src/foo.tar'
73 unmap_url('/usr/src', 'http://site/file', '/tmp')
74 = '/tmp/file'
75 (after retrieving it)
76 """
77 if is_url(src):
78 url_parts = urlparse.urlparse(src)
79 filename = os.path.basename(url_parts[2])
80 dest = os.path.join(destdir, filename)
81 return get_file(src, dest)
82 else:
83 return os.path.join(srcdir, src)
84
85
86def update_version(srcdir, preserve_srcdir, new_version, install,
87 *args, **dargs):
88 """
89 Make sure srcdir is version new_version
90
91 If not, delete it and install() the new version.
92
93 In the preserve_srcdir case, we just check it's up to date,
94 and if not, we rerun install, without removing srcdir
95 """
96 versionfile = os.path.join(srcdir, '.version')
97 install_needed = True
98
99 if os.path.exists(versionfile):
100 old_version = pickle.load(open(versionfile))
101 if old_version == new_version:
102 install_needed = False
103
104 if install_needed:
105 if not preserve_srcdir and os.path.exists(srcdir):
106 shutil.rmtree(srcdir)
107 install(*args, **dargs)
108 if os.path.exists(srcdir):
109 pickle.dump(new_version, open(versionfile, 'w'))
mbligh462c0152008-03-13 15:37:10 +0000110
111
112class run_randomly:
113 def __init__(self):
114 self.test_list = []
115
116
117 def add(self, *args, **dargs):
118 test = (args, dargs)
119 self.test_list.append(test)
120
121
122 def run(self, fn):
123 while self.test_list:
124 test_index = random.randint(0, len(self.test_list)-1)
125 (args, dargs) = self.test_list.pop(test_index)
126 fn(*args, **dargs)