~jocave/checkbox/hybrid-amd-gpu-mods

« back to all changes in this revision

Viewing changes to checkbox-old/scripts/network_check

  • Committer: Zygmunt Krynicki
  • Date: 2013-05-29 07:50:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2153.
  • Revision ID: zygmunt.krynicki@canonical.com-20130529075030-ngwz245hs2u3y6us
checkbox: move current checkbox code into checkbox-old

This patch cleans up the top-level directory of the project into dedicated
sub-project directories. One for checkbox-old (the current checkbox and all the
associated stuff), one for plainbox and another for checkbox-ng.

There are some associated changes, such as updating the 'source' mode of
checkbox provider in plainbox, and fixing paths in various test scripts that we
have.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
"""
 
3
Check that it's possible to establish a http connection against
 
4
ubuntu.com
 
5
"""
 
6
from subprocess import call
 
7
from argparse import ArgumentParser
 
8
import http.client
 
9
import urllib.request, urllib.error, urllib.parse
 
10
import sys
 
11
 
 
12
 
 
13
def check_url(url):
 
14
    """
 
15
    Open URL and return True if no exceptions were raised
 
16
    """
 
17
    try:
 
18
        urllib.request.urlopen(url)
 
19
    except (urllib.error.URLError, http.client.InvalidURL):
 
20
        return False
 
21
 
 
22
    return True
 
23
 
 
24
 
 
25
def main():
 
26
    """
 
27
    Check HTTP and connection
 
28
    """
 
29
    parser = ArgumentParser()
 
30
    parser.add_argument('-u', '--url',
 
31
                        action='store',
 
32
                        default='http://cdimage.ubuntu.com',
 
33
                        help='The target URL to try. Default is %(default)s')
 
34
    parser.add_argument('-a', '--auto',
 
35
                        action='store_true',
 
36
                        default=False,
 
37
                        help='Runs in Automated mode, with no visible output')
 
38
 
 
39
    args = parser.parse_args()
 
40
 
 
41
    url = {"http": args.url}
 
42
 
 
43
    results = {}
 
44
    for protocol, value in url.items():
 
45
        results[protocol] = check_url(value)
 
46
 
 
47
    bool2str = {True: 'Success', False: 'Failed'}
 
48
    message = ("HTTP connection: %(http)s\n"
 
49
               % dict([(protocol, bool2str[value])
 
50
                       for protocol, value in results.items()]))
 
51
 
 
52
    command = ["zenity", "--title=Network",
 
53
               "--text=%s" % message]
 
54
 
 
55
    if all(results.values()):
 
56
        command.append("--info")
 
57
    else:
 
58
        command.append("--error")
 
59
 
 
60
    if not args.auto:
 
61
        try:
 
62
            call(command)
 
63
        except OSError:
 
64
            print("Zenity missing; unable to report test result:\n %s" % message)
 
65
 
 
66
    if any(results.values()):
 
67
        return 0
 
68
    else:
 
69
        return 1
 
70
 
 
71
if __name__ == "__main__":
 
72
    sys.exit(main())